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 TRANSACTION_ID = 'transaction_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 | 12 | public function __construct(Merchant $merchant) |
|
71 | { |
||
72 | 12 | $this->merchant = $merchant; |
|
73 | 12 | } |
|
74 | |||
75 | /** |
||
76 | * Pay the order. |
||
77 | * |
||
78 | * @param Order $order |
||
79 | * |
||
80 | * @return \EasyWeChat\Support\Collection |
||
81 | */ |
||
82 | 1 | 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) |
|
95 | { |
||
96 | 1 | $order->notify_url = $order->get('notify_url', $this->merchant->notify_url); |
|
97 | 1 | $order->spbill_create_ip = ($order->trade_type == Order::NATIVE) ? get_server_ip() : get_client_ip(); |
|
98 | |||
99 | 1 | return $this->request(self::API_PREPARE_ORDER, $order->all()); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Query order. |
||
104 | * |
||
105 | * @param string $orderNo |
||
106 | * @param string $type |
||
107 | * |
||
108 | * @return \EasyWeChat\Support\Collection |
||
109 | */ |
||
110 | 1 | public function query($orderNo, $type = self::OUT_TRADE_NO) |
|
111 | { |
||
112 | $params = [ |
||
113 | 1 | $type => $orderNo, |
|
114 | 1 | ]; |
|
115 | |||
116 | 1 | return $this->request(self::API_QUERY, $params); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Query order by transaction_id. |
||
121 | * |
||
122 | * @param string $transactionId |
||
123 | * |
||
124 | * @return \EasyWeChat\Support\Collection |
||
125 | */ |
||
126 | 1 | public function queryByTransactionId($transactionId) |
|
130 | |||
131 | /** |
||
132 | * Close order by out_trade_no. |
||
133 | * |
||
134 | * @param $tradeNo |
||
135 | * |
||
136 | * @return \EasyWeChat\Support\Collection |
||
137 | */ |
||
138 | 1 | public function close($tradeNo) |
|
139 | { |
||
140 | $params = [ |
||
141 | 1 | 'out_trade_no' => $tradeNo, |
|
142 | 1 | ]; |
|
143 | |||
144 | 1 | return $this->request(self::API_CLOSE, $params); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Reverse order. |
||
149 | * |
||
150 | * @param string $orderNo |
||
151 | * @param string $type |
||
152 | * |
||
153 | * @return \EasyWeChat\Support\Collection |
||
154 | */ |
||
155 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
156 | { |
||
157 | $params = [ |
||
158 | 1 | $type => $orderNo, |
|
159 | 1 | ]; |
|
160 | |||
161 | 1 | return $this->safeRequest(self::API_REVERSE, $params); |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Reverse order by transaction_id. |
||
166 | * |
||
167 | * @param int $transactionId |
||
168 | * |
||
169 | * @return \EasyWeChat\Support\Collection |
||
170 | */ |
||
171 | public function reverseByTransactionId($transactionId) |
||
175 | |||
176 | /** |
||
177 | * Make a refund request. |
||
178 | * |
||
179 | * @param string $orderNo |
||
180 | * @param float $totalFee |
||
181 | * @param float $refundFee |
||
182 | * @param string $opUserId |
||
183 | * @param string $type |
||
184 | * |
||
185 | * @return \EasyWeChat\Support\Collection |
||
186 | */ |
||
187 | 1 | public function refund( |
|
206 | |||
207 | /** |
||
208 | * Refund by transaction id. |
||
209 | * |
||
210 | * @param string $orderNo |
||
211 | * @param float $totalFee |
||
212 | * @param float $refundFee |
||
213 | * @param string $opUserId |
||
214 | * |
||
215 | * @return \EasyWeChat\Support\Collection |
||
216 | */ |
||
217 | public function refundByTransactionId( |
||
226 | |||
227 | /** |
||
228 | * Query refund status. |
||
229 | * |
||
230 | * @param string $orderNo |
||
231 | * @param string $type |
||
232 | * |
||
233 | * @return \EasyWeChat\Support\Collection |
||
234 | */ |
||
235 | 1 | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
|
236 | { |
||
237 | $params = [ |
||
238 | 1 | $type => $orderNo, |
|
239 | 1 | ]; |
|
240 | |||
241 | 1 | return $this->request(self::API_QUERY_REFUND, $params); |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Query refund status by out_refund_no. |
||
246 | * |
||
247 | * @param string $refundNo |
||
248 | * |
||
249 | * @return \EasyWeChat\Support\Collection |
||
250 | */ |
||
251 | public function queryRefundByRefundNo($refundNo) |
||
252 | { |
||
253 | return $this->queryRefund($refundNo, self::OUT_REFUND_NO); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Query refund status by transaction_id. |
||
258 | * |
||
259 | * @param string $transactionId |
||
260 | * |
||
261 | * @return \EasyWeChat\Support\Collection |
||
262 | */ |
||
263 | public function queryRefundByTransactionId($transactionId) |
||
267 | |||
268 | /** |
||
269 | * Query refund status by refund_id. |
||
270 | * |
||
271 | * @param string $refundId |
||
272 | * |
||
273 | * @return \EasyWeChat\Support\Collection |
||
274 | */ |
||
275 | public function queryRefundByRefundId($refundId) |
||
279 | |||
280 | /** |
||
281 | * Download bill history as a table file. |
||
282 | * |
||
283 | * @param string $date |
||
284 | * @param string $type |
||
285 | * |
||
286 | * @return \Psr\Http\Message\ResponseInterface |
||
287 | */ |
||
288 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
297 | |||
298 | /** |
||
299 | * Convert long url to short url. |
||
300 | * |
||
301 | * @param string $url |
||
302 | * |
||
303 | * @return \EasyWeChat\Support\Collection |
||
304 | */ |
||
305 | 1 | public function urlShorten($url) |
|
309 | |||
310 | /** |
||
311 | * Report API status to WeChat. |
||
312 | * |
||
313 | * @param string $api |
||
314 | * @param int $timeConsuming |
||
315 | * @param string $resultCode |
||
316 | * @param string $returnCode |
||
317 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
318 | * |
||
319 | * @return \EasyWeChat\Support\Collection |
||
320 | */ |
||
321 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
335 | |||
336 | /** |
||
337 | * Get openid by auth code. |
||
338 | * |
||
339 | * @param string $authCode |
||
340 | * |
||
341 | * @return \EasyWeChat\Support\Collection |
||
342 | */ |
||
343 | 1 | public function authCodeToOpenId($authCode) |
|
347 | |||
348 | /** |
||
349 | * Merchant setter. |
||
350 | * |
||
351 | * @param Merchant $merchant |
||
352 | * |
||
353 | * @return $this |
||
354 | */ |
||
355 | 1 | public function setMerchant(Merchant $merchant) |
|
359 | |||
360 | /** |
||
361 | * Merchant getter. |
||
362 | * |
||
363 | * @return Merchant |
||
364 | */ |
||
365 | 1 | public function getMerchant() |
|
369 | |||
370 | /** |
||
371 | * Make a API request. |
||
372 | * |
||
373 | * @param string $api |
||
374 | * @param array $params |
||
375 | * @param string $method |
||
376 | * @param array $options |
||
377 | * @param bool $returnResponse |
||
378 | * |
||
379 | * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
380 | */ |
||
381 | 10 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
|
400 | |||
401 | /** |
||
402 | * Request with SSL. |
||
403 | * |
||
404 | * @param string $api |
||
405 | * @param array $params |
||
406 | * @param string $method |
||
407 | * |
||
408 | * @return \EasyWeChat\Support\Collection |
||
409 | */ |
||
410 | 2 | protected function safeRequest($api, array $params, $method = 'post') |
|
419 | |||
420 | /** |
||
421 | * Parse Response XML to array. |
||
422 | * |
||
423 | * @param string|\Psr\Http\Message\ResponseInterface $response |
||
424 | * |
||
425 | * @return \EasyWeChat\Support\Collection |
||
426 | */ |
||
427 | 9 | protected function parseResponse($response) |
|
435 | } |
||
436 |