php WeChat H5 payment

1. First go to the WeChat merchant platform to open H5, as shown below




This product has been activated in the company. If you have not activated it, you can fill in the required information, and then add a domain name to the development configuration. The domain name must be registered and approved.

Next, I will give you the code directly

$money= 1;// Recharge amount function http_post($url, $data) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_HEADER,0);
      curl_setopt($ ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      $res = curl_exec($ch);
      curl_close($ch);
      return $res;
 }
 $userip = $_SERVER["REMOTE_ADDR"]; // Get the IP of the user's device and go to Baidu on the Internet $appid = "ggg";// $mch_id = "fff" from WeChat;// WeChat official x
 $key = "kkkk" ;// Wechat merchant key
 set by yourself



$rand = rand(00000,99999);
 $out_trade_no = '20170804'.$rand;// Platform internal order number $nonce_str=MD5($out_trade_no);// Random string $body = " WeChat H5 payment ";/ / Content $total_fee = $money; // Amount $spbill_create_ip = $userip; //IP
 $notify_url = "http://www.erdangjiade.com/"; // Callback address $trade_type = 'MWEB';// Transaction The type is detailed in the API . $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.erdangjiade.com","wap_name":" WeChat H5 payment "}}';// Necessary parameters of scene information







$signA ="appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
 $str"Tmp=$signA."& // Note the order of splicing strings. Wechat has a test URL sequence according to his order. Click the correction test below, including whether the following XML   is correct . $sign = strtoupper(MD5($strSignTmp)); // Convert to uppercase after MD5 $post_data = "<xml>
                  <appid>$appid</appid>
                  <body>$body</body>
                  <mch_id>$mch_id</mch_id>
                  <nonce_str>$nonce_str</nonce_str>
                  <notify_url>
$notify_url</notify_url>
                 <out_trade_no>$out_trade_no</out_trade_no>



                 <scene_info>$scene_info</scene_info>
                 <spbill_create_ip>$spbill_create_ip</spbill_create_ip>
                 <total_fee>$total_fee</total_fee>
                 <trade_type>$trade_type</trade_type>
                 <sign>$sign</sign>
             </xml>";//拼接成XML 格式
 $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
 $dataxml = http_post($url,$post_data); //后台POST微信传参地址  同时取得微信返回的参数    POST 方法我写下面了
 $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
 echo "<pre>";
 echo <class="btn-green" id="getBrandWCPayRequests" href="<?php echo $objectxml['mweb_url'];?>">测试H5支付</a>;
 echo '<hr>';

其他常见错误,请参考:https://www.cnblogs.com/memor-y/p/7553837.html





Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326761223&siteId=291194637