首页 > 语言 > PHP > 正文

PHP实现唤起微信支付功能

2024-05-05 00:06:50
字体:
来源:转载
供稿:网友

本文实例为大家分享了PHP唤起微信支付的具体代码,供大家参考,具体内容如下

<?php/****/class H5wxAction extends CommonAction { public function __construct() { parent::__construct();   } /* 字段名 变量名 必填 类型 示例值 描述公众账号ID appid 是 String(32) wxd678efh567hg6787 微信分配的公众账号ID(企业号corpid即为此appId)商户号 mch_id 是 String(32) 1230000109 微信支付分配的商户号设备号 device_info 否 String(32) 013467007045764 终端设备号(门店号或收银设备ID),注意:PC网页或公众号内支付请传"WEB"随机字符串 nonce_str 是 String(32) 5K8264ILTKCH16CQ2502SI8ZNMTM67VS 随机字符串,不长于32位。推荐随机数生成算法签名 sign 是 String(32) C380BEC2BFD727A4B6845133519F3AD6 签名,详见签名生成算法签名类型 sign_type 否 String(32) HMAC-SHA256 签名类型,目前支持HMAC-SHA256和MD5,默认为MD5商品描述 body 是 String(128) 腾讯充值中心-QQ会员充值 商品简单描述,该字段须严格按照规范传递,具体请见参数规定商品详情 detail 否 String(6000) 单品优惠字段(暂未上线)附加数据 attach 否 String(127) 深圳分店 附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据商户订单号 out_trade_no 是 String(32) 20150806125346 商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号货币类型 fee_type 否 String(16) CNY 符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型总金额 total_fee 是 Int 888 订单总金额,单位为分,详见支付金额终端IP spbill_create_ip 是 String(16) 123.12.12.123 必须传正确的用户端IP,详见获取用户ip指引交易起始时间 time_start 否 String(14) 20091225091010 订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则交易结束时间 time_expire 否 String(14) 20091227091010 订单失效时间,格式为yyyyMMddHHmmss,如2009年12月27日9点10分10秒表示为20091227091010。其他详见时间规则注意:最短失效时间间隔必须大于5分钟商品标记 goods_tag 否 String(32) WXG 商品标记,代金券或立减优惠功能的参数,说明详见代金券或立减优惠通知地址 notify_url 是 String(256) http://www.weixin.qq.com/wxpay/pay.php 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。交易类型 trade_type 是 String(16) MWEB H5支付的交易类型为MWEB商品ID product_id 否 String(32) 12235413214070356458058 trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。指定支付方式 limit_pay 否 String(32) no_credit no_credit--指定不能使用信用卡支付用户标识 openid 否 String(128) oUpF8uMuAJO_M2pxb1Q9zNjWeS6o trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识。openid如何获取,可参考【获取openid】。企业号请使用【企业号OAuth2.0接口】获取企业号内成员userid,再调用【企业号userid转openid接口】进行转换场景信息 scene_info 是 String(256) //IOS移动应用{"h5_info": {"type":"IOS","app_name": "王者荣耀","bundle_id": "com.tencent.wzryIOS"}}//安卓移动应用{"h5_info": {"type":"Android","app_name": "王者荣耀","package_name": "com.tencent.tmgp.sgame"}}//WAP网站应用{"h5_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "腾讯充值"}} 该字段用于上报支付的场景信息,针对H5支付有以下三种场景,请根据对应场景上报,H5支付不建议在APP端使用,针对场景1,2请接入APP支付,不然可能会出现兼容性问题1,IOS移动应用{"h5_info": //h5支付固定传"h5_info"  {"type": "", //场景类型  "app_name": "", //应用名  "bundle_id": "" //bundle_id  }}2,安卓移动应用{"h5_info": //h5支付固定传"h5_info"  {"type": "", //场景类型  "app_name": "", //应用名  "package_name": "" //包名  }}3,WAP网站应用{"h5_info": //h5支付固定传"h5_info"  {"type": "", //场景类型 "wap_url": "",//WAP网站URL地址 "wap_name": "" //WAP 网站名 }}  */ public function config(){ $array=array( 'appid'=>'****', //公众账号ID appid 是 String(32) wxd678efh567hg6787 微信分配的公众账号ID(企业号corpid即为此appId) 'mch_id'=>'****',// 商户号 mch_id 是 String(32) 1230000109 微信支付分配的商户号 'nonce_str'=>rand(100000,999999),// 随机字符串 nonce_str 是 String(32) 5K8264ILTKCH16CQ2502SI8ZNMTM67VS 随机字符串,不长于32位。推荐随机数生成算法 'key'=>'****', 'APPSECRET'=>'*****',  ); return $array;  }  public function H5WeiXin(){  $config = $this->config();   $count=1; //$money= 1;//充值金额        $userip = $this->getIP(); //获得用户设备IP  $appid = $config['appid'];   $mch_id = $config['mch_id'];   $key = $config['key'];     $rand = rand(00000,99999);   $out_trade_no = $_POST['number'];//订单号   $nonce_str=MD5($rand);//随机字符串   $body = $data['title'];//内容   $total_fee = $count; //金额   $spbill_create_ip = $userip; //IP   $notify_url = 'http://'.$_SERVER['HTTP_HOST'].'/Wap/H5wx/WxHD'; //回调地址   $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].'/Wap/H5wx/is_wxpay?number='.$_POST['number']; //回调地址   $trade_type = 'MWEB';//交易类型  $scene_info ='{"h5_info":{"type":"Wap","wap_url":"'.$_SERVER['HTTP_HOST'].'","wap_name":"'.$data['title'].'"}}';//场景信息   $signA ="appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_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";   $strSignTmp = $signA."&key=$key"; //拼接字符串      $sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写   $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>      </xml>";//拼接成XML 格式   $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";  $dataxml = $this->http_post($url,$post_data);  $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA);    if($objectxml['return_code']=='SUCCESS'){ $json['url'] = $objectxml['mweb_url'].'&redirect_url='.$redirect_url; //唤起微信链接 $json['msg'] = '1';  }else{ $json['info'] ='参数错误'; $json['msg'] ='0'; }  echo json_encode($json);   } //查询是否支付成功 示例 根据需要调整 public function is_wxpay(){ $out_trade_no = $_GET['number'];  $type = substr($out_trade_no,0,1);  if($type=='B'){ $is_state = M('Bond')->where(array('number'=>$out_trade_no))->find(); $url = U('Court/details',array('id'=>$is_state['aid'])); }elseif($type=='W'){ $is_state = M('Wallet')->where(array('number'=>$out_trade_no))->find(); $url = U('Member/wallet'); }else{ $is_state =M('viplog')->where(array('number'=>$out_trade_no))->find(); $url = U('Member/vip'); }  if($is_state['state']=='1'){ cookie('id',$is_state['mid']); header("location: ".$url.""); }else{ echo '<h1>未查询到此订单的付款信息</h1>'; }   }  //微信回调地址 public function WxHD(){ $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; $array = $this->xmlToArray($GLOBALS['HTTP_RAW_POST_DATA']); M('api_log')->add(array('content'=>json_encode($this->xmlToArray($GLOBALS['HTTP_RAW_POST_DATA'])).$array['out_trade_no']));    if($array['return_code']=='SUCCESS'){  $out_trade_no=$array['out_trade_no']; //$number = explode('-',$out_trade_no); $type = substr($out_trade_no,0,1);   header("location: http://".$_SERVER['HTTP_HOST'].$url."");     }    }public function xmlToArray($xml){   //禁止引用外部xml实体  libxml_disable_entity_loader(true);  $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);  $val = json_decode(json_encode($xmlstring),true);  return $val;  } public function convertUrlQuery($query){ $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } return implode('&',$params);} public 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;  }   public function getIP() {  if(getenv('HTTP_CLIENT_IP')) {  $ip = getenv('HTTP_CLIENT_IP');  }elseif (getenv('HTTP_X_FORWARDED_FOR')) {  $ip = getenv('HTTP_X_FORWARDED_FOR');  }elseif (getenv('HTTP_X_FORWARDED')) {  $ip = getenv('HTTP_X_FORWARDED');  }elseif (getenv('HTTP_FORWARDED_FOR')) {  $ip = getenv('HTTP_FORWARDED_FOR');   }elseif (getenv('HTTP_FORWARDED')) {  $ip = getenv('HTTP_FORWARDED');  }else {  $ip = $_SERVER['REMOTE_ADDR'];  }  return $ip;  }   }  ?>

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


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选