Search code examples
phpstripe-paymentsstripe-tax

Stripe redirectToCheckout with php


I'm creating successfully a subscription for a customer with the redirectToCheckout mehtod. I'm creating the Stripe Session as following

$session = \Stripe\Checkout\Session::create([
            'payment_method_types' => ['card'],
            'locale' => 'de',
            'line_items' => [[
                'price' => env('STRIPE_PRICE'),
                'quantity' => 1,
            ]],
            'mode' => 'subscription',
            'success_url' => $success_url,
            'cancel_url' => $cancel_url,
        ]);

But I have two problems. This method is creating a new customer for me, unfortunately I need to set the language for the customer to German (not happening currently). Furthermore I need to define the tax for the line_items but didn't succeeded as suggested on the stripe docs for creating a Session Object.


Solution

  • In order to use a specific Customer (rather than have Checkout create one for you), you need to create it [0] before the CheckoutSession. Then you pass the Customer's id as CheckoutSession.customer [1].

    For taxes, you can specify the TaxRates in CheckoutSession.subscription_data.default_tax_rates[2].

    (pardon the dot notation, as this isn't PHP-specific and thus can be applied to any of Stripe's API libraries)

    [0] https://stripe.com/docs/api/customers/create

    [1] https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer

    [2] https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-default_tax_rates