php微信支付之如何实现APP支付
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,小编给大家分享一下php微信支付之如何实现APP支付,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体分析如下:Wecha
千家信息网最后更新 2025年11月07日php微信支付之如何实现APP支付
小编给大家分享一下php微信支付之如何实现APP支付,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
具体分析如下:
WechatAppPay文件代码如下:
file = __DIR__ . '/payAccessToken.txt'; } /** * 创建APP支付最终返回参数 * @throws \Exception * @return multitype:string NULL */ public function createAppPayData() { $this->generateConfig(); $prepayid = $this->getPrepayid(); try{ $array = [ 'appid' => $this->appid, 'appkey' => $this->paySignkey, 'noncestr' => $this->getRandomStr(), 'package' => 'Sign=WXPay', 'partnerid' => $this->partnerId, 'prepayid' => $prepayid, 'timestamp' => (string)time(), ]; $array['sign'] = $this->sha1Sign($array); unset($array['appkey']); } catch(\Exception $e) { throw new \Exception($e->getMessage()); } return $array; } /** * 验证支付成功后的通知参数 * * @throws \Exception * @return boolean */ public function verifyNotify() { try{ $staySignStr = $this->notify; unset($staySignStr['sign']); $sign = $this->signData($staySignStr); return $this->notify['sign'] === $sign; } catch(\Exception $e) { throw new \Exception($e->getMessage()); } } /** * 魔术方法,给添加支付参数进来 * * @param string $name 参数名 * @param string $value 参数值 */ public function __set($name, $value) { $this->$name = $value; } /** * 设置access token * @param string $token * @throws \Exception * @return boolean */ public function setAccessToken() { try{ if(!file_exists($this->file) || !is_file($this->file)) { $f = fopen($this->file, 'a'); fclose($f); } $content = file_get_contents($this->file); if(!empty($content)) { $info = json_decode($content, true); if( time() - $info['getTime'] < 7150 ) { $this->accessToken = $info['accessToken']; return true; } } //文件内容为空或access token已失效,重新获取 $this->outputAccessTokenToFile(); } catch(\Exception $e) { throw new \Exception($e->getMessage()); } return true; } /** * 写入access token 到文件 * @throws \Exception * @return boolean */ protected function outputAccessTokenToFile() { try{ $f = fopen($this->file, 'wb'); $token = [ 'accessToken' => $this->getAccessToken(), 'getTime' => time(), ]; flock($f, LOCK_EX); fwrite($f, json_encode($token)); flock($f, LOCK_UN); fclose($f); $this->accessToken = $token['accessToken']; } catch(\Exception $e) { throw new \Exception($e->getMessage()); } return true; } /** * 取access token * * @throws \Exception * @return string */ protected function getAccessToken() { $url = sprintf(self::ACCESS_TOKEN_URL, $this->appid, $this->appSecret); $result = json_decode( $this->getUrl($url), true ); if(isset($result['errcode'])) { throw new \Exception("get access token failed:{$result['errmsg']}"); } return $result['access_token']; } /** * 取预支付会话标识 * * @throws \Exception * @return string */ protected function getPrepayid() { $data = json_encode($this->config); $url = sprintf(self::POST_ORDER_URL, $this->accessToken); $result = json_decode( $this->postUrl($url, $data), true ); if( isset($result['errcode']) && $result['errcode'] != 0 ) { throw new \Exception($result['errmsg']); } if( !isset($result['prepayid']) ) { throw new \Exception('get prepayid failed, url request error.'); } return $result['prepayid']; } /** * 组装预支付参数 * * @throws \Exception */ protected function generateConfig() { try{ $this->config = [ 'appid' => $this->appid, 'traceid' => $this->traceid, 'noncestr' => $this->getRandomStr(), 'timestamp' => time(), 'package' => $this->generatePackage(), 'sign_method' => $this->sign_method, ]; $this->config['app_signature'] = $this->generateSign(); } catch(\Exception $e) { throw new \Exception($e->getMessage()); } } /** * 生成package字段 * * 生成规则: * 1、生成sign的值signValue * 2、对package参数再次拼接成查询字符串,值需要进行urlencode * 3、将sign=signValue拼接到2生成的字符串后面得到最终的package字符串 * * 第2步urlencode空格需要编码成%20而不是+ * * RFC 1738会把 空格编码成+ * RFC 3986会把空格编码成%20 * * @return string */ protected function generatePackage() { $this->package['sign'] = $this->signData($this->package); return http_build_query($this->package, '', '&', PHP_QUERY_RFC3986); } /** * 生成签名 * * @return string */ protected function generateSign() { $signArray = [ 'appid' => $this->appid, 'appkey' => $this->paySignkey, 'noncestr' => $this->config['noncestr'], 'package' => $this->config['package'], 'timestamp' => $this->config['timestamp'], 'traceid' => $this->traceid, ]; return $this->sha1Sign($signArray); } /** * 签名数据 * * 生成规则: * 1、字典排序,拼接成查询字符串格式,不需要urlencode * 2、上一步得到的字符串最后拼接上key=paternerKey * 3、MD5哈希字符串并转换成大写得到sign的值signValue * * @param array $data 待签名数据 * @return string 最终签名结果 */ protected function signData($data) { ksort($data); $str = $this->arrayToString($data); $str .= "&key={$this->partnerKey}"; return strtoupper( $this->signMd5($str) ); } /** * sha1签名 * 签名规则 * 1、字典排序 * 2、拼接查询字符串 * 3、sha1运算 * * @param array $arr * @return string */ protected function sha1Sign($arr) { ksort($arr); return sha1( $this->arrayToString($arr) ); }}以上是"php微信支付之如何实现APP支付"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
支付
参数
字符
字符串
生成
内容
文件
空格
篇文章
编码
规则
查询
字典
数据
排序
成功
不怎么
代码
再次
大写
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器ddos攻击教程
hp服务器离线存储管理员
网络技术培训研修日志
企业网站建立服务器
只画网络安全知识的手抄报
做软件开发国家补贴
宝鸡市网络安全和信息化委员会
sql数据库表复制表结构
软件开发实习报告总结3000字
腾讯软件开发的岗位
鼎桥终端应用软件开发
湖南正规软件开发服务参考价格
定制数据库引擎
fanuc数据库
泉州分发软件开发怎么做
有发展的协同软件开发
浪潮人工智能服务器怎么样
oa一般用的是什么数据库
ibm3850x5服务器尺寸
阿卡丽网络技术有限公司
服务器cpu烧坏属于什么故障
网络安全权威认证考试
北京科维森网络技术
人工智能对网络安全的保护
服务器程序加密
融创中国软件开发
王者全国巅峰是全服务器吗
广州工控软件开发怎么样
工程机械设备数据库
怎么查看本机的服务器ip地址