Client.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace HitPay;
  3. use HitPay\Request\CreatePayment;
  4. use HitPay\Response\CreatePayment as CreatePaymentResponse;
  5. use HitPay\Response\DeletePaymentRequest;
  6. use HitPay\Response\PaymentStatus;
  7. use HitPay\Response\Refund;
  8. use HitPay\Request\CreateSubscriptionPlan;
  9. use HitPay\Response\CreateSubscriptionPlan as CreateSubscriptionPlanResponse;
  10. use HitPay\Request\RecurringBilling;
  11. use HitPay\Response\RecurringBilling as RecurringBillingResponse;
  12. use HitPay\Request\UpdateRecurringBilling;
  13. use HitPay\Request\ChargeSavedCard;
  14. use HitPay\Response\ChargeSavedCard as ChargeSavedCardResponse;
  15. /**
  16. * Class Client
  17. * @package HitPay
  18. */
  19. class Client extends Request
  20. {
  21. const API_ENDPOINT = 'https://api.hit-pay.com/v1';
  22. const SANDBOX_API_ENDPOINT = 'https://api.sandbox.hit-pay.com/v1';
  23. const TYPE_CONTENT = 'application/x-www-form-urlencoded';
  24. protected $privateApiKey = '';
  25. /**
  26. * https://staging.hit-pay.com/docs.html?shell#create-payment-request
  27. *
  28. * @param CreatePayment $request
  29. * @return CreatePaymentResponse
  30. * @throws \Exception
  31. */
  32. public function createPayment(CreatePayment $request)
  33. {
  34. $result = $this->request('POST', '/payment-requests', (array)$request);
  35. return new CreatePaymentResponse($result);
  36. }
  37. /**
  38. * https://staging.hit-pay.com/docs.html?shell#get-payment-status
  39. *
  40. * @param $id
  41. * @return PaymentStatus
  42. * @throws \Exception
  43. */
  44. public function getPaymentStatus($id)
  45. {
  46. $result = $this->request('GET', '/payment-requests/' . $id);
  47. return new PaymentStatus($result);
  48. }
  49. /**
  50. * https://staging.hit-pay.com/docs.html?shell#delete-payment-request
  51. *
  52. * @param $id
  53. * @return PaymentStatus
  54. * @throws \Exception
  55. */
  56. public function deletePaymentRequest($id)
  57. {
  58. $result = $this->request('DELETE', '/payment-requests/' . $id);
  59. return new DeletePaymentRequest($result);
  60. }
  61. /**
  62. * @param $secret
  63. * @param array $args
  64. * @return string
  65. */
  66. public static function generateSignatureArray($secret, array $args)
  67. {
  68. $hmacSource = [];
  69. foreach ($args as $key => $val) {
  70. $hmacSource[$key] = "{$key}{$val}";
  71. }
  72. ksort($hmacSource);
  73. $sig = implode("", array_values($hmacSource));
  74. return hash_hmac('sha256', $sig, $secret);
  75. }
  76. /**
  77. * https://hit-pay.com/docs.html?php#refund
  78. *
  79. * @params $payment_id, $amount
  80. * @return Refund Response
  81. * @throws \Exception
  82. */
  83. public function refund($payment_id, $amount)
  84. {
  85. $result = $this->request('POST', '/refund', array('payment_id' => $payment_id, 'amount' => $amount));
  86. return new Refund($result);
  87. }
  88. /**
  89. * https://hit-pay.com/docs.html?shell#create-payment-request
  90. *
  91. * @param CreateSubscriptionPlan $request
  92. * @return CreateSubscriptionPlanResponse
  93. * @throws \Exception
  94. */
  95. public function createSubscriptionPlan(CreateSubscriptionPlan $request)
  96. {
  97. $result = $this->request('POST', '/subscription-plan', (array)$request);
  98. return new CreateSubscriptionPlanResponse($result);
  99. }
  100. /**
  101. * https://hit-pay.com/docs.html?shell#recurring-billing
  102. *
  103. * @param RecurringBilling $request
  104. * @return RecurringBillingResponse
  105. * @throws \Exception
  106. */
  107. public function recurringBilling(RecurringBilling $request)
  108. {
  109. $finalRequestParams = [];
  110. $requestParams = (array)$request;
  111. foreach ($requestParams as $key => $val) {
  112. if (!empty($val)) {
  113. $finalRequestParams[$key] = $val;
  114. }
  115. }
  116. $result = $this->request('POST', '/recurring-billing', $finalRequestParams);
  117. return new RecurringBillingResponse($result);
  118. }
  119. /**
  120. * https://staging.hit-pay.com/docs.html?shell#recurring-billing/{recurring-billing-id}
  121. *
  122. * @param $id
  123. * @return RecurringBillingResponse
  124. * @throws \Exception
  125. */
  126. public function getRecurringBillingStatus($id)
  127. {
  128. $result = $this->request('GET', '/recurring-billing/' . $id);
  129. return new RecurringBillingResponse($result);
  130. }
  131. /**
  132. * https://staging.hit-pay.com/docs.html?shell#recurring-billing/{recurring-billing-id}
  133. *
  134. * @param $id
  135. * @return RecurringBillingResponse
  136. * @throws \Exception
  137. */
  138. public function cancelSubscription($id)
  139. {
  140. $result = $this->request('DELETE', '/recurring-billing/' . $id);
  141. return new RecurringBillingResponse($result);
  142. }
  143. /**
  144. * https://staging.hit-pay.com/docs.html?shell#subscription-plan/{plan_id}
  145. *
  146. * @param $id
  147. * @return SubscriptionPlanResponse
  148. * @throws \Exception
  149. */
  150. public function getSubscriptionPlanDetails($id)
  151. {
  152. $result = $this->request('GET', '/subscription-plan/' . $id);
  153. return new CreateSubscriptionPlanResponse($result);
  154. }
  155. /**
  156. * https://staging.hit-pay.com/docs.html?shell#subscription-plan/{plan_id}
  157. *
  158. * @param $id
  159. * @return SubscriptionPlanResponse
  160. * @throws \Exception
  161. */
  162. public function updateSubscriptionPlan($id, CreateSubscriptionPlan $request)
  163. {
  164. $result = $this->request('PUT', '/subscription-plan/' . $id, (array)$request);
  165. return new CreateSubscriptionPlanResponse($result);
  166. }
  167. /**
  168. * https://staging.hit-pay.com/docs.html?shell#recurring-billing/{recurring-billing-id}
  169. *
  170. * @param $id
  171. * @return RecurringBillingResponse
  172. * @throws \Exception
  173. */
  174. public function updateRecurringBilling($id, UpdateRecurringBilling $request)
  175. {
  176. $result = $this->request('PUT', '/recurring-billing/' . $id, (array)$request);
  177. return new RecurringBillingResponse($result);
  178. }
  179. /**
  180. * https://staging.hit-pay.com/docs.html?shell#recurring-billing/{recurring-billing-id}
  181. *
  182. * @param $id
  183. * @return ChargeSavedCardResponse
  184. * @throws \Exception
  185. */
  186. public function chargeSavedCard($id, ChargeSavedCard $request)
  187. {
  188. $result = $this->request('POST', '/recurring-billing/' . $id, (array)$request);
  189. return new ChargeSavedCardResponse($result);
  190. }
  191. }