| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
-
- namespace Tests\Support;
-
- use App\Billing\SubscriptionGateway;
- use App\Billing\SubscriptionState;
- use App\Commerce\Payments\CheckoutLink;
- use App\Commerce\Payments\PaymentException;
- use App\Models\Plan;
- use App\Models\User;
-
- /**
- * A subscription provider for tests. Nothing leaves the machine.
- * Tests decide whether a payment page was completed by calling complete().
- */
- final class FakeSubscriptionGateway implements SubscriptionGateway
- {
- /** @var array<string, SubscriptionState> Subscriptions by id. */
- public array $subscriptions = [];
-
- /** @var array<string, array{customer: string, price: string, subscription: string|null}> 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.');
- }
- }
- }
|