Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
28 | class Alipay implements GatewayApplicationInterface |
||
29 | { |
||
30 | /** |
||
31 | * Const mode_normal. |
||
32 | */ |
||
33 | const MODE_NORMAL = 'normal'; |
||
34 | |||
35 | /** |
||
36 | * Const mode_dev. |
||
37 | */ |
||
38 | const MODE_DEV = 'dev'; |
||
39 | |||
40 | /** |
||
41 | * Const url. |
||
42 | */ |
||
43 | const URL = [ |
||
44 | self::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do', |
||
45 | self::MODE_DEV => 'https://openapi.alipaydev.com/gateway.do', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Alipay payload. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $payload; |
||
54 | |||
55 | /** |
||
56 | * Alipay gateway. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $gateway; |
||
61 | |||
62 | /** |
||
63 | * Bootstrap. |
||
64 | * |
||
65 | * @author yansongda <[email protected]> |
||
66 | * |
||
67 | * @param Config $config |
||
68 | */ |
||
69 | public function __construct(Config $config) |
||
70 | { |
||
71 | $this->gateway = Support::create($config)->getBaseUri(); |
||
72 | $this->payload = [ |
||
73 | 'app_id' => $config->get('app_id'), |
||
74 | 'method' => '', |
||
75 | 'format' => 'JSON', |
||
76 | 'charset' => 'utf-8', |
||
77 | 'sign_type' => 'RSA2', |
||
78 | 'version' => '1.0', |
||
79 | 'return_url' => $config->get('return_url'), |
||
80 | 'notify_url' => $config->get('notify_url'), |
||
81 | 'timestamp' => date('Y-m-d H:i:s'), |
||
82 | 'sign' => '', |
||
83 | 'biz_content' => '', |
||
84 | 'app_auth_token' => $config->get('app_auth_token'), |
||
85 | ]; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Magic pay. |
||
90 | * |
||
91 | * @author yansongda <[email protected]> |
||
92 | * |
||
93 | * @param string $method |
||
94 | * @param array $params |
||
95 | * |
||
96 | * @throws InvalidGatewayException |
||
97 | * |
||
98 | * @return Response|Collection |
||
99 | */ |
||
100 | public function __call($method, $params) |
||
104 | |||
105 | /** |
||
106 | * Pay an order. |
||
107 | * |
||
108 | * @author yansongda <[email protected]> |
||
109 | * |
||
110 | * @param string $gateway |
||
111 | * @param array $params |
||
112 | * |
||
113 | * @throws InvalidGatewayException |
||
114 | * |
||
115 | * @return Response|Collection |
||
116 | */ |
||
117 | public function pay($gateway, $params = []) |
||
136 | |||
137 | /** |
||
138 | * Verify sign. |
||
139 | * |
||
140 | * @author yansongda <[email protected]> |
||
141 | * |
||
142 | * @param null|array $data |
||
143 | * @param bool $refund |
||
144 | * |
||
145 | * @throws InvalidSignException |
||
146 | * @throws \Yansongda\Pay\Exceptions\InvalidConfigException |
||
147 | * |
||
148 | * @return Collection |
||
149 | */ |
||
150 | public function verify($data = null, $refund = false): Collection |
||
173 | |||
174 | /** |
||
175 | * Query an order. |
||
176 | * |
||
177 | * @author yansongda <[email protected]> |
||
178 | * |
||
179 | * @param string|array $order |
||
180 | * @param string $type |
||
181 | * @param bool $transfer @deprecated since v2.7.3 |
||
182 | * |
||
183 | * @throws InvalidSignException |
||
184 | * @throws \Yansongda\Pay\Exceptions\GatewayException |
||
185 | * @throws \Yansongda\Pay\Exceptions\InvalidConfigException |
||
186 | * |
||
187 | * @return Collection |
||
188 | */ |
||
189 | public function find($order, $type = 'wap', $transfer = false): Collection |
||
214 | |||
215 | /** |
||
216 | * Refund an order. |
||
217 | * |
||
218 | * @author yansongda <[email protected]> |
||
219 | * |
||
220 | * @param array $order |
||
221 | * |
||
222 | * @throws InvalidSignException |
||
223 | * @throws \Yansongda\Pay\Exceptions\GatewayException |
||
224 | * @throws \Yansongda\Pay\Exceptions\InvalidConfigException |
||
225 | * |
||
226 | * @return Collection |
||
227 | */ |
||
228 | public function refund($order): Collection |
||
238 | |||
239 | /** |
||
240 | * Cancel an order. |
||
241 | * |
||
242 | * @author yansongda <[email protected]> |
||
243 | * |
||
244 | * @param string|array $order |
||
245 | * |
||
246 | * @throws InvalidSignException |
||
247 | * @throws \Yansongda\Pay\Exceptions\GatewayException |
||
248 | * @throws \Yansongda\Pay\Exceptions\InvalidConfigException |
||
249 | * |
||
250 | * @return Collection |
||
251 | */ |
||
252 | View Code Duplication | public function cancel($order): Collection |
|
262 | |||
263 | /** |
||
264 | * Close an order. |
||
265 | * |
||
266 | * @author yansongda <[email protected]> |
||
267 | * |
||
268 | * @param string|array $order |
||
269 | * |
||
270 | * @throws InvalidSignException |
||
271 | * @throws \Yansongda\Pay\Exceptions\GatewayException |
||
272 | * @throws \Yansongda\Pay\Exceptions\InvalidConfigException |
||
273 | * |
||
274 | * @return Collection |
||
275 | */ |
||
276 | View Code Duplication | public function close($order): Collection |
|
286 | |||
287 | /** |
||
288 | * Download bill. |
||
289 | * |
||
290 | * @author yansongda <[email protected]> |
||
291 | * |
||
292 | * @param string|array $bill |
||
293 | * |
||
294 | * @throws InvalidSignException |
||
295 | * @throws \Yansongda\Pay\Exceptions\GatewayException |
||
296 | * @throws \Yansongda\Pay\Exceptions\InvalidConfigException |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | public function download($bill): string |
||
312 | |||
313 | /** |
||
314 | * Reply success to alipay. |
||
315 | * |
||
316 | * @author yansongda <[email protected]> |
||
317 | * |
||
318 | * @return Response |
||
319 | */ |
||
320 | public function success(): Response |
||
326 | |||
327 | /** |
||
328 | * extend. |
||
329 | * |
||
330 | * @author yansongda <[email protected]> |
||
331 | * |
||
332 | * @param string $method |
||
333 | * @param callable $function |
||
334 | * |
||
335 | * @return void |
||
336 | */ |
||
337 | public function extend(string $method, callable $function) |
||
340 | |||
341 | /** |
||
342 | * Make pay gateway. |
||
343 | * |
||
344 | * @author yansongda <[email protected]> |
||
345 | * |
||
346 | * @param string $gateway |
||
347 | * |
||
348 | * @throws InvalidGatewayException |
||
349 | * |
||
350 | * @return Response|Collection |
||
351 | */ |
||
352 | View Code Duplication | protected function makePay($gateway) |
|
364 | } |
||
365 |
If you suppress an error, we recommend checking for the error condition explicitly: