stripe icon indicating copy to clipboard operation
stripe copied to clipboard

How to get the checkout session url in custom module or controller via php

Open knufri opened this issue 1 year ago • 2 comments

Description

Hello, I have a custom controller in Craft and I need to get the checkout session url from the price id and quantity and some custom metadata. I want to use the same logic like the checkout twig tag with php:

{% set stripeProduct = [
        {
            'price': priceId,
            'quantity': 1,
        }
    ]
%}

{% set stripeMetaData = {
        'subcriptionHandle': subcriptionHandle,
        'customer': '',
        'email': email ?? '',
    }
%}

{% set checkoutSession = craft.enupalStripe.checkout(stripeProduct, stripeMetaData) %}

How to achieve that?

Additional info

  • Craft version: 4
  • PHP version: 8.3
  • Plugin version: 5.5.2
  • Is SCA and Stripe Checkout enabled?: YES

knufri avatar Nov 13 '24 15:11 knufri

Hi @knufri

Please feel free to use the following example:

use enupal\stripe\Stripe;
...


Stripe::$app->checkout->checkout($lineItems, $metadata);

let met know how it goes!

andrelopez avatar Nov 13 '24 15:11 andrelopez

It works perfectly! You saved my day. Here my code in case someone has the same question:

$lineItems = [
    [
        'price' => 'price_XYZ123',
        'quantity' => 1
    ]
];

// optional data
$metadata = [
    'subcriptionHandle' => 'foo',
    'customer' => 'bar',
    'email' => '[email protected]'
];

$checkoutSession = Stripe::$app->checkout->checkout($lineItems, $metadata);

echo $checkoutSession->url;

knufri avatar Nov 13 '24 16:11 knufri