PHP   发布时间:2019-11-13  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php微信支付之APP支付方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了微信开放平台移动应用集成微信支付功能。分享给大家供大家参。具体分析如下:

WechatAppPay文件代码如下:

title"> 代码如下:
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, '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, 'noncestr' => $this->config['noncestr'], 'package' => $this->config['package'], 'timestamp' => $this->config['timestamp'], ]; 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程序设计有所帮助。

大佬总结

以上是大佬教程为你收集整理的php微信支付之APP支付方法全部内容,希望文章能够帮你解决php微信支付之APP支付方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。