A detailed explanation of native code scanning payment mode II of wechat payment integrated by ThinkPHP framework

Time:2020-3-24

This paper describes the second mode of native code scanning payment, which integrates wechat payment with ThinkPHP framework. To share with you for your reference, as follows:

Hello everyone, this article is the third in a series of wechat payment tutorials after wechat payment’s native code scanning payment mode I: code scanning payment mode II

Introduce that there are two modes of downscan payment, mode one is slightly more complex than mode two. As for the specific content, process and wechat development documents of mode one and mode two, there is no more nonsense here. Next, hurry to the tutorial!

First of all, we still import the class library of wechat payment:

 

Next is the file under public:

The above is the same as the native code scanning mode. Please refer to / / www.jb51.net/article/159356.htm for details

Jsapi payment: www.jb51.net/article/159351.htm

Next, let’s look at the code of the controller:

Step1: also, initialize the introduction of wxpaypubhelper class library

/**
     * initialization
     */
    public function _initialize()
    {
        //Introducing wxpaypubhelper
        vendor('WxPayPubHelper.WxPayPubHelper');
    }

Step 2: This is different from scan code payment mode 1: produce QR code according to the order and use unified payment interface. See the code:

public function createQrcode()
    {
        //Use unified payment interface
        $unifiedOrder = new \UnifiedOrder_pub();
        
        //Set unified payment interface parameters
        //Set required parameters
        //Appid has been filled in, merchants do not need to fill in it repeatedly
        //MCH? ID has been filled in, and the merchant does not need to fill in it again
        //Noncestr has been filled in, merchants do not need to fill in it repeatedly
        //Spbill "create" IP has been filled in, the merchant does not need to fill in it repeatedly
        //Sign has been filled in, merchants do not need to fill in it repeatedly
        $unifiedor - > setparameter ("body", "contribute one cent"); // product description
        //Custom order number, for example only
        $timeStamp = time();
        $out_trade_no = C('WxPayConf_pub.APPID')."$timeStamp";
        $unifiedor - > setparameter ("out trade no", "$out trade no"); // merchant order number 
        $unifiedor - > setparameter ("total fee", "1"); // total amount
        $unifiedor - > setparameter ("notify" URL ", C ('wxpayconf [pub. Notify" URL)); // notification address 
        $unifiedor - > setparameter ("trade" type "," natural "); // trade type
        //It is not a required parameter, which can be selected by the merchant according to the actual situation
        //$unifiedor - > setparameter ("sub_mch_id", "XXXX"); // sub merchant No  
        //$unifiedor - > setparameter ("device_info", "XXXX"); // device number 
        //$unifiedor - > setparameter ("attach", "XXXX"); // additional data 
        //$unifiedor - > setparameter ("time" start "," XXXX "); // transaction start time
        //$unifiedor - > setparameter ("time" expire "," XXXX "); // transaction end time 
        //$unifiedor - > setparameter ("goods" tag "," XXXX "); // product tag 
        //$unifiedor - > setparameter ("openid", "XXXX"); // user ID
        //$unifiedor - > setparameter ("product" Id "," XXXX "); // product ID
        
        //Get unified payment interface results
        $unifiedOrderResult = $unifiedOrder->getResult();
        
        //The merchant sets the corresponding processing flow according to the actual situation
        if ($unifiedOrderResult["return_code"] == "FAIL") 
        {
            //Merchant adds processing flow by itself
            Echo "communication error:. $unifiedorderesult ['return ﹣ MSG ']." < br > ";
        }
        elseif($unifiedOrderResult["result_code"] == "FAIL")
        {
            //Merchant adds processing flow by itself
            Echo "error code:. $unifiedorderesult ['err_code ']." < br > ";
            Echo "error code Description:. $unifiedordresult ['err_code_des']." < br > ";
        }
        elseif($unifiedOrderResult["code_url"] != NULL)
        {
            //Get the code "URL" from the unified payment interface
            $code_url = $unifiedOrderResult["code_url"];
            //Merchant adds processing flow by itself
            //......
        }
        $this->assign('out_trade_no',$out_trade_no);
        $this->assign('code_url',$code_url);
        $this->assign('unifiedOrderResult',$unifiedOrderResult);
        
        $this->display('qrcode');
    }

