You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.1 KiB

  1. <?php
  2. namespace App\Utilities\Zarinpal\Drivers;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Exception\RequestException;
  5. class RestDriver implements DriverInterface
  6. {
  7. protected $baseUrl = 'https://www.zarinpal.com/pg/rest/WebGate/';
  8. /**
  9. * request driver.
  10. *
  11. * @param $inputs
  12. *
  13. * @return array
  14. */
  15. public function request($inputs)
  16. {
  17. $result = $this->restCall('PaymentRequest.json', $inputs);
  18. if ($result['Status'] == 100) {
  19. return ['Authority' => $result['Authority']];
  20. } else {
  21. return ['error' => $result['Status']];
  22. }
  23. }
  24. /**
  25. * requestWithExtra driver.
  26. *
  27. * @param $inputs
  28. *
  29. * @return array
  30. */
  31. public function requestWithExtra($inputs)
  32. {
  33. $result = $this->restCall('PaymentRequestWithExtra.json', $inputs);
  34. if ($result['Status'] == 100) {
  35. return ['Authority' => $result['Authority']];
  36. } else {
  37. return [
  38. 'Status' => 'error',
  39. 'error' => !empty($result['Status']) ? $result['Status'] : null,
  40. 'errorInfo' => !empty($result['errors']) ? $result['errors'] : null,
  41. ];
  42. }
  43. }
  44. /**
  45. * verify driver.
  46. *
  47. * @param $inputs
  48. *
  49. * @return array
  50. */
  51. public function verify($inputs)
  52. {
  53. $result = $this->restCall('PaymentVerification.json', $inputs);
  54. if ($result['Status'] == 100) {
  55. return [
  56. 'Status' => 'success',
  57. 'RefID' => $result['RefID'],
  58. ];
  59. } elseif ($result['Status'] == 101) {
  60. return [
  61. 'Status' => 'verified_before',
  62. 'RefID' => $result['RefID'],
  63. ];
  64. } else {
  65. return [
  66. 'Status' => 'error',
  67. 'error' => !empty($result['Status']) ? $result['Status'] : null,
  68. 'errorInfo' => !empty($result['errors']) ? $result['errors'] : null,
  69. ];
  70. }
  71. }
  72. /**
  73. * verifyWithExtra driver.
  74. *
  75. * @param $inputs
  76. *
  77. * @return array
  78. */
  79. public function verifyWithExtra($inputs)
  80. {
  81. $result = $this->restCall('PaymentVerificationWithExtra.json', $inputs);
  82. if ($result['Status'] == 100) {
  83. return [
  84. 'Status' => 'success',
  85. 'RefID' => $result['RefID'],
  86. 'ExtraDetail' => $result['ExtraDetail'],
  87. ];
  88. } elseif ($result['Status'] == 101) {
  89. return [
  90. 'Status' => 'verified_before',
  91. 'RefID' => $result['RefID'],
  92. 'ExtraDetail' => $result['ExtraDetail'],
  93. ];
  94. } else {
  95. return [
  96. 'Status' => 'error',
  97. 'error' => !empty($result['Status']) ? $result['Status'] : null,
  98. 'errorInfo' => !empty($result['errors']) ? $result['errors'] : null,
  99. ];
  100. }
  101. }
  102. /**
  103. * unverifiedTransactions driver.
  104. *
  105. * @param $inputs
  106. *
  107. * @return array
  108. */
  109. public function unverifiedTransactions($inputs)
  110. {
  111. $result = $this->restCall('UnverifiedTransactions.json', $inputs);
  112. if ($result['Status'] == 100) {
  113. return ['Status' => 'success', 'Authorities' => $result['Authorities']];
  114. } else {
  115. return [
  116. 'Status' => 'error',
  117. 'error' => !empty($result['Status']) ? $result['Status'] : null,
  118. 'errorInfo' => !empty($result['errors']) ? $result['errors'] : null,
  119. ];
  120. }
  121. }
  122. /**
  123. * refreshAuthority driver.
  124. *
  125. * @param $inputs
  126. *
  127. * @return array
  128. */
  129. public function refreshAuthority($inputs)
  130. {
  131. $result = $this->restCall('RefreshAuthority.json', $inputs);
  132. if ($result['Status'] == 100) {
  133. return ['Status' => 'success', 'refreshed' => true];
  134. } else {
  135. return ['Status' => 'error', 'error' => $result['Status']];
  136. }
  137. }
  138. /**
  139. * request rest and return the response.
  140. *
  141. * @param $uri
  142. * @param $data
  143. *
  144. * @return mixed
  145. */
  146. private function restCall($uri, $data)
  147. {
  148. try {
  149. $client = new Client(['base_uri' => $this->baseUrl]);
  150. $response = $client->request('POST', $uri, ['json' => $data]);
  151. $rawBody = $response->getBody()->getContents();
  152. $body = json_decode($rawBody, true);
  153. } catch (RequestException $e) {
  154. $response = $e->getResponse();
  155. $rawBody = is_null($response) ? '{"Status":-98,"message":"http connection error"}' : $response->getBody()->getContents();
  156. $body = json_decode($rawBody, true);
  157. }
  158. if (!isset($result['Status'])) {
  159. $result['Status'] = -99;
  160. }
  161. return $body;
  162. }
  163. /**
  164. * @param mixed $baseUrl
  165. *
  166. * @return void
  167. */
  168. public function setAddress($baseUrl)
  169. {
  170. $this->baseUrl = $baseUrl;
  171. }
  172. public function enableSandbox()
  173. {
  174. $this->setAddress('https://sandbox.zarinpal.com/pg/rest/WebGate/');
  175. }
  176. }