[英]Charge a specific customer card in Stripe
TLDR; 我想允许客户使用自己选择的应用程序中以前使用过的/已保存的卡下订单。 这要求能够指定对“以前”与客户关联的“哪张”卡进行收费。 根据文档 , 理论上这应该是可能的 ,但是我在实践中没有运气。
好吧...通话很便宜,这是代码:
// pseudo code of a customer placing an order for the first time
var stripe = require("stripe")(".....");
(async function(userId, cardToken) {
// Create a Customer AND save the payment method
const customer = await stripe.customers.create({
source: cardToken.id, // e.g. 'tok_mastercard' (retrieved from stripe.js)
email: 'paying.user@example.com',
});
// Charge the Customer instead of the card:
const charge = await stripe.charges.create({
amount: 1000,
currency: 'usd',
customer: customer.id
});
// save customer id to DB
await setCustomerIdToDatabase(userId, customer.id);
// save Card ID (not the card token ID) to DB
await addCustomerCardToDatabase(userId, cardToken.card.id); // <= note cardToken".card".id e.g. card_kldlkns...
})(userId, cardToken /* <= provided by app*/);
到现在为止还挺好。 Stripe将指定令牌中的卡保存为新创建客户的默认付款方式,然后成功对其进行收费。 但是后来我的痛苦开始了...
// pseudo code of a customer placing subsequent orders (WITH NEW CARDS)
var stripe = require("stripe")(".....");
(async function(userId, cardToken) {
const customerId = await getCustomerIdFromDatabase(userId);
// Create a Customer AND save the payment method
await stripe.customers.createSource(
customerId,
{
source: cardToken.id, // e.g. 'tok_mastercard' (retrieved from stripe.js)
}
);
// Attempt to charge the newly added card instead the Customer:
const charge = await stripe.charges.create({
amount: 1000,
currency: 'usd',
customer: customerId,
source: cardToken.card.id // <= note cardToken".card".id e.g. card_kldlkns...
});
/////////ERROR TRHOWN: No such token: card_kldlkns...
///////// what? I just saved it with .customers.createSource and you said it was OK???
/// unreachable code
// save Card ID (not the card token ID) to DB
await addCustomerCardToDatabase(userId, cardToken.card.id); // <= note cardToken".card".id e.g. card_kldlkns...
})(userId, cardToken /* <= provided by app*/);
惨败...这也是
// pseudo code of a customer placing subsequent orders (WITH SAVED CARDS)
var stripe = require("stripe")(".....");
(async function(userId, savedCardId) {
const customerId = await getCustomerIdFromDatabase(userId);
// Attempt to charge the newly added card instead the Customer:
const charge = await stripe.charges.create({
amount: 1000,
currency: 'usd',
customer: customerId,
source: savedCardId // e.g card_kldlkns...
});
/////////ERROR TRHOWN: No such token: card_kldlkns...
///////// what??????
})(userId, savedCardId /* <= provided by app*/);
我什么不来? 文档明确指出,如果我要为特定的卡收费, Stripe想要card.id和customer.id 。
任何帮助表示赞赏
原来这根本不是问题。
有一个错误的情况是代码调用了错误的Stripe API方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.