Skip to content

Commit

Permalink
[更新]增加文件缓存机制,默认目录为./Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed Dec 2, 2017
1 parent 502848c commit 4926544
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ PHP支付SDK(微信支付 + 支付宝支付)
## 声明
- 代码与框架部分参考于互联网开源项目
- `SDK`全部源码基于`MIT`协议开源,完全免费
- **开发交流`QQ`群:513350915(新)**
- **开发交流`QQ`群:513350915**

若对您有帮助,可以**赞助**支持下作者哦!
----
Expand Down
50 changes: 50 additions & 0 deletions src/Contracts/HttpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
class HttpService
{

/**
* 缓存路径
* @var null
*/
public static $cachePath = null;

/**
* 以get访问模拟访问
* @param string $url 访问URL
Expand Down Expand Up @@ -109,4 +115,48 @@ private static function build($data)
}
return $data;
}

/**
* 缓存配置与存储
* @param string $name 缓存名称
* @param string $value 缓存内容
* @param int $expired 缓存时间(0表示永久缓存)
* @return bool
*/
public static function setCache($name, $value = '', $expired = 3600)
{
$cachepath = empty(self::$cachePath) ? self::$cachePath : dirname(__DIR__) . '/Cache/';
file_exists($cachepath) || mkdir($cachepath, 0755, true);
$cachestri = serialize(['name' => $name, 'value' => $value, 'expired' => time() + intval($expired)]);
return file_put_contents($cachepath . md5($name), $cachestri);
}

/**
* 获取缓存内容
* @param string $name 缓存名称
* @return null|mixed
*/
public static function getCache($name)
{
$cachefile = (empty(self::$cachePath) ? self::$cachePath : dirname(__DIR__) . '/Cache/') . md5($name);
if (file_exists($cachefile) && ($content = file_get_contents($cachefile))) {
$data = unserialize($content);
if (isset($data['expired']) && (intval($data['expired']) === 0 || intval($data['expired']) >= time())) {
return $data['value'];
}
self::delCache($name);
}
return null;
}

/**
* 移除缓存文件
* @param string $name 缓存名称
* @return bool
*/
public static function delCache($name)
{
$cachefile = (empty(self::$cachePath) ? self::$cachePath : dirname(__DIR__) . '/Cache/') . md5($name);
return file_exists($cachefile) ? unlink($cachefile) : true;
}
}
24 changes: 17 additions & 7 deletions src/Gateways/Wechat/Wechat.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Pay\Contracts\Config;
use Pay\Contracts\GatewayInterface;
use Pay\Contracts\HttpService;
use Pay\Exceptions\Exception;
use Pay\Exceptions\GatewayException;
use Pay\Exceptions\InvalidArgumentException;
Expand Down Expand Up @@ -98,14 +99,19 @@ public function __construct(array $config)
$this->gateway_micropay = 'https://api.mch.weixin.qq.com/sandboxnew/pay/micropay';
$this->gateway_bill = 'https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill';
// 沙箱验证签名及沙箱密钥更新
$data = array('mch_id' => $this->userConfig->get('mch_id', ''), 'nonce_str' => $this->createNonceStr('32'));
$data['sign'] = $this->getSign($data);
$result = $this->fromXml($this->post('https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey', $this->toXml($data)));
if (isset($result['return_code']) && $result['return_code'] === 'SUCCESS') {
$this->userConfig->set('mch_key', $result['sandbox_signkey']);
} else {
throw new Exception('沙箱验证签名及获取沙箱密钥失败!');
$sandbox_signkey = HttpService::getCache('sandbox_signkey');
if (empty($sandbox_signkey)) {
$data = ['mch_id' => $this->userConfig->get('mch_id', ''), 'nonce_str' => $this->createNonceStr('32')];
$data['sign'] = $this->getSign($data);
$result = $this->fromXml($this->post('https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey', $this->toXml($data)));
if (isset($result['return_code']) && $result['return_code'] === 'SUCCESS') {
$sandbox_signkey = $result['sandbox_signkey'];
HttpService::setCache('sandbox_signkey', $sandbox_signkey);
} else {
throw new Exception('沙箱验证签名及获取沙箱密钥失败!');
}
}
$this->userConfig->set('mch_key', $sandbox_signkey);
}
$this->config = [
'appid' => $this->userConfig->get('app_id', ''),
Expand All @@ -121,6 +127,7 @@ public function __construct(array $config)
* 订单退款操作
* @param array $options
* @return array
* @throws GatewayException
*/
public function refund($options = [])
{
Expand All @@ -134,6 +141,7 @@ public function refund($options = [])
* 关闭正在进行的订单
* @param string $out_trade_no
* @return array
* @throws GatewayException
*/
public function close($out_trade_no = '')
{
Expand All @@ -146,6 +154,7 @@ public function close($out_trade_no = '')
* 查询订单状态
* @param string $out_trade_no
* @return array
* @throws GatewayException
*/
public function find($out_trade_no = '')
{
Expand Down Expand Up @@ -176,6 +185,7 @@ abstract protected function getTradeType();
/**
* @param array $options
* @return array
* @throws GatewayException
*/
protected function preOrder($options = [])
{
Expand Down

0 comments on commit 4926544

Please sign in to comment.