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 |
||
41 | class PaymentGateway extends BasePaymentGateway |
||
42 | { |
||
43 | /** |
||
44 | * Lệnh `getMerchantData` sử dụng cho việc tạo [[request()]] yêu cầu thông tin merchant. |
||
45 | */ |
||
46 | const RC_GET_MERCHANT_DATA = 'getMerchantData'; |
||
47 | |||
48 | /** |
||
49 | * @event RequestEvent được gọi trước khi tạo yêu câu lấy thông tin merchant. |
||
50 | */ |
||
51 | const EVENT_BEFORE_GET_MERCHANT_DATA = 'beforeGetMerchantData'; |
||
52 | |||
53 | /** |
||
54 | * @event RequestEvent được gọi sau khi tạo yêu câu lấy thông tin merchant. |
||
55 | */ |
||
56 | const EVENT_AFTER_GET_MERCHANT_DATA = 'afterGetMerchantData'; |
||
57 | |||
58 | /** |
||
59 | * Đường dẫn API của thanh toán Bảo Kim. |
||
60 | */ |
||
61 | const PURCHASE_URL = '/payment/order/version11'; |
||
62 | |||
63 | /** |
||
64 | * Đường dẫn API của thanh toán PRO. |
||
65 | */ |
||
66 | const PURCHASE_PRO_URL = '/payment/rest/payment_pro_api/pay_by_card'; |
||
67 | |||
68 | /** |
||
69 | * Đường dẫn API để lấy thông tin merchant. |
||
70 | */ |
||
71 | const PRO_SELLER_INFO_URL = '/payment/rest/payment_pro_api/get_seller_info'; |
||
72 | |||
73 | /** |
||
74 | * Đường dẫn API để truy vấn thông tin giao dịch. |
||
75 | */ |
||
76 | const QUERY_DR_URL = '/payment/order/queryTransaction'; |
||
77 | |||
78 | /** |
||
79 | * Đường dẫn API IPN của Bảo Kim để cập nhật và xác minh dữ liệu từ IPN request từ Bảo Kim bắn sang. |
||
80 | * Nói cách khác là sẽ có 2 IPN, 1 cái nằm trên server của bạn và 1 cái là của Bảo Kim để cập nhật đơn hàng của họ. |
||
81 | */ |
||
82 | const VERIFY_IPN_URL = '/bpn/verify'; |
||
83 | |||
84 | /** |
||
85 | * MUI thuộc tính trong mảng data khi tạo thanh toán PRO, cho phép chỉ định giao diện hiển thị charge. |
||
86 | */ |
||
87 | const MUI_CHARGE = 'charge'; |
||
88 | |||
89 | /** |
||
90 | * MUI thuộc tính trong mảng data khi tạo thanh toán PRO, cho phép chỉ định giao diện hiển thị base. |
||
91 | */ |
||
92 | const MUI_BASE = 'base'; |
||
93 | |||
94 | /** |
||
95 | * MUI thuộc tính trong mảng data khi tạo thanh toán PRO, cho phép chỉ định giao diện hiển thị iframe. |
||
96 | */ |
||
97 | const MUI_IFRAME = 'iframe'; |
||
98 | |||
99 | /** |
||
100 | * Transaction mode direct là thuộc tính khi tạo thanh toán Bảo Kim và PRO, cho phép chỉ định giao dịch trực tiếp. |
||
101 | */ |
||
102 | const DIRECT_TRANSACTION = 1; |
||
103 | |||
104 | /** |
||
105 | * Transaction mode safe là thuộc tính khi tạo thanh toán Bảo Kim và PRO, cho phép chỉ định giao dịch tạm giữ. |
||
106 | */ |
||
107 | const SAFE_TRANSACTION = 2; |
||
108 | |||
109 | /** |
||
110 | * @var bool Thiết lập true nếu muốn thanh toán trực tiếp không chuyển khách đến Bảo Kim. |
||
111 | */ |
||
112 | public $pro = false; |
||
113 | |||
114 | /** |
||
115 | * @see getMerchantData |
||
116 | * @var bool|string|array|\yii\caching\Cache Hổ trợ cho việc cache lại dữ liệu merchant lấy từ Bảo Kim nhầm tối ưu hóa hệ thống |
||
117 | * do dữ liệu này ít khi bị thay đổi. |
||
118 | */ |
||
119 | public $merchantDataCache = 'cache'; |
||
120 | |||
121 | /** |
||
122 | * @var int Thời gian cache dữ liệu của merchant lấy từ Bảo Kim. |
||
123 | */ |
||
124 | public $merchantDataCacheDuration = 86400; |
||
125 | |||
126 | /** |
||
127 | * @inheritdoc |
||
128 | */ |
||
129 | public $clientConfig = ['class' => PaymentClient::class]; |
||
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | public $requestDataConfig = ['class' => RequestData::class]; |
||
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | public $responseDataConfig = ['class' => ResponseData::class]; |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | public $verifiedDataConfig = ['class' => VerifiedData::class]; |
||
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | * @since 1.0.3.1 |
||
149 | */ |
||
150 | public $httpClientConfig = ['transport' => 'yii\httpclient\CurlTransport']; |
||
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 10 | public function getBaseUrl(): string |
|
159 | |||
160 | /** |
||
161 | * @throws \yii\base\InvalidConfigException |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | 15 | public function init() |
|
172 | |||
173 | /** |
||
174 | * @inheritdoc |
||
175 | * @since 1.0.3 |
||
176 | */ |
||
177 | 9 | public function requestCommands(): array |
|
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | * @throws \yii\base\InvalidConfigException |
||
185 | */ |
||
186 | 15 | protected function initSandboxEnvironment() |
|
191 | |||
192 | /** |
||
193 | * Phương thức hổ trợ lấy thông tin merchant thông qua email business. |
||
194 | * Đây là phương thức ánh xạ của [[request()]] sử dụng lệnh [[RC_GET_MERCHANT_DATA]]. |
||
195 | * |
||
196 | * @param string $emailBusiness Email muốn lấy thông tin từ Bảo Kim. |
||
197 | * @param int|string|null $clientId PaymentClient id sử dụng để lấy thông tin. |
||
198 | * Nếu không thiết lập [[getDefaultClient()]] sẽ được gọi để xác định client. |
||
199 | * @throws \ReflectionException|\yii\base\InvalidConfigException |
||
200 | * @return ResponseData|DataInterface Trả về [[ResponseData]] là dữ liệu của emailBusiness từ Bảo Kim phản hồi. |
||
201 | */ |
||
202 | 6 | public function getMerchantData(string $emailBusiness = null, $clientId = null): DataInterface |
|
225 | |||
226 | /** |
||
227 | * @inheritdoc |
||
228 | */ |
||
229 | 9 | public function beforeRequest(RequestEvent $event) |
|
237 | |||
238 | /** |
||
239 | * @inheritdoc |
||
240 | */ |
||
241 | 9 | public function afterRequest(RequestEvent $event) |
|
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | * @throws \yii\base\InvalidConfigException|\yii\httpclient\Exception|NotSupportedException |
||
253 | */ |
||
254 | 9 | protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, \yii\httpclient\Client $httpClient): array |
|
294 | |||
295 | /** |
||
296 | * @inheritdoc |
||
297 | */ |
||
298 | 2 | public function verifyRequest($command, \yii\web\Request $request = null, $clientId = null) |
|
319 | |||
320 | /** |
||
321 | * @inheritdoc |
||
322 | */ |
||
323 | 1 | protected function getVerifyRequestData($command, \yii\web\Request $request): array |
|
343 | |||
344 | } |
||
345 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.