| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
-
- namespace Tests\Support;
-
- use App\Commerce\Payments\CheckoutLink;
- use App\Commerce\Payments\PaymentException;
- use App\Commerce\Payments\PaymentGateway;
- use App\Commerce\Payments\PaymentStatus;
- use App\Models\Order;
-
- /**
- * A payment provider for tests. Nothing leaves the machine.
- * Tests decide whether a payment page was paid by calling pay().
- */
- final class FakeGateway implements PaymentGateway
- {
- /** @var array<string, PaymentStatus> */
- public array $payments = [];
-
- /** @var list<Order> */
- public array $started = [];
-
- public bool $failToStart = false;
-
- public function name(): string
- {
- return 'fake';
- }
-
- public function startCheckout(Order $order, string $successUrl, string $cancelUrl): CheckoutLink
- {
- if ($this->failToStart) {
- throw new PaymentException('The provider is down.');
- }
-
- $id = 'cs_fake_'.$order->id;
- $this->started[] = $order;
- $this->payments[$id] = new PaymentStatus($id, paid: false);
-
- return new CheckoutLink($id, 'https://pay.test/'.$id);
- }
-
- public function checkPayment(string $checkoutId): PaymentStatus
- {
- return $this->payments[$checkoutId] ?? throw new PaymentException('Unknown payment.');
- }
-
- public function readWebhook(string $payload, string $signature): ?PaymentStatus
- {
- if ($signature !== 'valid') {
- throw new PaymentException('Bad signature.');
- }
-
- $data = json_decode($payload, true);
-
- return isset($data['checkout_id']) ? $this->checkPayment($data['checkout_id']) : null;
- }
-
- /**
- * Pretend the shopper paid on the payment page.
- */
- public function pay(Order $order, ?int $amount = null): void
- {
- $id = (string) $order->fresh()?->payment_checkout_id;
-
- $this->payments[$id] = new PaymentStatus($id, paid: true, reference: 'pi_fake_'.$order->id, amount: $amount ?? $order->total);
- }
- }
|