|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tartan\Larapay\Adapter; |
| 4 | + |
| 5 | +use Tartan\Larapay\Adapter\Saman\Exception; |
| 6 | +use Illuminate\Support\Facades\Log; |
| 7 | +use Tartan\Larapay\Adapter\Pasargad\Helper; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class Fake |
| 11 | + * @package Tartan\Larapay\Adapter |
| 12 | + */ |
| 13 | +class Fake extends AdapterAbstract implements AdapterInterface |
| 14 | +{ |
| 15 | + protected $baseUrl; |
| 16 | + protected $endPoint; |
| 17 | + |
| 18 | + protected $reverseSupport = true; |
| 19 | + |
| 20 | + public function init() |
| 21 | + { |
| 22 | + parent::init(); |
| 23 | + |
| 24 | + $this->baseUrl = config('larapay.fake.base_url'); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @return string |
| 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 | + 'amount', |
| 39 | + 'redirect_url', |
| 40 | + ]); |
| 41 | + |
| 42 | + $sendParams = [ |
| 43 | + 'amount' => intval($this->amount), |
| 44 | + 'order_id' => $this->order_id, |
| 45 | + 'merchant_id' => $this->merchant_id, |
| 46 | + 'redirect' => $this->redirect_url, |
| 47 | + ]; |
| 48 | + |
| 49 | + try { |
| 50 | + Log::debug('PaymentRequest call', $sendParams); |
| 51 | + $result = Helper::post2https($sendParams, $this->baseUrl . '/api/token'); |
| 52 | + |
| 53 | + |
| 54 | + $resultObj = json_decode($result); |
| 55 | + Log::info('PaymentRequest response', $this->obj2array($resultObj)); |
| 56 | + |
| 57 | + |
| 58 | + if (isset($resultObj->status)) { |
| 59 | + |
| 60 | + if ($resultObj->status == 1) { |
| 61 | + $this->getTransaction()->setGatewayToken($resultObj->transId); // update transaction reference id |
| 62 | + |
| 63 | + return $resultObj->transId; |
| 64 | + } else { |
| 65 | + throw new Exception($resultObj->status); |
| 66 | + } |
| 67 | + } else { |
| 68 | + throw new Exception('larapay::larapay.invalid_response'); |
| 69 | + } |
| 70 | + } catch (\Exception $e) { |
| 71 | + throw new Exception('PayIr Fault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public function generateForm(): string |
| 76 | + { |
| 77 | + Log::debug(__METHOD__, $this->getParameters()); |
| 78 | + |
| 79 | + $token = $this->requestToken(); |
| 80 | + |
| 81 | + Log::info(__METHOD__, ['fetchedToken' => $token]); |
| 82 | + |
| 83 | + return view('larapay::fake-form', [ |
| 84 | + 'endPoint' => $this->getEndPoint(), |
| 85 | + 'submitLabel' => !empty($this->submit_label) ? $this->submit_label : trans("larapay::larapay.goto_gate"), |
| 86 | + 'autoSubmit' => boolval($this->auto_submit), |
| 87 | + ]); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @return bool |
| 92 | + * @throws \Tartan\Larapay\Adapter\Exception |
| 93 | + */ |
| 94 | + protected function verifyTransaction(): bool |
| 95 | + { |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return bool |
| 101 | + * @throws \Tartan\Larapay\Adapter\Exception |
| 102 | + */ |
| 103 | + protected function reverseTransaction(): bool |
| 104 | + { |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return bool |
| 110 | + */ |
| 111 | + public function canContinueWithCallbackParameters(): bool |
| 112 | + { |
| 113 | + |
| 114 | + if (!empty($this->parameters['transId'])) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return string |
| 123 | + * @throws \Tartan\Larapay\Adapter\Exception |
| 124 | + */ |
| 125 | + public function getGatewayReferenceId(): string |
| 126 | + { |
| 127 | + $this->checkRequiredParameters([ |
| 128 | + 'transId', |
| 129 | + ]); |
| 130 | + |
| 131 | + return $this->transId; |
| 132 | + } |
| 133 | + |
| 134 | + protected function getEndPoint(): string |
| 135 | + { |
| 136 | + if (config('larapay.mode') == 'production') { |
| 137 | + throw new \Tartan\Larapay\Adapter\Exception('You have used fake adapter in production environment!'); |
| 138 | + } else { |
| 139 | + return $this->baseUrl . '/ipg/gateway'; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments