origami / stripe
Stripe PaymentIntents 辅助包
2.3.0
2024-05-28 12:29 UTC
Requires
- php: ^7.2|^8.0.2
- illuminate/contracts: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- moneyphp/money: ^3.2
- stripe/stripe-php: ^7.0
README
本包是用于Laravel项目使用Stripe的PaymentIntents API和手动确认SCA设置的辅助包。
它受到了Laravel Cashier包中SCA更新和逻辑的启发。
安装
通过Composer安装此包。
composer require origami/stripe
需求
此包目前设计用于与Laravel >= 6.0项目一起工作。
配置
- 您应该在
config/services.php
文件中添加您的Stripe密钥
'stripe' => [ 'secret' => env('STRIPE_SECRET'), 'key' => env('STRIPE_KEY'), 'webhook' => [ 'secret' => env('STRIPE_WEBHOOK_SECRET'), ] ],
- 更新您的
.env
文件以包含密钥和密钥
STRIPE_KEY=
STRIPE_SECRET=
STRIPE_WEBHOOK_SECRET=
用法
支付对象
// Make a Payment from a payment_intent ID. $payment = Origami\Stripe\Payment::find($id); // Or create a Payment as a new payment_intent $payment = Origami\Stripe\Payment::create([ 'amount' => 1000, 'currency' => 'gbp', 'payment_method' => $method, 'capture_method' => 'manual', 'payment_method_types' => ['card'], 'confirmation_method' => 'manual', 'confirm' => true, ]));
可用方法
获取金额
返回金额为Money\Money
对象,使用moneyphp/money
$payment->amount();
检查状态
返回一个布尔值
public function hasStatus($status)
您可以通过数组或字符串传递
$payment->hasStatus(['requires_confirmation','requires_capture']); $payment->hasStatus('requires_confirmation');
还有针对PaymentIntent状态的辅助方法
$payment->requiresConfirmation(); $payment->requiresPaymentMethod(); $payment->requiresCapture(); $payment->requiresAction(); $payment->isCancelled(); $payment->isSucceeded(); $payment->isSuccessful(); // Alias for above
验证PaymentIntent
请参阅https://stripe.com/docs/payments/payment-intents/web-manual#creating-with-manual-confirmation
try { $payment = Origami\Stripe\Payment::create([ 'amount' => 1000, 'currency' => 'gbp', 'payment_method' => $method, 'capture_method' => 'manual', 'payment_method_types' => ['card'], 'confirmation_method' => 'manual', 'confirm' => true, ])); if (!$payment) { throw new Exception('Error fetching Stripe payment'); } $payment->validate(); // PaymentIntent is valid // capture_method: manual above means we need to capture in another controller // capture_method: automatic above means the payment was successully taken return response([ 'payment_intent_id' => $payment->id, 'success' => true, ]); } catch (Origami\Stripe\PaymentActionRequired $e) { // Action is required on the client end - see Stripe docs. return response()->json([ 'requires_action' => true, 'payment_intent_client_secret' => $e->payment->clientSecret(), 'success' => false, ], 200); } catch (Origami\Stripe\PaymentFailed $e) { // Payment failed - handle on the client end. return response()->json([ 'error' => $e->getMessage(), 'success' => false, ], 500); } catch (Stripe\Exception\CardException $e) { // Don't forget to handle Stripe's exceptions for declined cards, etc. return response()->json([ 'error' => $e->getMessage(), 'success' => false, ], 500); } catch (Exception $e) { // Something else went wrong. Log::error($e); return response()->json(['error' => 'Unexpected error', 'success' => false], 500); }