| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
-
- namespace App\Commerce\Payments;
-
- use App\Commerce\Payments\HitPay\HitPayGateway;
- use App\Models\Order;
- use App\Models\Store;
-
- /**
- * The ways a store can be paid. Stripe (cards, FPX, GrabPay) comes first; HitPay (DuitNow QR and e-wallets) is a
- * second choice when the merchant connected it. The shopper picks one at checkout.
- */
- class PaymentGateways
- {
- public function __construct(
- private readonly PaymentGateway $main,
- private readonly HitPayGateway $hitpay,
- ) {}
-
- /**
- * The ways shoppers can pay this store now, by name, in the order shown.
- *
- * @return array<string, PaymentGateway>
- */
- public function available(Store $store): array
- {
- $ways = [];
-
- foreach ([$this->main, $this->hitpay] as $gateway) {
- if ($gateway->canTakePayments($store)) {
- $ways[$gateway->name()] = $gateway;
- }
- }
-
- return $ways;
- }
-
- /**
- * The provider an order was sent to. Orders from before HitPay were all Stripe's.
- */
- public function for(Order $order): PaymentGateway
- {
- return $order->payment_provider === $this->hitpay->name() ? $this->hitpay : $this->main;
- }
- }
|