Request.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace HitPay;
  3. /**
  4. * Class Request
  5. * @package HitPay
  6. */
  7. class Request
  8. {
  9. const API_ENDPOINT = '';
  10. const SANDBOX_API_ENDPOINT = '';
  11. const TYPE_CONTENT = '';
  12. /**
  13. * @var string
  14. */
  15. protected $privateApiKey = '';
  16. /**
  17. * @var bool
  18. */
  19. protected $isLive = false;
  20. private $ch;
  21. /**
  22. * List of errors - https://staging.hit-pay.com/docs.html?shell#errors
  23. *
  24. * @var string[]
  25. */
  26. protected $errors = array(
  27. 400 => 'Bad Request -- Your request is invalid.',
  28. 401 => 'Unauthorized -- Your API key is wrong.',
  29. 404 => 'Not Found -- The payment request could not be found.',
  30. 500 => 'Internal Server Error -- We had a problem with our server. Try again later.',
  31. );
  32. /**
  33. * Request constructor.
  34. * @param $privateApiKey
  35. * @param bool $live
  36. * @throws \Exception
  37. */
  38. public function __construct($privateApiKey, $live = false)
  39. {
  40. if (!extension_loaded('curl')) {
  41. $this->ch = curl_init2();
  42. } else {
  43. $this->ch = curl_init();
  44. }
  45. $this->privateApiKey = $privateApiKey;
  46. $this->isLive = $live;
  47. }
  48. /**
  49. * @param $type PUT, DELETE, GET, POST
  50. * @param $path
  51. * @param array $request
  52. * @return bool
  53. * @throws \Exception
  54. */
  55. protected function request($type, $path, $request = array())
  56. {
  57. $endpoint = $this->isLive ? static::API_ENDPOINT : static::SANDBOX_API_ENDPOINT;
  58. if (!extension_loaded('curl')) {
  59. curl_setopt2($this->ch, CURLOPT_URL2, $endpoint . $path);
  60. curl_setopt2($this->ch, CURLOPT_HEADER2, false);
  61. curl_setopt2($this->ch, CURLOPT_SSL_VERIFYPEER2, false);
  62. curl_setopt2($this->ch, CURLOPT_RETURNTRANSFER2, true);
  63. curl_setopt2($this->ch, CURLOPT_CUSTOMREQUEST2, $type);
  64. if (!empty($request)) {
  65. $request = http_build_query($request);
  66. curl_setopt2($this->ch, CURLOPT_POSTFIELDS2, $request);
  67. }
  68. curl_setopt2($this->ch, CURLOPT_HTTPHEADER2, $this->getHeaders());
  69. $result = curl_exec2($this->ch);
  70. } else {
  71. curl_setopt($this->ch, CURLOPT_URL, $endpoint . $path);
  72. curl_setopt($this->ch, CURLOPT_HEADER, false);
  73. curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  74. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  75. curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $type);
  76. if (!empty($request)) {
  77. $request = http_build_query($request);
  78. curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
  79. }
  80. curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->getHeaders());
  81. $result = curl_exec($this->ch);
  82. }
  83. $result = !empty($result) ? json_decode($result) : null;
  84. $this->checkError($result);
  85. return $result;
  86. }
  87. /**
  88. * @param null $response
  89. * @return void
  90. * @throws \Exception
  91. */
  92. protected function checkError($response = null)
  93. {
  94. if (!extension_loaded('curl')) {
  95. $error = curl_error2($this->ch);
  96. $httpCode = curl_getinfo2($this->ch, CURLINFO_HTTP_CODE2);
  97. } else {
  98. $error = curl_error($this->ch);
  99. $httpCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
  100. }
  101. if (!empty($error)) {
  102. throw new \Exception($error);
  103. } elseif (isset($response->detail)) {
  104. throw new \Exception($response->detail);
  105. } elseif (isset($response->message)) {
  106. throw new \Exception($response->message.'.');
  107. } elseif ($httpCode != 200 && $httpCode != 201) {
  108. $message = isset($this->errors[$httpCode])
  109. ? $this->errors[$httpCode]
  110. : 'Error message does not exists.';
  111. throw new \Exception($message, $httpCode);
  112. }
  113. }
  114. /**
  115. * @return string[]
  116. */
  117. protected function getHeaders()
  118. {
  119. return [
  120. 'Content-Type: ' . static::TYPE_CONTENT,
  121. 'X-BUSINESS-API-KEY: ' . $this->privateApiKey,
  122. 'X-Requested-With: XMLHttpRequest'
  123. ];
  124. }
  125. /**
  126. *
  127. */
  128. public function __destruct()
  129. {
  130. if (!extension_loaded('curl')) {
  131. curl_close2($this->ch);
  132. } else {
  133. curl_close($this->ch);
  134. }
  135. }
  136. }