Home  >  Article  >  Backend Development  >  PHP code to implement WeChat and mini-program scanning payment

PHP code to implement WeChat and mini-program scanning payment

不言
不言Original
2018-09-11 14:30:572826browse

The content of this article is about the PHP code for implementing WeChat and mini-program scanning payment. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<?php //微信支付

/**

 * 适用于微信所有支付, 扫码支付 小程序支付   二维码的需要接入生成二维码的代码,这个可以共用

*    xml 根据文档自行删改, 共用微信统一下单dome 

 */    
class Payapi
{

        统一下单
      public function Pay($orderid,$total_fee){   参数是订单号 加金额,  可以改动, 
          $fee = $total_fee;//举例充值0.01
          $appid =&#39;&#39;;//如果是公众号 就是公众号的appid
          $body = &#39;名字&#39;;
          $mch_id =&#39;商户号&#39;;
          $nonce_str = $this->nonce_str();//随机字符串
          $notify_url =&#39;回调地址&#39;;
          $out_trade_no = $orderid;//商户订单号
          $spbill_create_ip = &#39;主机ip&#39;;
          $total_fee = $fee*100;//因为充值金额最小是1 而且单位为分 如果是充值1元所以这里需要*100
          $trade_type = &#39;NATIVE&#39;;//交易类型 默认


          //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
          $post[&#39;appid&#39;] = $appid;
          $post[&#39;body&#39;] = $body;
          $post[&#39;mch_id&#39;] = $mch_id;
          $post[&#39;nonce_str&#39;] = $nonce_str;//随机字符串
          $post[&#39;notify_url&#39;] = $notify_url;
        //  $post[&#39;openid&#39;] = $openid;
          $post[&#39;out_trade_no&#39;] = $out_trade_no;
          $post[&#39;spbill_create_ip&#39;] = $spbill_create_ip;//终端的ip
          $post[&#39;total_fee&#39;] = $total_fee;//总金额 最低为一块钱 必须是整数
          $post[&#39;trade_type&#39;] = $trade_type;
          $sign = $this->sign($post);//签名
          $post_xml = &#39;<xml>
                 <appid>&#39;.$appid.&#39;</appid>
                 <body>&#39;.$body.&#39;</body>
                 <mch_id>&#39;.$mch_id.&#39;</mch_id>
                 <nonce_str>&#39;.$nonce_str.&#39;</nonce_str>
                 <notify_url>&#39;.$notify_url.&#39;</notify_url>
                 <openid>&#39;.$openid.&#39;</openid>
                 <out_trade_no>&#39;.$out_trade_no.&#39;</out_trade_no>
                 <spbill_create_ip>&#39;.$spbill_create_ip.&#39;</spbill_create_ip>
                 <total_fee>&#39;.$total_fee.&#39;</total_fee>
                 <trade_type>&#39;.$trade_type.&#39;</trade_type>
                 <sign>&#39;.$sign.&#39;</sign>
              </xml> &#39;;
          //统一接口prepay_id
          $url = &#39;https://api.mch.weixin.qq.com/pay/unifiedorder&#39;;
          $xml = $this->http_request($url,$post_xml);
          $array = $this->xml($xml);//全要大写
          if($array[&#39;RETURN_CODE&#39;] == &#39;SUCCESS&#39; && $array[&#39;RESULT_CODE&#39;] == &#39;SUCCESS&#39;){
              $time = time();
              $tmp=&#39;&#39;;//临时数组用于签名
              $tmp[&#39;appId&#39;] = $appid;
              $tmp[&#39;nonceStr&#39;] = $nonce_str;
              $tmp[&#39;package&#39;] = &#39;prepay_id=&#39;.$array[&#39;PREPAY_ID&#39;];
              $tmp[&#39;signType&#39;] = &#39;MD5&#39;;
              $tmp[&#39;timeStamp&#39;] = "$time";
              $data[&#39;state&#39;] = 1;
              $data[&#39;timeStamp&#39;] ="$time";//时间戳
              $data[&#39;nonceStr&#39;] = $nonce_str;//随机字符串
              $data[&#39;signType&#39;] = &#39;MD5&#39;;//签名算法,暂支持 MD5
              $data[&#39;package&#39;] = &#39;prepay_id=&#39;.$array[&#39;PREPAY_ID&#39;];//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
              $data[&#39;paySign&#39;] = $this->sign($tmp);//签名,具体签名方案参见微信公众号支付帮助文档;
              $data[&#39;out_trade_no&#39;] = $out_trade_no;
               $data[&#39;CODE_URL&#39;]  =$array[&#39;CODE_URL&#39;];
          }else{
              $data[&#39;state&#39;] = 0;
              $data[&#39;text&#39;] = "错误";
              $data[&#39;RETURN_CODE&#39;] = $array[&#39;RETURN_CODE&#39;];
              $data[&#39;RETURN_MSG&#39;] = $array[&#39;RETURN_MSG&#39;];
          }
        return  json_encode($data);

      }



private function nonce_str(){//随即字符串
          $result = &#39;&#39;;
          $str = &#39;QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz&#39;;
          for ($i=0;$i<32;$i++){
              $result .= $str[rand(0,48)];
          }
          return $result;
      }




      //生成订单号
      private function order_number($openid){
        // date(&#39;Ymd&#39;,time()).time().rand(10,99);//18位
      return md5($openid.time().rand(10,99));//32位
      }



      //签名 $data要先排好顺序
      private function sign($data){
      ksort($data);
          $stringA = &#39;&#39;;
          foreach ($data as $key=>$value){
              if(!$value) continue;
              if($stringA) $stringA .= &#39;&&#39;.$key."=".$value;
              else $stringA = $key."=".$value;
          }
          $wx_key = &#39;&#39;;//申请支付后有给予一个商户账号和密码,登陆后自己设置key
      $stringSignTemp = $stringA.&#39;&key=&#39;.$wx_key;//申请支付后有给予一个商户账号和密码,登陆后自己设置key
      return strtoupper(md5($stringSignTemp));
    }
      //curl请求啊
      function http_request($url,$data = null,$headers=array())
      {
          $curl = curl_init();
          if( count($headers) >= 1 ){
              curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
          }
          curl_setopt($curl, CURLOPT_URL, $url);


          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);


          if (!empty($data)){
              curl_setopt($curl, CURLOPT_POST, 1);
              curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
          }
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
          $output = curl_exec($curl);
          curl_close($curl);
          return $output;
      }


      //获取xml
      public function xml($xml){
          $p = xml_parser_create();
          xml_parse_into_struct($p, $xml, $vals, $index);
          xml_parser_free($p);
          $data = "";
          foreach ($index as $key=>$value) {
              if($key == &#39;xml&#39; || $key == &#39;XML&#39;) continue;
              $tag = $vals[$value[0]][&#39;tag&#39;];
              $value = $vals[$value[0]][&#39;value&#39;];
              $data[$tag] = $value;
          }
          return $data;
      }

}

Related recommendations:

WeChat scan red code WeChat payment scan code payment PHP version

PHP realizes WeChat code scanning Payment function

The above is the detailed content of PHP code to implement WeChat and mini-program scanning payment. 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