PHP code example of x-class / omnipay-99bill
1. Go to this page and download the library: Download x-class/omnipay-99bill 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/ */
x-class / omnipay-99bill example snippets
$gateway->setTestMode(true);
$gateway = \Omnipay\Omnipay::create('Bill99');
$gateway->setMchId('mch-id'); //商户号
$gateway->setPrivateKey('private_key'); //私钥内容,可以是文件路径,也可以是内容,如果是内容,保持全在同一行。
$gateway->setReturnUrl('https://www.example.com/return');//支付成功后同步跳转的url
$gateway->setNotifyUrl('https://www.example.com/notify');//支付成功后异步通知url
$request = $gateway->purchase([
'orderId' => date('YmdHis') . mt_rand(1000, 9999),
'orderAmount' => 1,
]);
$response = $request->send();
$response->redirect();
exit;
$gateway = \Omnipay\Omnipay::create('Bill99');
$gateway->setMchId('mch-id'); //商户号
$gateway->setPrivateKey('private_key'); //私钥内容,可以是文件路径,也可以是内容,如果是内容,保持全在同一行。
$gateway->setReturnUrl('https://www.example.com/return');//支付成功后同步跳转的url
$gateway->setNotifyUrl('https://www.example.com/notify');//支付成功后异步通知url
$request = $gateway->wapPurchase([
'orderId' => date('YmdHis') . mt_rand(1000, 9999),
'orderAmount' => 1,
]);
$response = $request->send();
$response->redirect();
exit;
$gateway = Omnipay::create('Bill99');
$gateway->setMchId('mch-id'); //商户号
$gateway->setPublicKey('99bill_publickey'); //快钱下载的密钥内容,可以是文件路径,也可以是内容,如果是内容,保持全在同一行。
$request = $gateway->completePurchase();
$request->setParams(array_merge($_GET));//获取参数的方法可以用$_GET,也可以用某些框架自带的获取方法,总之要传入url中的Get参数
try {
$response = $request->send();
if ($response->isPaid()) {
$data = $response->getData();//业务参数
// @todo 支付成功业务逻辑处理,根据返回的业务参数,修改数据库中对应的订单状态
/** 这里需要注意,如果同步回调和同步回调(两者均为GET)在同一处处理,
* 需要通过一定方式区分是异步还是同步,两者返回信息不同,如用户登录状态
*/
//异步回调值,该返回值为快钱必需
die($response->asyncResult(true,$redirectUrl)); //成功第一个参数为true,第二个参数为要跳转的url
//同步回调
// redirect跳转页面...
} else {
// @todo 支付失败的业务逻辑
//异步回调值,该返回值为快钱必需
die($response->asyncResult(false)); //失败只需要传入false
}
} catch (Exception $e) {
// @todo 这里为支付异常业务逻辑
//异步回调值,该返回值为快钱必需
die($response->asyncResult(false)); //失败只需要传入false
}
$gateway = \Omnipay\Omnipay::create('Bill99');
$gateway->setMchId('mch-id'); //商户号
$gateway->setQueryKey('query_key');//交易查询key
//查询方式一
$request = $gateway->query([
'orderId' => '201805261456145505',
]);
//查询方式二
/*
$request = $gateway->query([
'queryType' => 1,
'startTime' => '20180501000101',
'endTime' => '20180527000101',
]);
*/
try {
$response = $request->send();
if ($response->isSuccessful()) {
$data=$response->getData();
//@todo 支付成功业务逻辑处理,根据返回的业务参数,修改数据库中对应的订单状态
}else{
// @todo 这里为支付异常业务逻辑
}
} catch (Exception $e) {
// @todo 这里为支付异常业务逻辑
}
$gateway = \Omnipay\Omnipay::create('Bill99');
$gateway->setMchId('mch-id'); //商户号
$gateway->setRefundKey('refund_key');//退款key
$request = $gateway->refund([
'orderId' => 'P270000180502933093',
'txOrder' => date('YmdHis'),
'amount' => 0.01,
'postdate' => date('YmdHis'),
]);
try {
$response = $request->send();
if ($response->isSuccessful()) {
$data=$response->getData();
// @todo 退款成功业务逻辑处理,根据返回的业务参数,修改数据库中对应的订单状态
}else{
// @todo 这里为退款异常业务逻辑
}
} catch (Exception $e) {
// @todo 这里为退款异常业务逻辑
}