Home  >  Article  >  Backend Development  >  PHP implementation method related to WeChat mini program payment

PHP implementation method related to WeChat mini program payment

WBOY
WBOYOriginal
2023-05-31 21:10:342788browse

With the widespread use of WeChat mini programs, WeChat mini program payment has become a necessary payment method for more and more merchants. In order to facilitate merchants to access the WeChat applet payment function, this article will introduce an implementation method based on PHP.

1. Preparation work
Before accessing WeChat mini program payment, you need to prepare the following steps:

1.1 Obtain WeChat payment merchant number and API key
The merchant number is the unique identifier applied by the merchant on the WeChat payment platform, and the API key is the payment key generated when applying for the merchant and is used for signature verification.
1.2 Configure WeChat payment certificate
Merchant needs to place the WeChat payment certificate in the security directory of the server and provide the certificate path.
1.3 Build an HTTPS server
WeChat Pay requires merchants to use the HTTPS protocol for data transmission, so an HTTPS certificate needs to be configured on the server.

2. WeChat Mini Program payment process
The WeChat Mini Program payment process generally includes the following steps:

2.1 User orders
Users select products through the Mini Program and place orders , the applet sends the order information to the merchant server for processing.
2.2 Merchant Server Ordering
After receiving the user’s order request, the merchant server generates a prepaid order and returns it to the mini program.
2.3 The mini program activates payment
After receiving the prepayment order, the mini program activates the payment function through the WeChat payment API.
2.4 Payment result notification
After the payment is completed, the WeChat server will notify the merchant server of the payment result, and the merchant server will send a response to the WeChat server after processing the order.

3.PHP implementation method
The following is the implementation method of WeChat applet payment based on PHP:

3.1 Merchant server ordering
After the merchant server receives the user's order request, It is necessary to generate a prepayment order and return the prepayment order information to the applet. The following is the PHP code implementation for placing an order:

<?php
public function unifiedOrder($data)
{
    $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
    $data['appid'] = $this->appId;
    $data['mch_id'] = $this->mchId;
    $data['nonce_str'] = $this->getNonceStr();
    $data['notify_url'] = $this->notifyUrl;
    $data['trade_type'] = 'JSAPI';
    $data['openid'] = $openid;
    $data['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
    $data['sign'] = $this->sign($data);

    $xml = $this->arrayToXml($data);
    $response = $this->postXmlCurl($xml, $url);
    $result = $this->xmlToArray($response);
    return $result;
}

3.2 Mini program calls up payment
After the mini program receives the prepayment order, it needs to call the WeChat payment API for payment. The following is the PHP code implementation of the payment call:

<?php
public function getPayParams($prepayId)
{
    $data['appId'] = $this->appId;
    $data['timeStamp'] = time();
    $data['nonceStr'] = $this->getNonceStr();
    $data['package'] = 'prepay_id=' . $prepayId;
    $data['signType'] = 'MD5';
    $data['paySign'] = $this->sign($data);

    return $data;
}

3.3 Payment result notification
After the WeChat payment is completed, the WeChat server will notify the merchant server of the payment result. The following is the PHP code implementation of payment result notification:

<?php
public function notify()
{
    $xml = file_get_contents("php://input");
    $data = $this->xmlToArray($xml);

    if ($this->checkSign($data)) {
        //处理支付结果
        $orderNo = $data['out_trade_no'];
        $transactionId = $data['transaction_id'];
        //返回成功信息
        return $this->arrayToXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
    } else {
        return $this->arrayToXml(['return_code' => 'FAIL', 'return_msg' => '签名验证失败']);
    }
}

4. Summary
Through the implementation of the above steps, we can easily access the WeChat mini program payment function and provide mini program users with a more convenient payment method. . It should be noted that the security and accuracy of data need to be ensured during the payment process, while potential payment risks must be avoided.

The above is the detailed content of PHP implementation method related to WeChat mini program payment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn