PHP微信H5支付开发

本秂侑毒 提交于 2020-01-19 19:05:25

近来公司项目要求用到微信H5开发,因为微信开发文档处处都是坑,我也走了不少弯路,现在就把H5支付的过程记录一下,已备后用!!

首先 先去商户平台申请开通 H5支付!!!!

我们从微信官方下载H5支付demo,(忘记了没有demo,自己写吧,蛋疼!)微信H5支付文档请点击这里(为了方便查看我用了_blank)。

官方是给我们提供了案例的大家可以移步查看—>微信官方体验链接:http://wxpay.wxutil.com/mch/pay/h5.v2.php,请在微信外浏览器打开。

官方提供的流程,大家可以看看是不是你想要的样子,以防止写错 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_3

大概思路:前台传过来参数后台接收比如金额(注意这里的金额,微信传的是分),订单号,客户端ip(获取客户端ip方法,官方给的方法不对,请查看我的另一篇)等。

用到的最重要的接口是 统一下单

复制代码
      $subject = data[subject];//      data['subject']; //商品描述       total_amount = $data[‘total_amount’]*100; //金额
$additional = $data[‘additional’]; ////附加数据
$order_id = $data[‘order_id’]; ////订单号
noncestr=MD5(nonce_str=MD5(order_id);//随机字符串
$spbill_create_ip = $data[‘spbill_create_ip’]; //终端ip
     //以上参数接收不必纠结,按照正常接收就行,相信大家都看得懂

    //$spbill_create_ip = '118.144.37.98'; //终端ip测试
    $trade_type = 'MWEB';//交易类型 具体看API 里面有详细介绍

    $notify_url = 'http://xy.xxx.com/medias/public/index.php/home/Wxh5pay/notify_url'; //回调地址
    $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.123.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=KaTeX parse error: Expected 'EOF', got '&' at position 11: scene_info&̲spbill_create_i…spbill_create_ip&total_fee=KaTeX parse error: Expected 'EOF', got '&' at position 13: total_amount&̲trade_type=trade_type";

    $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>";//拼接成XML 格式

    $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
    $dataxml = $this->http_post($url,$post_data); //后台POST微信传参地址  同时取得微信返回的参数,http_post方法请看下文
    $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
    if($objectxml['return_code'] == 'SUCCESS')  {
        if($objectxml['result_code'] == 'SUCCESS'){//如果这两个都为此状态则返回mweb_url,详情看‘统一下单’接口文档
            return $objectxml['mweb_url']; //mweb_url是微信返回的支付连接要把这个连接分配到前台
        }
        if($objectxml['result_code'] == 'FAIL'){

return $err_code_des = $objectxml[‘err_code_des’];

}}
复制代码

微信支付接口签名校验工具:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=20_1
大家可以使用以上工具,检测您的签名是不是正确。

function http_post($url, $data) {
ch=curlinit();curlsetopt(ch = curl_init(); curl_setopt(ch, CURLOPT_URL,url);curlsetopt(url); curl_setopt(ch, CURLOPT_HEADER,0);
curl_setopt(ch,CURLOPTRETURNTRANSFER,1);curlsetopt(ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt(ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
res=curlexec(res = curl_exec(ch);
curl_close($ch);
return $res;
}
以上就是微信H5支付,电脑支付请使用PC扫码支付,原理类似,看文档即可

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!