Stripe Connect: Charging an existing customer against a “connected” (Standalone) account

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

If attempting to charge a customer record (which has an associated credit-card) via a connected account, I get an error claiming, "No such customer: cus_xxxx" -- even though making a charge to the same-exact customer will work fine when not using a "connected" account (when charging via the platform account).

For example, consider the following Ruby code, assuming we have a "connected" (Standalone) account with ID acct_ABC123:

# Use the (secret) API key for the "platform" or base account. Stripe.api_key = 'sk_[...]'  customer = Stripe::Customer.create(email: 'customer@example.com')  # Associate a credit-card with the customer. token = # Generate a token (e.g., using Stripe Checkout). customer.sources.create(source: token)  # Attempt to charge the card via the connected account... Stripe::Charge.create({ amount: 150, currency: 'usd', customer: customer.id,                         application_fee: 25 }, stripe_account: 'acct_ABC123')

The last line there leads to a Stripe::InvalidRequestError exception, with the "No such customer" error mentioned above. However, the same charge will go through fine if we just try to run it on the "platform" account (without the stripe_account parameter and no application_fee)...

Stripe::Charge.create({ amount: 150, currency: 'usd', customer: customer.id }

回答1:

For some (confusing and slightly bizarre) reason, you must add the intermediate step of creating a new token when making charges against "Shared Customers" (customers that will be charged through one or more connected accounts). So, assuming we've already created the customer with an associated credit-card (as per the question), the working code ends up looking something like this...

token = Stripe::Token.create({ customer: customer.id },                              { stripe_account: 'acct_ABC123' }) Stripe::Charge.create({ amount: 150, currency: 'usd', source: token.id,                         application_fee: 25 }, stripe_account: 'acct_ABC123')

As an aside, I would consider Stripe's error message ("No such customer") to be a bug and the fact that this extra step (generating a token) is required only for "Stripe Connect" charges a confusing quirk.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!