AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HitPayApi.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Commerce\Payments\HitPay;
  3. use App\Commerce\Payments\PaymentException;
  4. use Illuminate\Http\Client\ConnectionException;
  5. use Illuminate\Http\Client\PendingRequest;
  6. use Illuminate\Http\Client\Response;
  7. use Illuminate\Support\Facades\Http;
  8. /**
  9. * Talks to HitPay's API with a merchant's own API key. Docs: docs.hitpayapp.com.
  10. */
  11. class HitPayApi
  12. {
  13. public function baseUrl(): string
  14. {
  15. return config('services.hitpay.sandbox') ? 'https://api.sandbox.hit-pay.com/v1' : 'https://api.hit-pay.com/v1';
  16. }
  17. /**
  18. * Whether HitPay accepts the API key. Reads the account's status, which changes nothing.
  19. *
  20. * @throws PaymentException When HitPay cannot be reached.
  21. */
  22. public function acceptsKey(#[\SensitiveParameter] string $apiKey): bool
  23. {
  24. $response = $this->send(fn () => $this->request($apiKey)->get('/account-status'));
  25. if ($response->successful()) {
  26. return true;
  27. }
  28. if (in_array($response->status(), [401, 403, 404, 422], true)) {
  29. return false;
  30. }
  31. throw new PaymentException('HitPay could not check the API key: HTTP '.$response->status());
  32. }
  33. /**
  34. * Create a payment page ("payment request").
  35. *
  36. * @param array<string, mixed> $fields
  37. * @return array<string, mixed>
  38. *
  39. * @throws PaymentException
  40. */
  41. public function createPaymentRequest(#[\SensitiveParameter] string $apiKey, array $fields): array
  42. {
  43. $response = $this->send(fn () => $this->request($apiKey)->asForm()->post('/payment-requests', $fields));
  44. $refused = $this->refusedMethods($response, $fields['payment_methods'] ?? []);
  45. if ($refused !== []) {
  46. throw new HitPayRefusedMethods($refused, 'HitPay refused payment methods: '.implode(', ', $refused).'. '.$response->json('message'));
  47. }
  48. if (! $response->successful() || ! is_string($response->json('id')) || ! is_string($response->json('url'))) {
  49. throw new PaymentException('HitPay could not create the payment page: HTTP '.$response->status().' '.mb_substr($response->body(), 0, 500));
  50. }
  51. return $response->json();
  52. }
  53. /**
  54. * A payment page and the payments made on it.
  55. *
  56. * @return array<string, mixed>
  57. *
  58. * @throws PaymentException
  59. */
  60. public function paymentRequest(#[\SensitiveParameter] string $apiKey, string $id): array
  61. {
  62. $response = $this->send(fn () => $this->request($apiKey)->get('/payment-requests/'.rawurlencode($id)));
  63. if (! $response->successful() || ! is_array($response->json())) {
  64. throw new PaymentException('HitPay could not find the payment: HTTP '.$response->status());
  65. }
  66. return $response->json();
  67. }
  68. /**
  69. * The methods HitPay said are unavailable for the account. It answers 422 with an error per method,
  70. * such as "payment_methods.0": "The selected payment method (duitnow) is unavailable for your account ...".
  71. *
  72. * @param list<string> $methods The methods that were sent, in order
  73. * @return list<string>
  74. */
  75. private function refusedMethods(Response $response, array $methods): array
  76. {
  77. if ($response->status() !== 422 || ! is_array($response->json('errors'))) {
  78. return [];
  79. }
  80. $refused = [];
  81. foreach (array_keys($response->json('errors')) as $field) {
  82. if (preg_match('/^payment_methods\.(\d+)$/', (string) $field, $match) === 1 && isset($methods[(int) $match[1]])) {
  83. $refused[] = $methods[(int) $match[1]];
  84. }
  85. }
  86. return array_values(array_unique($refused));
  87. }
  88. private function request(#[\SensitiveParameter] string $apiKey): PendingRequest
  89. {
  90. return Http::baseUrl($this->baseUrl())
  91. ->acceptJson()
  92. ->timeout(20)
  93. ->withHeaders([
  94. 'X-BUSINESS-API-KEY' => $apiKey,
  95. // HitPay's FAQ: without this header some calls answer "Invalid Business API Key".
  96. 'X-Requested-With' => 'XMLHttpRequest',
  97. ]);
  98. }
  99. /**
  100. * @param callable(): Response $call
  101. *
  102. * @throws PaymentException
  103. */
  104. private function send(callable $call): Response
  105. {
  106. try {
  107. return $call();
  108. } catch (ConnectionException $e) {
  109. throw new PaymentException('HitPay could not be reached: '.$e->getMessage(), 0, $e);
  110. }
  111. }
  112. }