1. Go to this page and download the library: Download kongkx/omnipay-unionpay 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/ */
kongkx / omnipay-unionpay example snippets
$config = config('services.unionpay');
$gateway = Omnipay::create('UnionPay_Express');
$gateway->setMerId($config['merId']);
$gateway->setEncryptSensitive(true); // base on merchance config;
// Sandbox
$gateway->setCertPassword($config['signPassword']);
$gateway->setCertPath($config['signPfx']);
// production
// $gateway->setCertId($config['certId']);
// $gateway->setPrivateKey($config['privateKey']);
// $gateway->setEnvironment('production');
$gateway->setEncryptCert($config['encryptKey']);
$gateway->setRootCert($config['rootKey']);
$gateway->setMiddleCert($config['middleKey']);
return $gateway;
$order = [
'orderId' => date('YmdHis'), //Your order ID
'txnTime' => date('YmdHis'), //Should be format 'YmdHis'
'orderDesc' => 'My order title', //Order Title
'txnAmt' => '100', //Order Total Fee
];
//For PC/Wap
$response = $gateway->purchase($order)->send();
$response->getRedirectHtml();
//For APP
$response = $gateway->createOrder($order)->send();
$response->getTradeNo();
$response = $gateway->query([
'orderId' => '20150815121214', //Your site trade no, not union tn.
'txnTime' => '20150815121214', //Order trade time
'txnAmt' => '200', //Order total fee (cent)
])->send();
var_dump($response->isSuccessful());
var_dump($response->getData());
$response = $gateway->consumeUndo([
'orderId' => '20150815121214', //Your site trade no, not union tn.
'txnTime' => date('YmdHis'), //Regenerate a new time
'txnAmt' => '200', //Order total fee
'queryId' => 'xxxxxxxxx', // from order query or notification
])->send();
var_dump($response->isSuccessful());
var_dump($response->getData());
// 注意:
1. 银联退款时,必须加上 queryId,
2. 作为商户生成的订单号orderId与退款时的订单号是不一样的。也就意味着退款时的订单号必须重新生成。
3. txnAmt 这个参数银联是精确到分的。直接返回元为单位的值,将会出现报错信息。
// get the queryId first
$response = $gateway->query([
'orderId' => '20150815121214', //Your site trade no, not union tn.
'txnTime' => '20150815121214', //Order trade time
'txnAmt' => 200 * 100, //Order total fee; notice that: you should multiply the txnAmt by 100 with the Unionpay gateway. Such as 200 * 100;
])->send();
$queryId = ($response->getData())['queryId'];
$response = $gateway->refund([
'orderId' => '20150815121214', //Your site trade no, not union tn. notice: this orderId must not be the same with the order's created orderId.
'txnTime' => date('YmdHis'), //Order trade time
'txnAmt' => 200 * 100, //Order total fee; notice that: you should multiply the txnAmt by 100 with the Unionpay gateway. Such as 200 * 100;
'queryId' => $queryId
])->send();
var_dump($response->isSuccessful());
var_dump($response->getData());
$response = $gateway->fileTransfer([
'txnTime' => '20150815121214', //Order trade time
'settleDate' => '0119', //Settle Date
'fileType' => '00', //File Type
])->send();
var_dump($response->isSuccessful());
var_dump($response->getData());
$config = config('services.unionpay');
$gateway = Omnipay::create('UnionPay_Wtz');
$gateway->setMerId($config['merId']);
$gateway->setBizType('000301'); // 000301: Standard Version; 000902: Token Version;
$gateway->setEncryptSensitive(true); // base on merchance config;
// Sandbox
$gateway->setCertPassword($config['signPassword']);
$gateway->setCertPath($config['signPfx']);
// production
// $gateway->setCertId($config['certId']);
// $gateway->setPrivateKey($config['privateKey']);
// $gateway->setEnvironment('production');
$gateway->setEncryptCert($config['encryptKey']);
$gateway->setRootCert($config['rootKey']);
$gateway->setMiddleCert($config['middleKey']);
return $gateway;