Home > Net >  Stripe : No card attached to customer after a checkout session payment
Stripe : No card attached to customer after a checkout session payment

Time:11-04

I'm using Stripe with PHP, I want to create subscription to customer after a payment made with Stripe Checkout Session.

But, the problem is that when we do a one shot payment with Stripe Checkout Session, Stripe does not attach the source (Credit card) to the customer, and when I try to subscribe this customer to a plan, Stripe return me the error : This customer has no attached payment source or default payment method.

I don't understand why Stripe don't attach the card to the customer with one shot payment because Stripe do it when we use a Checkout Session with Subscription.

How I create checkout session :

$checkout_session = Session::create([
    'payment_method_types' => ['card'],
    'mode' => 'payment',
    'customer' => 'cus_xxxxxxxx',
    'line_items' => [[
        'price' => 'price_xxxxx', // Oneshot payment
        'quantity' => 1,
    ]]
]);

How I try to subscribe customer (After successful payment) :

$subscription = Subscription::create([
    'customer' => 'cus_xxxxxxxx',
    'items' => [
        ['price' => 'price_XXXXXXX'] // Recurrent payment
    ],
    'trial_from_plan' => true,
]);

Thank you for your help

CodePudding user response:

When accepting a one-time payment with Checkout, it won't create a Customer or save card details by default. What you have to do is configure Checkout to do this for you during the payment. This is covered in their documentation here.

What you need to do is set the payment_intent_data[setup_future_usage] parameter like this:

$checkout_session = Session::create([
  'payment_method_types' => ['card'],
  'mode' => 'payment',
  'customer' => 'cus_xxxxxxxx',
  'line_items' => [[
    'price' => 'price_xxxxx', // Oneshot payment
    'quantity' => 1,
  ]],
  'payment_intent_data' => [
    'setup_future_usage' => 'off_session',
  ]
]);

After that, you will have a Customer cus_123 and a PaymentMethod pm_123 that you can re-use. That PaymentMethod will not be the default one though so you need to make sure that you pass its id in the default_payment_method parameter when you create the Subscription like this:

$subscription = Subscription::create([
  'customer' => 'cus_xxxxxxxx',
  'items' => [
    ['price' => 'price_XXXXXXX'] // Recurrent payment
  ],
  'trial_from_plan' => true,
  'default_payment_method' => 'pm_12345',
]);

Also note that you can start a Subscription directly from Checkout as documented here.

  • Related