Home  >  Article  >  WeChat Applet  >  WeChat Mini Program Payment Process

WeChat Mini Program Payment Process

迷茫
迷茫Original
2017-03-25 16:19:462427browse

Payment step logic:

1. The mini program initiates a request for prepayment

2. The server calls the interface to initiate prepayment information

3. The WeChat applet calls up the payment to complete the payment

1. The mini program initiates a request for prepayment

WeChat Mini Program Payment Process

2. The server calls the interface to initiate prepayment information

After the server receives the request, it calls WeChat's api interface. If the call is successful, a prepay_id will be obtained. This is equivalent to the prepayment id process of the mini program. A signature will be generated. Just generate it according to the instance parameters in the official document. If it is not required, you can choose not to fill it in, and then send an xml

# to the api.
#
<xml>
   <appid>wx2421b1c4370ec43b</appid>
   <attach>支付测试</attach>
   <body>JSAPI支付测试</body>
   <mch_id>10000100</mch_id>
   <detail><![CDATA[{ "goods_detail":[ { "goods_id":"iphone6s_16G", "wxpay_goods_id":"1001", "goods_name":"iPhone6s 16G", "quantity":1, "price":528800, "goods_category":"123456", "body":"苹果手机" }, { "goods_id":"iphone6s_32G", "wxpay_goods_id":"1002", "goods_name":"iPhone6s 32G", "quantity":1, "price":608800, "goods_category":"123789", "body":"苹果手机" } ] }]]></detail>
   <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
   <notify_url>[url]http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>[/url]
   <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
   <out_trade_no>1415659990</out_trade_no>
   <spbill_create_ip>14.23.150.211</spbill_create_ip>
   <total_fee>1</total_fee>
   <trade_type>JSAPI</trade_type>
   <sign>0CB01533B8C1EF103065174F50BCA001</sign>
</xml>

##It should be noted that the sign generation method is the same as the public account generation method. The following is the PHP signature generation method. It can be modified according to different frameworks. Remember to modify the key

##
 /**
     * 生成签名
     * @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
     */
    public function MakeSign()
    {
        //签名步骤一:按字典序排序参数
        ksort($this->_prepay);
        $string = $this->ToUrlParams();
        //签名步骤二:在string后加入KEY
        $string = $string . "&key=".WxPayConfig::KEY;
        //签名步骤三:MD5加密
        $string = md5($string);
        //签名步骤四:所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    /**
     * 格式化参数格式化成url参数
     */
    public function ToUrlParams()
    {
        $buff = "";
        foreach ($this->values as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
Call unified order api

##
/**
     * 统一下单调取返回值
     * @return mixed
     * $output[&#39;return_code&#39;] 状态码 SUCCESS/FAIL
     * $output[&#39;return_msg&#39;] 返回信息,如非空,为错误原因 签名失败 参数格式校验错误
     * $output[&#39;time&#39;] 当前时间戳
     * $output[&#39;nonceStr&#39;] 随机字符串
     * $output[&#39;prepay_id&#39;] 预支付id
     * $output[&#39;sign&#39;] 签名
     * */
    public function pay_place_order()
    {
        $xml = &#39;<xml>
                   <appid>&#39;.$this->_prepay[&#39;appid&#39;].&#39;</appid>
                   <body>&#39;.$this->_prepay[&#39;body&#39;].&#39;</body>
                   <mch_id>&#39;.$this->_prepay[&#39;mch_id&#39;].&#39;</mch_id>
                   <nonce_str>&#39;.$this->_prepay[&#39;nonce_str&#39;].&#39;</nonce_str>
                   <notify_url>&#39;.$this->_prepay[&#39;notify_url&#39;].&#39;</notify_url>
                   <openid>&#39;.$this->_prepay[&#39;openid&#39;].&#39;</openid>
                   <out_trade_no>&#39;.$this->_prepay[&#39;out_trade_no&#39;].&#39;</out_trade_no>
                   <spbill_create_ip>&#39;.$this->_prepay[&#39;spbill_create_ip&#39;].&#39;</spbill_create_ip>
                   <total_fee>&#39;.$this->_prepay[&#39;total_fee&#39;].&#39;</total_fee>
                   <trade_type>&#39;.$this->_prepay[&#39;trade_type&#39;].&#39;</trade_type>
                   <sign>&#39;.$this->MakeSign().&#39;</sign>
                </xml>&#39;;
//调用api,自定义对参数进行处理,改请求方式是自定义方式 
        $xml_result = post_request_https(&#39;https://api.mch.weixin.qq.com/pay/unifiedorder&#39;, $xml);

Regenerate sign from the returned parameters, get a new sign, and return to the applet

Note: The fields that generate the name of the returned applet signature and participate in the generated signature are as shown below. Remember to splice the key and use the value returned by calling the api

3. WeChat applet calls up payment and completes payment

WeChat Mini Program Payment ProcessNote: WeChat callback notification signature verification after successful payment , the required parameters are all returned parameter fields except the sign field, and the generated signature = sign (signature) in the returned field

#


The above is the detailed content of WeChat Mini Program Payment Process. 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