php微信支付之APP支付方法
本文實例講述了微信開放平臺移動應用集成微信支付功能。分享給大家供大家參考。具體分析如下,更多消息請關注應屆畢業生網! WechatAppPay文件代碼如下: ? <?php namespace commonservicesWechatPay; class WechatAppPay extends WechatPayBase { //package參數 public $package = []; //異步通知參數 public $notify = []; //推送預支付訂單參數 protected $config = []; //存儲access token和獲取時間的文件 protected $file; //access token protected $accessToken; //取access token的url const ACCESS_TOKEN_URL = '_type=client_credential&appid=%s&secret=%s'; //生成預支付訂單提交地址 const POST_ORDER_URL = '_token=%s'; public function __construct() { $this->file = __DIR__ . '/'; } /** * 創建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程序設計有所幫助。
相關文章
-
PHP後臺微信支付使用方法
主要爲大家詳細介紹了PHP後臺微信支付和支付寶支付開發的相關資料,具有一定的參考價值,感興趣的小夥伴們可以參考一下. 微信支付項目前提:本人用的是tp框架,PHP語言下載到微信平臺提供的微信支付接口文件,放在了tp第三 -
PHP如何開發paypal支付插件
以前的企業商務網站只限於國內支付寶的表單交易,無法面向國際外貿的支付業務如paypal,visa等,當然joomla國外cms內容管理系統已經提高這樣的支付插件,這裏我提到的是自定義paypal支付的功能。希望對大家有所啓發,更多 -
PHP開發微信支付實例代碼
本文介紹了php開發的微信支付代碼一例,如何用php開發微信支付功能?有需要的朋友參考下。在php開發中,用微信真的可以做太多事情了,先來看下微信支付的開發心得吧,以後會慢慢補全其他方面的開發代碼。微信支付,即便交了保證 -
php銀聯網頁支付實現方法
php銀聯網頁支付實現方法,實例分析了php操作銀聯網支付接口的技巧,具有一定參考借鑑價值,需要的朋友可以參考下.這裏介紹的`銀聯WAP支付功能,僅限消費功能。 1. PHP代碼如下:複製代碼 代碼如下:<?phpnamespace commo -
php支付寶接口用法分析
php支付寶接口用法,以實例形式較爲詳細的分析了php支付寶接口的.使用技巧,具有一定參考借鑑價值,需要的朋友可以參考下.現在流行的網站支持平臺,支付寶當仁不讓的老大了,現在我們就來告訴你如何使用支付寶api來做第三 -
Android開發微信APP支付功能的要點小結講解
基本概念包名值得是你APP的包,在創建工程時候設置的,需要在微信支付平臺上面設置。簽名指的是你生成APK時候所用的簽名文件的md5,去掉:全部小寫,需要在微信支付平臺上面設置。調試階段,簽名文件可以使用調試用的tore,簽名可 -
NPS支付系統外幣信用卡支付合作協議書
在充滿活力,日益開放的今天,越來越多人會去使用協議書,簽訂協議書能夠最大程度的保障自己的合法權利。協議書到底怎麼寫才合適呢?以下是小編整理的NPS支付系統外幣信用卡支付合作協議書,僅供參考,歡迎大家閱讀。甲方:電話: 傳 -
ThinkPHP實現支付寶接口功能實例
ThinkPHP實現支付寶接口功能的方法,實例講述了支付寶接口的下載及二次開發方法,以及對應的ThinkPHP開發技巧,需要的朋友可以參考下.最近做系統,需要實現在線支付功能,毫不猶豫,選擇的是支付寶的接口支付功能,這裏我用 -
PHPCms內容頁支持JavaScript的修改方法
在PHPCms內容頁中,出於完全考慮,默認是禁止JavaScript腳本的,所以我們在添加文章時,雖然加入了js代碼,但實際上並沒有起作用,而是以文本形式顯示。如果要讓內容頁支持JavaScript,則要做以下修改:在文件..\caches\caches_mod -
如何配置PHP使之能同時支持GIF,PNG和JPEG
安裝藍點Linux 2.0後,小編希望希望同時支持GIF,PNG和JPEG。經過嘗試,終於做到了,下面是我的簡單步驟 ,我將按照由底向上的順序介紹,即Jpeg->GD->PHP->Apache。 1、當前狀態查看安裝藍點Linux 2.0後,Keel,MySQL,Apache未做