14
14
15
15
use Pay \Contracts \Config ;
16
16
use Pay \Contracts \GatewayInterface ;
17
+ use Pay \Contracts \HttpService ;
17
18
use Pay \Exceptions \Exception ;
18
19
use Pay \Exceptions \GatewayException ;
19
20
use Pay \Exceptions \InvalidArgumentException ;
@@ -98,14 +99,19 @@ public function __construct(array $config)
98
99
$ this ->gateway_micropay = 'https://api.mch.weixin.qq.com/sandboxnew/pay/micropay ' ;
99
100
$ this ->gateway_bill = 'https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill ' ;
100
101
// 沙箱验证签名及沙箱密钥更新
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
+ }
108
113
}
114
+ $ this ->userConfig ->set ('mch_key ' , $ sandbox_signkey );
109
115
}
110
116
$ this ->config = [
111
117
'appid ' => $ this ->userConfig ->get ('app_id ' , '' ),
@@ -121,6 +127,7 @@ public function __construct(array $config)
121
127
* 订单退款操作
122
128
* @param array $options
123
129
* @return array
130
+ * @throws GatewayException
124
131
*/
125
132
public function refund ($ options = [])
126
133
{
@@ -134,6 +141,7 @@ public function refund($options = [])
134
141
* 关闭正在进行的订单
135
142
* @param string $out_trade_no
136
143
* @return array
144
+ * @throws GatewayException
137
145
*/
138
146
public function close ($ out_trade_no = '' )
139
147
{
@@ -146,6 +154,7 @@ public function close($out_trade_no = '')
146
154
* 查询订单状态
147
155
* @param string $out_trade_no
148
156
* @return array
157
+ * @throws GatewayException
149
158
*/
150
159
public function find ($ out_trade_no = '' )
151
160
{
@@ -176,6 +185,7 @@ abstract protected function getTradeType();
176
185
/**
177
186
* @param array $options
178
187
* @return array
188
+ * @throws GatewayException
179
189
*/
180
190
protected function preOrder ($ options = [])
181
191
{
0 commit comments