Using UnionPay business to realize wechat applet payment


Recently, the company’s applet needs to use the payment function. We use the payment of UnionPay business. How can we connect the payment of UnionPay business’s wechat applet in the wechat applet? Here I record the payment process of wechat app that I implemented UnionPay business in wechat app

1: Interface part:

Note that the payment of test number is not supported in the payment of small program of UnionPay business at present, only the formal parameters are used to realize the payment of small program

1: Address of payment interface of UnionPay business applet:  

2: Description of main parameters:


Source: for example: WWW.TEST.COM


Message type: wechat applet payment parameters are fixed to: wx.unifiedOrder


Merchant No


Terminal number


Message request time, format yyyy MM DD HH: mm: SS


Order number. Please note that the prefix of the order number passed to UnionPay business must be the source number. For example, if the source number of the test number is 3194, then the order number here must start with 3194


Institution merchant number, fixed here as: minidefault


Order amount. The unit here is cent. For example, if it is one yuan, the parameter is 100


Transaction type: fixed as mini here


Signature algorithm: there are three kinds of parameters: MD5, SHA1 and RSA. Here I use MD5


Payment user openid


The payment result notification address and asynchronous callback address. Note: the payment notification address must be a pseudo-static address, such as xxxxx / back / index, not XXXX/ index.php?r=back/index


For signature, sort the above parameters in A-Z order. After sorting, connect them with & characters. If there are escape characters after connecting, remove the escape characters, and then use MD5 encryption to obtain the signature

A simple example of placing an order for an interface wechat applet is as follows:

$url = '';  
$requestTimestamp = date('Y-m-d H:i:s',time());  
$data = [  
    'msgsrc' = > xxx ', // message source  
    'msgType' => ' wx.unifiedOrder '// message type  
    'requestTimestamp' => $requestTimestamp,  
    'mid' => 'XXX',  
    'tid' => 'XXX',  
    'merOrderId' => 'XXX',  
    'instMid' => 'MINIDEFAULT',  
    'totalAmount' => 1,  
    'tradeType' => 'MINI',  
    'signType' => 'MD5',  
    'subOpenId' => 'XXX',  
    'notifyurl' = > xxx ', // callback address  
//Get sign parameter  
$options = '';  
foreach ($data as $key => $value) {  
    $options .= $key . '=' . $value .'&';  
$options = rtrim($options, '&');  
//If there are escape characters, remove them  
    $options = stripslashes($options);  
$sign = md5($options . $params['mdKey']);  
$data['sign'] = $sign;  
//Call UnionPay business WeChat small program single interface  
$client = new Client();  
$response = $client->createRequest()  
    ->Setmethod ('post ') // request method  
    ->Seturl ($URL) // request address  
    ->SetData ($data) // data to array  
    ->setHeaders(['Content-Type'=>'application/json']) //header  
    ->setFormat(Client::FORMAT_ JSON) // format of submitted data  
if (!$response->isOk) {  
    return [  
        'type' => 'error',  
        'message' = > 'payment call failed'  
if ($response->data\['errCode'] == 'SUCCESS') {  
    return [  
        'type' => 'success',  
        'message' = > payment call succeeded ',  
        'data' = > $response - > data \ ['minipayrequest '\], // request message for applet payment, with signature information  
} else {  
    return [  
        'type' => 'error',  
        'message' = > 'payment call failed' . $response->data['errMsg']  

2: Applet side:

The minipayrequest parameter returned by UnionPay business interface contains all the parameters of wechat applet call payment, so we can realize wechat applet payment only by filling in the data in the interface at wechat applet end

//Paydata here is the data parameter returned by the interface  
  timeStamp: payData.timeStamp,  
  nonceStr: payData.nonceStr,  
  package: payData.package,  
  signType: payData.signType,  
  paySign: payData.paySign,  
  success: (res) => {  
    //Payment processed successfully  
  fail: (res) => {  
    //Payment failure handling  

Here we have realized the connection of UnionPay business payment in wechat applet. The payment reconciliation of wechat applet is post transfer. The specific information is as follows:
Using UnionPay business to realize wechat applet payment

You can process it according to the specific needs of your project. Here, note that you need to enter success or failed at the end of the callback, otherwise this notice will appear

Recommended Today

Getting started with websocket

HTML5 has many attractive new features, such as canvas, local storage, multimedia programming interface, websocket and so on. Among them, the emergence of websocket makes it possible for browsers to provide support for socket, thus providing a two-way channel based on TCP connection between browser and server. With websocket, web developers can easily build real-time […]