About TP5-WeChat H5 Payment

Directly upload the code to use the official DEMO

We directly have a method to pay:

There will be a few problems

1: Remember that money is a digital type

2: When returning to the jump link at the end: remember to add &redirect_url= address to the tail, otherwise it will not be lifted

3: For browsers like Baidu APP, there will be no callback after successful payment. The value can get the ID value, and use this to determine whether to pay.

4: You can go to WeChat to obtain information such as APPid of other merchants!

 

        $key              = ;//appkey
        $appid            = ;//appid
        $mch_id           = ;//商户号
        $total_amount     = 1;  //总金额       
        $subject          = '商品描述'; //商品描述
        $additional       = '附加数据'; //附加数据
        $order_id         =  ; //订单号
        $nonce_str        =MD5($order_id);//随机字符串
        $spbill_create_ip =$this->getClientIp(); //终端ip     //以上参数接收不必纠结,按照正常接收就行,相信大家都看得懂
        $trade_type       = 'MWEB';//交易类型 具体看API 里面有详细介绍
        $notify_url       = '回调地址'; //回调地址
        $scene_info       ='{"h5_info":{"type":"Wap","wap_url":"https://mtxqg.mtxqg.com/","wap_name":"商品购买"}}'; //场景信息
        //对参数按照key=value的格式,并按照参数名ASCII字典序排序生成字符串
        $signA            = "appid=$appid&body=$subject&mch_id=$mch_id&nonce_str=$nonce_str&notify_url=$notify_url&out_trade_no=$order_id&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_amount&trade_type=$trade_type";
        //dump($signA);
        $strSignTmp       = $signA."&key=$key"; //拼接字符串
        $sign             = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
        $post_data        =  "<xml>
                        <appid>$appid</appid>
                        <body>$subject</body>
                        <mch_id>$mch_id</mch_id>
                        <nonce_str>$nonce_str</nonce_str>
                        <notify_url>$notify_url</notify_url>
                        <out_trade_no>$order_id</out_trade_no>
                        <scene_info>$scene_info</scene_info>
                        <spbill_create_ip>$spbill_create_ip</spbill_create_ip>
                        <total_fee>$total_amount</total_fee>
                        <trade_type>$trade_type</trade_type>
                        <sign>$sign</sign>
                        </xml>";

        $url            = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $dataxml        = $this->http_post($url,$post_data); 
        $objectxml      = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); 
        
        if($objectxml['return_code'] == 'SUCCESS')  {
            
            if($objectxml['result_code'] == 'SUCCESS'){
                
                
                Log::write($objectxml);
                
           
            }
          
        }

    

Guess you like

Origin blog.csdn.net/munchmills/article/details/126544256