zoukankan      html  css  js  c++  java
  • 微信H5支付 在其他浏览器调用微信支付

    微信H5支付的相关资料不是很多、不过步骤上来说不是很复杂 比公众号支付简单很多。

    先上官方文档吧 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1

    微信H5支付是需要单独申请的 这里就不多说了 自己去申请就OK了 

    这里要注意 设置的 授权回调页面域名 是指的支付成功后跳转的页面

    申请通过了之后 我们会得到几个参数 下来就展示下代码吧

     1 public function wxPay(){
     2         $id=intval($_GET['id']);
     3         $msg=M("Order")->where("id=".$id)->find();  //获取支付的订单信息
     4         $info=M("Product")->where("id=".$msg['pid'])->find();     //获取后台配置的微信参数
     5         
     6         $userip = $_SERVER["REMOTE_ADDR"]; //获得用户设备IP
     7         $appid = $info['appid'];//微信给的appid
     8         $mch_id = $info['mchid'];//微信官方的
     9         $key = $info['mykey'];//自己设置的微信商家key
    10         $out_trade_no = $msg['dh']; //订单号
    11         
    12 
    13         $nonce_str=MD5($out_trade_no);//随机字符串
    14         $total_fee = $msg['price']*100; //金额*100
    15         $spbill_create_ip = $userip; //IP
    16         $notify_url = "http://".$_SERVER['SERVER_NAME']."/index.php/Home/Back/wx_back"; //回调地址 jishu.whwlhd.com/index.php/Home/Pay/wx/id/
    17         $trade_type = 'MWEB';//交易类型 具体看API 里面有详细介绍
    18         $body="H5支付";
    19     
    20          $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://jishu.whwlhd.com","wap_name":"支付"}}';//场景信息 必要参数
    21          $signA ="appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_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";
    22 
    23          $strSignTmp = $signA."&key=$key"; //拼接字符串  注意顺序微信有个测试网址 顺序按照他的来 直接点下面的校正测试 包括下面XML  是否正确
    24          $sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
    25 
    26          $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>
    27         </xml>";//拼接成XML 格式
    28 
    29 
    30         $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
    31 
    32         $dataxml = $this->http_post($url,$post_data,$headers); 
    33         $objectxml = (array)simplexml_load_string($dataxml,'SimpleXMLElement',LIBXML_NOCDATA); //将微信返回的XML 转换成数组
    34         
    35         if($objectxml['return_code'] == 'SUCCESS'){
    36             $redirect_url = urlencode("http://jishu.whwlhd.com/index.php?a=index&m=Pay&uid=26");
    37             $url = $objectxml['mweb_url'].'&redirect_url='.$redirect_url;
    38             echo "<script> window.location.href='$url'</script>";
    39             exit;
    40         }     
    41     }
     1 public function http_post($url='',$post_data=array(),$header=array(),$timeout=30) {
     2             
     3             $ch = curl_init();  
     4             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查  
     5             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在  
     6             curl_setopt($ch, CURLOPT_URL, $url);  
     7             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
     8             curl_setopt($ch, CURLOPT_POST, true);  
     9             curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
    10             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
    11             curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  
    12             
    13             $response = curl_exec($ch);  
    14 
    15             curl_close($ch);
    16 
    17             return $response;
    18         }
  • 相关阅读:
    win7与centos虚拟机的共享文件夹创建
    MySQL视图
    MySQL分区表与合并表
    PHP读写XML文件的四种方法
    备份与恢复
    MySQL日志
    MySQL锁问题
    优化数据库对象
    ActiveReport资料
    对ArrayList 进行深拷贝
  • 原文地址:https://www.cnblogs.com/we-jack/p/9072995.html
Copyright © 2011-2022 走看看