栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP微信H5支付开发实例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP微信H5支付开发实例

最近由于业务所需,对接了微信H5支付,然而微信支付对这块并没有现成的demo可用,所以就必须自己老老实实对照开发文档去写咯!但这对于刚接触的童鞋来说,坑多多少少还是有的,所以寻思着把自己的经验分享出来,毕竟现成的用的还是多巴适的嘛!

好了,官方文档的那一套就不多说了,详情见官方文档。

在这里,我主要分成了三个文件:WxPay.Config.php(支付配置文件)、Weixin.class.php(支付类)以及PayMentController.class.php(支付文件)。

首先,WxPay.Config.php配置文件主要包含了商户appId、商户号、商家key、异步回调URL、支付场景信息,如下:

class WxPayConfig
{
  public static $appid = '微信支付的公众号appid';
  public static $mchid = '微信支付分配的商户号';
  public static $key = '微信商户自己设置的安全key';
  public static $notify_url = '商户侧接收微信支付异步通知的URL';
  public static $scene_info = '{"h5_info":{"type":"Wap","wap_url":" 发起微信H5支付H5的URL","wap_name":"支付"}}'; 
}

然后,封装Weixin.class.php支付类,主要调用统一下单Api,这里不多说了,直接上代码:

 
     $appid 
     $attach 
     $body 
     $mch_id 
     $nonce_str 
     $notify_url 
     $out_trade_no 
     $spbill_create_ip 
     $total_fee 
     $trade_type
     $scene_info
     $sign 
   ";
   //6.以POST方式向微信传参,并取得微信返回的支付参数
    $dataxml = httpRequest($url,'POST',$post_data);
    $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML转换成数组
    return $objectxml;
  }
}

最后,PayMentController.class.php支付文件,支付文件接收前端发起支付的请求并处理后,调用Weixin.class.php支付类并接受结果后返回给前端(此处分享已经去掉接口验证等系列代码逻辑):

public function getPay(){
     //1.引入支付类文件
     include_once "plugins/Payment/weixin/Weixin.class.php";
     $payment = new Weixin();
     $order_id = I('order_id');
     //2.判断参数是否为空
     if (!empty($order_id)){
   //3.根据订单id查询订单是否存在
   $order = M('Order')->where(array('id'=>$order_id))->find();
   if ($order){//订单存在
//4.判断该笔订单是否已经支付,如已支付则返回支付失败并给出相应提示
if ($order['pay_status'] == '1'){
     exit(json_encode(array('status'=>'205','msg'=>'该订单已支付,请勿重复提交!')));
}
$bodys = '订单:'.$order['order_sn'] . '支付';
//5.调用支付类中封装的支付方法并对应传参
$result = $payment->getCode($order,$bodys);
//6.当return_code和result_code均为SUCCESS,代表下单成功,将支付参数返回
if($result['return_code'] == 'SUCCESS'){
     if($result['result_code'] == 'SUCCESS'){
   exit(json_encode(array('status'=>'0','msg'=>'下单成功,请支付!','result'=>$result['mweb_url'])));
     }elseif($result['result_code'] == 'FAIL'){
   exit(json_encode(array('status'=>'-201','msg'=>$result['err_code_des'])));
     }
}else{
 exit(json_encode(array('status'=>'-1','msg'=>'未知错误,请稍后重试!')));
    }
   }else{
//报错:数据不存在
exit(json_encode(array('status'=>'-200','msg'=>'订单不存在,请核实后再提交!')));
   }
     }else{
   //报错:缺少参数
   exit(json_encode(array('status'=>'-204','msg'=>'参数缺失,请核实!')));
     }
}

前端在接收到支付URL后执行即可唤醒微信支付。

附一:获取用户终端设备ip方法

function getIP(){      
    if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR"))
 $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
    else $ip = "Unknow";
    return $ip;
}

######附二:CURL请求方法


  function httpRequest($url, $method, $postfields = null, $headers = array(), $debug = false) {
    $method = strtoupper($method);
    $ci = curl_init();
    
    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); 
    curl_setopt($ci, CURLOPT_TIMEOUT, 7); 
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    switch ($method) {
      case "POST":
 curl_setopt($ci, CURLOPT_POST, true);
 if (!empty($postfields)) {
   $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
   curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
 }
 break;
      default:
 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); 
 break;
    }
    $ssl = preg_match('/^https:///i',$url) ? TRUE : FALSE;
    curl_setopt($ci, CURLOPT_URL, $url);
    if($ssl){
      curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
      curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
    }
    curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ci, CURLOPT_MAXREDIRS, 2);
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
    $response = curl_exec($ci);
    $requestinfo = curl_getinfo($ci);
    if ($debug) {
      echo "=====post data======rn";
      var_dump($postfields);
      echo "=====info===== rn";
      print_r($requestinfo);
      echo "=====response=====rn";
      print_r($response);
    }
    curl_close($ci);
    return $response;
}

好了,一点点菜鸟心得,有不当之处欢迎留言指证交流,一起成长!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/39614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号