enupal / stripe

Allows customers sign up for recurring and one-time payments with Stripe, perfect for orders, donations, subscriptions, and events. Create simple payment forms in seconds easily without coding. For Craft CMS
https://enupal.com/craft-plugins/stripe-payments/
Other
33 stars 19 forks source link

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

Open knufri opened 2 weeks ago

knufri commented 2 weeks ago

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

andrelopez commented 2 weeks ago

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!

knufri commented 2 weeks ago

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' => 'john@doe.com'
];

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

echo $checkoutSession->url;