kongkx/omnipay-unionpay

Omnipay支付处理库的银联网关

v3.1.0 2019-06-18 15:32 UTC

This package is auto-updated.

Last update: 2024-09-20 02:42:03 UTC


README

Build Status Latest Stable Version Total Downloads

Omnipay PHP支付处理库的银联驱动程序

基于 lokielse/omnipay-unionpay 更新

清理旧网关接口,仅支持以下银联API

  • Union_Wtz (Union No Redirect Payment) 银联无跳转支付(alpha)版本:5.1.0
  • Union_Express (Union Express Payment) 银联全产品网关(PC,APP,WAP支付)版本:5.1.0

Omnipay 是一个与框架无关的多网关支付处理库,适用于PHP 7.1+。本包实现了Omnipay对银联的支持。

安装

Omnipay通过 Composer 安装。要安装,只需将其添加到您的 composer.json 文件中

{
  "require": {
    "kongkx/omnipay-unionpay": "^3.1.0"
  }
}

然后运行composer更新您的依赖项

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update

快速网关使用

使用方法

沙盒参数可以在: 银联开发者中心 找到

初始化网关

$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->completePurchase(['request_params'=>$_REQUEST])->send();

if ($response->isPaid()) {
    //pay success
}else{
    //pay fail
}

查询订单状态

$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());

有关一般使用说明,请参阅主要的 Omnipay 仓库。

Wtz网关

以下示例适用于wtz标准版本。

注意:您可以通过查阅测试用例了解接口之间的依赖关系。

初始化网关

$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;

开放查询

// 通过账号查询
$params = array(
    'orderId' => date('YmdHis'),
    'txnTime' => date('YmdHis'),
    'txnSubType' => '00',
    'accNo'  => '6226090000000048',
);

$response = $this->gateway->openQuery($params)->send();
$customerInfo = $response->getCustomerInfo(); // 包含 phoneNo,可用于请求消费短信
var_dump($response);
var_dump($customerInfo);

前端开放

// 获取模版
$params = array(
    'orderId'    => date('YmdHis'),
    'txnTime'    => date('YmdHis'),
    'accNo'      => '6226090000000048',
    'payTimeout' => date('YmdHis', strtotime('+15 minutes'))
);
$request = $this->gateway->frontOpen($params)
$response = $request->send();
$form = $response->getRedirectForm();
var_dump($response->getData());
var_dump($form);

短信消费

$params = array(
    'orderId' => date('YmdHis'),
    'txnTime' => date('YmdHis'),
    'txnAmt'  => 100,
    'accNo'   => '6226388000000095',
    'customerInfo' => [
        'phoneNo' => '18100000000',
    ]
);

$request = $this->gateway->smsConsume($params)
$response = $request->send();
var_dump($request->getData());
var_dump($response->getData());

消费

$params = array(
    'orderId' => date('YmdHis'),
    'txnTime' => date('YmdHis'),
    'txnAmt'  => 100,
    'accNo'   => '6226388000000095',
    'customerInfo' => [
        'smsCode' => '111111',  // 除了123456和654321固定反失败,其余固定成功。
        // 'smsCode' => '123456',
    ]
);

$request = $this->gateway->consume($params);
$response = $request->send();

var_dump($request->getData());
var_dump($response->getData());

查询

$params = array(
    'orderId' => $preData['orderId'],
    'txnTime' => $preData['txnTime'],
);
$request = $this->gateway->query($params);
$response = $request->send();

var_dump($request->getData());
var_dump($response->getData());

退款

$params = array(
    'orderId'   => date('YmdHis'), // 全新订单号
    'txnTime'   => date('YmdHis'),
    'origQryId' => $origQryId, // 查询/消费接口 返回的 queryId
    'txnAmt'    => 100,
);

$request = $this->gateway->refund($params)
$response = $request->send();

var_dump($request->getData());
var_dump($response->getData());

支持

如果您在使用Omnipay时遇到一般问题,我们建议在 Stack Overflow 上发布。确保添加 omnipay 标签,以便易于找到。

如果您想了解发布公告,讨论项目的想法或提出更详细的问题,还可以订阅一个 邮件列表

如果您认为您发现了一个错误,请使用 GitHub问题跟踪器 报告它,或者更好的是,分支库并提交一个pull请求。

关于测试

  • 测试默认使用银联Demo的商户号进行测试,但建议使用您自己申请的商户号,通过环境变量传入。

    export UNIONPAY_WTZ_MER_ID={merId}  #测试商户号
    export UNIONPAY_WTZ_TOKEN_ORDER_ID={orderId} # 供 token 版测试使用,完成一次 frontOpen, 记下该 `orderId`
    export UNIONPAY_WTZ_TOKEN_TXN_TIME={txnTime} # 供 token 版测试使用,完成一次 frontOpen, 记下该 `txnTime`
    export UNIONPAY_EXPRESS_MER_ID={anotherMerId} 
    phpunit 
  • 测试只能保证报文格式正确