|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Tartan\Larapay\Adapter; |
| 5 | + |
| 6 | +use Tartan\Larapay\Adapter\Nextpay\Exception; |
| 7 | +use Tartan\Larapay\Adapter\Nextpay\Helper; |
| 8 | +use Tartan\Log\Facades\XLog; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class Nextpay |
| 12 | + * @package Tartan\Larapay\Adapter |
| 13 | + */ |
| 14 | +class Nextpay extends AdapterAbstract implements AdapterInterface |
| 15 | +{ |
| 16 | + |
| 17 | + public $endPoint = 'https://nextpay.org/nx/gateway/token'; |
| 18 | + public $endPointForm = 'https://nextpay.org/nx/gateway/payment/{trans_id}'; |
| 19 | + public $endPointVerify = 'https://nextpay.org/nx/gateway/verify'; |
| 20 | + |
| 21 | + public $reverseSupport = false; |
| 22 | + |
| 23 | + /** |
| 24 | + * @return string |
| 25 | + * @throws Exception |
| 26 | + * @throws \Tartan\Larapay\Adapter\Exception |
| 27 | + */ |
| 28 | + protected function requestToken(): string |
| 29 | + { |
| 30 | + if ($this->getTransaction()->checkForRequestToken() == false) { |
| 31 | + throw new Exception('larapay::larapay.could_not_request_payment'); |
| 32 | + } |
| 33 | + |
| 34 | + $this->checkRequiredParameters([ |
| 35 | + 'api_key', |
| 36 | + 'amount', |
| 37 | + 'redirect_url', |
| 38 | + 'order_id', |
| 39 | + ]); |
| 40 | + |
| 41 | + $sendParams = [ |
| 42 | + 'api_key' => $this->api_key, |
| 43 | + 'amount' => intval($this->amount), |
| 44 | + 'order_id' => ($this->order_id), |
| 45 | + 'payer_desc' => $this->description ? $this->description : '', |
| 46 | + 'customer_phone' => $this->mobile ? $this->mobile : '', |
| 47 | + 'callback_uri' => $this->redirect_url, |
| 48 | + ]; |
| 49 | + |
| 50 | + try { |
| 51 | + XLog::debug('PaymentRequest call', $sendParams); |
| 52 | + $result = Helper::post2https($sendParams, $this->endPoint); |
| 53 | + $resultObj = json_decode($result); |
| 54 | + |
| 55 | + XLog::info('PaymentRequest response', $this->obj2array($resultObj)); |
| 56 | + |
| 57 | + if (isset($resultObj->code)) { |
| 58 | + |
| 59 | + if ($resultObj->code == -1) { |
| 60 | + $this->getTransaction()->setGatewayToken(strval($resultObj->trans_id)); // update transaction reference id |
| 61 | + return $resultObj->trans_id; |
| 62 | + } else { |
| 63 | + throw new Exception('larapay::larapay.nextpay.errors.error_'.$resultObj->code); |
| 64 | + } |
| 65 | + } else { |
| 66 | + throw new Exception('larapay::larapay.invalid_response'); |
| 67 | + } |
| 68 | + } catch (\Exception $e) { |
| 69 | + throw new Exception('Nextpay Fault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * @return string |
| 76 | + * @throws Exception |
| 77 | + * @throws \Tartan\Larapay\Adapter\Exception |
| 78 | + */ |
| 79 | + protected function generateForm(): string |
| 80 | + { |
| 81 | + $authority = $this->requestToken(); |
| 82 | + |
| 83 | + $form = view('larapay::nextpay-form', [ |
| 84 | + 'endPoint' => strtr($this->endPointForm, ['{trans_id}' => $authority]), |
| 85 | + 'submitLabel' => !empty($this->submit_label) ? $this->submit_label : trans("larapay::larapay.goto_gate"), |
| 86 | + 'autoSubmit' => true, |
| 87 | + ]); |
| 88 | + return $form->__toString(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return array |
| 93 | + * @throws Exception |
| 94 | + * @throws \Tartan\Larapay\Adapter\Exception |
| 95 | + */ |
| 96 | + public function formParams(): array |
| 97 | + { |
| 98 | + $authority = $this->requestToken(); |
| 99 | + |
| 100 | + return [ |
| 101 | + 'endPoint' => strtr($this->endPointForm, ['{trans_id}' => $authority]), |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return bool |
| 107 | + * @throws Exception |
| 108 | + * @throws \Tartan\Larapay\Adapter\Exception |
| 109 | + */ |
| 110 | + protected function verifyTransaction(): bool |
| 111 | + { |
| 112 | + |
| 113 | + if ($this->getTransaction()->checkForVerify() == false) { |
| 114 | + throw new Exception('larapay::larapay.could_not_verify_payment'); |
| 115 | + } |
| 116 | + |
| 117 | + $this->checkRequiredParameters([ |
| 118 | + 'api_key', |
| 119 | + 'trans_id', |
| 120 | + 'amount' |
| 121 | + ]); |
| 122 | + |
| 123 | + $sendParams = [ |
| 124 | + 'api_key' => $this->api_key, |
| 125 | + 'trans_id' => $this->trans_id, |
| 126 | + 'amount' => $this->amount, |
| 127 | + ]; |
| 128 | + |
| 129 | + try { |
| 130 | + XLog::debug('PaymentVerification call', $sendParams); |
| 131 | + $result = Helper::post2https($sendParams, $this->endPointVerify); |
| 132 | + $response = json_decode($result); |
| 133 | + XLog::info('PaymentVerification response', $this->obj2array($response)); |
| 134 | + if (isset($response->code , $response->Shaparak_Ref_Id)) { |
| 135 | + if ($response->code == 0) { |
| 136 | + $this->getTransaction()->setVerified(); |
| 137 | + $this->getTransaction()->setReferenceId(strval($response->Shaparak_Ref_Id)); // update transaction reference id |
| 138 | + return true; |
| 139 | + } else { |
| 140 | + throw new Exception('larapay::larapay.nextpay.errors.error_'.$response->code); |
| 141 | + } |
| 142 | + } else { |
| 143 | + throw new Exception('larapay::larapay.invalid_response'); |
| 144 | + } |
| 145 | + } catch (\Exception $e) { |
| 146 | + throw new Exception('Nextpay Fault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @return bool |
| 152 | + */ |
| 153 | + public function canContinueWithCallbackParameters(): bool |
| 154 | + { |
| 155 | + if (!empty($this->parameters['trans_id'])) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @return string |
| 164 | + * @throws \Tartan\Larapay\Adapter\Exception |
| 165 | + */ |
| 166 | + public function getGatewayReferenceId(): string |
| 167 | + { |
| 168 | + $this->checkRequiredParameters([ |
| 169 | + 'trans_id', |
| 170 | + ]); |
| 171 | + |
| 172 | + return strval($this->trans_id); |
| 173 | + } |
| 174 | +} |
0 commit comments