国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

php微信支付之APP支付方法

2020-03-22 19:03:01
字體:
來源:轉載
供稿:網友
本文實例講述了微信開放平臺移動應用集成微信支付功能。分享給大家供大家參考。具體分析如下:WechatAppPay文件代碼如下:
復制代碼 代碼如下:
php
namespace common/services/WechatPay;
html' target='_blank'>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 = 'https://api.weixin.qq.com/cgi-bin/token grant_type=client_credential&appid=%s&secret=%s';
//生成預支付訂單提交地址
const POST_ORDER_URL = 'https://api.weixin.qq.com/pay/genprepay access_token=%s';
public function __construct()
{
$this- 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(),

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浮梁县| 南川市| 漠河县| 五家渠市| 东乡族自治县| 宿松县| 青田县| 清新县| 永福县| 准格尔旗| 克拉玛依市| 奎屯市| 淮北市| 汕尾市| 潞城市| 英超| 岳普湖县| 乃东县| 瑞昌市| 苏尼特右旗| 开鲁县| 太白县| 沅陵县| 手机| 太保市| 德兴市| 青海省| 右玉县| 泰州市| 清远市| 泸溪县| 楚雄市| 台中县| 安龙县| 石泉县| 清丰县| 灵宝市| 上蔡县| 临夏县| 天镇县| 沐川县|