• PHP微信H5支付开发


      近来公司项目要求用到微信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']; //商品描述        
          $total_amount = $data['total_amount']*100; //金额 $additional = $data['additional']; ////附加数据 $order_id = $data['order_id']; ////订单号 $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=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$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 = 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;
    }

     以上就是微信H5支付,电脑支付请使用PC扫码支付,原理类似,看文档即可

  • 相关阅读:
    LeetCode
    LeetCode
    Centos7防火墙快速开放端口配置方法
    SQLServer2008R2无人值守批处理脚本自动化安装
    sql server2014企业版无人值守批处理脚本自动化安装
    什么是Docker?
    安全终端模拟工具Xshell 5使用密钥认证登录配置详细教程
    SVN服务端VisualSVN数据转移说明
    RTX服务端用户数据迁移说明
    win7系统保护配置现错误“文件名、目录名或卷标语法不正确。(0x8007007B)
  • 原文地址:https://www.cnblogs.com/qichao123/p/7544016.html
Copyright © 2020-2023  润新知