微信h5支付是移动端电商网页必不可少的功能,今天我们就带领大家来实现thinkphp6对接实现微信h5支付。
一、准备工作
做微信支付同样需要企业资质,需要拥有一个已经认证的微信服务号,当然这个是需要缴纳300元给微信的。
具体申请的资质后,我们打开微信支付平台,在导航上点击“产品中心”-》点击‘h5支付’,在打开的页面中,填写并配置相关域名信息,填写好之后就可以等待微信官方审核通过了。如果申请不通过,微信会告知原因,直到h5支付通过开通成功。
二、对接微信h5支付
我们首先打开微信h5支付的官方文档,很遗憾,微信h5支付并没有像native扫码支付那样给我们准备官方sdk&demo,没有官方sdk&demo,我们就需要自己集成了。
微信h5支付并不难,我们完全阅读统一下单章节。
1、准备参数。
微信官方已经给我们详细的列举了参数,这些参数获取并不难,我们需要注意两个参数:一个是签名sign,一个是终端ipspbill_create_ip。
签名sign官方已经给出了详细的说明,我们按key1=value1&key2=value2形式根据ascii码从小到大排序。排序后得到stringa,然后再用得到的stringa和key拼接,最后都转成大写。
我们来看详细代码:
public function index(){ $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址 //1.获取调用统一下单接口所需必备参数 $appid = 'wxff5b6b241a4fb11';//微信公众号appid $mch_id = '152223331';//微信支付商户号 $key = 'b304911d6a9f7728d264dfd695ebae1';//自己设置的微信商家key $out_trade_no = time();//平台内部订单号 $nonce_str=md5(time());//随机字符串 $body = '商品购买';//付款内容 $total_fee = 1;//订单总金额,单位为分 $spbill_create_ip = $this -> get_client_ip(); //获得用户设备ip $attach = 'weixinh5';//附加数据(自定义,在支付通知中原样返回) $notify_url = "http://www.xxx.cn/mobile/wechatpay/notify";//通知地址 $trade_type = 'mweb';//交易类型,微信h5支付时固定为mweb //2.将参数按照key=value的格式,并按照参数名ascii字典序排序生成字符串 $signa ="appid=$appid&attach=$attach&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type"; //3.拼接字符串 $strsigntmp = $signa."&key=$key"; //4.md5加密后转换成大写 $sign = strtoupper(md5($strsigntmp)); //5.拼接成所需xml格式 $post_data = "<xml> <appid>$appid</appid> <attach>$attach</attach> <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> <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>"; }在这里用到一个$this -> get_client_ip()获取ip地址,我们看该方法的代码:
public function get_client_ip() { if(getenv('http_client_ip') && strcasecmp(getenv('http_client_ip'), 'unknown')) { $ip = getenv('http_client_ip'); } elseif(getenv('http_x_forwarded_for') && strcasecmp(getenv('http_x_forwarded_for'), 'unknown')) { $ip = getenv('http_x_forwarded_for'); } elseif(getenv('remote_addr') && strcasecmp(getenv('remote_addr'), 'unknown')) { $ip = getenv('remote_addr'); } elseif(isset($_server['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], 'unknown')) { $ip = $_server['remote_addr']; } return preg_match ( '/[\\\\d\\\\.]{7,15}/', $ip, $matches ) ? $matches [0] : ''; }2、将参数post到官方地址。
官方的地址是url地址:https://api.mch.weixin.qq.com/pay/unifiedorder,这个地址是不允许更改的。我们把准备好的xml参数post过去。
$dataxml = $this -> httprequest($url,'post',$post_data);
这里的httprequest方法是用来发送post数据的:
public function httprequest($url, $method, $postfields = null, $headers = array(), $debug = false) { $method = strtoupper($method); $ci = curl_init(); /* curl settings */ curl_setopt($ci, cu
【滁州网站建设】如何选择适合企业的网站建设公司?SEO优化干货:什么样的网站页面才算是高质量的页面百度云服务器新用户阿里云云服务器ecs怎么用阿里云oss挂载服务器以上两个域名请帮忙看下转入的情况wps段落设置在哪里 wps段落在哪打开域名云服务器怎么搭建