1 | <?php |
||
31 | class API extends AbstractAPI |
||
32 | { |
||
33 | /** |
||
34 | * Merchant instance. |
||
35 | * |
||
36 | * @var Merchant |
||
37 | */ |
||
38 | protected $merchant; |
||
39 | |||
40 | // api |
||
41 | const API_PAY_ORDER = 'https://api.mch.weixin.qq.com/pay/micropay'; |
||
42 | const API_PREPARE_ORDER = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; |
||
43 | const API_QUERY = 'https://api.mch.weixin.qq.com/pay/orderquery'; |
||
44 | const API_CLOSE = 'https://api.mch.weixin.qq.com/pay/closeorder'; |
||
45 | const API_REVERSE = 'https://api.mch.weixin.qq.com/secapi/pay/reverse'; |
||
46 | const API_REFUND = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; |
||
47 | const API_QUERY_REFUND = 'https://api.mch.weixin.qq.com/pay/refundquery'; |
||
48 | const API_DOWNLOAD_BILL = 'https://api.mch.weixin.qq.com/pay/downloadbill'; |
||
49 | const API_REPORT = 'https://api.mch.weixin.qq.com/payitil/report'; |
||
50 | const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
51 | const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
52 | |||
53 | // order id types. |
||
54 | const TRANSCATION_ID = 'transcation_id'; |
||
55 | const OUT_TRADE_NO = 'out_trade_no'; |
||
56 | const OUT_REFUND_NO = 'out_refund_no'; |
||
57 | const REFUND_ID = 'refund_id'; |
||
58 | |||
59 | // bill types. |
||
60 | const BILL_TYPE_ALL = 'ALL'; |
||
61 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
62 | const BILL_TYPE_REFUND = 'REFUND'; |
||
63 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
64 | |||
65 | /** |
||
66 | * API constructor. |
||
67 | * |
||
68 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
69 | */ |
||
70 | 11 | public function __construct(Merchant $merchant) |
|
74 | |||
75 | /** |
||
76 | * Pay the order. |
||
77 | * |
||
78 | * @param Order $order |
||
79 | * |
||
80 | * @return \EasyWeChat\Support\Collection |
||
81 | */ |
||
82 | public function pay(Order $order) |
||
86 | |||
87 | /** |
||
88 | * Prepare order to pay. |
||
89 | * |
||
90 | * @param Order $order |
||
91 | * |
||
92 | * @return \EasyWeChat\Support\Collection |
||
93 | */ |
||
94 | 1 | public function prepare(Order $order) |
|
98 | |||
99 | /** |
||
100 | * Query order. |
||
101 | * |
||
102 | * @param string $orderNo |
||
103 | * @param string $type |
||
104 | */ |
||
105 | 1 | public function query($orderNo, $type = self::OUT_TRADE_NO) |
|
113 | |||
114 | /** |
||
115 | * Query order by transcation_id. |
||
116 | * |
||
117 | * @param string $transcationId |
||
118 | * |
||
119 | * @return \EasyWeChat\Support\Collection |
||
120 | */ |
||
121 | 1 | public function queryByTranscationId($transcationId) |
|
125 | |||
126 | /** |
||
127 | * Close order by out_trade_no. |
||
128 | * |
||
129 | * @param $tradeNo |
||
130 | * |
||
131 | * @return \EasyWeChat\Support\Collection |
||
132 | */ |
||
133 | 1 | public function close($tradeNo) |
|
141 | |||
142 | /** |
||
143 | * Reverse order. |
||
144 | * |
||
145 | * @param string $orderNo |
||
146 | * @param string $type |
||
147 | * |
||
148 | * @return \EasyWeChat\Support\Collection |
||
149 | */ |
||
150 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
158 | |||
159 | /** |
||
160 | * Reverse order by transcation_id. |
||
161 | * |
||
162 | * @param int $transcationId |
||
163 | * |
||
164 | * @return \EasyWeChat\Support\Collection |
||
165 | */ |
||
166 | public function reverseByTranscationId($transcationId) |
||
170 | |||
171 | /** |
||
172 | * Make a refund request. |
||
173 | * |
||
174 | * @param string $orderNo |
||
175 | * @param float $totalFee |
||
176 | * @param float $refundFee |
||
177 | * @param string $opUserId |
||
178 | * @param string $type |
||
179 | * |
||
180 | * @return \EasyWeChat\Support\Collection |
||
181 | */ |
||
182 | 1 | public function refund( |
|
199 | |||
200 | /** |
||
201 | * Refund by transcation id. |
||
202 | * |
||
203 | * @param string $orderNo |
||
204 | * @param float $totalFee |
||
205 | * @param float $refundFee |
||
206 | * @param string $opUserId |
||
207 | * |
||
208 | * @return \EasyWeChat\Support\Collection |
||
209 | */ |
||
210 | public function refundByTranscationId( |
||
218 | |||
219 | /** |
||
220 | * Query refund status. |
||
221 | * |
||
222 | * @param string $orderNo |
||
223 | * @param string $type |
||
224 | * |
||
225 | * @return \EasyWeChat\Support\Collection |
||
226 | */ |
||
227 | 1 | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
|
235 | |||
236 | /** |
||
237 | * Query refund status by out_refund_no. |
||
238 | * |
||
239 | * @param string $refundNo |
||
240 | * |
||
241 | * @return \EasyWeChat\Support\Collection |
||
242 | */ |
||
243 | public function queryRefundByRefundNo($refundNo) |
||
247 | |||
248 | /** |
||
249 | * Query refund status by transaction_id. |
||
250 | * |
||
251 | * @param string $transcationId |
||
252 | * |
||
253 | * @return \EasyWeChat\Support\Collection |
||
254 | */ |
||
255 | public function queryRefundByTranscationId($transcationId) |
||
259 | |||
260 | /** |
||
261 | * Query refund status by refund_id. |
||
262 | * |
||
263 | * @param string $refundId |
||
264 | * |
||
265 | * @return \EasyWeChat\Support\Collection |
||
266 | */ |
||
267 | public function queryRefundByRefundId($refundId) |
||
271 | |||
272 | /** |
||
273 | * Download bill history as a table file. |
||
274 | * |
||
275 | * @param string $date |
||
276 | * @param string $type |
||
277 | * |
||
278 | * @return stream |
||
279 | */ |
||
280 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
289 | |||
290 | /** |
||
291 | * Convert long url to short url. |
||
292 | * |
||
293 | * @param string $url |
||
294 | * |
||
295 | * @return \EasyWeChat\Support\Collection |
||
296 | */ |
||
297 | 1 | public function urlShorten($url) |
|
301 | |||
302 | /** |
||
303 | * Report API status to WeChat. |
||
304 | * |
||
305 | * @param string $api |
||
306 | * @param int $timeConsuming |
||
307 | * @param string $resultCode |
||
308 | * @param string $returnCode |
||
309 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
310 | * |
||
311 | * @return \EasyWeChat\Support\Collection |
||
312 | */ |
||
313 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
327 | |||
328 | /** |
||
329 | * Get openid by auth code. |
||
330 | * |
||
331 | * @param string $authCode |
||
332 | * |
||
333 | * @return \EasyWeChat\Support\Collection |
||
334 | */ |
||
335 | 1 | public function authCodeToOpenId($authCode) |
|
339 | |||
340 | /** |
||
341 | * Merchant setter. |
||
342 | * |
||
343 | * @param Merchant $merchant |
||
344 | * |
||
345 | * @return $this |
||
346 | */ |
||
347 | 1 | public function setMerchant(Merchant $merchant) |
|
351 | |||
352 | /** |
||
353 | * Merchant getter. |
||
354 | * |
||
355 | * @return Merchant |
||
356 | */ |
||
357 | 1 | public function getMerchant() |
|
361 | |||
362 | /** |
||
363 | * Make a API request. |
||
364 | * |
||
365 | * @param string $api |
||
366 | * @param array $params |
||
367 | * @param string $method |
||
368 | * |
||
369 | * @return \EasyWeChat\Support\Collection |
||
370 | */ |
||
371 | 9 | protected function request($api, array $params, $method = 'post') |
|
382 | |||
383 | /** |
||
384 | * Parse Response XML to array. |
||
385 | * |
||
386 | * @param string $response |
||
387 | * |
||
388 | * @return \EasyWeChat\Support\Collection |
||
389 | */ |
||
390 | 9 | protected function parseResponse($response) |
|
398 | } |
||
399 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: