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