Home  >  Article  >  WeChat Applet  >  Detailed explanation of mini program payment and refund process

Detailed explanation of mini program payment and refund process

hzc
hzcforward
2020-07-03 11:10:557045browse

The payment and refund process of the WeChat mini program

When I was working on the WeChat mini program recently, it involved the payment and refund process of the mini program, so I also roughly summarized this aspect. I’ve read everything over and over, and I’ll summarize it in this blog.


First of all, let me explain that the main logic of WeChat applet payment is concentrated on the backend. The frontend only needs to carry the data required for payment to request the backend interface and then handle the corresponding success or failure based on the returned results. I use PHP on the backend. Of course, in this blog I am not going to post a bunch of code to explain the specific implementation of payment, but will mainly focus on the entire payment process and some details. Therefore, friends who use other back-end languages ​​​​can take a look if necessary. Many times, development needs and corresponding problem solutions really need to go beyond the language syntax level and be considered from the perspective of systems and processes. Okay, no more nonsense. Into the title.

1. Payment

Payment is mainly divided into several steps:

The front end carries the data required for payment (commodity id, purchase quantity, etc.) to initiate a payment request

After receiving the payment request, the backend processes the payment data, and then carries the processed data requestWeChat serverPayment unified order interface

The backend receives the return data from the previous request to the WeChat server, processes it again, and then returns to the frontend so that the frontend can start payment.

Front-end performs payment action

After the front-end payment is completed, the WeChat server will send a payment notification to the back-end (that is, WeChat wants to tell you that the customer has paid), and the back-end will act according to this notification Make sure the payment is completed, and then take the corresponding actions after the payment is completed, such as modifying the order status, adding transaction logs, etc.

从这几个步骤可以看出,后端主要的作用就是将支付需要的数据传给微信服务器,再根据微信服务器的响应确定支付是否完成。

这个流程还是蛮容易理解的。形象的说,前端就是个顾客,后端就是店家,微信服务器的统一下单接口就像收银员。顾客跟店家说,
我是谁谁谁,现在我要付多少多少钱给你买什么什么。店家就跟收银员说,那个谁谁谁要付多少钱,你准备收钱吧。收银员收到钱
后,就去告诉店家,我已经收到钱了,你给他东西吧。

The following is a detailed explanation of the specific implementation of each step.

1. Front-end request for payment

前端请求支付,就是简单的携带支付需要的数据,例如用户标识,支付金额,支付订单 ID 等等跟 **你的业务逻辑有关** 或者跟
 **下一步请求微信服务器支付统一下单接口需要的数据有关** 的相关数据,使用微信小程序的 wx.request( ) 去请求后端的支
 付接口。

2. Front-end request for payment

后端接收到前端发送的支付请求后,可以进行一下相关验证,例如判断一下用户有没有问题,支付金额对不对等等。
在验证没什么问题,可以向微信服务器申请支付之后,后端需要使用 微信规定的数据格式 去请求微信的支付统一下单接口。

Request data specified by WeChat:
This requires more code accomplish. Because the amount of data required is large, it also needs to be encrypted and sent in XML format.
First of all, the following data are parameters that must be provided to the WeChat server when using mini program payment.

Mini program appid. There is probably no one who writes small programs who doesn’t know this. . .

User ID openid. That is the user's mini program ID. I explained how to obtain it in my last blog.

Merchant number mch_id. After successfully applying for WeChat payment merchant certification, the email sent to you by WeChat will contain

merchant order number out_trade_no. The order number

generated by the merchant for this payment is the total amount total_fee. The total amount of the order, a very important point is that the unit is cents, so pay special attention to it.

WeChat server callback notification interface address notify_url. After WeChat confirms that the money has arrived, it will send multiple messages to this address to tell you that the customer has paid.

You need to return a message to WeChat to indicate that you have received the notification. . This address cannot have a port number, and must be able to directly accept POST method requests.

Transaction type trade_type. The WeChat applet payment value is unified as JSAPI

Product Information Body. Similar to the format of "Tencent-Game"

Terminal IP address spbill_create_ip. Terminal address IP, which is the IP address requesting payment.

Random string nonce_str. A string randomly generated by the backend is required to ensure data security. WeChat requires no longer than 32 bits.

signature sign. Use all the above parameters to process the encryption and generate the signature accordingly. (The specific processing method can be seen in the code below, which can be reused directly.)

