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 |
||
| 32 | class API extends AbstractAPI |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Merchant instance. |
||
| 36 | * |
||
| 37 | * @var Merchant |
||
| 38 | */ |
||
| 39 | protected $merchant; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Sandbox box mode. |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $sandboxEnabled = false; |
||
| 47 | |||
| 48 | const API_HOST = 'https://api.mch.weixin.qq.com'; |
||
| 49 | |||
| 50 | // api |
||
| 51 | const API_PAY_ORDER = '/pay/micropay'; |
||
| 52 | const API_PREPARE_ORDER = '/pay/unifiedorder'; |
||
| 53 | const API_QUERY = '/pay/orderquery'; |
||
| 54 | const API_CLOSE = '/pay/closeorder'; |
||
| 55 | const API_REVERSE = '/secapi/pay/reverse'; |
||
| 56 | const API_REFUND = '/secapi/pay/refund'; |
||
| 57 | const API_QUERY_REFUND = '/pay/refundquery'; |
||
| 58 | const API_DOWNLOAD_BILL = '/pay/downloadbill'; |
||
| 59 | const API_REPORT = '/payitil/report'; |
||
| 60 | |||
| 61 | const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
| 62 | const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
| 63 | const API_SANDBOX_SIGN_KEY = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'; |
||
| 64 | |||
| 65 | // order id types. |
||
| 66 | const TRANSACTION_ID = 'transaction_id'; |
||
| 67 | const OUT_TRADE_NO = 'out_trade_no'; |
||
| 68 | const OUT_REFUND_NO = 'out_refund_no'; |
||
| 69 | const REFUND_ID = 'refund_id'; |
||
| 70 | |||
| 71 | // bill types. |
||
| 72 | const BILL_TYPE_ALL = 'ALL'; |
||
| 73 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
| 74 | const BILL_TYPE_REFUND = 'REFUND'; |
||
| 75 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * API constructor. |
||
| 79 | * |
||
| 80 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
| 81 | */ |
||
| 82 | 12 | public function __construct(Merchant $merchant) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Pay the order. |
||
| 89 | * |
||
| 90 | * @param Order $order |
||
| 91 | * |
||
| 92 | * @return \EasyWeChat\Support\Collection |
||
| 93 | */ |
||
| 94 | 1 | public function pay(Order $order) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Prepare order to pay. |
||
| 101 | * |
||
| 102 | * @param Order $order |
||
| 103 | * |
||
| 104 | * @return \EasyWeChat\Support\Collection |
||
| 105 | */ |
||
| 106 | 1 | public function prepare(Order $order) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Query order. |
||
| 118 | * |
||
| 119 | * @param string $orderNo |
||
| 120 | * @param string $type |
||
| 121 | * |
||
| 122 | * @return \EasyWeChat\Support\Collection |
||
| 123 | */ |
||
| 124 | 1 | View Code Duplication | public function query($orderNo, $type = self::OUT_TRADE_NO) |
| 132 | |||
| 133 | /** |
||
| 134 | * Query order by transaction_id. |
||
| 135 | * |
||
| 136 | * @param string $transactionId |
||
| 137 | * |
||
| 138 | * @return \EasyWeChat\Support\Collection |
||
| 139 | */ |
||
| 140 | 1 | public function queryByTransactionId($transactionId) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Close order by out_trade_no. |
||
| 147 | * |
||
| 148 | * @param $tradeNo |
||
| 149 | * |
||
| 150 | * @return \EasyWeChat\Support\Collection |
||
| 151 | */ |
||
| 152 | 1 | public function close($tradeNo) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Reverse order. |
||
| 163 | * |
||
| 164 | * @param string $orderNo |
||
| 165 | * @param string $type |
||
| 166 | * |
||
| 167 | * @return \EasyWeChat\Support\Collection |
||
| 168 | */ |
||
| 169 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Reverse order by transaction_id. |
||
| 180 | * |
||
| 181 | * @param int $transactionId |
||
| 182 | * |
||
| 183 | * @return \EasyWeChat\Support\Collection |
||
| 184 | */ |
||
| 185 | public function reverseByTransactionId($transactionId) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Make a refund request. |
||
| 192 | * |
||
| 193 | * @param string $orderNo |
||
| 194 | * @param float $totalFee |
||
| 195 | * @param float $refundFee |
||
| 196 | * @param string $opUserId |
||
| 197 | * @param string $type |
||
| 198 | * @param string $refundAccount |
||
| 199 | * |
||
| 200 | * @return \EasyWeChat\Support\Collection |
||
| 201 | */ |
||
| 202 | 1 | public function refund( |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Refund by transaction id. |
||
| 226 | * |
||
| 227 | * @param string $orderNo |
||
| 228 | * @param float $totalFee |
||
| 229 | * @param float $refundFee |
||
| 230 | * @param string $opUserId |
||
| 231 | * @param string $refundAccount |
||
| 232 | * |
||
| 233 | * @return \EasyWeChat\Support\Collection |
||
| 234 | */ |
||
| 235 | public function refundByTransactionId( |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Query refund status. |
||
| 248 | * |
||
| 249 | * @param string $orderNo |
||
| 250 | * @param string $type |
||
| 251 | * |
||
| 252 | * @return \EasyWeChat\Support\Collection |
||
| 253 | */ |
||
| 254 | 1 | View Code Duplication | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
| 262 | |||
| 263 | /** |
||
| 264 | * Query refund status by out_refund_no. |
||
| 265 | * |
||
| 266 | * @param string $refundNo |
||
| 267 | * |
||
| 268 | * @return \EasyWeChat\Support\Collection |
||
| 269 | */ |
||
| 270 | public function queryRefundByRefundNo($refundNo) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Query refund status by transaction_id. |
||
| 277 | * |
||
| 278 | * @param string $transactionId |
||
| 279 | * |
||
| 280 | * @return \EasyWeChat\Support\Collection |
||
| 281 | */ |
||
| 282 | public function queryRefundByTransactionId($transactionId) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Query refund status by refund_id. |
||
| 289 | * |
||
| 290 | * @param string $refundId |
||
| 291 | * |
||
| 292 | * @return \EasyWeChat\Support\Collection |
||
| 293 | */ |
||
| 294 | public function queryRefundByRefundId($refundId) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Download bill history as a table file. |
||
| 301 | * |
||
| 302 | * @param string $date |
||
| 303 | * @param string $type |
||
| 304 | * |
||
| 305 | * @return \Psr\Http\Message\ResponseInterface |
||
| 306 | */ |
||
| 307 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Convert long url to short url. |
||
| 319 | * |
||
| 320 | * @param string $url |
||
| 321 | * |
||
| 322 | * @return \EasyWeChat\Support\Collection |
||
| 323 | */ |
||
| 324 | 1 | public function urlShorten($url) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Report API status to WeChat. |
||
| 331 | * |
||
| 332 | * @param string $api |
||
| 333 | * @param int $timeConsuming |
||
| 334 | * @param string $resultCode |
||
| 335 | * @param string $returnCode |
||
| 336 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
| 337 | * |
||
| 338 | * @return \EasyWeChat\Support\Collection |
||
| 339 | */ |
||
| 340 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get openid by auth code. |
||
| 357 | * |
||
| 358 | * @param string $authCode |
||
| 359 | * |
||
| 360 | * @return \EasyWeChat\Support\Collection |
||
| 361 | */ |
||
| 362 | 1 | public function authCodeToOpenId($authCode) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Merchant setter. |
||
| 369 | * |
||
| 370 | * @param Merchant $merchant |
||
| 371 | * |
||
| 372 | * @return $this |
||
| 373 | */ |
||
| 374 | 1 | public function setMerchant(Merchant $merchant) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Merchant getter. |
||
| 381 | * |
||
| 382 | * @return Merchant |
||
| 383 | */ |
||
| 384 | 1 | public function getMerchant() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Set sandbox mode. |
||
| 391 | * |
||
| 392 | * @param bool $enabled |
||
| 393 | * |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | 10 | public function sandboxMode($enabled = false) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Make a API request. |
||
| 405 | * |
||
| 406 | * @param string $api |
||
| 407 | * @param array $params |
||
| 408 | * @param string $method |
||
| 409 | * @param array $options |
||
| 410 | * @param bool $returnResponse |
||
| 411 | * |
||
| 412 | * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
| 413 | */ |
||
| 414 | 10 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Request with SSL. |
||
| 436 | * |
||
| 437 | * @param string $api |
||
| 438 | * @param array $params |
||
| 439 | * @param string $method |
||
| 440 | * |
||
| 441 | * @return \EasyWeChat\Support\Collection |
||
| 442 | */ |
||
| 443 | 2 | protected function safeRequest($api, array $params, $method = 'post') |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Parse Response XML to array. |
||
| 455 | * |
||
| 456 | * @param ResponseInterface $response |
||
| 457 | * |
||
| 458 | * @return \EasyWeChat\Support\Collection |
||
| 459 | */ |
||
| 460 | 9 | protected function parseResponse($response) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Wrap API. |
||
| 471 | * |
||
| 472 | * @param string $resource |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | 8 | protected function wrapApi($resource) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Get sandbox sign key |
||
| 483 | * |
||
| 484 | * @return \EasyWeChat\Support\Collection |
||
| 485 | */ |
||
| 486 | public function getSandboxSignKey() |
||
| 490 | } |
||
| 491 |