| Conditions | 6 |
| Paths | 32 |
| Total Lines | 63 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 72 | public function purchase(): string |
||
| 73 | { |
||
| 74 | $phone = $this->invoice->getDetail('phone') |
||
| 75 | ?? $this->invoice->getDetail('cellphone') |
||
| 76 | ?? $this->invoice->getDetail('mobile'); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @see https://docs.mydigipay.com/upg.html#_request_fields_2 |
||
| 80 | */ |
||
| 81 | $data = [ |
||
| 82 | 'amount' => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), |
||
| 83 | 'cellNumber' => $phone, |
||
| 84 | 'providerId' => $this->invoice->getUuid(), |
||
| 85 | 'callbackUrl' => $this->settings->callbackUrl, |
||
| 86 | ]; |
||
| 87 | |||
| 88 | if (!is_null($basketDetailsDto = $this->invoice->getDetail('basketDetailsDto'))) { |
||
| 89 | $data['basketDetailsDto'] = $basketDetailsDto; |
||
| 90 | } |
||
| 91 | |||
| 92 | if (!is_null($preferredGateway = $this->invoice->getDetail('preferredGateway'))) { |
||
| 93 | $data['preferredGateway'] = $preferredGateway; |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!is_null($splitDetailsList = $this->invoice->getDetail('splitDetailsList'))) { |
||
| 97 | $data['splitDetailsList'] = $splitDetailsList; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @see https://docs.mydigipay.com/upg.html#_query_parameters_2 |
||
| 102 | */ |
||
| 103 | $digipayType = $this->invoice->getDetail('digipayType') ?? 11; |
||
| 104 | |||
| 105 | $response = $this |
||
| 106 | ->client |
||
| 107 | ->request( |
||
| 108 | 'POST', |
||
| 109 | $this->settings->apiPurchaseUrl, |
||
| 110 | [ |
||
| 111 | RequestOptions::BODY => json_encode($data), |
||
| 112 | RequestOptions::QUERY => ['type' => $digipayType], |
||
| 113 | RequestOptions::HEADERS => [ |
||
| 114 | 'Agent' => $this->invoice->getDetail('agent') ?? 'WEB', |
||
| 115 | 'Content-Type' => 'application/json', |
||
| 116 | 'Authorization' => 'Bearer ' . $this->oauthToken, |
||
| 117 | 'Digipay-Version' => '2022-02-02', |
||
| 118 | ], |
||
| 119 | RequestOptions::HTTP_ERRORS => false, |
||
| 120 | ] |
||
| 121 | ); |
||
| 122 | |||
| 123 | if ($response->getStatusCode() != 200) { |
||
| 124 | // error has happened |
||
| 125 | $message = $body['result']['message'] ?? 'خطا در هنگام درخواست برای پرداخت رخ داده است.'; |
||
|
|
|||
| 126 | throw new PurchaseFailedException($message); |
||
| 127 | } |
||
| 128 | |||
| 129 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 130 | $this->invoice->transactionId($body['ticket']); |
||
| 131 | $this->setPaymentUrl($body['redirectUrl']); |
||
| 132 | |||
| 133 | // return the transaction's id |
||
| 134 | return $this->invoice->getTransactionId(); |
||
| 135 | } |
||
| 239 |