After processing all the above data, organize the data in XML format and send it to WeChat Payment Unification using the POST method Order interface https://api.mch.weixin.qq.com/pay/unifiedorder.

3. The backend accepts the data returned by the WeChat server

After the WeChat server receives the payment data, if there is no problem with the data, it will return the corresponding data for payment. The most important thing is The data field named prepay_id needs to return this data to the front end so that the front end can continue to pay.

Therefore, after the back-end receives the return data from the WeChat server, it needs to perform corresponding processing, and finally return the following data to the front-end:

  1. appid No Need to say more

  2. timeStamp current timestamp

  3. nonceStr random string

  4. package That’s it Mentioned prepay_id, but remember the format is like "prepay_id= prepay_id_item". Otherwise an error will result.

  5. signType Encryption method, generally should be MD5

  6. paySign Process and encrypt the above data accordingly.

到这里,后端的支付接口已经完成了接收前端支付请求,并返回了前端支付所需数据的功能。

4. 前端发起支付

前端在接收到返回数据后,使用 wx.requestPayment() 来请求发起支付。此 API 需要的对象参数各项值就是我们上一步返回的各个数据。

5.后端接受微信服务器回调

前端完成支付后,微信服务器确认支付已经完成。就会向第一步中设置的回调地址发送通知。后端的接收回调接口在接收到通知后,就可以判断支付是否完成,从而决定后续动作。

需要注意的是,在接收到微信服务器的回调通知后,根据通知的result_code字段判断支付是否成功。在接受到成功的通知后,后端需要返回success数据向微信服务器告知已得到回调通知。否则微信服务器会不停的向后端发送消息。另外微信的通知是以XML格式发送的,在接受处理时需要注意。

微信的大概支付流程就是这样。以下是PHP语法的微信支付类,可以比照上面的步骤介绍,加深理解。在需要支付时,直接传入参数实例化此类再调用类的 pay 方法即可。

//微信支付类
class WeiXinPay{

    //=======【基本信息设置】=====================================
    //微信公众号身份的唯一标识
    protected $APPID = appid;//填写您的appid。微信公众平台里的
    protected $APPSECRET = secret;
    //受理商ID,身份标识
    protected $MCHID = '11111111';//商户id
    //商户支付密钥Key
    protected $KEY = '192006250b4c09247ec02edce69f6a2d';
    //回调通知接口
    protected $APPURL =      'https://smart.afei.com/receivesuc';
    //交易类型
    protected $TRADETYPE = 'JSAPI';
    //商品类型信息
    protected $BODY = 'wx/book';

    //微信支付类的构造函数
    function __construct($openid,$outTradeNo,$totalFee){
        $this->openid = $openid; //用户唯一标识
        $this->outTradeNo = $outTradeNo; //商品编号
        $this->totalFee = $totalFee; //总价
    }
    
    //微信支付类向外暴露的支付接口
    public function pay(){
        $result = $this->weixinapp();
        return $result;
    }
     
     //对微信统一下单接口返回的支付相关数据进行处理
     private function weixinapp(){
         $unifiedorder=$this->unifiedorder();

         $parameters=array(
          'appId'=>$this->APPID,//小程序ID
          'timeStamp'=>''.time().'',//时间戳
          'nonceStr'=>$this->createNoncestr(),//随机串
          'package'=>'prepay_id='.$unifiedorder['prepay_id'],//数据包
          'signType'=>'MD5'//签名方式
             );
         $parameters['paySign']=$this->getSign($parameters);

         return $parameters;
     }

    /*
     *请求微信统一下单接口
     */
    private function unifiedorder(){
        $parameters = array(
            'appid' => $this->APPID,//小程序id
            'mch_id'=> $this->MCHID,//商户id
            'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],//终端ip
            'notify_url'=>$this->APPURL, //通知地址
            'nonce_str'=> $this->createNoncestr(),//随机字符串
            'out_trade_no'=>$this->outTradeNo,//商户订单编号
            'total_fee'=>floatval($this->totalFee), //总金额
            'open_id'=>$this->openid,//用户openid
            'trade_type'=>$this->TRADETYPE,//交易类型
            'body' =>$this->BODY, //商品信息
        );
        $parameters['sign'] = $this->getSign($parameters);
        $xmlData = $this->arrayToXml($parameters);
        $xml_result = $this->postXmlCurl($xmlData,'https://api.mch.weixin.qq.com/pay/unifiedorder',60);
        $result = $this->xmlToArray($xml_result);
        return $result;
    }

