*/ public array $payments = []; /** @var list */ public array $started = []; public bool $failToStart = false; /** Whether stores can take payments. Most tests do not care about connecting Stripe. */ public bool $takingPayments = true; public ?string $successUrl = null; public ?string $cancelUrl = null; public function name(): string { return 'fake'; } public function canTakePayments(Store $store): bool { return $this->takingPayments; } 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->successUrl = $successUrl; $this->cancelUrl = $cancelUrl; $this->payments[$id] = new PaymentStatus($id, paid: false); return new CheckoutLink($id, 'https://pay.test/'.$id, 'acct_fake_'.$order->store_id); } public function checkPayment(Order $order): PaymentStatus { return $this->find((string) $order->payment_checkout_id); } public function readWebhook(string $payload, string $signature, bool $fromMerchants = false): ?PaymentStatus { if ($signature !== ($fromMerchants ? 'valid-connect' : 'valid')) { throw new PaymentException('Bad signature.'); } $data = json_decode($payload, true); if (! isset($data['checkout_id'])) { return null; } $status = $this->find($data['checkout_id']); // Messages about merchants' accounts say which account the payment was made in. return new PaymentStatus($status->checkoutId, $status->paid, $status->reference, $status->amount, $data['account'] ?? null); } private function find(string $checkoutId): PaymentStatus { return $this->payments[$checkoutId] ?? throw new PaymentException('Unknown payment.'); } /** * 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); } }