Skip to content

Commit 4926544

Browse files
committed
[更新]增加文件缓存机制,默认目录为./Cache
1 parent 502848c commit 4926544

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ PHP支付SDK(微信支付 + 支付宝支付)
2020
## 声明
2121
- 代码与框架部分参考于互联网开源项目
2222
- `SDK`全部源码基于`MIT`协议开源,完全免费
23-
- **开发交流`QQ`群:513350915(新)**
23+
- **开发交流`QQ`群:513350915**
2424

2525
若对您有帮助,可以**赞助**支持下作者哦!
2626
----

src/Contracts/HttpService.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
class HttpService
2121
{
2222

23+
/**
24+
* 缓存路径
25+
* @var null
26+
*/
27+
public static $cachePath = null;
28+
2329
/**
2430
* 以get访问模拟访问
2531
* @param string $url 访问URL
@@ -109,4 +115,48 @@ private static function build($data)
109115
}
110116
return $data;
111117
}
118+
119+
/**
120+
* 缓存配置与存储
121+
* @param string $name 缓存名称
122+
* @param string $value 缓存内容
123+
* @param int $expired 缓存时间(0表示永久缓存)
124+
* @return bool
125+
*/
126+
public static function setCache($name, $value = '', $expired = 3600)
127+
{
128+
$cachepath = empty(self::$cachePath) ? self::$cachePath : dirname(__DIR__) . '/Cache/';
129+
file_exists($cachepath) || mkdir($cachepath, 0755, true);
130+
$cachestri = serialize(['name' => $name, 'value' => $value, 'expired' => time() + intval($expired)]);
131+
return file_put_contents($cachepath . md5($name), $cachestri);
132+
}
133+
134+
/**
135+
* 获取缓存内容
136+
* @param string $name 缓存名称
137+
* @return null|mixed
138+
*/
139+
public static function getCache($name)
140+
{
141+
$cachefile = (empty(self::$cachePath) ? self::$cachePath : dirname(__DIR__) . '/Cache/') . md5($name);
142+
if (file_exists($cachefile) && ($content = file_get_contents($cachefile))) {
143+
$data = unserialize($content);
144+
if (isset($data['expired']) && (intval($data['expired']) === 0 || intval($data['expired']) >= time())) {
145+
return $data['value'];
146+
}
147+
self::delCache($name);
148+
}
149+
return null;
150+
}
151+
152+
/**
153+
* 移除缓存文件
154+
* @param string $name 缓存名称
155+
* @return bool
156+
*/
157+
public static function delCache($name)
158+
{
159+
$cachefile = (empty(self::$cachePath) ? self::$cachePath : dirname(__DIR__) . '/Cache/') . md5($name);
160+
return file_exists($cachefile) ? unlink($cachefile) : true;
161+
}
112162
}

src/Gateways/Wechat/Wechat.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Pay\Contracts\Config;
1616
use Pay\Contracts\GatewayInterface;
17+
use Pay\Contracts\HttpService;
1718
use Pay\Exceptions\Exception;
1819
use Pay\Exceptions\GatewayException;
1920
use Pay\Exceptions\InvalidArgumentException;
@@ -98,14 +99,19 @@ public function __construct(array $config)
9899
$this->gateway_micropay = 'https://api.mch.weixin.qq.com/sandboxnew/pay/micropay';
99100
$this->gateway_bill = 'https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill';
100101
// 沙箱验证签名及沙箱密钥更新
101-
$data = array('mch_id' => $this->userConfig->get('mch_id', ''), 'nonce_str' => $this->createNonceStr('32'));
102-
$data['sign'] = $this->getSign($data);
103-
$result = $this->fromXml($this->post('https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey', $this->toXml($data)));
104-
if (isset($result['return_code']) && $result['return_code'] === 'SUCCESS') {
105-
$this->userConfig->set('mch_key', $result['sandbox_signkey']);
106-
} else {
107-
throw new Exception('沙箱验证签名及获取沙箱密钥失败!');
102+
$sandbox_signkey = HttpService::getCache('sandbox_signkey');
103+
if (empty($sandbox_signkey)) {
104+
$data = ['mch_id' => $this->userConfig->get('mch_id', ''), 'nonce_str' => $this->createNonceStr('32')];
105+
$data['sign'] = $this->getSign($data);
106+
$result = $this->fromXml($this->post('https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey', $this->toXml($data)));
107+
if (isset($result['return_code']) && $result['return_code'] === 'SUCCESS') {
108+
$sandbox_signkey = $result['sandbox_signkey'];
109+
HttpService::setCache('sandbox_signkey', $sandbox_signkey);
110+
} else {
111+
throw new Exception('沙箱验证签名及获取沙箱密钥失败!');
112+
}
108113
}
114+
$this->userConfig->set('mch_key', $sandbox_signkey);
109115
}
110116
$this->config = [
111117
'appid' => $this->userConfig->get('app_id', ''),
@@ -121,6 +127,7 @@ public function __construct(array $config)
121127
* 订单退款操作
122128
* @param array $options
123129
* @return array
130+
* @throws GatewayException
124131
*/
125132
public function refund($options = [])
126133
{
@@ -134,6 +141,7 @@ public function refund($options = [])
134141
* 关闭正在进行的订单
135142
* @param string $out_trade_no
136143
* @return array
144+
* @throws GatewayException
137145
*/
138146
public function close($out_trade_no = '')
139147
{
@@ -146,6 +154,7 @@ public function close($out_trade_no = '')
146154
* 查询订单状态
147155
* @param string $out_trade_no
148156
* @return array
157+
* @throws GatewayException
149158
*/
150159
public function find($out_trade_no = '')
151160
{
@@ -176,6 +185,7 @@ abstract protected function getTradeType();
176185
/**
177186
* @param array $options
178187
* @return array
188+
* @throws GatewayException
179189
*/
180190
protected function preOrder($options = [])
181191
{

0 commit comments

Comments
 (0)