gemfourmedia / omnipay-zalopay
ZaloPay gateway driver for Omnipay.
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 gateway for Omnipay League.
Installation
WIP: via Composer:
composer require gemfourmedia/omnipay-zalopay
Usage
Init gateway:
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', ]);
This gateway object will use to handle request/response from ZaloPay
Create Purchase:
$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 }
Read more detail here.
Validate ZaloPay redirect:
$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(); }
Read more detail here.
Check callback (IPN) from ZaloPay
$response = $gateway->notification()->send(); if ($response->isSuccessful()) { // TODO: Handle data. $data = $response->getData(); } else { print $response->getMessage(); }
Read more detail here.
Query transaction:
$gateway()->queryTransaction(['app_trans_id' => $appTransId])->send() if ($response->isSuccessful()) { // TODO: handle data. $data = $response->getData() } else { print $response->getMessage(); }
Read more detail here.
Refund:
WIP
Query Refund:
WIP
Debug:
Some general methods use when isSuccessful()
return FALSE
:
print $response->getCode(); // Error Code From ZaloPay. print $response->getMessage(); // Error Message From ZaloPay.
Detail status errors for getCode()
read here.