CakePHP 4.x plugin for Stripe Payment Intents
If you are using composer simply add it using the following command:
composer require hakito/cakephp-stripe-payment-intents-plugin
Download the plugin to app/Plugin/StripePaymentIntents.
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]);
}
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'],
],
]
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.
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
});