royryando / laravel-duitku
为 Laravel 简单的 Duitku 支付网关库。
Requires
- php: >=5.5
- ext-json: *
- guzzlehttp/guzzle: ^6.3 || ^7.0
Requires (Dev)
- orchestra/testbench: ^3.1.0
README
为 Laravel 简单的 Duitku 支付网关库。
要求
- PHP ≥ 5.5
- Laravel ≥ 5.1
安装
-
通过 composer 安装
composer require royryando/laravel-duitku
-
在 config/app.php 中添加 duitku 服务提供者:(Laravel 5.5+ 使用包自动发现,因此无需手动添加 ServiceProvider。)
'providers' => [ Royryando\Duitku\DuitkuServiceProvider::class ];
配置
- 在 .env 中添加所需变量
DUITKU_MERCHANT_CODE= DUITKU_API_KEY= DUITKU_CALLBACL_URL=https://example.com/callback/payment DUITKU_RETURN_URL=https://example.com/callback/return DUITKU_ENV=dev/production
用法
获取所有可用支付方式
参考:https://docs.duitku.com/api/id/#payment-method
通过整数参数 amount 从 Duitku 门面调用 paymentMethods 函数
Duitku::paymentMethods(100000)
返回一个数组,例如
[ ... [ 'code' => 'M1', 'name' => 'Bank Mandiri', 'image' => 'https://example.com/image.jpg', 'fee' => 0 ], ... ]
创建发票
参考:https://docs.duitku.com/api/id/#request-transaction
通过从 Duitku 门面调用 createInvoice 并传递以下参数创建发票或查询
订单 ID、金额、支付方式、产品名称、客户名称、客户电子邮件、到期时间(分钟)
Duitku::createInvoice('ORDER_ID', 100000, 'M1', 'Product Name', 'John Doe', 'john@example.com', 120);
成功时的返回
[ 'success' => true, 'reference' => 'D7999PJ38HNY7TSKHSGX', 'payment_url' => 'https://url.to.payment.example.com/', 'va_number' => '0000123123123', 'amount' => 100000, 'message' => 'SUCCESS' // message from Duitku ]
失败时的返回
[ 'success' => false, 'message' => 'The selected payment channel not available' // message from Duitku ]
检查发票状态
参考:https://docs.duitku.com/api/id/#check-transaction
通过调用检查发票或查询状态
Duitku::checkInvoiceStatus('order ID')
返回一个数组,例如
[ 'reference' => 'D7999PJ38HNY7TSKHSGX', // reference code from Duitku 'amount' => 100000, 'message' => 'SUCCESS', 'code' => '00', // 00=>Success, 01=>Pending, 02=>Failed/Expired ]
处理回调
参考:https://docs.duitku.com/api/id/#callback
-
创建一个新的控制器并扩展
Royryando\Duitku\Http\Controllers\DuitkuBaseController
use Royryando\Duitku\Http\Controllers\DuitkuBaseController; class DuitkuController extends DuitkuBaseController { // }
该控制器将处理来自 Duitku 的所有回调请求并存储成功/失败支付函数
-
在控制器内部,重写
onPaymentSuccess
函数。此函数会在收到成功的交易回调时触发... protected function onPaymentSuccess( string $orderId, string $productDetail, int $amount, string $paymentCode, string $shopeeUserHash, string $reference, string $additionalParam ): void { // Your code here } ...
-
在控制器内部,重写
onPaymentFailed
函数。此函数会在收到回调的失败状态时触发... protected function onPaymentFailed( string $orderId, string $productDetail, int $amount, string $paymentCode, string $shopeeUserHash, string $reference, string $additionalParam ): void { // Your code here } ...
-
在应用的路由 web.php 中添加具有支付回调功能的路由
Route::post('callback/payment', [\App\Http\Controllers\DuitkuController::class, 'paymentCallback']);
-
排除回调路由的 CSRF 验证
编辑
App\Http\Middleware\VerifyCsrfToken.php
protected $except = [ 'callback/payment', ];
待办事项
- 添加测试
- 添加退货回调支持
- 添加提现 API 支持