PHP code example of gemfourmedia / omnipay-zalopay
1. Go to this page and download the library: Download gemfourmedia/omnipay-zalopay library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gemfourmedia / omnipay-zalopay example snippets
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',
]);
$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' => '[email protected]',
'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
}
$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();
}