PHP code example of programmatordev / kirby-stripe-checkout

1. Go to this page and download the library: Download programmatordev/kirby-stripe-checkout library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

programmatordev / kirby-stripe-checkout example snippets


// config.php

return [
    'programmatordev.stripe-checkout' => [
        'stripePublicKey' => null,
        'stripeSecretKey' => null,
        'stripeWebhookSecret' => null,
        'currency' => 'EUR',
        'uiMode' => 'hosted',
        'successPage' => null,
        'cancelPage' => null,
        'returnPage' => null,
        'ordersPage' => 'orders',
        'settingsPage' => 'checkout-settings',
        'cartSnippet' => null,
        'translations' => []
    ]
];

// site/config/config.php

return [
    'programmatordev.stripe-checkout' => [
        'translations' => [
            'en' => [
                // overwrites "Orders" to "Tickets"
                'stripe-checkout.fields.orders.ordersHeadline.label' => 'Tickets'
            ]
        ]
    ]
];

// site/config/config.php

return [
    'programmatordev.stripe-checkout.translations' => [
        'translations' => [
            // The German translation is not currently bundled with the plugin, so you can provide your own
            'de' => [
                'stripe-checkout.fields.product.price.label' => 'Preis',
                'stripe-checkout.fields.product.thumbnail.label' => 'Vorschaubild',
                'stripe-checkout.fields.settings.shippingHeadline.label' => 'Versand',
                'stripe-checkout.fields.settings.shippingEnabled.label' => 'Versandeinstellungen',
                'stripe-checkout.fields.settings.shippingAllowedCountries.label' => 'Erlaubte Länder',
                // ...
            ]
        ]
    ]
];

// config.php

return [
    'hooks' => [
        'stripe-checkout.session.create:before' => function (array $sessionParams): array
        {
            // for example, if you want to enable promotion codes
            // https://docs.stripe.com/api/checkout/sessions/create?lang=php#create_checkout_session-allow_promotion_codes
            $sessionParams['allow_promotion_codes'] = true;

            return $sessionParams;
        }
    ]
];

// config.php

use Stripe\Checkout\Session;
use Stripe\Event;

return [
    'hooks' => [
        'stripe-checkout.order.create:before' => function (array $orderContent, Session $checkoutSession, Event $stripeEvent): array
        {
            // change order content
            // ...

            return $orderContent;
        }
    ]
];

// config.php

use Kirby\Cms\Page;
use Stripe\Checkout\Session;
use Stripe\Event;

return [
    'hooks' => [
        'stripe-checkout.payment:succeeded' => function (Page $orderPage, Session $checkoutSession, Event $stripeEvent): void
        {
            // email the customer when the payment succeeds
            kirby()->email([
                'from' => '[email protected]',
                'to' => $orderPage->customer()->toObject()->email()->value(),
                'subject' => 'Thank you for your order!',
                'body' => 'Your order will be processed soon.',
            ]);
        }
    ]
];

// config.php

use Kirby\Cms\Page;
use Stripe\Checkout\Session;
use Stripe\Event;

return [
    'hooks' => [
        'stripe-checkout.payment:pending' => function (Page $orderPage, Session $checkoutSession, Event $stripeEvent): void
        {
            // email the customer when the payment is pending
            kirby()->email([
                'from' => '[email protected]',
                'to' => $orderPage->customer()->toObject()->email()->value(),
                'subject' => 'Thank you for your order!',
                'body' => 'Your order is pending payment.',
            ]);
        }
    ]
];

// config.php

use Kirby\Cms\Page;
use Stripe\Checkout\Session;
use Stripe\Event;

return [
    'hooks' => [
        'stripe-checkout.payment:failed' => function (Page $orderPage, Session $checkoutSession, Event $stripeEvent): void
        {
            // email the customer when the payment has failed
            kirby()->email([
                'from' => '[email protected]',
                'to' => $orderPage->customer()->toObject()->email()->value(),
                'subject' => 'Bad news!',
                'body' => 'Your order has been canceled because the payment has failed.',
            ]);
        }
    ]
];

use ProgrammatorDev\StripeCheckout\Cart\Cart;

cart(array $options = []): Cart

cart([
    // the same as configured in the plugin options
    'currency' => option('programmatordev.stripe-checkout.currency'),
    'cartSnippet' => option('programmatordev.stripe-checkout.cartSnippet')
]);

addItem(string $id, int $quantity, ?array $options = null): string

$cart = cart();

// a key is returned and uniquely identifies that item in the cart
$key = $cart->addItem(id: 'products/cd', quantity: 1);

// you can add options per item
$key = $cart->addItem(
    id: 'products/t-shirt',
    quantity: 1,
    options: ['Color' => 'Green', 'Size' => 'Medium']
);

updateItem(string $key, int $quantity): void

$cart = cart();

$key = $cart->addItem(id: 'products/cd', quantity: 1);

$cart->updateItem(key: $key, quantity: 3);

removeItem(string $key): void

$cart = cart();

$key = $cart->addItem(id: 'products/cd', quantity: 1);

$cart->removeItem($key);

use Kirby\Toolkit\Collection;
use ProgrammatorDev\StripeCheckout\Cart\Item

/** @return Collection<string, Item> */
items(): Collection

use ProgrammatorDev\StripeCheckout\Cart\Item;

$cart = cart();
$items = $cart->items();

/** @var Item $item */
foreach ($items as $key => $item) {
    $item->id();
    $item->quantity()
    $item->options();
    $item->productPage();
    $item->name();
    $item->price();
    $item->totalAmount();
    $item->thumbnail();
}

totalQuantity(): int

$cart = cart();
echo $cart->totalQuantity(); // 3

totalAmount(): int|float

$cart = cart();
echo $cart->totalAmount(); // 100

currency(): string

$cart = cart();
echo $cart->currency(); // EUR

currencySymbol(): string

$cart = cart();
echo $cart->currencySymbol(); // €

cartSnippet(bool $render = false): ?string

$cart = cart();
echo $cart->cartSnippet(render: false); // snippet name or null
echo $cart->cartSnippet(render: true); // rendered snippet HTML

destroy(): void

$cart = cart();
$cart->destroy();

toArray(bool $

$cart = cart();
$cart->toArray();