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.

HitPayGateway.php 5.4KB

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