Stripe & PHP Video Tutorial: Stripe Checkout Payment

In this tutorial I suggest you discover the implementation of Stripe Checkout in PHP. This integration allows to let Stripe manage the payment process from their interface, which allows for easier integration.

Basic principle

In order to interact with the Stripe API we will use the package stripe/stripe-php :

composer require stripe/stripe-php

Session Creation

To start the payment process you will have to create a Session (you can adapt the parameters according to what you want to do).

$session = Session::create([
    'line_items'                  => [
        array_map(fn(array $product) => [
            'quantity'   => 1,
            'price_data' => [
                'currency'     => 'EUR',
                'product_data' => [
                    'name' => $product['name']
                ],
                'unit_amount'  => $product['price']
            ]
        ], $cart->getProducts())
    ],
    'mode'                        => 'payment',
    'success_url'                 => 'http://localhost:8000/success.php',
    'cancel_url'                  => 'http://localhost:8000/',
    'billing_address_collection'  => 'required',
    'shipping_address_collection' => [
        'allowed_countries' => ['FR']
    ],
    'metadata'                    => [
        'cart_id' => $cart->getId()
    ]
]);

The API return returns an object of type Session that contains a property url which contains the url to which the user must be redirected to start the payment process. You can keep the session ID if you want to use it for reconciliation when using webhooks.

header("HTTP/1.1 303 See Other");
header("Location: " . $session->url);

The user will be able to enter their payment method (and their billing information if the option is activated) and will be redirected to the success page.

Webhook

Now that the user has made the payment, our server needs to be notified of the payment. For this, Stripe offers a webhook system that will notify our server via an HTTP call when certain events occur.

We start by checking if the payload is valid using the header stripe-signature.

$signature = $request->getHeaderLine('stripe-signature');
$body = (string)$request->getBody();
$event = Webhook::constructEvent(
  $body,
  $signature,
  WEBHOOK_SECRET
);

In our case we will only be interested in the event checkout.session.completed.

if ($event->type !== 'checkout.session.completed') {
    return;
}
// On récupère la Session Stripe
$data = $event->data['object']; 
// On peut utiliser l'API pour récupérer 
// des informations supplémentaires si nécessaire
$client = new StripeClient(STRIPE_SECRET);
$items = $client->checkout->sessions->allLineItems($data['id']);

It is up to you to process this data according to what you wish to do.

Test

To test your webhook you can use the Stripe ordering system.

stripe login 

We can then tell Stripe to redirect webhooks calls to our development server.

stripe listen --forward-to localhost:4242/stripe_webhooks

It is also possible to use this command to create events with specific data.

stripe trigger checkout.session.completed
# On peut aussi passer des données
stripe trigger checkout.session.completed --add checkout_session:metadata[cart_id]=234

It is also possible to restart a specific event with its id.

stripe events resend evt_1CiPtv2eZvKYlo2CcUZsDcO6
Response