Subscriptions by id. */ public array $subscriptions = []; /** @var array Payment pages by id. */ public array $checkouts = []; public bool $down = false; public ?string $successUrl = null; private int $counter = 0; public function createCustomer(User $user): string { $this->failIfDown(); return 'cus_fake_'.$user->id; } public function createPrice(Plan $plan): array { $this->failIfDown(); return ['product' => 'prod_fake_'.$plan->key, 'price' => 'price_fake_'.$plan->key]; } public function startCheckout(string $customerId, string $priceId, string $successUrl, string $cancelUrl): CheckoutLink { $this->failIfDown(); $id = 'cs_sub_'.(++$this->counter); $this->checkouts[$id] = ['customer' => $customerId, 'price' => $priceId, 'subscription' => null]; $this->successUrl = $successUrl; return new CheckoutLink($id, 'https://pay.test/'.$id); } public function findByCheckout(string $checkoutId): ?SubscriptionState { $this->failIfDown(); $subscriptionId = $this->checkouts[$checkoutId]['subscription'] ?? null; return $subscriptionId !== null ? $this->subscriptions[$subscriptionId] : null; } public function find(string $subscriptionId): SubscriptionState { $this->failIfDown(); return $this->subscriptions[$subscriptionId] ?? throw new PaymentException('Unknown subscription.'); } public function changePrice(string $subscriptionId, string $priceId): SubscriptionState { return $this->replace($subscriptionId, priceId: $priceId, cancelAtPeriodEnd: false); } public function setCancelAtPeriodEnd(string $subscriptionId, bool $cancel): SubscriptionState { return $this->replace($subscriptionId, cancelAtPeriodEnd: $cancel); } public function readWebhook(string $payload, string $signature): ?SubscriptionState { if ($signature !== 'valid') { throw new PaymentException('Bad signature.'); } $data = json_decode($payload, true); return isset($data['subscription_id']) ? $this->find($data['subscription_id']) : null; } /** * Pretend the merchant paid on the payment page. Returns the new subscription id. */ public function complete(string $checkoutId): string { $checkout = $this->checkouts[$checkoutId]; $id = 'sub_fake_'.(++$this->counter); $this->subscriptions[$id] = new SubscriptionState($id, $checkout['customer'], 'active', $checkout['price'], now()->addMonth()->getTimestamp(), false); $this->checkouts[$checkoutId]['subscription'] = $id; return $id; } /** * Pretend the provider changed the subscription by itself, for example after a failed payment. */ public function setStatus(string $subscriptionId, string $status): void { $this->replace($subscriptionId, status: $status); } private function replace(string $subscriptionId, ?string $status = null, ?string $priceId = null, ?bool $cancelAtPeriodEnd = null): SubscriptionState { $current = $this->find($subscriptionId); return $this->subscriptions[$subscriptionId] = new SubscriptionState( $current->subscriptionId, $current->customerId, $status ?? $current->status, $priceId ?? $current->priceId, $current->currentPeriodEnd, $cancelAtPeriodEnd ?? $current->cancelAtPeriodEnd, ); } private function failIfDown(): void { if ($this->down) { throw new PaymentException('The provider is down.'); } } }