diff --git a/README.md b/README.md index b809f33..af7b1ed 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ PHP支付SDK(微信支付 + 支付宝支付) ## 声明 - 代码与框架部分参考于互联网开源项目 - `SDK`全部源码基于`MIT`协议开源,完全免费 -- **开发交流`QQ`群:513350915(新)** +- **开发交流`QQ`群:513350915** 若对您有帮助,可以**赞助**支持下作者哦! ---- diff --git a/src/Contracts/HttpService.php b/src/Contracts/HttpService.php index 853ae35..88657b5 100644 --- a/src/Contracts/HttpService.php +++ b/src/Contracts/HttpService.php @@ -20,6 +20,12 @@ class HttpService { + /** + * 缓存路径 + * @var null + */ + public static $cachePath = null; + /** * 以get访问模拟访问 * @param string $url 访问URL @@ -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; + } } \ No newline at end of file diff --git a/src/Gateways/Wechat/Wechat.php b/src/Gateways/Wechat/Wechat.php index 4d2d0f2..93ac140 100644 --- a/src/Gateways/Wechat/Wechat.php +++ b/src/Gateways/Wechat/Wechat.php @@ -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; @@ -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', ''), @@ -121,6 +127,7 @@ public function __construct(array $config) * 订单退款操作 * @param array $options * @return array + * @throws GatewayException */ public function refund($options = []) { @@ -134,6 +141,7 @@ public function refund($options = []) * 关闭正在进行的订单 * @param string $out_trade_no * @return array + * @throws GatewayException */ public function close($out_trade_no = '') { @@ -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 = '') { @@ -176,6 +185,7 @@ abstract protected function getTradeType(); /** * @param array $options * @return array + * @throws GatewayException */ protected function preOrder($options = []) {