hakito / cakephp-stripe-payment-intents-plugin
CakePHP Stripe Payment Intents 插件
v5.0
2024-01-04 20:18 UTC
Requires
- cakephp/cakephp: ^5.0
- stripe/stripe-php: ^7.12
Requires (Dev)
- phpunit/phpunit: ^10.0
README
CakePHP-StripePaymentIntents-Plugin
CakePHP 4.x Stripe Payment Intents 插件
安装
使用 composer
如果您使用 composer,请使用以下命令添加
composer require hakito/cakephp-stripe-payment-intents-plugin
不使用 composer
将插件下载到 app/Plugin/StripePaymentIntents。
加载插件
在 Application.php 中加载插件
public function bootstrap(): void { // Call parent to load bootstrap from files. parent::bootstrap(); $this->addPlugin(\StripePaymentIntents\Plugin::class, ['routes' => true]); }
配置
将以下配置添加到您的 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, ]
您还可以设置日志记录
'Log' => [ 'stripe' => [ 'className' => FileLog::class, 'path' => LOGS, 'file' => 'stripe', 'scopes' => ['Stripe'], 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency', 'info'], ], ]
使用方法
在您的支付处理控制器中
// Load the component public function initialize(): void { parent::initialize(); $this->loadComponent('StripePaymentIntents.StripePaymentIntents'); }
为结账流程创建或检索支付意向
// create new $pi = $this->StripePaymentIntents->Create(1234, ['metadata' => ['order_id' => $orderId]]); // 12.34 // or update shopping cart $pi = $this->StripePaymentIntents->Retrieve('pi_xyz');
设置视图数据
$this->set('StripeClientSecret', $pi->client_secret); $this->set('StripePublicKey', $this->StripePaymentIntents->GetPublicKey());
根据 Stripe 文档 实现视图行为。
Stripe webhook 事件
您必须通过实现事件处理器来处理 Stripe 事件
\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 });