stripe
stripe copied to clipboard
How to get the checkout session url in custom module or controller via php
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
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!
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;