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