how to create a customer after a payment intent is created

1,995 views
Skip to first unread message

Kenji Hino

unread,
Sep 25, 2022, 8:26:17 PM9/25/22
to Stripe API Discussion
Hi,
I've just started to learn coding with the payment intent api.

What I want to do is close to this:

However, I don't want to create a Customer object before creating a payment intent because the customer may not finish the payment and it leaves the customer object without any Paymethod associated with.  Is there any way to create a Customer object during the charge process? Please help.

Remi J.

unread,
Sep 25, 2022, 8:37:04 PM9/25/22
to api-d...@lists.stripe.com
Hello,

You don't need to create a Customer upfront if you don't want to. You can create the PaymentIntent and pass `setup_future_usage` to configure the payment method to be saved afterwards. Once the PaymentIntent is confirmed successfully, you can then create a Customer via the API [1] and then update the PaymentIntent [2] to set the `customer` parameter [3] to your new Customer id `cus_123`. This will associate that payment with your new customer and attach the PaymentMethod to it automatically if needed.

Hope this helps!
Remi


--
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Kenji Hino

unread,
Sep 26, 2022, 5:14:22 PM9/26/22
to Stripe API Discussion, re...@stripe.com
I was able to associate the payment with a customer I created.
However, it didn't attach the PaymentMethod to the customer. Is there any more thing I need to do?

Here is the code:

          $paymentIntent = $stripe->paymentIntents->create([
              'payment_method_types' => ['card'],
              'amount'               => 1999,
              'currency'             => 'usd',
              'setup_future_usage'   => 'off_session',
            ]);

then after the card is confirmed at the front end, I  called this:

           $paymentIntent = $stripe->paymentIntents->update(
                $request->id,  // this the PaymentIntent id
                [
                    'customer' => $customer->id, // $customer is an object I created
                ],
            );

Remi J.

unread,
Sep 26, 2022, 6:36:15 PM9/26/22
to Kenji Hino, Stripe API Discussion
Hey Kenji,

Sorry about the confusion on that one. I was convinced we automatically attached the PaymentMethod after updating the PaymentIntent but it doesn't look like this is happening. What you should do instead if attach the PaymentMethod itself and that operation will automatically associate the PaymentIntent and the underlying Charge with that Customer for you.

$paymentMethod = $stripe->paymentMethods->attach(
  'pm_123',
  [
    'customer' => 'cus_123',
  ],
);

Hope this clarifies the confusion!
Best,
Remi

Kenji Hino

unread,
Sep 26, 2022, 8:13:04 PM9/26/22
to Stripe API Discussion, re...@stripe.com, Stripe API Discussion, Kenji Hino
Remi,

It worked. Thanks!

Payment Intent id, Payment Method id, Customer id.. all these ids can be public?  Because I am going to use them as part of the url like https://localhost/id=pm_123.

Remi J.

unread,
Sep 26, 2022, 8:14:53 PM9/26/22
to Kenji Hino, Stripe API Discussion
None of those ids can be used without your Secret API key so they are not secret per se. On the other hand, we discourage putting that kind of ids in your URL since anyone can change the value and could pretend to be someone else. It's best to use cookies or similar to recognize the customer while they are redirected and then find the corresponding PaymentIntent and related information from it.
Reply all
Reply to author
Forward
0 new messages