ttrig / laravel-billmate
Laravel 用于操作 Billmate API 的包
v3.0.0
2023-05-30 16:13 UTC
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.0.1
- illuminate/support: ^10.0
Requires (Dev)
- graham-campbell/testbench: ^6.0
- laravel/pint: ^1.10
- mockery/mockery: ^1.3.1
- phpunit/phpunit: ^10.0
README
Laravel Billmate
Laravel 包,用于与 Billmate API 交互。
安装
composer require ttrig/laravel-billmate
配置
使用以下命令发布配置文件
php artisan vendor:publish --provider="Ttrig\Billmate\ServiceProvider"
在 .env
中添加 Billmate ID 和密钥。
BILLMATE_ID=123 BILLMATE_KEY=abc BILLMATE_TEST=true
更新 config/billmate.php
以使用您自己的控制器。
'accept_action' => 'App\Http\Controllers\BillmateController@accept', 'cancel_action' => 'App\Http\Controllers\BillmateController@cancel', 'callback_action' => \Ttrig\Billmate\Controllers\CallbackController::class,
通用支付流程
- https://developer.billmate.se/checkout-documentation
- https://developer.billmate.se/api-integration/getpaymentinfo
- https://developer.billmate.se/api-integration/initcheckout
使用示例
结账
use Ttrig\Billmate\Article as BillmateArticle; use Ttrig\Billmate\Service as BillmateService; class CheckoutController extends Controller { public function index(BillmateService $billmate) { $articles = collect()->push(new BillmateArticle([ 'title' => '1kg potatoes', 'price' => 30, ])); $checkout = $billmate->initCheckout($articles); return view('checkout', compact('checkout')); } }
您可以通过将回调作为 initCheckout
的第二个参数传递来查看或更新发送给 Billmate 的数据。
$billmate->initCheckout($articles, function (&$data) { data_set($data, 'PaymentData.autoactivate', '1'); });
查看
要渲染 Billmate 结账iframe,您可以在 blade 模板中使用 $checkout->iframe()
或编写自己的iframe,并将 $checkout->url
传递给其 src
属性。
JavaScript
为了在更新时更新结账的高度,我们需要这个 JavaScript。
window.addEventListener('message', function (event) { if (event.origin !== 'https://checkout.billmate.se') { return } try { var json = JSON.parse(event.data) } catch (e) { return } if (json.event === 'content_height') { $('#checkout').height(json.data) } })
重定向控制器
您需要自己的控制器来处理接受和取消重定向。
use Ttrig\Billmate\Order as BillmateOrder; use Ttrig\Billmate\Service as BillmateService; class YourRedirectController extends Controller { public function accept(BillmateService $billmate) { $order = new BillmateOrder(request()->data); $paymentInfo = $billmate->getPaymentInfo($order); return view('payment.accept'); } public function cancel() { return view('payment.cancel'); } }
回调控制器
如果您在 config/billmate.php
中使用 Ttrig\Billmate\Controllers\CallbackController::class
作为 "callback_action",则需要监听您的 EventServiceProvider
中的 Ttrig\Billmate\Events\OrderCreated
事件来处理订单。
protected $listen = [ \Ttrig\Billmate\Events\OrderCreated::class => [ \App\Listeners\DoSomething::class, ], ];
有关事件的更多信息,请参阅 https://laravel.net.cn/docs/10.x/events。
贡献
贡献使得开源社区成为一个如此神奇的地方,可以学习、启发和创造。您所做出的任何贡献都 非常受赞赏。
- 分支项目
- 创建您的功能分支(
git checkout -b amazing-feature
) - 提交您的更改(
git commit -m '添加一些惊人的功能'
) - 推送到分支(
git push origin amazing-feature
) - 打开拉取请求
许可证
laravel-billmate 是开源软件,受 MIT 许可证 许可。