Corresponding to qrcode.html page:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    < title > wechat secure payment < / Title >
</head>
<body>
    <div align="center" id="qrcode">
    </div>
    <div align="center">
        <p>Order No.: < PHP echo $out? Trade? No;? ></p>
    </div>
    <div align="center">
        <form  action="./order_query.php" method="post">
            <input name="out_trade_no" type='hidden' value="<?php echo $out_trade_no; ?>">
            < button type = "submit" > query order status < / button >
        </form>
    </div>
    <br>
    <div align="center">
        <form  action="./refund.php" method="post">
            <input name="out_trade_no" type='hidden' value="<?php echo $out_trade_no; ?>">
            <input name="refund_fee" type='hidden' value="1">
            < button type = "submit" > apply for refund < / button >
        </form>
    </div>
    <br>
    <div align="center">
        < a href = ".. / index. PHP" rel = "external nofollow" > back to home page</a>
    </div>
</body>
    <script src="__PUBLIC__/js/qrcode.js"></script>
    <script>
        if(<?php echo $unifiedOrderResult["code_url"] != NULL; ?>)
        {
            var url = "<?php echo $code_url;?>";
            //Parameter 1 indicates image size, value range 1-10; parameter 2 indicates quality, value range 'l', 'm', 'Q', 'H'
            var qr = qrcode(10, 'M');
            qr.addData(url);
            qr.make();
            var wording=document.createElement('p');
            Wording.innerhtml = "scan me, scan me";
            var code=document.createElement('DIV');
            code.innerHTML = qr.createImgTag();
            var element=document.getElementById("qrcode");
            element.appendChild(wording);
            element.appendChild(code);
        }
    </script>
</html>

Mode 2 does not need to be configured with a public platform, so it’s simple here

Step3: asynchronous notification, the same here:

public function notify()
    {
        //Using the general notification interface
        $notify = new \Notify_pub();
         
        //Store wechat callback
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $notify->saveData($xml);
         
        //Verify the signature and respond to wechat.
        //When interacting with the background notification, if wechat does not receive the response from the merchant successfully or overtime, wechat considers the notification failed,
        //Wechat will regularly re launch notifications through certain strategies (for example, 8 times in 30 minutes),
        //Try to improve the success rate of notifications as much as possible, but wechat does not guarantee that notifications will eventually succeed.
        if($notify->checkSign() == FALSE){
            $notify - > setreturnparameter ("return" code "," fail "); // return status code
            $notify - > setreturnparameter ("return" MSG "," signature failed "); // return information
        }else{
            $notify - > setreturnparameter ("return" code "," success "); // set the return code
        }
        $returnXml = $notify->returnXml();
        echo $returnXml;
         
        //==The merchant sets the corresponding processing flow according to the actual situation, for example only=======
         
        //Record the callback information in the form of log file
        //         $log_ = new Log_();
        $log ﹐ name = ﹐ root ﹐ "/ public / notify ﹐ URL. Log"; // log file path
         
        $this - > log u result ($log u name, "[notify received]: \ n" $XML. "" \ n ");
         
        if($notify->checkSign() == TRUE)
        {
            if ($notify->data["return_code"] == "FAIL") {
                //The order status should be updated here. The merchant can add or delete the order by itself
                Log_result ($log_name, "[communication error]: \ n" $XML. "\ n");
            }
            elseif($notify->data["result_code"] == "FAIL"){
                //The order status should be updated here. The merchant can add or delete the order by itself
                Log_result ($log_name, "[business error]: \ n" $XML. "\ n");
            }
            else{
                //The order status should be updated here. The merchant can add or delete the order by itself
                Log_result ($log_name, "[payment succeeded]: \ n" $XML. "\ n");
            }
             
            //The merchant adds its own processing flow,
            //Example: update order status
            //Example: database operation
            //For example: push payment completion information
        }
    }

In this way, mode 2 scan payment is OK. Is it relatively simple?

Here is the test screenshot:

Code scanning screenshot:

Scanning result:

 

If you have any questions, please leave a message. Next, we will introduce the detailed course of wechat payment by credit card

Wechat payment tutorial jsapi:
//www.jb51.net/article/159351.htm

Wechat payment tutorial code scanning mode 1:
//www.jb51.net/article/159356.htm

Wechat payment tutorial credit card payment:
//www.jb51.net/article/159401.htm

For more information about ThinkPHP, readers who are interested in it can see the following topics: introduction to ThinkPHP, summary of operation skills of ThinkPHP template, summary of common methods of ThinkPHP, introduction to CodeIgniter, advanced tutorial of CI (CodeIgniter) framework, introduction to Zend framework and technical summary of PHP template.

I hope that this article will be helpful for you to design your PHP program based on the think PHP framework.