   //数组转字符串方法
   protected function arrayToXml($arr){
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                 $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml;
    }

    protected function xmlToArray($xml){
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $array_data;
    }
    
    //发送xml请求方法
    private static function postXmlCurl($xml, $url, $second = 30)
    {
        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        set_time_limit(0);
        //运行curl
        $data = curl_exec($ch);
        //返回结果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            curl_close($ch);
            throw new WxPayException("curl出错,错误码:$error");
        }
    }


    /*
     * 对要发送到微信统一下单接口的数据进行签名
     */
    protected function getSign($Obj){
         foreach ($Obj as $k => $v){
          $Parameters[$k] = $v;
         }
         //签名步骤一:按字典序排序参数
         ksort($Parameters);
         $String = $this->formatBizQueryParaMap($Parameters, false);
         //签名步骤二:在string后加入KEY
         $String = $String."&key=".$this->KEY;
         //签名步骤三:MD5加密
         $String = md5($String);
         //签名步骤四:所有字符转为大写
         $result_ = strtoupper($String);
         return $result_;
     }

    /*
     *排序并格式化参数方法,签名时需要使用
     */
    protected function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if($urlencode)
            {
               $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar;
        if (strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }

    /*
     * 生成随机字符串方法
     */
    protected function createNoncestr($length = 32 ){
         $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
         $str ="";
         for ( $i = 0; $i < $length; $i++ ) {
          $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
         }
         return $str;
         }
}

以上就是微信支付的相关流程。在理清思路后,流程还是比较清晰和简单的。重点在于需要注意一些细节问题,例如数据格式,加密方法等。

下面说一下微信小程序退款的具体实现

二.退款

小程序退款的流程和付款相似,但有一些细节上的不同。

首先退款的步骤通常如下:

  1. 用户前端点击退款按钮后,后端接收到用户的退款请求通过商城后台呈现给商户,商户确定允许退款后,后端再发起向微信退款接口的请求来请求退款。
  2. 后端向微信退款接口发送请求后,得到响应信息,确定退款是否完成,根据退款是否完成再去进行改变订单状态等业务逻辑。

退款的步骤相对微信支付来说比较简单。

值得注意的有以下两点:
1.向微信退款接口请求退款后,根据得到的响应是可以直接确定退款是否完成的。不再需要设置专门的回调接口等待微信通知。当然如果需要也是可以在微信商户平台设置回调接口接受从而接受微信回调的,但并不是必须的。
2.退款请求需要在请求服务器安装微信提供的安全证书,也就是说,发起退款请求相比较支付请求在请求时请求方法不能复用,因为微信退款需要携带证书的请求,此证书可在申请微信商户号成功后从微信商户平台自行下载,Linux下的PHP开发环境的证书只需要放在网站根目录的cert文件夹中即可。其他开发环境可能需要导入操作。

下面讲解一下退款的具体步骤

一. 用户发起退款请求

用户在前端发起退款请求,后端接收到退款请求,将相应订单标记为申请退款,展示在后台.商户查看后,如果同意退款再进行相应操作.
此后才进入真正的退款流程.

