400-800-9385
网站建设资讯详细

程序员谈如何对接微信H5支付

发表日期:2018-12-17 11:46:38   作者来源:方维网络   浏览:4940   标签:H5网站建设    微信网站建设    
微信支付已经变得越来越普遍,尤其是小程序,微网页,基本微信支付是标配,大家都知道微信支付简单使用,但是对于一个网站开发人员,如何配置和对接微信支付呢?
下面方维网络技术人员为你简单介绍

手机网站在非微信的浏览器中打开,需要用到微信H5支付
第一步:登录商户平台,在 产品中心>产品大全>我的产品>H5支付 点击“申请开通” 
 

微信支付申请1

第二步:填写支付域名

微信支付域名配置

提交审核一般会在3-5个工作日。
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';  
  $headers[] = 'Connection: Keep-Alive';  
  $headers[] = 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3'; 
  $headers[] = 'Accept-Encoding: gzip, deflate'; 
  $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0';
  $userip =$this->get_client_ip(); //获得用户设备IP
  注意:这个Thinkphp 框架的自带函数,会报“网络环境未能通过安全验证,请稍后再试”因为商户侧统一下单传的终端IP(spbill_create_ip)与用户实际调起支付时微信侧检测到的终端IP不一致导致的,一般是商户在统一下单时没有传递正确的终端IP到spbill_create_ip导致,我们要修改一下这个函数:
/*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] : '';
}
}*/
  $appid = "xxxxxxxxxxx";//微信
  $mch_id = "xxxxxxxxxxx";//微信官方的
  $key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";//自己设置的微信商家key
  $nonce_str=MD5($order_no);//随机字符串
  //dump($orderArr['orderno']);
  $total_fee = $total*100; //微信金额是以分为单位
  $spbill_create_ip = $userip; //IP
  $notify_url = 'http://xxxxxxxxxxxxxxxxxxxxxx/wxnotify'; //回调地址
  $trade_type = 'MWEB';//交易类型 具体看API 里面有详细介绍
  $body="H5支付";
$scene_info ='{"h5_info":{"type":"Wap","wap_url":"https://www.szfangwei.cn/","wap_name":"支付"}}';//场景信息 必要参数
$signA ="appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$order_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
 
$strSignTmp = $signA."&key=$key"; //拼接字符串  注意顺序微信有个测试网址 顺序按照他的来 直接点下面的校正测试 包括下面XML  是否正确
$sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
$post_data="$appid$body$mch_id$nonce_str$notify_url$order_no$scene_info$spbill_create_ip$total_fee$trade_type$sign
  ";//将拼接成XML 格式
  $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
  $dataxml = $this->http_post($url,$post_data,$headers);
/* function http_post($url='',$post_data=array(),$header=array(),$timeout=30) {
 
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在 
curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
  curl_setopt($ch, CURLOPT_POST, true); 
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
  $response = curl_exec($ch); 
  curl_close($ch);
  return $response;
  }   */
  header("content-type:text/html;charset=utf-8");
  $objectxml = (array)simplexml_load_string($dataxml,'SimpleXMLElement',LIBXML_NOCDATA); //将微信返回的XML 转换成数组
 

如没特殊注明,文章均为方维网络原创,转载请注明来自https://www.szfangwei.cn/news/4814.html