gemfourmedia / omnipay-zalopay
Omnipay 的 ZaloPay 网关驱动程序。
v1.0.0
2023-02-05 18:29 UTC
Requires
- php: ^7.1|^8.0
- omnipay/common: ^3.0
Requires (Dev)
- omnipay/tests: ^3.0
- scrutinizer/ocular: ^1.5
README
ZaloPay 网关为 Omnipay League。
安装
正在进行中:通过 Composer
composer require gemfourmedia/omnipay-zalopay
用法
初始化网关
use Omnipay\Omnipay; $gateway = Omnipay::create('ZaloPay'); $gateway->initialize([ 'app_user' => 'Your App Name', 'app_id' => 'Provided by ZaloPay', 'key1' => 'Provided by ZaloPay', 'key2' => 'Provided by ZaloPay', 'testMode' => true // 'Enable sandbox mode', ]);
此网关对象将用于处理 ZaloPay 的请求/响应
创建购买
$appTime = floor(microtime(true) * 1000); $appTransId = date('ymd').$this->order->order_number.$appTime; $response = $gateway->purchase([ // Required parameters 'app_trans_id' => $appTransId, 'app_time' => $appTime, 'amount' => $order->amount, //Eg: 1000000 'description' => 'YOUR APP - Payment for order No. #'.$order->order_number, 'item' => json_encode([]), 'embed_data' => json_encode((object)['redirecturl' => 'https://your-domain.com/callback_url']), 'bank_code' => '', //''|CC|ATM|Domestic bank code detail at https://docs.zalopay.vn/v2/docs/gateway/api.html#mo-ta_dac-ta-api 'returnUrl' => 'https://your-domain.com/callback_listener', // This will assign to callback_url is use by ZaloPay // Optional parameters 'order_type' => 'GOODS', // can be:GOODS/TRANSPORTATION/HOTEL/FOOD/TELCARD/BILLING 'title' => 'Order Title', 'device_info' => json_encode([]), 'currency' => 'VND', 'phone' => '0902381299', 'email' => 'gemfourmedia@gmail.com', 'address' => '123 Alexandre De Rhodes', 'sub_app_id' => '', ])->send(); if ($response->isRedirect()) { $redirectUrl = $response->getRedirectUrl(); // TODO: redirect to $redirectUrl for customer can make payment via ZaloPay }
了解更多细节这里。
验证 ZaloPay 重定向
$response = $gateway->completePurchase()->send(); if ($response->isSuccessful()) { // TODO: Handle data. $data = $response->getData(); $appTransId = $data['apptransid']; // SUGGESTION: do query transaction to make sure transaction is successful: // $gateway()->queryTransaction(['app_trans_id' => $appTransId])->send() } else { print $response->getMessage(); }
了解更多细节这里。
检查 ZaloPay 的回调(IPN)
$response = $gateway->notification()->send(); if ($response->isSuccessful()) { // TODO: Handle data. $data = $response->getData(); } else { print $response->getMessage(); }
了解更多细节这里。
查询交易
$gateway()->queryTransaction(['app_trans_id' => $appTransId])->send() if ($response->isSuccessful()) { // TODO: handle data. $data = $response->getData() } else { print $response->getMessage(); }
了解更多细节这里。
退款
正在进行中
查询退款
正在进行中
调试
当 isSuccessful()
返回 FALSE
时使用的一些通用方法
print $response->getCode(); // Error Code From ZaloPay. print $response->getMessage(); // Error Message From ZaloPay.
对于 getCode()
的详细状态错误,请阅读这里。