user13800891
user13800891

Reputation: 59

How to get customer data from stripe prebuilt checkout

I am trying to integrate stripe prebuilt checkout on my website. It successfully adds customers and payment on my stripe portal. What I need is to get customer details(customer id) so i can add that on mysql database table. As its prebuilt checkout form, stripe automatically adds the customer using the email provided with the transaction. Here is my code

\Stripe\Stripe::setApiKey('sk_test_51J...........esLwtMQx7IXNxp00epljtC43');

header('Content-Type: application/json');

$YOUR_DOMAIN = 'mydomain.com';

$checkout_session = \Stripe\Checkout\Session::create([
    'payment_method_types' => ['card'],
    'line_items' => [[
      'price'=>"price_1Jt.....vImqj",
      'quantity'=>1,
    ]],
    'mode' => 'subscription',
    // 'success_url' => $YOUR_DOMAIN . '/success.html?session_id={CHECKOUT_SESSION_ID}',
    'success_url' => $YOUR_DOMAIN . '/success.php',
    'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
  ]);

Upvotes: 1

Views: 1420

Answers (1)

alex
alex

Reputation: 2799

You can pass in an existing Customer id when creating a Checkout Session : https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer. If customer is left blank for Checkout Sessions in payment or subscription mode, Checkout will create a new Customer object based on information provided during the payment flow.

To obtain the Customer id for a successful Checkout Session, you would want to set up webhooks to listen for the checkout.session.completed event.

You can refer to these documentations on how to setup webhooks :

Upvotes: 2

Related Questions