getcandy / stripe
GetCandy 的 Stripe 支付驱动程序。
Requires
- php: ^8.0
- getcandy/core: ^2.0-beta12
- livewire/livewire: ^2.0
- stripe/stripe-php: ^7.114
Requires (Dev)
- mockery/mockery: ^1.4.4
- orchestra/testbench: ^6.0|^7.0
- phpunit/phpunit: ^9.5.10
This package is auto-updated.
Last update: 2024-09-14 15:37:31 UTC
README
此插件使您能够在 GetCandy 店面启用 Stripe 支付。
Alpha 版本
此插件目前处于 Alpha 版,尽管我们已经采取每一步以确保它按预期工作,但在添加更多测试并证明之前,它不会被视为 Alpha 版本。
需要的测试
- 从 Stripe 收到的成功收费响应。
- 从 Stripe 收到的失败收费响应。
- 测试
手动
配置是否适当响应。 - 测试
自动
配置是否适当响应。 - 确保事务在数据库中正确存储
- 确保在使用同一购物车时,支付意图不会重复。
- 确保根据 Stripe 的响应返回适当的响应。
- 测试退款和部分退款创建预期的交易
- 确保我们可以手动释放支付或部分支付并处理不同的响应。
要求
- GetCandy >=
2.0-beta11
- 拥有带密钥和公钥的 Stripe 账户
- Laravel Livewire(如果使用前端组件)
- Alpinejs(如果使用前端组件)
安装
需要 composer 包
composer require getcandy/stripe
发布配置
这将发布配置到 config/getcandy/stripe.php
下。
php artisan vendor:publish --tag=getcandy.stripe.config
发布视图(可选)
GetCandy Stripe 随附一些辅助组件,您可以在结账时使用,如果您打算编辑它们提供的视图,则可以发布它们。
php artisan vendor:publish --tag=getcandy.stripe.components
启用驱动程序
在 config/getcandy/payments.php
中设置驱动程序
<?php return [ // ... 'types' => [ 'card' => [ // ... 'driver' => 'stripe', ], ], ];
添加您的 Stripe 凭证
确保您已在 config/services.php
中设置了 Stripe 凭证
'stripe' => [ 'key' => env('STRIPE_SECRET'), 'public_key' => env('STRIPE_PK'), ],
密钥可在您的 Stripe 账户中找到 https://dashboard.stripe.com/apikeys
配置
以下是在 config/getcandy/stripe.php
中此包使用的可用配置选项列表
后端使用
创建 PaymentIntent
use \GetCandy\Stripe\Facades\Stripe; Stripe::createIntent(\GetCandy\Models\Cart $cart);
此方法将从一个购物车创建 Stripe PaymentIntent,并将生成的 ID 添加到元数据中以供稍后检索。如果购物车已经存在 PaymentIntent,则将从 Stripe 获取它并返回,以避免创建重复的 PaymentIntent。
$cart->meta->payment_intent;
获取现有的 PaymentIntent
use \GetCandy\Stripe\Facades\Stripe; Stripe::fetchIntent($paymentIntentId);
这两种方法都将返回一个 Stripe\PaymentIntent
对象。
店面使用
此插件提供了一些有用的组件,您可以在店面中使用,它们是用 Laravel Livewire 和 AlpineJs 构建的,请记住这一点。
如果您正在使用 Demo Store,它已经为您设置好了,您可以参考源代码来了解发生了什么。
设置脚本
将此放在 Storefront 的 <head>
中。
@stripeScripts
添加支付组件
在您希望支付表单出现的地方添加此组件
@livewire('stripe.payment', [ 'cart' => $cart, 'returnUrl' => route('checkout.view'), ])
returnUrl
是我们希望在 Stripe 在其服务器上处理完支付后将其重定向到的地方。
不要将其指向订单确认页面,如下所示
处理结果
您会注意到,我们已指示Stripe将用户重定向回结账页面,这是因为尽管Stripe已根据您的政策处理了支付或分配了资金,但我们仍需要GetCandy处理结果并创建订单所需的交易。
当Stripe重定向我们时,查询字符串中应包含两个参数。payment_intent_client_secret
和payment_intent
。然后我们可以检查这些值,并使用GetCandy的支付驱动程序将它们传递出去。
因此,假设我们正在使用Livewire并在一个CheckoutPage
组件上(例如在演示商店中)
if ($request->payment_intent) { $payment = \GetCandy\Facades\Payments::driver('card')->cart($cart)->withData([ 'payment_intent_client_secret' => $request->payment_intent_client_secret, 'payment_intent' => $request->payment_intent, ])->authorize(); if ($payment->success) { redirect()->route('checkout-success.view'); return; } }
那么就完成了,您应该在GetCandy中看到带有正确交易的订单。
如果您已将您的政策设置为手动
,则需要进入中心并手动捕获支付。
贡献
欢迎贡献,如果您正在考虑添加功能,请首先提交一个问题,这样我们就可以确定是否应该包括它。
测试
目前我们使用一个手动MockClient来模拟Stripe API将返回的响应。随着测试的编写,这可能会得到改进,但它应该很明显它在做什么,所以请随意添加您自己的响应。