AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BillingTest.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace Tests\Feature\Billing;
  3. use App\Billing\SubscriptionGateway;
  4. use App\Models\Plan;
  5. use App\Models\Store;
  6. use App\Models\StoreGeneration;
  7. use App\Models\Subscription;
  8. use App\Models\User;
  9. use App\StorePlan\SamplePlan;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Inertia\Testing\AssertableInertia as Assert;
  12. use Tests\Support\FakeSubscriptionGateway;
  13. use Tests\TestCase;
  14. class BillingTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. private FakeSubscriptionGateway $gateway;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->gateway = new FakeSubscriptionGateway;
  22. $this->app->instance(SubscriptionGateway::class, $this->gateway);
  23. }
  24. /** Go through the payment page for a plan and come back. */
  25. private function subscribe(User $user, string $plan): void
  26. {
  27. $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => $plan])
  28. ->assertRedirect('https://pay.test/cs_sub_1');
  29. $this->gateway->complete('cs_sub_1');
  30. $this->actingAs($user)->get(route('billing', ['session_id' => 'cs_sub_1']))->assertRedirect(route('billing'));
  31. }
  32. private function storeWithDesign(User $user): Store
  33. {
  34. $store = Store::factory()->for($user)->create();
  35. $store->plan = SamplePlan::for($store);
  36. $store->save();
  37. return $store;
  38. }
  39. public function test_the_plans_match_the_price_table()
  40. {
  41. $this->assertSame(
  42. [
  43. ['trial', 0, 1, 2, 30, 'total'],
  44. ['starter', 3900, 1, 3, 100, 'month'],
  45. ['growth', 9900, 3, 10, 500, 'month'],
  46. ['pro', 24900, 10, 30, 2000, 'month'],
  47. ],
  48. Plan::query()->orderBy('sort')->get()
  49. ->map(fn (Plan $plan) => [$plan->key, $plan->price, $plan->stores, $plan->regenerations, $plan->edits, $plan->period])
  50. ->all(),
  51. );
  52. }
  53. public function test_a_new_account_starts_a_fourteen_day_free_trial()
  54. {
  55. $this->travelTo('2026-11-15 10:00:00');
  56. $user = User::factory()->create();
  57. $this->assertSame('2026-11-29 10:00:00', $user->trial_ends_at->toDateTimeString());
  58. $this->assertSame('trial', $user->currentPlan()->key);
  59. $this->actingAs($user)->get(route('billing'))
  60. ->assertOk()
  61. ->assertInertia(fn (Assert $page) => $page
  62. ->component('billing/index')
  63. ->where('usage.active', true)
  64. ->where('usage.on_trial', true)
  65. ->where('usage.plan', 'trial')
  66. ->where('usage.stores.limit', 1)
  67. ->where('usage.regenerations.limit', 2)
  68. ->where('usage.edits.limit', 30)
  69. ->where('subscription', null)
  70. // Only paid plans are offered.
  71. ->has('plans', 3)
  72. ->where('plans.0.key', 'starter')
  73. ->where('plans.0.price', 39)
  74. ->where('plans.2.price', 249));
  75. }
  76. public function test_when_the_trial_ends_everything_is_switched_off_until_a_plan_is_chosen()
  77. {
  78. $user = User::factory()->create();
  79. $store = $this->storeWithDesign($user);
  80. $this->actingAs($user)->post(route('stores.publish', $store->slug));
  81. auth()->logout();
  82. $this->get('http://'.$store->slug.'.localhost/')->assertOk();
  83. $this->travel(15)->days();
  84. $this->assertNull($user->fresh()->currentPlan());
  85. // The live store goes offline.
  86. $this->get('http://'.$store->slug.'.localhost/')->assertNotFound();
  87. $base = 'http://localhost';
  88. $this->actingAs($user);
  89. $this->get($base.route('billing', [], false))
  90. ->assertInertia(fn (Assert $page) => $page
  91. ->where('usage.active', false)
  92. ->where('usage.plan', null)
  93. ->where('usage.stores.limit', 0));
  94. // No AI, no new stores, no publishing.
  95. $this->post($base.route('stores.generate', $store->slug, false));
  96. $this->post($base.route('stores.edits', $store->slug, false), ['instruction' => 'Make it blue']);
  97. $this->assertSame(0, StoreGeneration::count());
  98. $this->get($base.route('stores.create', [], false))->assertRedirect($base.route('billing', [], false));
  99. $this->post($base.route('stores.store', [], false), ['name' => 'Second', 'category' => 'food', 'language' => 'en', 'description' => str_repeat('A good store. ', 3)]);
  100. $this->assertSame(1, $user->stores()->count());
  101. $this->post($base.route('stores.unpublish', $store->slug, false));
  102. $this->post($base.route('stores.publish', $store->slug, false));
  103. $this->assertFalse($store->fresh()->isPublished());
  104. // Nothing is deleted: the merchant can still see the store and its design.
  105. $this->get($base.route('stores.show', $store->slug, false))->assertOk();
  106. $this->assertNotNull($store->fresh()->plan);
  107. }
  108. public function test_a_merchant_can_subscribe_and_the_plan_takes_effect()
  109. {
  110. $user = User::factory()->create();
  111. $this->travel(20)->days();
  112. $this->subscribe($user, 'growth');
  113. $subscription = $user->fresh()->subscription;
  114. $this->assertSame('cus_fake_'.$user->id, $subscription->stripe_customer_id);
  115. $this->assertSame('sub_fake_2', $subscription->stripe_subscription_id);
  116. $this->assertSame('active', $subscription->status);
  117. $this->assertSame('growth', $subscription->plan->key);
  118. $this->assertNotNull($subscription->current_period_end);
  119. $this->assertSame('price_fake_growth', Plan::findByKey('growth')->stripe_price_id);
  120. // The payment page sends the merchant back to the billing page.
  121. $this->assertSame(route('billing'), $this->gateway->successUrl);
  122. $this->actingAs($user)->get(route('billing'))
  123. ->assertInertia(fn (Assert $page) => $page
  124. ->where('usage.active', true)
  125. ->where('usage.on_trial', false)
  126. ->where('usage.plan', 'growth')
  127. ->where('usage.period', 'month')
  128. ->where('usage.stores.limit', 3)
  129. ->where('usage.regenerations.limit', 10)
  130. ->where('subscription.active', true)
  131. ->where('subscription.plan', 'growth')
  132. ->where('subscription.cancel_at_period_end', false));
  133. }
  134. public function test_an_unfinished_or_borrowed_payment_page_gives_no_plan()
  135. {
  136. $user = User::factory()->create();
  137. $other = User::factory()->create();
  138. // Started but never paid.
  139. $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'starter']);
  140. $this->actingAs($user)->get(route('billing', ['session_id' => 'cs_sub_1']))->assertRedirect(route('billing'));
  141. $this->assertNull($user->fresh()->subscription->stripe_subscription_id);
  142. // Someone else pays, and this user tries to use their payment page id.
  143. $this->actingAs($other)->post(route('billing.subscribe'), ['plan' => 'pro']);
  144. $this->gateway->complete('cs_sub_2');
  145. $this->actingAs($user)->get(route('billing', ['session_id' => 'cs_sub_2']));
  146. $this->assertNull($user->fresh()->subscription->stripe_subscription_id);
  147. $this->assertSame('trial', $user->fresh()->currentPlan()->key);
  148. }
  149. public function test_only_real_paid_plans_can_be_chosen()
  150. {
  151. $user = User::factory()->create();
  152. $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'trial'])->assertSessionHasErrors('plan');
  153. $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'enterprise'])->assertSessionHasErrors('plan');
  154. $this->actingAs($user)->post(route('billing.subscribe'), [])->assertSessionHasErrors('plan');
  155. $this->assertSame([], $this->gateway->checkouts);
  156. }
  157. public function test_a_paying_merchant_can_switch_plans_without_a_new_payment_page()
  158. {
  159. $user = User::factory()->create();
  160. $this->subscribe($user, 'starter');
  161. $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'pro'])->assertRedirect(route('billing'));
  162. $this->assertSame('pro', $user->fresh()->currentPlan()->key);
  163. $this->assertSame('price_fake_pro', $this->gateway->subscriptions['sub_fake_2']->priceId);
  164. $this->assertCount(1, $this->gateway->checkouts);
  165. // And back down again.
  166. $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'starter']);
  167. $this->assertSame('starter', $user->fresh()->currentPlan()->key);
  168. }
  169. public function test_cancelling_keeps_the_plan_until_the_period_ends_and_can_be_undone()
  170. {
  171. $user = User::factory()->create();
  172. $this->subscribe($user, 'starter');
  173. $this->actingAs($user)->post(route('billing.cancel'))->assertRedirect(route('billing'));
  174. $subscription = $user->fresh()->subscription;
  175. $this->assertTrue($subscription->cancel_at_period_end);
  176. $this->assertTrue($subscription->isActive());
  177. $this->assertSame('starter', $user->fresh()->currentPlan()->key);
  178. $this->actingAs($user)->post(route('billing.resume'));
  179. $this->assertFalse($user->fresh()->subscription->cancel_at_period_end);
  180. // Later Stripe ends the subscription for good. The billing page picks that up by itself.
  181. $this->travel(20)->days();
  182. $this->gateway->setStatus('sub_fake_2', 'canceled');
  183. $this->actingAs($user)->get(route('billing'))
  184. ->assertInertia(fn (Assert $page) => $page
  185. ->where('usage.active', false)
  186. ->where('subscription.active', false)
  187. ->where('subscription.status', 'canceled'));
  188. $this->assertNull($user->fresh()->currentPlan());
  189. }
  190. public function test_cancel_and_resume_need_a_subscription()
  191. {
  192. $user = User::factory()->create();
  193. $this->actingAs($user)->post(route('billing.cancel'))->assertRedirect(route('billing'));
  194. $this->actingAs($user)->post(route('billing.resume'))->assertRedirect(route('billing'));
  195. $this->assertNull($user->fresh()->subscription?->stripe_subscription_id);
  196. }
  197. public function test_a_failed_payment_keeps_the_plan_while_stripe_retries()
  198. {
  199. $user = User::factory()->create();
  200. Subscription::factory()->for($user)->plan('growth')->status('past_due')->create();
  201. $this->assertSame('growth', $user->fresh()->currentPlan()->key);
  202. foreach (['canceled', 'unpaid', 'incomplete', 'incomplete_expired'] as $status) {
  203. $user->subscription()->update(['status' => $status]);
  204. $this->assertSame('trial', $user->fresh()->currentPlan()->key, "A [$status] subscription must not count as a plan.");
  205. }
  206. }
  207. public function test_the_webhook_keeps_a_plan_in_step_with_stripe()
  208. {
  209. $user = User::factory()->create();
  210. $this->subscribe($user, 'starter');
  211. auth()->logout();
  212. $this->gateway->setStatus('sub_fake_2', 'canceled');
  213. $this->postJson(route('stripe.webhook'), ['subscription_id' => 'sub_fake_2'], ['Stripe-Signature' => 'forged'])->assertStatus(400);
  214. $this->assertSame('active', $user->fresh()->subscription->status);
  215. $this->postJson(route('stripe.webhook'), ['subscription_id' => 'sub_fake_2'], ['Stripe-Signature' => 'valid'])->assertOk();
  216. $this->assertSame('canceled', $user->fresh()->subscription->status);
  217. }
  218. public function test_if_the_payment_service_is_down_the_merchant_sees_a_message_and_keeps_the_trial()
  219. {
  220. $user = User::factory()->create();
  221. $this->gateway->down = true;
  222. $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'starter'])->assertRedirect(route('billing'));
  223. $this->actingAs($user)->get(route('billing'))->assertOk();
  224. $this->assertSame('trial', $user->fresh()->currentPlan()->key);
  225. }
  226. public function test_the_number_of_stores_is_limited_by_the_plan()
  227. {
  228. $user = User::factory()->create();
  229. $data = fn (string $name) => ['name' => $name, 'category' => 'food', 'language' => 'en', 'description' => str_repeat('A good store. ', 3)];
  230. $this->actingAs($user)->post(route('stores.store'), $data('One'));
  231. $this->actingAs($user)->post(route('stores.store'), $data('Two'))->assertRedirect(route('billing'));
  232. $this->actingAs($user)->get(route('stores.create'))->assertRedirect(route('billing'));
  233. $this->assertSame(1, $user->stores()->count());
  234. Subscription::factory()->for($user)->plan('growth')->create();
  235. $user = $user->fresh();
  236. $this->actingAs($user)->get(route('stores.create'))->assertOk();
  237. $this->actingAs($user)->post(route('stores.store'), $data('Two'));
  238. $this->actingAs($user)->post(route('stores.store'), $data('Three'));
  239. $this->actingAs($user)->post(route('stores.store'), $data('Four'));
  240. $this->assertSame(3, $user->stores()->count());
  241. $this->actingAs($user)->get(route('dashboard'))
  242. ->assertInertia(fn (Assert $page) => $page
  243. ->where('usage.stores.used', 3)
  244. ->where('usage.stores.limit', 3));
  245. }
  246. public function test_guests_cannot_open_billing()
  247. {
  248. $this->get(route('billing'))->assertRedirect(route('login'));
  249. $this->post(route('billing.subscribe'), ['plan' => 'starter'])->assertRedirect(route('login'));
  250. $this->post(route('billing.cancel'))->assertRedirect(route('login'));
  251. }
  252. }