Customizing the Checkout UI
Sending custom options to Stripe Checkout
You can either hardcode an array or use a dynamic callback to customize the configuration sent to Stripe when creating the session for the checkout.
site/config/config.php
<?php
return [
'bnomei.klub.stripe.checkout' => [
'billing_address_collection' => 'required',
],
// or
'bnomei.klub.stripe.checkout' => function (array $options = []) {
// https://docs.stripe.com/api/checkout/sessions/create?lang=php
// example: payments/one-time products need shipping address
// and allow additional payment methods
if ($options['mode'] === 'payment') {
return [
'billing_address_collection' => 'required',
'shipping_address_collection' => [
'allowed_countries' => ['DE'],
],
'payment_method_types' => [
'card',
'giropay',
'paypal',
'klarna',
],
];
}
// example: allow discount for existing members
$page = kirby()->site()->page(); // current page
$user = kirby()->user(); // current user if any
if ($user && $user->isMember()) {
return [
'discounts' => [...],
'submit_type' => 'book',
];
}
return [
'billing_address_collection' => 'required',
'custom_fields' => [...],
];
}
];
Reacting on the Stripe Checkout Webhook
Klub will trigger hooks, which you can use to add custom fulfilment logic, like updating the user or setting up a dynamic redirect based on the data in the Stripe session.
site/plugins/klub-ext/index.php
<?php
Kirby::plugin('klub/extensions', [
'hooks' => [
'klub.subscribe:after' => function ($user, $session) {
// check a custom field to update newsletter subscription
// https://docs.stripe.com/api/checkout/sessions/create#create_checkout_session-custom_fields
foreach($session->custom_fields as $field) {
if ($field->key === 'newsletter') {
$user = $user->update([
'newsletter' => boolval($field->value),
]);
}
}
// change the redirect set from the form because the user booked the VIP package
// https://docs.stripe.com/api/checkout/sessions/object#checkout_session_object-metadata
if ($session->metadata['price_id'] === 'price_1PvwcVGgXQrirdlo7pq50uQt') {
kirby()->session()->set('klub.session.redirect', url('/vip'));
}
}
],
]);