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 |
||
| 34 | class API extends AbstractAPI |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Merchant instance. |
||
| 38 | * |
||
| 39 | * @var Merchant |
||
| 40 | */ |
||
| 41 | protected $merchant; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Sandbox box mode. |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $sandboxEnabled = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Sandbox sign key. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $sandboxSignKey; |
||
| 56 | |||
| 57 | const API_HOST = 'https://api.mch.weixin.qq.com'; |
||
| 58 | |||
| 59 | // api |
||
| 60 | const API_PAY_ORDER = '/pay/micropay'; |
||
| 61 | const API_PREPARE_ORDER = '/pay/unifiedorder'; |
||
| 62 | const API_QUERY = '/pay/orderquery'; |
||
| 63 | const API_CLOSE = '/pay/closeorder'; |
||
| 64 | const API_REVERSE = '/secapi/pay/reverse'; |
||
| 65 | const API_REFUND = '/secapi/pay/refund'; |
||
| 66 | const API_QUERY_REFUND = '/pay/refundquery'; |
||
| 67 | const API_DOWNLOAD_BILL = '/pay/downloadbill'; |
||
| 68 | const API_REPORT = '/payitil/report'; |
||
| 69 | |||
| 70 | const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
| 71 | const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
| 72 | const API_SANDBOX_SIGN_KEY = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'; |
||
| 73 | |||
| 74 | // order id types. |
||
| 75 | const TRANSACTION_ID = 'transaction_id'; |
||
| 76 | const OUT_TRADE_NO = 'out_trade_no'; |
||
| 77 | const OUT_REFUND_NO = 'out_refund_no'; |
||
| 78 | const REFUND_ID = 'refund_id'; |
||
| 79 | |||
| 80 | // bill types. |
||
| 81 | const BILL_TYPE_ALL = 'ALL'; |
||
| 82 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
| 83 | const BILL_TYPE_REFUND = 'REFUND'; |
||
| 84 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * API constructor. |
||
| 88 | * |
||
| 89 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
| 90 | */ |
||
| 91 | 12 | public function __construct(Merchant $merchant) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Pay the order. |
||
| 98 | * |
||
| 99 | * @param Order $order |
||
| 100 | * |
||
| 101 | * @return \EasyWeChat\Support\Collection |
||
| 102 | */ |
||
| 103 | 1 | public function pay(Order $order) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Prepare order to pay. |
||
| 110 | * |
||
| 111 | * @param Order $order |
||
| 112 | * |
||
| 113 | * @return \EasyWeChat\Support\Collection |
||
| 114 | */ |
||
| 115 | 1 | public function prepare(Order $order) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Query order. |
||
| 127 | * |
||
| 128 | * @param string $orderNo |
||
| 129 | * @param string $type |
||
| 130 | * |
||
| 131 | * @return \EasyWeChat\Support\Collection |
||
| 132 | */ |
||
| 133 | 1 | View Code Duplication | public function query($orderNo, $type = self::OUT_TRADE_NO) |
| 141 | |||
| 142 | /** |
||
| 143 | * Query order by transaction_id. |
||
| 144 | * |
||
| 145 | * @param string $transactionId |
||
| 146 | * |
||
| 147 | * @return \EasyWeChat\Support\Collection |
||
| 148 | */ |
||
| 149 | 1 | public function queryByTransactionId($transactionId) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Close order by out_trade_no. |
||
| 156 | * |
||
| 157 | * @param $tradeNo |
||
| 158 | * |
||
| 159 | * @return \EasyWeChat\Support\Collection |
||
| 160 | */ |
||
| 161 | 1 | public function close($tradeNo) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Reverse order. |
||
| 172 | * |
||
| 173 | * @param string $orderNo |
||
| 174 | * @param string $type |
||
| 175 | * |
||
| 176 | * @return \EasyWeChat\Support\Collection |
||
| 177 | */ |
||
| 178 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Reverse order by transaction_id. |
||
| 189 | * |
||
| 190 | * @param int $transactionId |
||
| 191 | * |
||
| 192 | * @return \EasyWeChat\Support\Collection |
||
| 193 | */ |
||
| 194 | public function reverseByTransactionId($transactionId) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Make a refund request. |
||
| 201 | * |
||
| 202 | * @param string $orderNo |
||
| 203 | * @param float $totalFee |
||
| 204 | * @param float $refundFee |
||
| 205 | * @param string $opUserId |
||
| 206 | * @param string $type |
||
| 207 | * @param string $refundAccount |
||
| 208 | * |
||
| 209 | * @return \EasyWeChat\Support\Collection |
||
| 210 | */ |
||
| 211 | 1 | public function refund( |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Refund by transaction id. |
||
| 235 | * |
||
| 236 | * @param string $orderNo |
||
| 237 | * @param float $totalFee |
||
| 238 | * @param float $refundFee |
||
| 239 | * @param string $opUserId |
||
| 240 | * @param string $refundAccount |
||
| 241 | * |
||
| 242 | * @return \EasyWeChat\Support\Collection |
||
| 243 | */ |
||
| 244 | public function refundByTransactionId( |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Query refund status. |
||
| 257 | * |
||
| 258 | * @param string $orderNo |
||
| 259 | * @param string $type |
||
| 260 | * |
||
| 261 | * @return \EasyWeChat\Support\Collection |
||
| 262 | */ |
||
| 263 | 1 | View Code Duplication | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
| 271 | |||
| 272 | /** |
||
| 273 | * Query refund status by out_refund_no. |
||
| 274 | * |
||
| 275 | * @param string $refundNo |
||
| 276 | * |
||
| 277 | * @return \EasyWeChat\Support\Collection |
||
| 278 | */ |
||
| 279 | public function queryRefundByRefundNo($refundNo) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Query refund status by transaction_id. |
||
| 286 | * |
||
| 287 | * @param string $transactionId |
||
| 288 | * |
||
| 289 | * @return \EasyWeChat\Support\Collection |
||
| 290 | */ |
||
| 291 | public function queryRefundByTransactionId($transactionId) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Query refund status by refund_id. |
||
| 298 | * |
||
| 299 | * @param string $refundId |
||
| 300 | * |
||
| 301 | * @return \EasyWeChat\Support\Collection |
||
| 302 | */ |
||
| 303 | public function queryRefundByRefundId($refundId) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Download bill history as a table file. |
||
| 310 | * |
||
| 311 | * @param string $date |
||
| 312 | * @param string $type |
||
| 313 | * |
||
| 314 | * @return \Psr\Http\Message\ResponseInterface |
||
| 315 | */ |
||
| 316 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Convert long url to short url. |
||
| 328 | * |
||
| 329 | * @param string $url |
||
| 330 | * |
||
| 331 | * @return \EasyWeChat\Support\Collection |
||
| 332 | */ |
||
| 333 | 1 | public function urlShorten($url) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Report API status to WeChat. |
||
| 340 | * |
||
| 341 | * @param string $api |
||
| 342 | * @param int $timeConsuming |
||
| 343 | * @param string $resultCode |
||
| 344 | * @param string $returnCode |
||
| 345 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
| 346 | * |
||
| 347 | * @return \EasyWeChat\Support\Collection |
||
| 348 | */ |
||
| 349 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get openid by auth code. |
||
| 366 | * |
||
| 367 | * @param string $authCode |
||
| 368 | * |
||
| 369 | * @return \EasyWeChat\Support\Collection |
||
| 370 | */ |
||
| 371 | 1 | public function authCodeToOpenId($authCode) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Merchant setter. |
||
| 378 | * |
||
| 379 | * @param Merchant $merchant |
||
| 380 | * |
||
| 381 | * @return $this |
||
| 382 | */ |
||
| 383 | 1 | public function setMerchant(Merchant $merchant) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Merchant getter. |
||
| 390 | * |
||
| 391 | * @return Merchant |
||
| 392 | */ |
||
| 393 | 1 | public function getMerchant() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Set sandbox mode. |
||
| 400 | * |
||
| 401 | * @param bool $enabled |
||
| 402 | * |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | 10 | public function sandboxMode($enabled = false) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Make a API request. |
||
| 414 | * |
||
| 415 | * @param string $api |
||
| 416 | * @param array $params |
||
| 417 | * @param string $method |
||
| 418 | * @param array $options |
||
| 419 | * @param bool $returnResponse |
||
| 420 | * |
||
| 421 | * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
| 422 | */ |
||
| 423 | 10 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Request with SSL. |
||
| 450 | * |
||
| 451 | * @param string $api |
||
| 452 | * @param array $params |
||
| 453 | * @param string $method |
||
| 454 | * |
||
| 455 | * @return \EasyWeChat\Support\Collection |
||
| 456 | */ |
||
| 457 | 2 | protected function safeRequest($api, array $params, $method = 'post') |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Parse Response XML to array. |
||
| 469 | * |
||
| 470 | * @param ResponseInterface $response |
||
| 471 | * |
||
| 472 | * @return \EasyWeChat\Support\Collection |
||
| 473 | */ |
||
| 474 | 9 | protected function parseResponse($response) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Wrap API. |
||
| 485 | * |
||
| 486 | * @param string $resource |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | 8 | protected function wrapApi($resource) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Get sandbox sign key. |
||
| 497 | */ |
||
| 498 | protected function getSandboxSignKey() |
||
| 521 | } |
||
| 522 |