'sk_test_fake']); $this->accounts = new FakeMerchantAccounts; $this->app->instance(MerchantAccounts::class, $this->accounts); $this->user = User::factory()->create(['email' => 'ali@example.com']); $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali', 'plan' => null]); $this->store->plan = SamplePlan::for($this->store); $this->store->save(); $this->actingAs($this->user); } private function connect() { return $this->post(route('stores.payments.connect', $this->store->slug), [], ['X-Inertia' => 'true']); } private function connectedAccount(bool $ready = true): StripeAccount { $account = $this->store->stripeAccount()->create([ 'account_id' => 'acct_fake_'.$this->store->id, 'charges_enabled' => $ready, 'payouts_enabled' => $ready, 'details_submitted' => $ready, 'checked_at' => now(), ]); $this->accounts->statuses[$account->account_id] = new MerchantAccountStatus($ready, $ready, $ready); return $account; } public function test_settings_show_that_the_store_cannot_take_orders_until_stripe_is_connected() { $this->get(route('stores.settings', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('payments.available', true) ->where('payments.connected', false) ->where('payments.can_take_payments', false)); } public function test_connecting_opens_a_stripe_account_and_sends_the_merchant_to_stripes_form() { $response = $this->connect(); $response->assertStatus(409); $this->assertSame('https://connect.stripe.test/setup/acct_fake_'.$this->store->id, $response->headers->get('X-Inertia-Location')); $this->assertSame('acct_fake_'.$this->store->id, $this->store->stripeAccount()->firstOrFail()->account_id); $this->assertSame(route('stores.payments.return', $this->store->slug), $this->accounts->links[0]['return']); $this->assertSame(route('stores.payments.resume', $this->store->slug), $this->accounts->links[0]['refresh']); // Clicking again (such as "Finish setup") uses the same account. $this->connect()->assertStatus(409); $this->assertCount(1, $this->accounts->created); $this->assertCount(2, $this->accounts->links); $this->assertSame(1, StripeAccount::count()); } public function test_coming_back_unfinished_says_so() { $this->connect(); $this->get(route('stores.payments.return', $this->store->slug)) ->assertRedirect(route('stores.settings', $this->store->slug)) ->assertInertiaFlash('toast.type', 'error'); $this->get(route('stores.settings', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('payments.connected', true) ->where('payments.details_submitted', false) ->where('payments.can_take_payments', false)); $this->assertSame([], $this->accounts->fpxRequested); } public function test_coming_back_finished_saves_the_account_and_asks_for_fpx_once() { $this->connect(); $this->accounts->approve('acct_fake_'.$this->store->id); $this->get(route('stores.payments.return', $this->store->slug)) ->assertRedirect(route('stores.settings', $this->store->slug)) ->assertInertiaFlash('toast.type', 'success'); $account = $this->store->stripeAccount()->firstOrFail(); $this->assertTrue($account->charges_enabled); $this->assertTrue($account->payouts_enabled); $this->assertTrue($account->fpx_requested); $this->get(route('stores.payments.return', $this->store->slug)); $this->assertCount(1, $this->accounts->fpxRequested); $this->get(route('stores.settings', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('payments.can_take_payments', true) ->where('payments.payouts_enabled', true)); } public function test_an_expired_stripe_link_is_replaced_with_a_fresh_one() { $this->connectedAccount(ready: false); $this->get(route('stores.payments.resume', $this->store->slug)) ->assertRedirect('https://connect.stripe.test/setup/acct_fake_'.$this->store->id); } public function test_stripe_being_down_shows_a_message_instead_of_an_error_page() { $this->accounts->down = true; $this->connect() ->assertRedirect(route('stores.settings', $this->store->slug)) ->assertInertiaFlash('toast.type', 'error'); $this->assertNull($this->store->stripeAccount()->first()); } public function test_without_the_apps_stripe_key_connecting_is_not_offered() { config(['services.stripe.secret' => '']); $this->get(route('stores.settings', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('payments.available', false)); $this->connect()->assertRedirect(route('stores.settings', $this->store->slug)); $this->assertSame([], $this->accounts->created); } public function test_another_merchants_store_cannot_be_connected() { $other = Store::factory()->create(); $this->post(route('stores.payments.connect', $other->slug))->assertNotFound(); $this->get(route('stores.payments.return', $other->slug))->assertNotFound(); $this->get(route('stores.payments.resume', $other->slug))->assertNotFound(); } public function test_a_store_takes_payments_only_once_stripe_approves_and_stripe_is_asked_at_most_once_a_minute() { $connect = app(StripeConnect::class); $this->assertFalse($connect->canTakePayments($this->store)); $this->connectedAccount(ready: false); $this->store->refresh(); // Checked just now, so Stripe is not asked again yet. $this->assertFalse($connect->canTakePayments($this->store)); $this->assertSame(0, $this->accounts->statusCalls); $this->accounts->approve('acct_fake_'.$this->store->id); $this->travel(2)->minutes(); $this->assertTrue($connect->canTakePayments($this->store->refresh())); $this->assertSame(1, $this->accounts->statusCalls); // A fully set up store is not asked about again. $this->travel(2)->minutes(); $this->assertTrue($connect->canTakePayments($this->store->refresh())); $this->assertSame(1, $this->accounts->statusCalls); } public function test_the_real_gateway_will_not_open_a_payment_page_for_a_store_without_stripe() { $order = Order::factory()->for($this->store)->create(); $this->assertFalse(app(StripeGateway::class)->canTakePayments($this->store)); $this->expectException(PaymentException::class); app(StripeGateway::class)->startCheckout($order, 'https://shop.test/ok', 'https://shop.test/cart'); } public function test_shoppers_cannot_check_out_until_the_store_can_take_payments() { $gateway = new FakeGateway; $gateway->takingPayments = false; $this->app->instance(PaymentGateway::class, $gateway); $product = Product::factory()->for($this->store)->create(['price' => 1290, 'stock' => 10]); $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]); $this->get(route('stores.preview.checkout', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('view.takingOrders', false)); $this->post(route('stores.preview.checkout.store', $this->store->slug), [ 'name' => 'Aminah binti Ali', 'email' => 'aminah@example.com', 'phone' => '012-345 6789', 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor', ])->assertSessionHasErrors(['cart' => 'This store is not taking orders yet. Please check back soon.']); $this->assertSame(0, Order::count()); $this->assertSame([], $gateway->started); } public function test_an_order_remembers_the_merchant_account_and_the_merchants_webhook_marks_it_paid() { $gateway = new FakeGateway; $this->app->instance(PaymentGateway::class, $gateway); $product = Product::factory()->for($this->store)->create(['price' => 1290, 'stock' => 10]); $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]); $this->post(route('stores.preview.checkout.store', $this->store->slug), [ 'name' => 'Aminah binti Ali', 'email' => 'aminah@example.com', 'phone' => '012-345 6789', 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor', ]); $order = Order::firstOrFail(); $account = 'acct_fake_'.$this->store->id; $this->assertSame($account, $order->payment_account); $gateway->pay($order); auth()->logout(); $this->postJson(route('stripe.connect-webhook'), ['checkout_id' => $order->payment_checkout_id, 'account' => $account], ['Stripe-Signature' => 'forged']) ->assertStatus(400); // A payment in some other merchant's account never marks this order. $this->postJson(route('stripe.connect-webhook'), ['checkout_id' => $order->payment_checkout_id, 'account' => 'acct_someone_else'], ['Stripe-Signature' => 'valid-connect']) ->assertOk(); $this->assertFalse($order->fresh()->isPaid()); $this->postJson(route('stripe.connect-webhook'), ['checkout_id' => $order->payment_checkout_id, 'account' => $account], ['Stripe-Signature' => 'valid-connect']) ->assertOk(); $this->assertTrue($order->fresh()->isPaid()); $this->assertSame(9, $product->fresh()->stock); Mail::assertSent(OrderConfirmation::class, 1); } public function test_the_real_gateway_reads_merchant_messages_with_their_own_secret() { config(['services.stripe.webhook_secret' => 'whsec_ours', 'services.stripe.connect_webhook_secret' => 'whsec_merchants']); $payload = (string) json_encode([ 'id' => 'evt_1', 'object' => 'event', 'account' => 'acct_123', 'type' => 'checkout.session.completed', 'data' => ['object' => [ 'id' => 'cs_test_123', 'object' => 'checkout.session', 'payment_status' => 'paid', 'payment_intent' => 'pi_123', 'amount_total' => 2580, ]], ]); $time = time(); $sign = fn (string $secret) => "t=$time,v1=".hash_hmac('sha256', "$time.$payload", $secret); $status = app(StripeGateway::class)->readWebhook($payload, $sign('whsec_merchants'), fromMerchants: true); $this->assertSame('cs_test_123', $status->checkoutId); $this->assertSame('acct_123', $status->account); $this->assertTrue($status->paid); $this->expectException(PaymentException::class); app(StripeGateway::class)->readWebhook($payload, $sign('whsec_ours'), fromMerchants: true); } public function test_the_overview_asks_merchants_to_connect_stripe() { $this->store->forceFill(['status' => StoreStatus::Published, 'published_plan' => $this->store->plan])->save(); $this->get(route('dashboard')) ->assertInertia(fn (Assert $page) => $page->where('gettingStarted.steps.payments', false)); $this->connectedAccount(); $this->get(route('dashboard')) ->assertInertia(fn (Assert $page) => $page->where('gettingStarted.steps.payments', true)); } public function test_a_live_store_that_cannot_take_payments_needs_attention() { $this->store->forceFill(['status' => StoreStatus::Published, 'published_plan' => $this->store->plan])->save(); // A sale already made, so the overview shows its full view with "Needs attention". Order::factory()->for($this->store)->create(['status' => OrderStatus::Paid, 'paid_at' => now(), 'shipped_at' => now()]); $this->get(route('dashboard')) ->assertInertia(fn (Assert $page) => $page ->where('stats.attention.0.kind', 'payments') ->where('stats.attention.0.store.slug', $this->store->slug)); $this->connectedAccount(); $this->get(route('dashboard')) ->assertInertia(fn (Assert $page) => $page->where('stats.attention', fn ($items) => collect($items)->doesntContain('kind', 'payments'))); } }