当前位置:网站首页>Wechat H5 payment on WAP, for non wechat browsers
Wechat H5 payment on WAP, for non wechat browsers
2022-07-26 09:47:00 【Color the sky】
When I was working on a project wap Wechat payment is realized on the end , I heard from my friends that wechat has wap Terminal h5 payment , Just look at this interface , Record it for future reference
( For the specific interface, you can check the merchant platform description :https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1)
<?php /** * Non wechat browser Call wechat h5 payment */ 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 Order method */ 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'; // Place of order $appid='appid';// official account appid $appsecret='appsecret'; $mch_id=' Merchant platform id';// Merchant platform id $nonce_str='qyzf'.rand(100000, 999999);// random number $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 Order wechat payment "}}'; // $appidInfo=$this->getTokens($appid,$appsecret); // echo '======='; // p($appidInfo); // echo '*******';die; // $openid=$appidInfo['openid']; $total_fee=1; $trade_type='MWEB'; $attach='wap Payment test '; $body='H5 Payment test '; $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 '*******************'; // Prohibit external references xml Entity 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'].'"> Test wechat payment </a>';die; } /** *curl request */ 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 obtain 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 Asynchronous notification */ 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'].'******'); } /** * effect : Format parameters , The signature process needs to use */ 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; } /** * effect : Generate signature */ public function getSign($Obj) { p($Obj);//die; foreach ($Obj as $k => $v) { $Parameters[$k] = $v; } // Signature step 1 : Sort parameters in dictionary order ksort($Parameters); $String = $this->formatBizQueryParaMap($Parameters, false); //echo '【string1】'.$String.'</br>'; // Signature step 2 : stay string Then add KEY 5a02bd8ecxxxxxxxxxxxxc1aae7d199 The secret key here is The merchant platform settings must be changed, otherwise the signature error will be reported $String = $String."&key=5a02bd8ecxxxxxxxxxxxxc1aae7d199"; //echo "【string2】".$String."</br>"; // Signature step 3 :MD5 encryption $String = md5($String); //echo "【string3】 ".$String."</br>"; // Signature step 4 : All characters are capitalized $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; } }
test demo(PHP edition ): http://download.csdn.net/download/u010533511/10155017
边栏推荐
- 网络流学习笔记
- Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
- The problem of accessing certsrv after configuring ADCs
- Xiaobai makes a wave of deep copy and shallow copy
- Customize permission validation in blazor
- Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
- Why does new public chain Aptos meet market expectations?
- 正则表达式
- Node memory overflow and V8 garbage collection mechanism
- 高斯消元的应用
猜你喜欢
阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
Search module use case writing
Node 内存溢出及V8垃圾回收机制
Redis sentinel mode setup under Windows
Registration module use case writing
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
Xiaobai makes a wave of deep copy and shallow copy
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
随机推荐
JS 一行代码 获取数组最大值与最小值
新公链Aptos何以拉满市场期待值?
2019 ICPC Asia Yinchuan regional (water problem solution)
Fiddler packet capturing tool for mobile packet capturing
Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
JS 连等赋值操作
matlab中的AR模型短时预测交通流
Calling DLL to start thread
The difference between thread join and object wait
Drawing shadow error diagram with MATLAB
CSV data file settings of JMeter configuration components
网站设计需要的基本知识
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
MFC handy notes
Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
Flutter Event 派发
Gauss elimination
Xiaobai makes a wave of deep copy and shallow copy
Server and client dual authentication (2)
[Online deadlock analysis] by index_ Deadlock event caused by merge