问题描述
我正在关注文档 here 关于使其他企业能够直接接受付款。
我已经成功创建了一个关联账户,并在关联账户上接受了客户的付款,但我还没有能够保存该客户的付款信息以备将来付款。
在整个文档中,特别是 here 假设您已经在平台帐户上创建了一个带有付款方式的客户,然后您应该尝试将付款方式克隆到连接的帐户。
在将客户克隆到连接的帐户之前,我终生无法弄清楚如何在平台帐户上创建具有付款信息的客户。
根据文档,我从客户端开始,其中 accountID 是连接帐户的 ID:
const [stripePromise,setstripePromise] = useState(() =>
loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY,{
stripeAccount: uid.accountID,})
);
我的条纹元素是用 stripePromise 创建的。
尝试保存卡详细信息时,我在客户端执行此操作:
这就是我认为我的错误所在我在尝试创建平台付款方式时使用了关联帐户凭据。
const handleRememberMe = async () => {
const { token } = await stripe.createtoken(
elements.getElement(CardElement)
);
console.log(token);
const res = await fetchPostJSON("/api/pay_performer",{
accountID: accountID,amount: value,cardToken: token,});
该 API 调用转到“/api/pay_performer”:
//Handle onboarding a new connect user for x
require("dotenv").config();
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY,{
apiVersion: "2020-08-27",});
import setCookie from "../../../utils/cookies";
export default async function createPayment(req,res) {
if (req.method === "POST") {
// Connected account ID and the token generated on the client to be saved.
const { accountID,amount,cardToken } = req.body;
console.log(`API side cardToken: ${cardToken.used}`);
try {
// Trying to create a customer with that token.
const customer = await stripe.customers.create({
email: "test2@example.com",source: cardToken.card,});
customer.sources = cardToken.card;
// Beginning the cloning process for the connected account.
const token = await stripe.tokens.create(
{
customer: customer.id,},{
stripeAccount: accountID,}
);
const clonedCustomer = await stripe.customers.create(
{
source: token.id,}
);
console.log(`Default Source: ${clonedCustomer.default_source}`);
console.log(`AccountID: ${accountID},Amount: ${amount}`);
const paymentIntent = await stripe.paymentIntents.create(
{
payment_method_types: ["card"],payment_method: clonedCustomer.default_source
? clonedCustomer.default_source
: null,amount: amount * 100,currency: "usd",application_fee_amount: 1,customer: clonedCustomer.id,}
);
const secret = paymentIntent.client_secret;
console.log(secret);
const payment_method = paymentIntent.payment_method;
return res.status(200).send({ secret,payment_method });
} catch (e) {
console.log(e);
return res.status(500).send({ error: e.message });
}
}
}
但我收到一个错误,指出提供的令牌不是有效的令牌。我假设这是因为我使用客户端上已连接帐户的凭据创建了令牌,然后尝试将其应用于平台帐户。
我如何有一个单独的用户界面来首先创建平台客户,然后在购买时返回并将它们克隆到连接的帐户。
我不应该将令牌传递给服务器而只通过 HTTPS 传递卡信息吗?非常迷茫,文档也没有帮助!
感谢任何帮助!
解决方法
为了创建客户、收集卡信息并将 PaymentMethod 附加到他们,您可以例如使用 SetupIntent 和 Elements [1] 或创建 PaymentMethod [2]。核心点是,如果系统最初应该将这些信息存储在平台上,那么连接的帐户根本就没有发挥作用。从广义上讲,步骤是(不在任何地方提供已连接帐户的帐户 ID,因为这仅发生在平台帐户上),使用平台的密钥:
- 创建客户
- 使用 Stripe.js 和 Elements 收集卡片数据
- 将 pm_xxx 附加到后端的客户
[1] https://stripe.com/docs/payments/save-and-reuse
[2] https://stripe.com/docs/js/payment_methods/create_payment_method