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