search
HomeWeChat AppletMini Program DevelopmentWeChat Mini Program Payment Process

WeChat Mini Program Payment Process

Mar 25, 2017 pm 04:19 PM
developWeChat appletpay

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)