AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HitPayGateway.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Commerce\Payments\HitPay;
  3. use App\Commerce\Payments\CheckoutLink;
  4. use App\Commerce\Payments\PaymentException;
  5. use App\Commerce\Payments\PaymentGateway;
  6. use App\Commerce\Payments\PaymentStatus;
  7. use App\Models\Order;
  8. use App\Models\Store;
  9. use App\Support\MalaysianPhone;
  10. /**
  11. * DuitNow QR, Touch 'n Go and ShopeePay through the merchant's own HitPay account. HitPay hosts the payment page.
  12. *
  13. * The page is made with the merchant's API key, so the money goes straight to them. We take no cut.
  14. * Messages from HitPay are not trusted: they only tell us to ask HitPay about an order (HitPayWebhookController).
  15. */
  16. final class HitPayGateway implements PaymentGateway
  17. {
  18. public function __construct(private readonly HitPayApi $api) {}
  19. public function name(): string
  20. {
  21. return 'hitpay';
  22. }
  23. public function canTakePayments(Store $store): bool
  24. {
  25. return $this->methods($store) !== [];
  26. }
  27. public function methods(Store $store): array
  28. {
  29. return $store->hitpayAccount?->offeredMethods() ?? [];
  30. }
  31. public function startCheckout(Order $order, string $successUrl, string $cancelUrl): CheckoutLink
  32. {
  33. $account = $order->store->hitpayAccount;
  34. if ($account === null || $account->offeredMethods() === []) {
  35. throw new PaymentException('The store has not connected HitPay.');
  36. }
  37. $methods = $this->methods($order->store);
  38. $phone = MalaysianPhone::normalize($order->customer_phone);
  39. $webhook = rtrim((string) config('app.url'), '/').route('hitpay.webhook', $order->public_id, false);
  40. $fields = [
  41. 'amount' => number_format($order->total / 100, 2, '.', ''),
  42. 'currency' => 'MYR',
  43. 'payment_methods' => $methods,
  44. 'email' => $order->customer_email,
  45. 'name' => $order->customer_name,
  46. // HitPay wants the country code with a plus, such as +60123456789.
  47. ...($phone !== null ? ['phone' => '+'.$phone] : []),
  48. 'purpose' => mb_substr($order->store->name.' order #'.$order->number, 0, 255),
  49. 'reference_number' => (string) $order->number,
  50. 'redirect_url' => $successUrl,
  51. 'allow_repeated_payments' => 'false',
  52. // The store sends its own email once the order is paid.
  53. 'send_email' => 'false',
  54. // HitPay only calls public https addresses, so a development machine relies on the shopper coming back.
  55. ...(str_starts_with($webhook, 'https://') ? ['webhook' => $webhook] : []),
  56. ];
  57. try {
  58. $request = $this->api->createPaymentRequest($account->api_key, $fields);
  59. // Methods refused earlier were accepted this time: the merchant switched them on in HitPay.
  60. if ($account->refused_methods !== null && array_diff($account->refused_methods, $methods) === []) {
  61. $account->clearRefused();
  62. $account->save();
  63. }
  64. } catch (HitPayRefusedMethods $e) {
  65. // Some ticked methods are switched off in the merchant's HitPay account. Remember them, so the
  66. // merchant is told in Settings and shoppers are not offered them for a while, then try once more
  67. // with the methods that are left.
  68. $account->refused($e->methods);
  69. report($e);
  70. $left = array_values(array_diff($methods, $e->methods));
  71. if ($left === []) {
  72. throw $e;
  73. }
  74. $request = $this->api->createPaymentRequest($account->api_key, [...$fields, 'payment_methods' => $left]);
  75. }
  76. return new CheckoutLink($request['id'], $request['url']);
  77. }
  78. public function checkPayment(Order $order): PaymentStatus
  79. {
  80. $account = $order->store->hitpayAccount;
  81. if ($order->payment_checkout_id === null || $account === null) {
  82. throw new PaymentException('The order has no HitPay payment page, or the store disconnected HitPay.');
  83. }
  84. $request = $this->api->paymentRequest($account->api_key, $order->payment_checkout_id);
  85. $payments = is_array($request['payments'] ?? null) ? $request['payments'] : [];
  86. $payment = collect($payments)->first(fn ($payment) => is_array($payment) && ($payment['status'] ?? null) === 'succeeded');
  87. $inRinggit = strtolower((string) ($request['currency'] ?? '')) === 'myr';
  88. return new PaymentStatus(
  89. checkoutId: (string) ($request['id'] ?? $order->payment_checkout_id),
  90. paid: $inRinggit && ($payment !== null || ($request['status'] ?? null) === 'completed'),
  91. reference: is_string($payment['id'] ?? null) ? $payment['id'] : null,
  92. amount: self::sen($payment['amount'] ?? $request['amount'] ?? null),
  93. );
  94. }
  95. /**
  96. * Not used: HitPay's messages go to HitPayWebhookController, which asks HitPay directly instead of trusting them.
  97. */
  98. public function readWebhook(string $payload, string $signature, bool $fromMerchants = false): ?PaymentStatus
  99. {
  100. return null;
  101. }
  102. /** HitPay sends amounts as text in ringgit, such as "50.00". */
  103. private static function sen(mixed $amount): ?int
  104. {
  105. return is_numeric($amount) ? (int) round(((float) $amount) * 100) : null;
  106. }
  107. }