PHPackages                             kongkx/omnipay-unionpay - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. kongkx/omnipay-unionpay

ActiveLibrary[Payment Processing](/categories/payments)

kongkx/omnipay-unionpay
=======================

UnionPay gateway for Omnipay payment processing library

v3.1.0(7y ago)03MITPHP

Since Aug 17Pushed 7y ago1 watchersCompare

[ Source](https://github.com/kongkx/omnipay-unionpay)[ Packagist](https://packagist.org/packages/kongkx/omnipay-unionpay)[ Docs](https://github.com/kongkx/omnipay-unionpay)[ RSS](/packages/kongkx-omnipay-unionpay/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (5)Versions (17)Used By (0)

Omnipay: UnionPay
=================

[](#omnipay-unionpay)

[![Build Status](https://camo.githubusercontent.com/a5d925c09772f19baf9cd5cd156e61715395b8d2c21b9848629dad2c727dfc1e/68747470733a2f2f7472617669732d63692e636f6d2f6b6f6e676b782f6f6d6e697061792d756e696f6e7061792e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/kongkx/omnipay-unionpay)[![Latest Stable Version](https://camo.githubusercontent.com/05fb46b802fecb9093535a1cc29654d713cfb3355f95ea59d6b67e8de4f0f1bb/68747470733a2f2f706f7365722e707567782e6f72672f6b6f6e676b782f6f6d6e697061792d756e696f6e7061792f76657273696f6e2e706e67)](https://packagist.org/packages/kongkx/omnipay-unionpay)[![Total Downloads](https://camo.githubusercontent.com/8071b494c1bad3d63eb72e11ff89bbe5f76ad9e6f316540cc5e62fb666f65fe9/68747470733a2f2f706f7365722e707567782e6f72672f6b6f6e676b782f6f6d6e697061792d756e696f6e7061792f642f746f74616c2e706e67)](https://packagist.org/packages/kongkx/omnipay-unionpay)

**UnionPay driver for the Omnipay PHP payment processing library**

在 [lokielse/omnipay-unionpay](https://github.com/lokielse/omnipay-unionpay) 的基础上更新

清理老网关接口，仅支持一下银联API

- Union\_Wtz (Union No Redirect Payment) 银联无跳转支付（alpha）Version: 5.1.0
- Union\_Express (Union Express Payment) 银联全产品网关（PC，APP，WAP 支付） Version: 5.1.0

[Omnipay](https://github.com/omnipay/omnipay) is a framework agnostic, multi-gateway payment processing library for PHP 7.1+. This package implements UnionPay support for Omnipay.

Installation
------------

[](#installation)

Omnipay is installed via [Composer](http://getcomposer.org/). To install, simply add it to your `composer.json` file:

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

And run composer to update your dependencies:

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

```

Express Gateway Usage
---------------------

[](#express-gateway-usage)

### Usage

[](#usage)

Sandbox Param can be found at: [UnionPay Developer Center](https://open.unionpay.com/ajweb/account/testPara)

### Init Gateway

[](#init-gateway)

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

### Consume

[](#consume)

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

### Return/Notify

[](#returnnotify)

Handle Return/Notification from UnionPay.

```
$response = $gateway->completePurchase(['request_params'=>$_REQUEST])->send();

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

### Query Order Status

[](#query-order-status)

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

### Consume Undo

[](#consume-undo)

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

### Refund

[](#refund)

```
// 注意：
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());
```

### File Transfer

[](#file-transfer)

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

For general usage instructions, please see the main [Omnipay](https://github.com/omnipay/omnipay)repository.

Wtz Gateway
-----------

[](#wtz-gateway)

Examples below work with wtz standard version.

**Note: You may checkout test cases for usage refrenece. 你可以通过查阅测试用例，了解接口之间的依赖关系**

### Init Gateway

[](#init-gateway-1)

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

### Open Query

[](#open-query)

```
// 通过账号查询
$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);
```

### Front Open

[](#front-open)

```
// 获取模版
$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);
```

### Sms Consume

[](#sms-consume)

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

### Consume

[](#consume-1)

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

### Query

[](#query)

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

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

### Refund

[](#refund-1)

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

Support
-------

[](#support)

If you are having general issues with Omnipay, we suggest posting on [Stack Overflow](http://stackoverflow.com/). Be sure to add the [omnipay tag](http://stackoverflow.com/questions/tagged/omnipay) so it can be easily found.

If you want to keep up to date with release anouncements, discuss ideas for the project, or ask more detailed questions, there is also a [mailing list](https://groups.google.com/forum/#!forum/omnipay) which you can subscribe to.

If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/lokielse/omnipay-unionpay/issues), or better yet, fork the library and submit a pull request.

About Test
----------

[](#about-test)

- 测试默认使用银联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
    ```
- 测试 只能保证 报文格式正确

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 76% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~100 days

Recently: every ~150 days

Total

15

Last Release

2569d ago

Major Versions

v0.3.10 → v3.0-alpha.12018-03-30

### Community

Maintainers

![](https://www.gravatar.com/avatar/237b163c89263c483a6a7c8646dcb65714e8201db4ba2dd1d06f073f9970fbe9?d=identicon)[kongkx](/maintainers/kongkx)

---

Top Contributors

[![lokielse](https://avatars.githubusercontent.com/u/1573211?v=4)](https://github.com/lokielse "lokielse (79 commits)")[![kongkx](https://avatars.githubusercontent.com/u/3049272?v=4)](https://github.com/kongkx "kongkx (23 commits)")[![jiker-burce](https://avatars.githubusercontent.com/u/24581223?v=4)](https://github.com/jiker-burce "jiker-burce (2 commits)")

---

Tags

laravelsdkpaymentgatewaypaymerchantomnipaypurchaseunionunionpay

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/kongkx-omnipay-unionpay/health.svg)

```
[![Health](https://phpackages.com/badges/kongkx-omnipay-unionpay/health.svg)](https://phpackages.com/packages/kongkx-omnipay-unionpay)
```

###  Alternatives

[lokielse/omnipay-unionpay

UnionPay gateway for Omnipay payment processing library

11358.3k2](/packages/lokielse-omnipay-unionpay)[lokielse/omnipay-alipay

Alipay gateway for Omnipay payment processing library

557422.2k11](/packages/lokielse-omnipay-alipay)[lokielse/omnipay-wechatpay

Wechat gateway for Omnipay payment processing library

314225.4k7](/packages/lokielse-omnipay-wechatpay)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
