| Total Complexity | 45 |
| Total Lines | 437 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 2 | Features | 2 |
Complex classes like SnappPay 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.
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 SnappPay, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class SnappPay extends Driver |
||
| 17 | { |
||
| 18 | const VERSION = '1.8'; |
||
| 19 | const RELEASE_DATE = '2023-01-08'; |
||
| 20 | |||
| 21 | const OAUTH_URL = '/api/online/v1/oauth/token'; |
||
| 22 | const ELIGIBLE_URL = '/api/online/offer/v1/eligible'; |
||
| 23 | const TOKEN_URL = '/api/online/payment/v1/token'; |
||
| 24 | const VERIFY_URL = '/api/online/payment/v1/verify'; |
||
| 25 | const SETTLE_URL = '/api/online/payment/v1/settle'; |
||
| 26 | const REVERT_URL = '/api/online/payment/v1/revert'; |
||
| 27 | const STATUS_URL = '/api/online/payment/v1/status'; |
||
| 28 | const CANCEL_URL = '/api/online/payment/v1/cancel'; |
||
| 29 | const UPDATE_URL = '/api/online/payment/v1/update'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * SnappPay Client. |
||
| 33 | * |
||
| 34 | * @var Client |
||
| 35 | */ |
||
| 36 | protected $client; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Invoice |
||
| 40 | * |
||
| 41 | * @var Invoice |
||
| 42 | */ |
||
| 43 | protected $invoice; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Driver settings |
||
| 47 | * |
||
| 48 | * @var object |
||
| 49 | */ |
||
| 50 | protected $settings; |
||
| 51 | /** |
||
| 52 | * SnappPay Oauth Data |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $oauthToken; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * SnappPay payment url |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $paymentUrl; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * SnappPay constructor. |
||
| 67 | * Construct the class with the relevant settings. |
||
| 68 | */ |
||
| 69 | public function __construct(Invoice $invoice, $settings) |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @throws PurchaseFailedException |
||
| 79 | */ |
||
| 80 | public function purchase(): string |
||
| 140 | } |
||
| 141 | |||
| 142 | public function pay(): RedirectionForm |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @throws InvalidPaymentException |
||
| 151 | */ |
||
| 152 | public function verify(): ReceiptInterface |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @throws PurchaseFailedException |
||
| 185 | */ |
||
| 186 | protected function oauth() |
||
| 187 | { |
||
| 188 | $response = $this |
||
| 189 | ->client |
||
| 190 | ->post( |
||
| 191 | $this->settings->apiPaymentUrl.self::OAUTH_URL, |
||
| 192 | [ |
||
| 193 | RequestOptions::HEADERS => [ |
||
| 194 | 'Authorization' => 'Basic '.base64_encode("{$this->settings->client_id}:{$this->settings->client_secret}"), |
||
| 195 | ], |
||
| 196 | RequestOptions::FORM_PARAMS => [ |
||
| 197 | 'grant_type' => 'password', |
||
| 198 | 'scope' => 'online-merchant', |
||
| 199 | 'username' => $this->settings->username, |
||
| 200 | 'password' => $this->settings->password, |
||
| 201 | ], |
||
| 202 | RequestOptions::HTTP_ERRORS => false, |
||
| 203 | ] |
||
| 204 | ); |
||
| 205 | |||
| 206 | if ($response->getStatusCode() != 200) { |
||
| 207 | throw new PurchaseFailedException('خطا در هنگام احراز هویت.'); |
||
| 208 | } |
||
| 209 | |||
| 210 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 211 | |||
| 212 | return $body['access_token']; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @throws PurchaseFailedException |
||
| 217 | */ |
||
| 218 | public function eligible() |
||
| 219 | { |
||
| 220 | if (is_null($amount = $this->invoice->getAmount())) { |
||
| 221 | throw new PurchaseFailedException('"amount" is required for this method.'); |
||
| 222 | } |
||
| 223 | |||
| 224 | $response = $this->client->get($this->settings->apiPaymentUrl.self::ELIGIBLE_URL, [ |
||
| 225 | RequestOptions::HEADERS => [ |
||
| 226 | 'Authorization' => 'Bearer '.$this->oauthToken, |
||
| 227 | ], |
||
| 228 | RequestOptions::QUERY => [ |
||
| 229 | 'amount' => $this->normalizerAmount($amount), |
||
| 230 | ], |
||
| 231 | ]); |
||
| 232 | |||
| 233 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 234 | |||
| 235 | if ($response->getStatusCode() != 200) { |
||
| 236 | throw new InvalidPaymentException('', (int) $response->getStatusCode()); |
||
| 237 | } |
||
| 238 | |||
| 239 | return $body; |
||
| 240 | } |
||
| 241 | |||
| 242 | private function normalizerAmount(int $amount): int |
||
| 245 | } |
||
| 246 | |||
| 247 | private function normalizerCartList(array &$data): void |
||
| 248 | { |
||
| 249 | if (isset($data['cartList']['shippingAmount'])) { |
||
| 250 | $data['cartList'] = [$data['cartList']]; |
||
| 251 | } |
||
| 252 | |||
| 253 | foreach ($data['cartList'] as &$item) { |
||
| 254 | if (isset($item['shippingAmount'])) { |
||
| 255 | $item['shippingAmount'] = $this->normalizerAmount($item['shippingAmount']); |
||
| 256 | } |
||
| 257 | |||
| 258 | if (isset($item['taxAmount'])) { |
||
| 259 | $item['taxAmount'] = $this->normalizerAmount($item['taxAmount']); |
||
| 260 | } |
||
| 261 | |||
| 262 | if (isset($item['totalAmount'])) { |
||
| 263 | $item['totalAmount'] = $this->normalizerAmount($item['totalAmount']); |
||
| 264 | } |
||
| 265 | |||
| 266 | foreach ($item['cartItems'] as &$cartItem) { |
||
| 267 | $cartItem['amount'] = $this->normalizerAmount($cartItem['amount']); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | public function settle(): array |
||
| 273 | { |
||
| 274 | $data = [ |
||
| 275 | 'paymentToken' => $this->invoice->getTransactionId(), |
||
| 276 | ]; |
||
| 277 | |||
| 278 | $response = $this |
||
| 279 | ->client |
||
| 280 | ->post( |
||
| 281 | $this->settings->apiPaymentUrl.self::SETTLE_URL, |
||
| 282 | [ |
||
| 283 | RequestOptions::BODY => json_encode($data), |
||
| 284 | RequestOptions::HEADERS => [ |
||
| 285 | 'Content-Type' => 'application/json', |
||
| 286 | 'Authorization' => 'Bearer '.$this->oauthToken, |
||
| 287 | ], |
||
| 288 | RequestOptions::HTTP_ERRORS => false, |
||
| 289 | ] |
||
| 290 | ); |
||
| 291 | |||
| 292 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 293 | |||
| 294 | if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
||
| 295 | // error has happened |
||
| 296 | $message = $body['errorData']['message'] ?? 'خطا در Settle تراکنش'; |
||
| 297 | throw new PurchaseFailedException($message); |
||
| 298 | } |
||
| 299 | |||
| 300 | return $body['response']; |
||
| 301 | } |
||
| 302 | |||
| 303 | public function revert() |
||
| 304 | { |
||
| 305 | $data = [ |
||
| 306 | 'paymentToken' => $this->invoice->getTransactionId(), |
||
| 307 | ]; |
||
| 308 | |||
| 309 | $response = $this |
||
| 310 | ->client |
||
| 311 | ->post( |
||
| 312 | $this->settings->apiPaymentUrl.self::REVERT_URL, |
||
| 313 | [ |
||
| 314 | RequestOptions::BODY => json_encode($data), |
||
| 315 | RequestOptions::HEADERS => [ |
||
| 316 | 'Content-Type' => 'application/json', |
||
| 317 | 'Authorization' => 'Bearer '.$this->oauthToken, |
||
| 318 | ], |
||
| 319 | RequestOptions::HTTP_ERRORS => false, |
||
| 320 | ] |
||
| 321 | ); |
||
| 322 | |||
| 323 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 324 | |||
| 325 | if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
||
| 326 | // error has happened |
||
| 327 | $message = $body['errorData']['message'] ?? 'خطا در Revert تراکنش'; |
||
| 328 | throw new PurchaseFailedException($message); |
||
| 329 | } |
||
| 330 | |||
| 331 | return $body['response']; |
||
| 332 | } |
||
| 333 | |||
| 334 | public function status() |
||
| 335 | { |
||
| 336 | $data = [ |
||
| 337 | 'paymentToken' => $this->invoice->getTransactionId(), |
||
| 338 | ]; |
||
| 339 | |||
| 340 | $response = $this |
||
| 341 | ->client |
||
| 342 | ->get( |
||
| 343 | $this->settings->apiPaymentUrl.self::STATUS_URL, |
||
| 344 | [ |
||
| 345 | RequestOptions::BODY => json_encode($data), |
||
| 346 | RequestOptions::HEADERS => [ |
||
| 347 | 'Content-Type' => 'application/json', |
||
| 348 | 'Authorization' => 'Bearer '.$this->oauthToken, |
||
| 349 | ], |
||
| 350 | RequestOptions::HTTP_ERRORS => false, |
||
| 351 | ] |
||
| 352 | ); |
||
| 353 | |||
| 354 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 355 | |||
| 356 | if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
||
| 357 | // error has happened |
||
| 358 | $message = $body['errorData']['message'] ?? 'خطا در status تراکنش'; |
||
| 359 | throw new PurchaseFailedException($message); |
||
| 360 | } |
||
| 361 | |||
| 362 | return $body['response']; |
||
| 363 | } |
||
| 364 | |||
| 365 | public function cancel() |
||
| 366 | { |
||
| 367 | $data = [ |
||
| 368 | 'paymentToken' => $this->invoice->getTransactionId(), |
||
| 369 | ]; |
||
| 370 | |||
| 371 | $response = $this |
||
| 372 | ->client |
||
| 373 | ->post( |
||
| 374 | $this->settings->apiPaymentUrl.self::CANCEL_URL, |
||
| 375 | [ |
||
| 376 | RequestOptions::BODY => json_encode($data), |
||
| 377 | RequestOptions::HEADERS => [ |
||
| 378 | 'Content-Type' => 'application/json', |
||
| 379 | 'Authorization' => 'Bearer '.$this->oauthToken, |
||
| 380 | ], |
||
| 381 | RequestOptions::HTTP_ERRORS => false, |
||
| 382 | ] |
||
| 383 | ); |
||
| 384 | |||
| 385 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 386 | |||
| 387 | if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
||
| 388 | // error has happened |
||
| 389 | $message = $body['errorData']['message'] ?? 'خطا در Cancel تراکنش'; |
||
| 390 | throw new PurchaseFailedException($message); |
||
| 391 | } |
||
| 392 | |||
| 393 | return $body['response']; |
||
| 394 | } |
||
| 395 | |||
| 396 | public function update() |
||
| 397 | { |
||
| 398 | $data = [ |
||
| 399 | 'amount' => $this->normalizerAmount($this->invoice->getAmount()), |
||
| 400 | 'paymentMethodTypeDto' => 'INSTALLMENT', |
||
| 401 | 'paymentToken' => $this->invoice->getTransactionId(), |
||
| 402 | ]; |
||
| 403 | |||
| 404 | if (!is_null($discountAmount = $this->invoice->getDetail('discountAmount'))) { |
||
| 405 | $data['discountAmount'] = $this->normalizerAmount($discountAmount); |
||
| 406 | } |
||
| 407 | |||
| 408 | if (!is_null($externalSourceAmount = $this->invoice->getDetail('externalSourceAmount'))) { |
||
| 409 | $data['externalSourceAmount'] = $externalSourceAmount; |
||
| 410 | } |
||
| 411 | |||
| 412 | if (is_null($this->invoice->getDetail('cartList'))) { |
||
| 413 | throw new PurchaseFailedException('"cartList" is required for this driver'); |
||
| 414 | } |
||
| 415 | |||
| 416 | $data['cartList'] = $this->invoice->getDetail('cartList'); |
||
| 417 | |||
| 418 | $this->normalizerCartList($data); |
||
| 419 | |||
| 420 | $response = $this |
||
| 421 | ->client |
||
| 422 | ->post( |
||
| 423 | $this->settings->apiPaymentUrl.self::TOKEN_URL, |
||
| 424 | [ |
||
| 425 | RequestOptions::BODY => json_encode($data), |
||
| 426 | RequestOptions::HEADERS => [ |
||
| 427 | 'Content-Type' => 'application/json', |
||
| 428 | 'Authorization' => 'Bearer '.$this->oauthToken, |
||
| 429 | ], |
||
| 430 | RequestOptions::HTTP_ERRORS => false, |
||
| 431 | ] |
||
| 432 | ); |
||
| 433 | |||
| 434 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 435 | |||
| 436 | if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
||
| 437 | // error has happened |
||
| 438 | $message = $body['errorData']['message'] ?? 'خطا در بروزرسانی تراکنش رخ داده است.'; |
||
| 439 | throw new PurchaseFailedException($message); |
||
| 440 | } |
||
| 441 | |||
| 442 | return $body['response']; |
||
| 443 | } |
||
| 444 | |||
| 445 | public function getPaymentUrl(): string |
||
| 446 | { |
||
| 447 | return $this->paymentUrl; |
||
| 448 | } |
||
| 449 | |||
| 450 | public function setPaymentUrl(string $paymentUrl): void |
||
| 453 | } |
||
| 454 | } |
||
| 455 |