Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: '[email protected]')

# 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 }
like image 543
Chris W. Avatar asked Mar 10 '23 01:03

Chris W.


1 Answers

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.

like image 132
Chris W. Avatar answered May 14 '23 15:05

Chris W.