[英]Solved - Stripe Clone Customer and Create Direct Charge on Connected Account
我正在嘗試在 Stripe 的關聯帳戶上創建直接收費。 我一直在瀏覽關於 Stripe 的文檔和帖子,但似乎無法讓它工作。
客戶及其付款方式存儲在平台帳戶中。 我正在嘗試在連接的帳戶上克隆客戶,但不想將他們的付款方式存儲在連接的帳戶上。 根據文檔,我可以通過不將其附加到我嘗試在連接的帳戶上創建的客戶來生成一次性使用的付款意圖。
嘗試克隆客戶時,出現錯誤:
StripeInvalidRequestError: The customer must have an active payment source attached.
在下面的代碼中,我確保付款方式已附加到平台客戶,當我進入控制台時,我會看到他們的付款方式,並將其標記為“默認”。
任何幫助表示贊賞!
const customerId = 'cus_xxx'; // platform customer id
const paymentMethod = 'pm_xxxx'; // platform customer payment method
const getDefaultCard = async (customer) => {
const { invoice_settings } = await stripe.customers.retrieve(customer);
return invoice_settings ? invoice_settings.default_payment_method : null;
};
const getCards = async (customer) => {
if (!customer) {
return [];
}
const default_card = await getDefaultCard(customer);
const { data } = await Stripe.stripe.paymentMethods.list({
customer,
type: "card",
});
if (!data) return [];
return data.map(({ id, card: { last4, exp_month, exp_year, brand } }) => ({
id,
last4,
exp_month,
exp_year,
brand,
default: id === default_card,
}));
};
// check to see if the current payment method is
// attached to the platform customer
const cards = await getCards(customerId);
let found = cards.filter((card) => card.id === paymentMethod).length > 0;
// if the card is not found, attach it to the platform customer
if (!found) {
const res = await Stripe.stripe.paymentMethods.attach(paymentMethod, {
customer: customerId,
});
}
const defaultCards = cards.filter((card) => card.default);
if (!defaultCards || !defaultCards.length) {
// attach a default payment source to the user before
// cloning to the connected account
await stripe.customers.update(user.cus_id, {
invoice_settings: {
default_payment_method: paymentMethod,
},
});
}
// Get customer token - Results in ERROR
const token = await stripe.tokens.create(
{
customer: customerId,
card: paymentMethod,
},
{
stripeAccount,
}
);
/** DOESN'T GET PAST TOKEN CREATION ABOVE **/
// clone customer
const newCustomer = await stripe.customers.create(
{
source: token.id,
card: paymentMethod,
},
{
stripeAccount,
}
);
const amount = 1000;
const application_fee_amount = 100;
const intent = await Stripe.stripe.paymentIntents.create(
{
customer: newCustomer.id,
amount,
currency: "usd",
payment_method_types: ["card"],
payment_method: paymentMethod,
description: "Test Connected Direct Charge",
application_fee_amount,
},
{
stripeAccount,
}
);
// Now confirm payment
const result = await Stripe.stripe.paymentIntents.confirm(intent.id, {
payment_method: paymentMethod,
});
看起來您正在嘗試將平台帳戶上的付款方式克隆到已連接帳戶上的令牌。 這不可能; Payment Methods 是較新的 Stripe API,不直接與令牌兼容(較舊的 API)。
您應該將平台上的付款方式克隆到連接帳戶上的付款方式,而不是使用如下代碼:
const paymentMethod = await stripe.paymentMethods.create({
customer: '{{CUSTOMER_ID}}',
payment_method: '{{PAYMENT_METHOD_ID}}',
}, {
stripeAccount: '{{CONNECTED_ACCOUNT_ID}}',
});
我能夠通過首先確保平台上存在用戶的付款方式來解決這個問題。 然后在我的付款意圖中,以下成功克隆了客戶和付款方式並成功收費:
// clone payment method
const cloned_pm = await stripe.paymentMethods.create({
customer, // platform customer id
payment_method, // platform payment method for platform customer
},{
stripeAccount
});
// create token to customer
const token = await stripe.tokens.create({
customer, // platform customer id
card: payment_method, // platform payment method as above
},{
stripeAccount
});
// clone customer
const newCustomer = await stripe.customers.create({
source: token.id,
},{
stripeAccount
});
// create intent - used the cloned customer and payment methods
const intent = await stripe.paymentIntents.create(
{
customer: newCustomer.id,
amount: 1000,
application_fee_amount: 100,
currency: "usd",
payment_method_types: ["card"],
payment_method: cloned_pm.id,
...
},
{
stripeAccount,
}
);
然后在前端客戶端:
const { error, paymentIntent } = await stripe.confirmCardPayment(
payment_intent_secret,
{ payment_method: cloned_payment_method_id }
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.