二. 商户发起退款请求

    商户同意退款后,后端即向微信提供的退款 API 发起请求.
    同请求微信支付API一样.退款请求也需要将需要的参数进行签名后以XML发送到微信的退款API 
    [https://api.mch.weixin.qq.com/pay/refund](https://api.mch.weixin.qq.com/pay/refund)

退款请求需要的参数如下(多个参数在支付API请求时也有使用):

  1. 小程序 appid。
  2. 商户号 mch_id 。申请开通微信支付商户认证成功后微信发给你的邮件里有
  3. 商户订单号 out_trade_no 。退款订单在支付时生成的订单号
  4. 退款订单号 out_refund_no 。由后端生成的退款单号,需要保证唯一,因为多个同样的退款单号只会退款一次。
  5. 总金额 total_fee 。订单总金额,单位为分。
  6. 退款金额 refund_fee 需要退款的金额,单位同样为分
  7. 操作员 op_user_id .与商户号相同即可
  8. 随机字符串 nonce_str 。同支付请求
  9. 签名 sign 。使用上面的所有参数进行相应处理加密生成签名。(具体处理方式与支付相同,可直接复用。)

三. 退款完成

在发起退款请求后,就可以直接根据请求的响应XML中的	result_code字段来判断退款是否成功,
从而对订单状态进行处理和后续操作。不需要像支付那样等待另一个接口的通知来确定请求状态。当然如上文所说,
如果需要微信服务器发送通知到后端的话,可以到微信商户平台进行设置。

退款因为流程与支付大同小异,因此退款的PHP类我选择了直接继承支付类,
代码如下,注意区分退款请求方法postXmlSSLCurl和支付请求方法postXmlCurl的区别,这也就是上文提到的退款需要的双向证书的使用。

   
     class WinXinRefund extends WeiXinPay{
        protected \$SSLCERT_PATH = &#39;cert/apiclient_cert.pem&#39;;//证书路径
        protected \$SSLKEY_PATH =  &#39;cert/apiclient_key.pem&#39;;//证书路径
        protected \$opUserId = &#39;1234567899&#39;;//商户号
        
    function __construct($openid,$outTradeNo,$totalFee,$outRefundNo,$refundFee){
        //初始化退款类需要的变量
        $this->openid = $openid;
        $this->outTradeNo = $outTradeNo;
        $this->totalFee = $totalFee;
        $this->outRefundNo = $outRefundNo;
        $this->refundFee = $refundFee;
    } 

    public function refund(){
        //对外暴露的退款接口
        $result = $this->wxrefundapi();
        return $result;
    }

    private function wxrefundapi(){
        //通过微信api进行退款流程
        $parma = array(
            'appid'=> $this->APPID,
            'mch_id'=> $this->MCHID,
            'nonce_str'=> $this->createNoncestr(),
            'out_refund_no'=> $this->outRefundNo,
            'out_trade_no'=> $this->outTradeNo,
            'total_fee'=> $this->totalFee,
            'refund_fee'=> $this->refundFee,
            'op_user_id' => $this->opUserId,
        );
        $parma['sign'] = $this->getSign($parma);
        $xmldata = $this->arrayToXml($parma);
        $xmlresult = $this->postXmlSSLCurl($xmldata,'https://api.mch.weixin.qq.com/secapi/pay/refund');
        $result = $this->xmlToArray($xmlresult);
        return $result;
    }

    //需要使用证书的请求
    function postXmlSSLCurl($xml,$url,$second=30)
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        //这里设置代理,如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        //设置header
        curl_setopt($ch,CURLOPT_HEADER,FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
        //设置证书
        //使用证书:cert 与 key 分别属于两个.pem文件
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT, $this->SSLCERT_PATH);
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY, $this->SSLKEY_PATH);
        //post提交方式
        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
        $data = curl_exec($ch);
        //返回结果
        if($data){
            curl_close($ch);
            return $data;
        }
        else {
            $error = curl_errno($ch);
            echo "curl出错,错误码:$error"."<br>";
            curl_close($ch);
            return false;
        }
    }}

## 三. 总结
    以上就是关于微信支付和退款的流程及相关知识的介绍。文中的 PHP类 均封装直接可用。
    因为微信支付和退款涉及的东西较为繁杂,很多人直接看官方文档可能会一头雾水,所以看过此文了解流程和要点后,再去看微信官方文档。一方面可以更清晰的了解小程序的支付和退款流程。另一方面,本文因为篇幅有限及作者能力有限,肯定有无暇顾及或有所纰漏之处。为求稳妥,还是需要多看看官方开发文档。毕竟事涉支付,出个BUG可不是小事。
    最后扯点闲话吧。这篇博客本来应该在三个月前就发表的,也算当时我从一无所知到独立完成微信小程序商城前后端的总结系列的第一篇。但是公司突然出现人员和项目的变动,导致管理和项目上都混乱不堪,再加上个人的惰性,导致此篇博客一直拖到三个月后的今天才断断续续写完。这三个月我的心态因为各种事起起伏伏,也颇有一番风味。
    借用李志的一句歌词结束这篇博客吧。下一篇是什么时候也说不定了,我苦笑。
    
>我再也不会把自己,愚蠢的交给过去。我的生活和我的想法,从此相隔万里。

推荐教程:《微信小程序

The above is the detailed content of Detailed explanation of mini program payment and refund process. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete