| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
-
- namespace Tests\Feature\Billing;
-
- use App\Billing\SubscriptionGateway;
- use App\Models\Plan;
- use App\Models\Store;
- use App\Models\StoreGeneration;
- use App\Models\Subscription;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\FakeSubscriptionGateway;
- use Tests\TestCase;
-
- class BillingTest extends TestCase
- {
- use RefreshDatabase;
-
- private FakeSubscriptionGateway $gateway;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- $this->gateway = new FakeSubscriptionGateway;
- $this->app->instance(SubscriptionGateway::class, $this->gateway);
- }
-
- /** Go through the payment page for a plan and come back. */
- private function subscribe(User $user, string $plan): void
- {
- $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => $plan])
- ->assertRedirect('https://pay.test/cs_sub_1');
-
- $this->gateway->complete('cs_sub_1');
-
- $this->actingAs($user)->get(route('billing', ['session_id' => 'cs_sub_1']))->assertRedirect(route('billing'));
- }
-
- private function storeWithDesign(User $user): Store
- {
- $store = Store::factory()->for($user)->create();
- $store->plan = SamplePlan::for($store);
- $store->save();
-
- return $store;
- }
-
- public function test_the_plans_match_the_price_table()
- {
- $this->assertSame(
- [
- ['trial', 0, 1, 2, 30, 'total'],
- ['starter', 3900, 1, 3, 100, 'month'],
- ['growth', 9900, 3, 10, 500, 'month'],
- ['pro', 24900, 10, 30, 2000, 'month'],
- ],
- Plan::query()->orderBy('sort')->get()
- ->map(fn (Plan $plan) => [$plan->key, $plan->price, $plan->stores, $plan->regenerations, $plan->edits, $plan->period])
- ->all(),
- );
- }
-
- public function test_a_new_account_starts_a_fourteen_day_free_trial()
- {
- $this->travelTo('2026-11-15 10:00:00');
-
- $user = User::factory()->create();
-
- $this->assertSame('2026-11-29 10:00:00', $user->trial_ends_at->toDateTimeString());
- $this->assertSame('trial', $user->currentPlan()->key);
-
- $this->actingAs($user)->get(route('billing'))
- ->assertOk()
- ->assertInertia(fn (Assert $page) => $page
- ->component('billing/index')
- ->where('usage.active', true)
- ->where('usage.on_trial', true)
- ->where('usage.plan', 'trial')
- ->where('usage.stores.limit', 1)
- ->where('usage.regenerations.limit', 2)
- ->where('usage.edits.limit', 30)
- ->where('subscription', null)
- // Only paid plans are offered.
- ->has('plans', 3)
- ->where('plans.0.key', 'starter')
- ->where('plans.0.price', 39)
- ->where('plans.2.price', 249));
- }
-
- public function test_when_the_trial_ends_everything_is_switched_off_until_a_plan_is_chosen()
- {
- $user = User::factory()->create();
- $store = $this->storeWithDesign($user);
- $this->actingAs($user)->post(route('stores.publish', $store->slug));
- auth()->logout();
-
- $this->get('http://'.$store->slug.'.localhost/')->assertOk();
-
- $this->travel(15)->days();
-
- $this->assertNull($user->fresh()->currentPlan());
-
- // The live store goes offline.
- $this->get('http://'.$store->slug.'.localhost/')->assertNotFound();
-
- $base = 'http://localhost';
- $this->actingAs($user);
-
- $this->get($base.route('billing', [], false))
- ->assertInertia(fn (Assert $page) => $page
- ->where('usage.active', false)
- ->where('usage.plan', null)
- ->where('usage.stores.limit', 0));
-
- // No AI, no new stores, no publishing.
- $this->post($base.route('stores.generate', $store->slug, false));
- $this->post($base.route('stores.edits', $store->slug, false), ['instruction' => 'Make it blue']);
- $this->assertSame(0, StoreGeneration::count());
-
- $this->get($base.route('stores.create', [], false))->assertRedirect($base.route('billing', [], false));
- $this->post($base.route('stores.store', [], false), ['name' => 'Second', 'category' => 'food', 'language' => 'en', 'description' => str_repeat('A good store. ', 3)]);
- $this->assertSame(1, $user->stores()->count());
-
- $this->post($base.route('stores.unpublish', $store->slug, false));
- $this->post($base.route('stores.publish', $store->slug, false));
- $this->assertFalse($store->fresh()->isPublished());
-
- // Nothing is deleted: the merchant can still see the store and its design.
- $this->get($base.route('stores.show', $store->slug, false))->assertOk();
- $this->assertNotNull($store->fresh()->plan);
- }
-
- public function test_a_merchant_can_subscribe_and_the_plan_takes_effect()
- {
- $user = User::factory()->create();
- $this->travel(20)->days();
-
- $this->subscribe($user, 'growth');
-
- $subscription = $user->fresh()->subscription;
-
- $this->assertSame('cus_fake_'.$user->id, $subscription->stripe_customer_id);
- $this->assertSame('sub_fake_2', $subscription->stripe_subscription_id);
- $this->assertSame('active', $subscription->status);
- $this->assertSame('growth', $subscription->plan->key);
- $this->assertNotNull($subscription->current_period_end);
- $this->assertSame('price_fake_growth', Plan::findByKey('growth')->stripe_price_id);
- // The payment page sends the merchant back to the billing page.
- $this->assertSame(route('billing'), $this->gateway->successUrl);
-
- $this->actingAs($user)->get(route('billing'))
- ->assertInertia(fn (Assert $page) => $page
- ->where('usage.active', true)
- ->where('usage.on_trial', false)
- ->where('usage.plan', 'growth')
- ->where('usage.period', 'month')
- ->where('usage.stores.limit', 3)
- ->where('usage.regenerations.limit', 10)
- ->where('subscription.active', true)
- ->where('subscription.plan', 'growth')
- ->where('subscription.cancel_at_period_end', false));
- }
-
- public function test_an_unfinished_or_borrowed_payment_page_gives_no_plan()
- {
- $user = User::factory()->create();
- $other = User::factory()->create();
-
- // Started but never paid.
- $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'starter']);
- $this->actingAs($user)->get(route('billing', ['session_id' => 'cs_sub_1']))->assertRedirect(route('billing'));
- $this->assertNull($user->fresh()->subscription->stripe_subscription_id);
-
- // Someone else pays, and this user tries to use their payment page id.
- $this->actingAs($other)->post(route('billing.subscribe'), ['plan' => 'pro']);
- $this->gateway->complete('cs_sub_2');
- $this->actingAs($user)->get(route('billing', ['session_id' => 'cs_sub_2']));
-
- $this->assertNull($user->fresh()->subscription->stripe_subscription_id);
- $this->assertSame('trial', $user->fresh()->currentPlan()->key);
- }
-
- public function test_only_real_paid_plans_can_be_chosen()
- {
- $user = User::factory()->create();
-
- $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'trial'])->assertSessionHasErrors('plan');
- $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'enterprise'])->assertSessionHasErrors('plan');
- $this->actingAs($user)->post(route('billing.subscribe'), [])->assertSessionHasErrors('plan');
-
- $this->assertSame([], $this->gateway->checkouts);
- }
-
- public function test_a_paying_merchant_can_switch_plans_without_a_new_payment_page()
- {
- $user = User::factory()->create();
- $this->subscribe($user, 'starter');
-
- $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'pro'])->assertRedirect(route('billing'));
-
- $this->assertSame('pro', $user->fresh()->currentPlan()->key);
- $this->assertSame('price_fake_pro', $this->gateway->subscriptions['sub_fake_2']->priceId);
- $this->assertCount(1, $this->gateway->checkouts);
-
- // And back down again.
- $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'starter']);
-
- $this->assertSame('starter', $user->fresh()->currentPlan()->key);
- }
-
- public function test_cancelling_keeps_the_plan_until_the_period_ends_and_can_be_undone()
- {
- $user = User::factory()->create();
- $this->subscribe($user, 'starter');
-
- $this->actingAs($user)->post(route('billing.cancel'))->assertRedirect(route('billing'));
-
- $subscription = $user->fresh()->subscription;
- $this->assertTrue($subscription->cancel_at_period_end);
- $this->assertTrue($subscription->isActive());
- $this->assertSame('starter', $user->fresh()->currentPlan()->key);
-
- $this->actingAs($user)->post(route('billing.resume'));
- $this->assertFalse($user->fresh()->subscription->cancel_at_period_end);
-
- // Later Stripe ends the subscription for good. The billing page picks that up by itself.
- $this->travel(20)->days();
- $this->gateway->setStatus('sub_fake_2', 'canceled');
-
- $this->actingAs($user)->get(route('billing'))
- ->assertInertia(fn (Assert $page) => $page
- ->where('usage.active', false)
- ->where('subscription.active', false)
- ->where('subscription.status', 'canceled'));
-
- $this->assertNull($user->fresh()->currentPlan());
- }
-
- public function test_cancel_and_resume_need_a_subscription()
- {
- $user = User::factory()->create();
-
- $this->actingAs($user)->post(route('billing.cancel'))->assertRedirect(route('billing'));
- $this->actingAs($user)->post(route('billing.resume'))->assertRedirect(route('billing'));
-
- $this->assertNull($user->fresh()->subscription?->stripe_subscription_id);
- }
-
- public function test_a_failed_payment_keeps_the_plan_while_stripe_retries()
- {
- $user = User::factory()->create();
- Subscription::factory()->for($user)->plan('growth')->status('past_due')->create();
-
- $this->assertSame('growth', $user->fresh()->currentPlan()->key);
-
- foreach (['canceled', 'unpaid', 'incomplete', 'incomplete_expired'] as $status) {
- $user->subscription()->update(['status' => $status]);
-
- $this->assertSame('trial', $user->fresh()->currentPlan()->key, "A [$status] subscription must not count as a plan.");
- }
- }
-
- public function test_the_webhook_keeps_a_plan_in_step_with_stripe()
- {
- $user = User::factory()->create();
- $this->subscribe($user, 'starter');
- auth()->logout();
-
- $this->gateway->setStatus('sub_fake_2', 'canceled');
-
- $this->postJson(route('stripe.webhook'), ['subscription_id' => 'sub_fake_2'], ['Stripe-Signature' => 'forged'])->assertStatus(400);
- $this->assertSame('active', $user->fresh()->subscription->status);
-
- $this->postJson(route('stripe.webhook'), ['subscription_id' => 'sub_fake_2'], ['Stripe-Signature' => 'valid'])->assertOk();
- $this->assertSame('canceled', $user->fresh()->subscription->status);
- }
-
- public function test_if_the_payment_service_is_down_the_merchant_sees_a_message_and_keeps_the_trial()
- {
- $user = User::factory()->create();
- $this->gateway->down = true;
-
- $this->actingAs($user)->post(route('billing.subscribe'), ['plan' => 'starter'])->assertRedirect(route('billing'));
- $this->actingAs($user)->get(route('billing'))->assertOk();
-
- $this->assertSame('trial', $user->fresh()->currentPlan()->key);
- }
-
- public function test_the_number_of_stores_is_limited_by_the_plan()
- {
- $user = User::factory()->create();
- $data = fn (string $name) => ['name' => $name, 'category' => 'food', 'language' => 'en', 'description' => str_repeat('A good store. ', 3)];
-
- $this->actingAs($user)->post(route('stores.store'), $data('One'));
- $this->actingAs($user)->post(route('stores.store'), $data('Two'))->assertRedirect(route('billing'));
- $this->actingAs($user)->get(route('stores.create'))->assertRedirect(route('billing'));
-
- $this->assertSame(1, $user->stores()->count());
-
- Subscription::factory()->for($user)->plan('growth')->create();
- $user = $user->fresh();
-
- $this->actingAs($user)->get(route('stores.create'))->assertOk();
- $this->actingAs($user)->post(route('stores.store'), $data('Two'));
- $this->actingAs($user)->post(route('stores.store'), $data('Three'));
- $this->actingAs($user)->post(route('stores.store'), $data('Four'));
-
- $this->assertSame(3, $user->stores()->count());
-
- $this->actingAs($user)->get(route('dashboard'))
- ->assertInertia(fn (Assert $page) => $page
- ->where('usage.stores.used', 3)
- ->where('usage.stores.limit', 3));
- }
-
- public function test_guests_cannot_open_billing()
- {
- $this->get(route('billing'))->assertRedirect(route('login'));
- $this->post(route('billing.subscribe'), ['plan' => 'starter'])->assertRedirect(route('login'));
- $this->post(route('billing.cancel'))->assertRedirect(route('login'));
- }
- }
|