hakito/cakephp-stripe-payment-intents-plugin

CakePHP Plugin for Stripe Payment Intents

v5.0 2024-01-04 20:18 UTC

This package is auto-updated.

Last update: 2024-12-04 22:11:37 UTC


README

Build Status Latest Stable Version Latest Unstable Version License

CakePHP-StripePaymentIntents-Plugin

CakePHP 4.x plugin for Stripe Payment Intents

Installation

Using composer

If you are using composer simply add it using the following command:

composer require hakito/cakephp-stripe-payment-intents-plugin

Without composer

Download the plugin to app/Plugin/StripePaymentIntents.

Load the plugin

Load the Plugin in your Application.php

public function bootstrap(): void
{
    // Call parent to load bootstrap from files.
    parent::bootstrap();

    $this->addPlugin(\StripePaymentIntents\Plugin::class, ['routes' => true]);
}

Confguration

Add the following config to your app.php

'StripePaymentIntents' => [
    'mode'=> 'test',
    'currency' => 'eur',
    'keys' => [
        'test' => [
            'secret' => 'sk_test_4eC39HqLyjWDarjtT1zdp7dc',
            'public' => 'pk_test_abc',
        ],
        'live' => [
            'secret' => 'sk_live_key',
            'public' => 'pk_live_key'
        ]
    ],
    'logging' => false,
]

You can also setup logging

'Log' =>
[
    'stripe' =>
    [
        'className' => FileLog::class,
        'path' => LOGS,
        'file' => 'stripe',
        'scopes' => ['Stripe'],
        'levels' => ['warning', 'error', 'critical', 'alert', 'emergency', 'info'],
    ],
]

Usage

In your payment handling controller:

// Load the component
public function initialize(): void
{
    parent::initialize();
    $this->loadComponent('StripePaymentIntents.StripePaymentIntents');
}

Create or retrieve a payment intent for the checkout process

// create new
$pi = $this->StripePaymentIntents->Create(1234, ['metadata' => ['order_id' => $orderId]]); // 12.34
// or update shopping cart
$pi = $this->StripePaymentIntents->Retrieve('pi_xyz');

Set the view data

$this->set('StripeClientSecret', $pi->client_secret);
$this->set('StripePublicKey', $this->StripePaymentIntents->GetPublicKey());

Implement the view behavior according to stripe documentation.

Stripe webhook events

You have to handle stripe events implementing an event handler:

\Cake\Event\EventManager::instance()->on('StripePaymentIntents.Event',
function (\Cake\Event\Event $event, \Stripe\Event $stripeEvent)
{
    return ['handled' => true]; // If you don't set the handled flag to true
                                // the plugin will throw an exception
});