Using UnionPay business to realize wechat applet payment

Time:2020-5-28

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:

https://qr.chinaums.com/netpay-route-server/api/  

2: Description of main parameters:

(1):msgSrc

Source: for example: WWW.TEST.COM

(2):msgType

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

(3):mid

Merchant No

(4):tid

Terminal number

(5):requestTimestamp

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

(6):merOrderId

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

(7):instMid

Institution merchant number, fixed here as: minidefault

(8):totalAmount

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

(9):tradeType

Transaction type: fixed as mini here

(10):signType

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

(11):subOpenId

Payment user openid

(12):notifyUrl

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

(13):sign

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 = 'https://qr.chinaums.com/netpay-route-server/api/';  
$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  
ksort($data);  
reset($data);  
$options = '';  
foreach ($data as $key => $value) {  
    $options .= $key . '=' . $value .'&';  
}  
$options = rtrim($options, '&');  
//If there are escape characters, remove them  
if(get_magic_quotes_gpc()){  
    $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  
    ->send();  
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  
wx.requestPayment({  
  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 […]