在 Stripe 中为特定的客户卡充值
Posted
技术标签:
【中文标题】在 Stripe 中为特定的客户卡充值【英文标题】:Charge a specific customer card in Stripe 【发布时间】:2019-03-25 18:04:03 【问题描述】:TLDR;我想允许客户使用他们在我的应用程序中选择的以前使用/保存的卡下订单。这需要能够指定要对“哪张”卡(以前与客户关联)进行收费。 This should be possible in theory according to the docs,但我在实践中没有运气。
好吧...话不多说,代码如下:
// 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*/);
我在这里没有得到什么? The docs clearly says Stripe wants the card.id and the customer.id back 如果我想对特定卡收费。
感谢任何帮助
【问题讨论】:
在customer
和source
都等于卡ID 的情况下调用stripe.charges.create
应该对给定客户的特定卡收费。如果您收到 no such token
错误,请先尝试列出给定客户的来源,以确保具有特定 ID card_dddyyyxxx
的卡确实存在,stripe.com/docs/api/cards/list
@duck true,但正如您所见,卡只是通过stripe.customers.createSource
调用添加到客户,然后在成功解决承诺后立即收费(没有抛出错误)。在这种情况下,在收取帮助之前检查卡是否真的存在并没有多大帮助,因为这种情况无法补救。我所能做的就是自己向客户抛出一个错误,而不是让 stripe 抛出它。也许这根本不是这样做的方法,因此这篇文章
是的,我意识到这不是您在生产中实现的,我纯粹是在调试方面说...如果您收到卡不存在的错误(“没有这样的令牌”),通过查看在您致电 stripe.charges.create
时实际保存给客户的卡(如果有)来进行调试会很有帮助
【参考方案1】:
事实证明这根本不是问题。
有一个错误条件是代码调用了错误的条带 API 方法
【讨论】:
以上是关于在 Stripe 中为特定的客户卡充值的主要内容,如果未能解决你的问题,请参考以下文章
在 django python 中添加新卡之前,我如何检查 Stripe 客户是不是已经拥有特定卡