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 | |||
| 64 | // order id types. |
||
| 65 | const TRANSACTION_ID = 'transaction_id'; |
||
| 66 | const OUT_TRADE_NO = 'out_trade_no'; |
||
| 67 | const OUT_REFUND_NO = 'out_refund_no'; |
||
| 68 | const REFUND_ID = 'refund_id'; |
||
| 69 | |||
| 70 | // bill types. |
||
| 71 | const BILL_TYPE_ALL = 'ALL'; |
||
| 72 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
| 73 | const BILL_TYPE_REFUND = 'REFUND'; |
||
| 74 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * API constructor. |
||
| 78 | * |
||
| 79 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
| 80 | */ |
||
| 81 | 12 | public function __construct(Merchant $merchant) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Pay the order. |
||
| 88 | * |
||
| 89 | * @param Order $order |
||
| 90 | * |
||
| 91 | * @return \EasyWeChat\Support\Collection |
||
| 92 | */ |
||
| 93 | 1 | public function pay(Order $order) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Prepare order to pay. |
||
| 100 | * |
||
| 101 | * @param Order $order |
||
| 102 | * |
||
| 103 | * @return \EasyWeChat\Support\Collection |
||
| 104 | */ |
||
| 105 | 1 | public function prepare(Order $order) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Query order. |
||
| 117 | * |
||
| 118 | * @param string $orderNo |
||
| 119 | * @param string $type |
||
| 120 | * |
||
| 121 | * @return \EasyWeChat\Support\Collection |
||
| 122 | */ |
||
| 123 | 1 | View Code Duplication | public function query($orderNo, $type = self::OUT_TRADE_NO) |
| 131 | |||
| 132 | /** |
||
| 133 | * Query order by transaction_id. |
||
| 134 | * |
||
| 135 | * @param string $transactionId |
||
| 136 | * |
||
| 137 | * @return \EasyWeChat\Support\Collection |
||
| 138 | */ |
||
| 139 | 1 | public function queryByTransactionId($transactionId) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Close order by out_trade_no. |
||
| 146 | * |
||
| 147 | * @param $tradeNo |
||
| 148 | * |
||
| 149 | * @return \EasyWeChat\Support\Collection |
||
| 150 | */ |
||
| 151 | 1 | public function close($tradeNo) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Reverse order. |
||
| 162 | * |
||
| 163 | * @param string $orderNo |
||
| 164 | * @param string $type |
||
| 165 | * |
||
| 166 | * @return \EasyWeChat\Support\Collection |
||
| 167 | */ |
||
| 168 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Reverse order by transaction_id. |
||
| 179 | * |
||
| 180 | * @param int $transactionId |
||
| 181 | * |
||
| 182 | * @return \EasyWeChat\Support\Collection |
||
| 183 | */ |
||
| 184 | public function reverseByTransactionId($transactionId) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Make a refund request. |
||
| 191 | * |
||
| 192 | * @param string $orderNo |
||
| 193 | * @param float $totalFee |
||
| 194 | * @param float $refundFee |
||
| 195 | * @param string $opUserId |
||
| 196 | * @param string $type |
||
| 197 | * @param string $refundAccount |
||
| 198 | * |
||
| 199 | * @return \EasyWeChat\Support\Collection |
||
| 200 | */ |
||
| 201 | 1 | public function refund( |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Refund by transaction id. |
||
| 225 | * |
||
| 226 | * @param string $orderNo |
||
| 227 | * @param float $totalFee |
||
| 228 | * @param float $refundFee |
||
| 229 | * @param string $opUserId |
||
| 230 | * @param string $refundAccount |
||
| 231 | * |
||
| 232 | * @return \EasyWeChat\Support\Collection |
||
| 233 | */ |
||
| 234 | public function refundByTransactionId( |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Query refund status. |
||
| 247 | * |
||
| 248 | * @param string $orderNo |
||
| 249 | * @param string $type |
||
| 250 | * |
||
| 251 | * @return \EasyWeChat\Support\Collection |
||
| 252 | */ |
||
| 253 | 1 | View Code Duplication | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
| 261 | |||
| 262 | /** |
||
| 263 | * Query refund status by out_refund_no. |
||
| 264 | * |
||
| 265 | * @param string $refundNo |
||
| 266 | * |
||
| 267 | * @return \EasyWeChat\Support\Collection |
||
| 268 | */ |
||
| 269 | public function queryRefundByRefundNo($refundNo) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Query refund status by transaction_id. |
||
| 276 | * |
||
| 277 | * @param string $transactionId |
||
| 278 | * |
||
| 279 | * @return \EasyWeChat\Support\Collection |
||
| 280 | */ |
||
| 281 | public function queryRefundByTransactionId($transactionId) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Query refund status by refund_id. |
||
| 288 | * |
||
| 289 | * @param string $refundId |
||
| 290 | * |
||
| 291 | * @return \EasyWeChat\Support\Collection |
||
| 292 | */ |
||
| 293 | public function queryRefundByRefundId($refundId) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Download bill history as a table file. |
||
| 300 | * |
||
| 301 | * @param string $date |
||
| 302 | * @param string $type |
||
| 303 | * |
||
| 304 | * @return \Psr\Http\Message\ResponseInterface |
||
| 305 | */ |
||
| 306 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Convert long url to short url. |
||
| 318 | * |
||
| 319 | * @param string $url |
||
| 320 | * |
||
| 321 | * @return \EasyWeChat\Support\Collection |
||
| 322 | */ |
||
| 323 | 1 | public function urlShorten($url) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Report API status to WeChat. |
||
| 330 | * |
||
| 331 | * @param string $api |
||
| 332 | * @param int $timeConsuming |
||
| 333 | * @param string $resultCode |
||
| 334 | * @param string $returnCode |
||
| 335 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
| 336 | * |
||
| 337 | * @return \EasyWeChat\Support\Collection |
||
| 338 | */ |
||
| 339 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get openid by auth code. |
||
| 356 | * |
||
| 357 | * @param string $authCode |
||
| 358 | * |
||
| 359 | * @return \EasyWeChat\Support\Collection |
||
| 360 | */ |
||
| 361 | 1 | public function authCodeToOpenId($authCode) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Merchant setter. |
||
| 368 | * |
||
| 369 | * @param Merchant $merchant |
||
| 370 | * |
||
| 371 | * @return $this |
||
| 372 | */ |
||
| 373 | 1 | public function setMerchant(Merchant $merchant) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Merchant getter. |
||
| 380 | * |
||
| 381 | * @return Merchant |
||
| 382 | */ |
||
| 383 | 1 | public function getMerchant() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Set sandbox mode. |
||
| 390 | * |
||
| 391 | * @param bool $enabled |
||
| 392 | * |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | 10 | public function sandboxMode($enabled = false) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Make a API request. |
||
| 404 | * |
||
| 405 | * @param string $api |
||
| 406 | * @param array $params |
||
| 407 | * @param string $method |
||
| 408 | * @param array $options |
||
| 409 | * @param bool $returnResponse |
||
| 410 | * |
||
| 411 | * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
| 412 | */ |
||
| 413 | 10 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Request with SSL. |
||
| 435 | * |
||
| 436 | * @param string $api |
||
| 437 | * @param array $params |
||
| 438 | * @param string $method |
||
| 439 | * |
||
| 440 | * @return \EasyWeChat\Support\Collection |
||
| 441 | */ |
||
| 442 | 2 | protected function safeRequest($api, array $params, $method = 'post') |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Parse Response XML to array. |
||
| 454 | * |
||
| 455 | * @param ResponseInterface $response |
||
| 456 | * |
||
| 457 | * @return \EasyWeChat\Support\Collection |
||
| 458 | */ |
||
| 459 | 9 | protected function parseResponse($response) |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Wrap API. |
||
| 470 | * |
||
| 471 | * @param string $resource |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 8 | protected function wrapApi($resource) |
|
| 479 | } |
||
| 480 |