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:
Complex classes like API often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use API, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class API extends AbstractAPI |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Merchant instance. |
||
| 39 | * |
||
| 40 | * @var Merchant |
||
| 41 | */ |
||
| 42 | protected $merchant; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sandbox box mode. |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $sandboxEnabled = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Sandbox sign key. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $sandboxSignKey; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Cache. |
||
| 60 | * |
||
| 61 | * @var Cache |
||
| 62 | */ |
||
| 63 | protected $cache; |
||
| 64 | |||
| 65 | const API_HOST = 'https://api.mch.weixin.qq.com'; |
||
| 66 | |||
| 67 | // api |
||
| 68 | const API_PAY_ORDER = '/pay/micropay'; |
||
| 69 | const API_PREPARE_ORDER = '/pay/unifiedorder'; |
||
| 70 | const API_QUERY = '/pay/orderquery'; |
||
| 71 | const API_CLOSE = '/pay/closeorder'; |
||
| 72 | const API_REVERSE = '/secapi/pay/reverse'; |
||
| 73 | const API_REFUND = '/secapi/pay/refund'; |
||
| 74 | const API_QUERY_REFUND = '/pay/refundquery'; |
||
| 75 | const API_DOWNLOAD_BILL = '/pay/downloadbill'; |
||
| 76 | const API_REPORT = '/payitil/report'; |
||
| 77 | |||
| 78 | const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
| 79 | const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
| 80 | const API_SANDBOX_SIGN_KEY = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'; |
||
| 81 | |||
| 82 | // order id types. |
||
| 83 | const TRANSACTION_ID = 'transaction_id'; |
||
| 84 | const OUT_TRADE_NO = 'out_trade_no'; |
||
| 85 | const OUT_REFUND_NO = 'out_refund_no'; |
||
| 86 | const REFUND_ID = 'refund_id'; |
||
| 87 | |||
| 88 | // bill types. |
||
| 89 | const BILL_TYPE_ALL = 'ALL'; |
||
| 90 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
| 91 | const BILL_TYPE_REFUND = 'REFUND'; |
||
| 92 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * API constructor. |
||
| 96 | * |
||
| 97 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
| 98 | * @param \EasyWeChat\Payment\Cache|null $cache |
||
| 99 | */ |
||
| 100 | 12 | public function __construct(Merchant $merchant, Cache $cache = null) |
|
| 101 | { |
||
| 102 | 12 | $this->merchant = $merchant; |
|
| 103 | 12 | $this->cache = $cache; |
|
| 104 | 12 | } |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Pay the order. |
||
| 108 | * |
||
| 109 | * @param Order $order |
||
| 110 | * |
||
| 111 | * @return \EasyWeChat\Support\Collection |
||
| 112 | */ |
||
| 113 | 1 | public function pay(Order $order) |
|
| 114 | { |
||
| 115 | 1 | return $this->request($this->wrapApi(self::API_PAY_ORDER), $order->all()); |
|
|
|
|||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Prepare order to pay. |
||
| 120 | * |
||
| 121 | * @param Order $order |
||
| 122 | * |
||
| 123 | * @return \EasyWeChat\Support\Collection |
||
| 124 | */ |
||
| 125 | 1 | public function prepare(Order $order) |
|
| 126 | { |
||
| 127 | 1 | $order->notify_url = $order->get('notify_url', $this->merchant->notify_url); |
|
| 128 | 1 | if (is_null($order->spbill_create_ip)) { |
|
| 129 | 1 | $order->spbill_create_ip = ($order->trade_type === Order::NATIVE) ? get_server_ip() : get_client_ip(); |
|
| 130 | 1 | } |
|
| 131 | |||
| 132 | 1 | return $this->request($this->wrapApi(self::API_PREPARE_ORDER), $order->all()); |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Query order. |
||
| 137 | * |
||
| 138 | * @param string $orderNo |
||
| 139 | * @param string $type |
||
| 140 | * |
||
| 141 | * @return \EasyWeChat\Support\Collection |
||
| 142 | */ |
||
| 143 | 1 | View Code Duplication | public function query($orderNo, $type = self::OUT_TRADE_NO) |
| 144 | { |
||
| 145 | $params = [ |
||
| 146 | 1 | $type => $orderNo, |
|
| 147 | 1 | ]; |
|
| 148 | |||
| 149 | 1 | return $this->request($this->wrapApi(self::API_QUERY), $params); |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Query order by transaction_id. |
||
| 154 | * |
||
| 155 | * @param string $transactionId |
||
| 156 | * |
||
| 157 | * @return \EasyWeChat\Support\Collection |
||
| 158 | */ |
||
| 159 | 1 | public function queryByTransactionId($transactionId) |
|
| 160 | { |
||
| 161 | 1 | return $this->query($transactionId, self::TRANSACTION_ID); |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Close order by out_trade_no. |
||
| 166 | * |
||
| 167 | * @param $tradeNo |
||
| 168 | * |
||
| 169 | * @return \EasyWeChat\Support\Collection |
||
| 170 | */ |
||
| 171 | 1 | public function close($tradeNo) |
|
| 172 | { |
||
| 173 | $params = [ |
||
| 174 | 1 | 'out_trade_no' => $tradeNo, |
|
| 175 | 1 | ]; |
|
| 176 | |||
| 177 | 1 | return $this->request($this->wrapApi(self::API_CLOSE), $params); |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Reverse order. |
||
| 182 | * |
||
| 183 | * @param string $orderNo |
||
| 184 | * @param string $type |
||
| 185 | * |
||
| 186 | * @return \EasyWeChat\Support\Collection |
||
| 187 | */ |
||
| 188 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
| 189 | { |
||
| 190 | $params = [ |
||
| 191 | 1 | $type => $orderNo, |
|
| 192 | 1 | ]; |
|
| 193 | |||
| 194 | 1 | return $this->safeRequest($this->wrapApi(self::API_REVERSE), $params); |
|
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Reverse order by transaction_id. |
||
| 199 | * |
||
| 200 | * @param int $transactionId |
||
| 201 | * |
||
| 202 | * @return \EasyWeChat\Support\Collection |
||
| 203 | */ |
||
| 204 | public function reverseByTransactionId($transactionId) |
||
| 205 | { |
||
| 206 | return $this->reverse($transactionId, self::TRANSACTION_ID); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Make a refund request. |
||
| 211 | * |
||
| 212 | * @param string $orderNo |
||
| 213 | * @param float $totalFee |
||
| 214 | * @param float $refundFee |
||
| 215 | * @param string $opUserId |
||
| 216 | * @param string $type |
||
| 217 | * @param string $refundAccount |
||
| 218 | * |
||
| 219 | * @return \EasyWeChat\Support\Collection |
||
| 220 | */ |
||
| 221 | 1 | public function refund( |
|
| 222 | $orderNo, |
||
| 223 | $refundNo, |
||
| 224 | $totalFee, |
||
| 225 | $refundFee = null, |
||
| 226 | $opUserId = null, |
||
| 227 | $type = self::OUT_TRADE_NO, |
||
| 228 | $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS' |
||
| 229 | ) { |
||
| 230 | $params = [ |
||
| 231 | 1 | $type => $orderNo, |
|
| 232 | 1 | 'out_refund_no' => $refundNo, |
|
| 233 | 1 | 'total_fee' => $totalFee, |
|
| 234 | 1 | 'refund_fee' => $refundFee ?: $totalFee, |
|
| 235 | 1 | 'refund_fee_type' => $this->merchant->fee_type, |
|
| 236 | 1 | 'refund_account' => $refundAccount, |
|
| 237 | 1 | 'op_user_id' => $opUserId ?: $this->merchant->merchant_id, |
|
| 238 | 1 | ]; |
|
| 239 | |||
| 240 | 1 | return $this->safeRequest($this->wrapApi(self::API_REFUND), $params); |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Refund by transaction id. |
||
| 245 | * |
||
| 246 | * @param string $orderNo |
||
| 247 | * @param float $totalFee |
||
| 248 | * @param float $refundFee |
||
| 249 | * @param string $opUserId |
||
| 250 | * @param string $refundAccount |
||
| 251 | * |
||
| 252 | * @return \EasyWeChat\Support\Collection |
||
| 253 | */ |
||
| 254 | public function refundByTransactionId( |
||
| 255 | $orderNo, |
||
| 256 | $refundNo, |
||
| 257 | $totalFee, |
||
| 258 | $refundFee = null, |
||
| 259 | $opUserId = null, |
||
| 260 | $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS' |
||
| 261 | ) { |
||
| 262 | return $this->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, self::TRANSACTION_ID, $refundAccount); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Query refund status. |
||
| 267 | * |
||
| 268 | * @param string $orderNo |
||
| 269 | * @param string $type |
||
| 270 | * |
||
| 271 | * @return \EasyWeChat\Support\Collection |
||
| 272 | */ |
||
| 273 | 1 | View Code Duplication | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
| 274 | { |
||
| 275 | $params = [ |
||
| 276 | 1 | $type => $orderNo, |
|
| 277 | 1 | ]; |
|
| 278 | |||
| 279 | 1 | return $this->request($this->wrapApi(self::API_QUERY_REFUND), $params); |
|
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Query refund status by out_refund_no. |
||
| 284 | * |
||
| 285 | * @param string $refundNo |
||
| 286 | * |
||
| 287 | * @return \EasyWeChat\Support\Collection |
||
| 288 | */ |
||
| 289 | public function queryRefundByRefundNo($refundNo) |
||
| 290 | { |
||
| 291 | return $this->queryRefund($refundNo, self::OUT_REFUND_NO); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Query refund status by transaction_id. |
||
| 296 | * |
||
| 297 | * @param string $transactionId |
||
| 298 | * |
||
| 299 | * @return \EasyWeChat\Support\Collection |
||
| 300 | */ |
||
| 301 | public function queryRefundByTransactionId($transactionId) |
||
| 302 | { |
||
| 303 | return $this->queryRefund($transactionId, self::TRANSACTION_ID); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Query refund status by refund_id. |
||
| 308 | * |
||
| 309 | * @param string $refundId |
||
| 310 | * |
||
| 311 | * @return \EasyWeChat\Support\Collection |
||
| 312 | */ |
||
| 313 | public function queryRefundByRefundId($refundId) |
||
| 314 | { |
||
| 315 | return $this->queryRefund($refundId, self::REFUND_ID); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Download bill history as a table file. |
||
| 320 | * |
||
| 321 | * @param string $date |
||
| 322 | * @param string $type |
||
| 323 | * |
||
| 324 | * @return \Psr\Http\Message\ResponseInterface |
||
| 325 | */ |
||
| 326 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
| 327 | { |
||
| 328 | $params = [ |
||
| 329 | 1 | 'bill_date' => $date, |
|
| 330 | 1 | 'bill_type' => $type, |
|
| 331 | 1 | ]; |
|
| 332 | |||
| 333 | 1 | return $this->request($this->wrapApi(self::API_DOWNLOAD_BILL), $params, 'post', [\GuzzleHttp\RequestOptions::STREAM => true], true)->getBody(); |
|
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Convert long url to short url. |
||
| 338 | * |
||
| 339 | * @param string $url |
||
| 340 | * |
||
| 341 | * @return \EasyWeChat\Support\Collection |
||
| 342 | */ |
||
| 343 | 1 | public function urlShorten($url) |
|
| 344 | { |
||
| 345 | 1 | return $this->request(self::API_URL_SHORTEN, ['long_url' => $url]); |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Report API status to WeChat. |
||
| 350 | * |
||
| 351 | * @param string $api |
||
| 352 | * @param int $timeConsuming |
||
| 353 | * @param string $resultCode |
||
| 354 | * @param string $returnCode |
||
| 355 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
| 356 | * |
||
| 357 | * @return \EasyWeChat\Support\Collection |
||
| 358 | */ |
||
| 359 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
| 360 | { |
||
| 361 | $params = array_merge([ |
||
| 362 | 'interface_url' => $api, |
||
| 363 | 'execute_time_' => $timeConsuming, |
||
| 364 | 'return_code' => $returnCode, |
||
| 365 | 'return_msg' => null, |
||
| 366 | 'result_code' => $resultCode, |
||
| 367 | 'user_ip' => get_client_ip(), |
||
| 368 | 'time' => time(), |
||
| 369 | ], $other); |
||
| 370 | |||
| 371 | return $this->request($this->wrapApi(self::API_REPORT), $params); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get openid by auth code. |
||
| 376 | * |
||
| 377 | * @param string $authCode |
||
| 378 | * |
||
| 379 | * @return \EasyWeChat\Support\Collection |
||
| 380 | */ |
||
| 381 | 1 | public function authCodeToOpenId($authCode) |
|
| 382 | { |
||
| 383 | 1 | return $this->request(self::API_AUTH_CODE_TO_OPENID, ['auth_code' => $authCode]); |
|
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Merchant setter. |
||
| 388 | * |
||
| 389 | * @param Merchant $merchant |
||
| 390 | * |
||
| 391 | * @return $this |
||
| 392 | */ |
||
| 393 | 1 | public function setMerchant(Merchant $merchant) |
|
| 394 | { |
||
| 395 | 1 | $this->merchant = $merchant; |
|
| 396 | 1 | } |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Merchant getter. |
||
| 400 | * |
||
| 401 | * @return Merchant |
||
| 402 | */ |
||
| 403 | 1 | public function getMerchant() |
|
| 404 | { |
||
| 405 | 1 | return $this->merchant; |
|
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Set sandbox mode. |
||
| 410 | * |
||
| 411 | * @param bool $enabled |
||
| 412 | * |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | 10 | public function sandboxMode($enabled = false) |
|
| 416 | { |
||
| 417 | 10 | $this->sandboxEnabled = $enabled; |
|
| 418 | |||
| 419 | 10 | return $this; |
|
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Make a API request. |
||
| 424 | * |
||
| 425 | * @param string $api |
||
| 426 | * @param array $params |
||
| 427 | * @param string $method |
||
| 428 | * @param array $options |
||
| 429 | * @param bool $returnResponse |
||
| 430 | * |
||
| 431 | * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
| 432 | */ |
||
| 433 | 10 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
|
| 434 | { |
||
| 435 | 10 | $params = array_merge($params, $this->merchant->only(['sub_appid', 'sub_mch_id'])); |
|
| 436 | |||
| 437 | 10 | $params['appid'] = $this->merchant->app_id; |
|
| 438 | 10 | $params['mch_id'] = $this->merchant->merchant_id; |
|
| 439 | 10 | $params['device_info'] = $this->merchant->device_info; |
|
| 440 | 10 | $params['nonce_str'] = uniqid(); |
|
| 441 | 10 | $params = array_filter($params); |
|
| 442 | |||
| 443 | 10 | $params['sign'] = $this->generateSign($params); |
|
| 444 | |||
| 445 | 10 | $options = array_merge([ |
|
| 446 | 10 | 'body' => XML::build($params), |
|
| 447 | 10 | ], $options); |
|
| 448 | |||
| 449 | 10 | $response = $this->getHttp()->request($api, $method, $options); |
|
| 450 | |||
| 451 | 10 | return $returnResponse ? $response : $this->parseResponse($response); |
|
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Generate sign. |
||
| 456 | * |
||
| 457 | * @param array $params |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | 10 | protected function generateSign($params) |
|
| 462 | { |
||
| 463 | 10 | $key = ($this->sandboxEnabled && $this->sandboxSignKey) ? $this->sandboxSignKey : $this->merchant->key; |
|
| 464 | |||
| 465 | 10 | return generate_sign($params, $key, 'md5'); |
|
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Request with SSL. |
||
| 470 | * |
||
| 471 | * @param string $api |
||
| 472 | * @param array $params |
||
| 473 | * @param string $method |
||
| 474 | * |
||
| 475 | * @return \EasyWeChat\Support\Collection |
||
| 476 | */ |
||
| 477 | 2 | protected function safeRequest($api, array $params, $method = 'post') |
|
| 478 | { |
||
| 479 | $options = [ |
||
| 480 | 2 | 'cert' => $this->merchant->get('cert_path'), |
|
| 481 | 2 | 'ssl_key' => $this->merchant->get('key_path'), |
|
| 482 | 2 | ]; |
|
| 483 | |||
| 484 | 2 | return $this->request($api, $params, $method, $options); |
|
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Parse Response XML to array. |
||
| 489 | * |
||
| 490 | * @param ResponseInterface $response |
||
| 491 | * |
||
| 492 | * @return \EasyWeChat\Support\Collection |
||
| 493 | */ |
||
| 494 | 9 | protected function parseResponse($response) |
|
| 495 | { |
||
| 496 | 9 | if ($response instanceof ResponseInterface) { |
|
| 497 | $response = $response->getBody(); |
||
| 498 | } |
||
| 499 | |||
| 500 | 9 | return new Collection((array) XML::parse($response)); |
|
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Wrap API. |
||
| 505 | * |
||
| 506 | * @param string $resource |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | 8 | protected function wrapApi($resource) |
|
| 511 | { |
||
| 512 | 8 | return self::API_HOST.($this->sandboxEnabled ? '/sandboxnew' : '').$resource; |
|
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Get sandbox sign key. |
||
| 517 | */ |
||
| 518 | protected function getSandboxSignKey() |
||
| 519 | { |
||
| 520 | // Try to get sandbox_signkey from cache |
||
| 521 | $cacheKey = 'sandbox_signkey'.$this->merchant->merchant_id.$this->merchant->sub_merchant_id; |
||
| 522 | /** @var \Doctrine\Common\Cache\Cache $cache */ |
||
| 523 | $cache = $this->getCache(); |
||
| 524 | $this->sandboxSignKey = $cache->fetch($cacheKey); |
||
| 525 | |||
| 526 | if (!$this->sandboxSignKey) { |
||
| 527 | // Try to acquire a new sandbox_signkey from WeChat |
||
| 528 | $result = $this->request(self::API_SANDBOX_SIGN_KEY, []); |
||
| 529 | if ($result->return_code === 'SUCCESS') { |
||
| 530 | $cache->save($cacheKey, $result->sandbox_signkey); |
||
| 531 | $this->sandboxSignKey = $result->sandbox_signkey; |
||
| 532 | |||
| 533 | return; |
||
| 534 | } |
||
| 535 | |||
| 536 | throw new Exception($result->return_msg); |
||
| 537 | } |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Return the cache manager. |
||
| 542 | * |
||
| 543 | * @return \Doctrine\Common\Cache\Cache |
||
| 544 | */ |
||
| 545 | public function getCache() |
||
| 549 | } |
||
| 550 |