当前位置:网站首页>wap端微信h5支付,用于非微信浏览器
wap端微信h5支付,用于非微信浏览器
2022-07-26 09:23:00 【为天空着色】
做项目碰到了wap端实现微信支付,听朋友说微信有wap端的h5支付,就看了下这块的接口,记录下供以后方便查看
(具体的接口可以查看商户平台说明:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1)
<?php /** *非微信浏览器 调用微信h5支付 */ namespace app\mobile\controller; use think\Controller; use think\Loader; use think\Request; use think\Url; use think\Config; use think\Page; class Wxpay extends MobileBase { /** * @return array 下单方法 */ public function wxpaymoney(Request $request) { $param=$request->param(); if (!empty($param)){ $param = json_decode(json_encode(simplexml_load_string($param, 'SimpleXMLElement', LIBXML_NOCDATA)), true); echo '-------'; p($param); echo '*******'; } $url='https://api.mch.weixin.qq.com/pay/unifiedorder'; //下单地址 $appid='appid';//公众号appid $appsecret='appsecret'; $mch_id='商户平台id';//商户平台id $nonce_str='qyzf'.rand(100000, 999999);//随机数 $out_trade_no='wap'.date('YmdHis').rand(100000,999999); $ip=$this->getClientIp(); $scene_info='{"h5_info": {"type":"Wap","app_name": "project info","package_name": "wap订单微信支付"}}'; // $appidInfo=$this->getTokens($appid,$appsecret); // echo '======='; // p($appidInfo); // echo '*******';die; // $openid=$appidInfo['openid']; $total_fee=1; $trade_type='MWEB'; $attach='wap支付测试'; $body='H5支付测试'; $notify_url='http://m.xxxxx.com/Mobile/Wxpay/wxnotify.html'; $arr=[ 'appid'=>$appid, 'mch_id'=>$mch_id, 'nonce_str'=>$nonce_str, 'out_trade_no'=>$out_trade_no, 'spbill_create_ip'=>$ip, 'scene_info'=>$scene_info, // 'openid'=>$openid, 'total_fee'=>$total_fee, 'trade_type'=>$trade_type, 'attach'=>$attach, 'body'=>$body, 'notify_url'=>$notify_url ]; $sign=$this->getSign($arr); //<openid>'.$openid.'</openid> $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>'.$ip.'</spbill_create_ip> <total_fee>'.$total_fee.'</total_fee> <trade_type>'.$trade_type.'</trade_type> <scene_info>'.$scene_info.'</scene_info> <sign>'.$sign.'</sign> </xml>'; $result=$this->https_request($url,$data); echo '===================='; var_dump($result); echo '*******************'; //禁止引用外部xml实体 libxml_disable_entity_loader(true); $result_info = json_decode(json_encode(simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA)), true); p($result_info); echo '<a href="'.$result_info['mweb_url'].'">测试微信支付</a>';die; } /** *curl请求 */ private function https_request($url, $data = null) { p($data); $curl = curl_init (); curl_setopt ( $curl, CURLOPT_URL, $url ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); if (! empty ( $data )) { curl_setopt ( $curl, CURLOPT_POST, 1 ); curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); } curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); $output = curl_exec ( $curl ); curl_close ( $curl ); return $output; } /** * @return 获取ip */ private function getClientIp() { $cip='unknown'; if ($_SERVER['REMOTE_ADDR']){ $cip=$_SERVER['REMOTE_ADDR']; }elseif (getenv($_SERVER['REMOTE_ADDR'])){ $cip=getenv($_SERVER['REMOTE_ADDR']); } return $cip; } /** * @return 异步通知 */ public function wxnotify(Request $request) { Loader::import('payment.wxwap.PayNotifyCallBacks'); $notify=new \PayNotifyCallBacks(); $notify->Handle(false); die; } /** * @param Request */ public function wxcallback(Request $request) { $data=$request->param(); file_put_contents('./testwx1.txt','888888888888888'.$data['orderid'].'-----'.$data['transaction_id'].'******'); } /** * 作用:格式化参数,签名过程需要使用 */ public function formatBizQueryParaMap($paraMap, $urlencode) { // var_dump($paraMap);//die; $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if($urlencode) { $v = urlencode($v); } //$buff .= strtolower($k) . "=" . $v . "&"; $buff .= $k . "=" . $v . "&"; } $reqPar=''; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff)-1); } p($reqPar);//die; return $reqPar; } /** * 作用:生成签名 */ public function getSign($Obj) { p($Obj);//die; foreach ($Obj as $k => $v) { $Parameters[$k] = $v; } //签名步骤一:按字典序排序参数 ksort($Parameters); $String = $this->formatBizQueryParaMap($Parameters, false); //echo '【string1】'.$String.'</br>'; //签名步骤二:在string后加入KEY 5a02bd8ecxxxxxxxxxxxxc1aae7d199 这里的秘钥是 商户平台设置的一定要改不然报签名错误 $String = $String."&key=5a02bd8ecxxxxxxxxxxxxc1aae7d199"; //echo "【string2】".$String."</br>"; //签名步骤三:MD5加密 $String = md5($String); //echo "【string3】 ".$String."</br>"; //签名步骤四:所有字符转为大写 $result_ = strtoupper($String); //echo "【result】 ".$result_."</br>"; return $result_; } private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }
测试demo(PHP版): http://download.csdn.net/download/u010533511/10155017
边栏推荐
- Processing of inconsistent week values obtained by PHP and MySQL
- CF1481C Fence Painting
- 【Flutter -- 布局】Align、Center、Padding 使用详解
- 使用openLayer画箭头
- 会议OA项目(三)---我的会议(会议排座、送审)
- Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
- MySQL 强化知识点
- Pat grade a a1013 battle over cities
- volatile 靠的是MESI协议解决可见性问题?(下)
- Qt | 关于如何使用事件过滤器 eventFilter
猜你喜欢
Redis principle and usage - installation and distributed configuration
Study notes of canal
Selection and practice of distributed tracking system
OFDM 十六讲- OFDM
csdn空格用什么表示
The Child and Binary Tree-多项式开根求逆
js在控制台输出菱形
Study notes of dataX
el-table实现增加/删除行,某参数跟着变
[online problem] timeout waiting for connection from pool problem troubleshooting
随机推荐
省政府召开全省高温天气安全防范工作电视电话会议
[arkit, realitykit] turn pictures into 3D models
微信小程序学习笔记2
Canal 的学习笔记
Windows backs up the database locally by command
NTT(快速数论变换)多项式求逆 一千五百字解析
会议OA项目(三)---我的会议(会议排座、送审)
字节缓冲流&字符流详解
The child and binary tree- open root inversion of polynomials
Unity topdown character movement control
838. 堆排序
VectorTileLayer更换style
Redis principle and usage - installation and distributed configuration
php执行shell脚本
Li Mu D2L (VI) -- model selection
JS - DataTables control on the number of displays per page
【Flutter -- 布局】Align、Center、Padding 使用详解
760. 字符串长度
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
Custom password input box, no rounded corners