AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Tests\Support;
  3. use App\Billing\SubscriptionGateway;
  4. use App\Billing\SubscriptionState;
  5. use App\Commerce\Payments\CheckoutLink;
  6. use App\Commerce\Payments\PaymentException;
  7. use App\Models\Plan;
  8. use App\Models\User;
  9. /**
  10. * A subscription provider for tests. Nothing leaves the machine.
  11. * Tests decide whether a payment page was completed by calling complete().
  12. */
  13. final class FakeSubscriptionGateway implements SubscriptionGateway
  14. {
  15. /** @var array<string, SubscriptionState> Subscriptions by id. */
  16. public array $subscriptions = [];
  17. /** @var array<string, array{customer: string, price: string, subscription: string|null}> Payment pages by id. */
  18. public array $checkouts = [];
  19. public bool $down = false;
  20. public ?string $successUrl = null;
  21. private int $counter = 0;
  22. public function createCustomer(User $user): string
  23. {
  24. $this->failIfDown();
  25. return 'cus_fake_'.$user->id;
  26. }
  27. public function createPrice(Plan $plan): array
  28. {
  29. $this->failIfDown();
  30. return ['product' => 'prod_fake_'.$plan->key, 'price' => 'price_fake_'.$plan->key];
  31. }
  32. public function startCheckout(string $customerId, string $priceId, string $successUrl, string $cancelUrl): CheckoutLink
  33. {
  34. $this->failIfDown();
  35. $id = 'cs_sub_'.(++$this->counter);
  36. $this->checkouts[$id] = ['customer' => $customerId, 'price' => $priceId, 'subscription' => null];
  37. $this->successUrl = $successUrl;
  38. return new CheckoutLink($id, 'https://pay.test/'.$id);
  39. }
  40. public function findByCheckout(string $checkoutId): ?SubscriptionState
  41. {
  42. $this->failIfDown();
  43. $subscriptionId = $this->checkouts[$checkoutId]['subscription'] ?? null;
  44. return $subscriptionId !== null ? $this->subscriptions[$subscriptionId] : null;
  45. }
  46. public function find(string $subscriptionId): SubscriptionState
  47. {
  48. $this->failIfDown();
  49. return $this->subscriptions[$subscriptionId] ?? throw new PaymentException('Unknown subscription.');
  50. }
  51. public function changePrice(string $subscriptionId, string $priceId): SubscriptionState
  52. {
  53. return $this->replace($subscriptionId, priceId: $priceId, cancelAtPeriodEnd: false);
  54. }
  55. public function setCancelAtPeriodEnd(string $subscriptionId, bool $cancel): SubscriptionState
  56. {
  57. return $this->replace($subscriptionId, cancelAtPeriodEnd: $cancel);
  58. }
  59. public function readWebhook(string $payload, string $signature): ?SubscriptionState
  60. {
  61. if ($signature !== 'valid') {
  62. throw new PaymentException('Bad signature.');
  63. }
  64. $data = json_decode($payload, true);
  65. return isset($data['subscription_id']) ? $this->find($data['subscription_id']) : null;
  66. }
  67. /**
  68. * Pretend the merchant paid on the payment page. Returns the new subscription id.
  69. */
  70. public function complete(string $checkoutId): string
  71. {
  72. $checkout = $this->checkouts[$checkoutId];
  73. $id = 'sub_fake_'.(++$this->counter);
  74. $this->subscriptions[$id] = new SubscriptionState($id, $checkout['customer'], 'active', $checkout['price'], now()->addMonth()->getTimestamp(), false);
  75. $this->checkouts[$checkoutId]['subscription'] = $id;
  76. return $id;
  77. }
  78. /**
  79. * Pretend the provider changed the subscription by itself, for example after a failed payment.
  80. */
  81. public function setStatus(string $subscriptionId, string $status): void
  82. {
  83. $this->replace($subscriptionId, status: $status);
  84. }
  85. private function replace(string $subscriptionId, ?string $status = null, ?string $priceId = null, ?bool $cancelAtPeriodEnd = null): SubscriptionState
  86. {
  87. $current = $this->find($subscriptionId);
  88. return $this->subscriptions[$subscriptionId] = new SubscriptionState(
  89. $current->subscriptionId,
  90. $current->customerId,
  91. $status ?? $current->status,
  92. $priceId ?? $current->priceId,
  93. $current->currentPeriodEnd,
  94. $cancelAtPeriodEnd ?? $current->cancelAtPeriodEnd,
  95. );
  96. }
  97. private function failIfDown(): void
  98. {
  99. if ($this->down) {
  100. throw new PaymentException('The provider is down.');
  101. }
  102. }
  103. }