gateway = new FakeGateway; $this->app->instance(PaymentGateway::class, $this->gateway); $this->user = User::factory()->create(); $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']); $this->store->plan = SamplePlan::for($this->store); $this->store->save(); $this->actingAs($this->user); } private function checkout(string $state, int $price = 1000, int $quantity = 1): Order { $product = Product::factory()->for($this->store)->create(['price' => $price, 'stock' => 50]); // Start every order with an empty cart, since an unpaid order keeps its cart. session()->forget('cart.'.$this->store->id); $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => $quantity]); $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' => 'Kota Kinabalu', 'postcode' => '88000', 'state' => $state, ])->assertRedirect(); return Order::latest('id')->firstOrFail(); } private function settings(array $overrides = []) { return $this->put(route('stores.settings.update', $this->store->slug), [ 'whatsapp_number' => '', 'shipping_west' => '8.00', 'shipping_east' => '15.00', 'free_shipping_over' => '', ...$overrides, ]); } public function test_a_new_store_has_sensible_starting_shipping_fees() { $this->get(route('stores.settings', $this->store->slug)) ->assertOk() ->assertInertia(fn (Assert $page) => $page ->component('stores/settings') ->where('settings.shipping_west', '8.00') ->where('settings.shipping_east', '15.00') ->where('settings.free_shipping_over', null) ->where('settings.whatsapp_number', null)); } public function test_the_merchant_can_change_shipping_fees_and_whatsapp_number() { $this->settings([ 'whatsapp_number' => '+60 12-345 6789', 'shipping_west' => '6.5', 'shipping_east' => '18', 'free_shipping_over' => '150', ])->assertRedirect(route('stores.settings', $this->store->slug)); $this->store->refresh(); $this->assertSame('60123456789', $this->store->whatsapp_number); $this->assertSame(650, $this->store->shipping_west); $this->assertSame(1800, $this->store->shipping_east); $this->assertSame(15000, $this->store->free_shipping_over); // Clearing the fields removes WhatsApp and free shipping. $this->settings(); $this->store->refresh(); $this->assertNull($this->store->whatsapp_number); $this->assertNull($this->store->free_shipping_over); } public function test_settings_are_checked() { $this->settings(['whatsapp_number' => '12345', 'shipping_west' => '-1', 'shipping_east' => 'abc', 'free_shipping_over' => '1.234']) ->assertSessionHasErrors(['shipping_west', 'shipping_east', 'free_shipping_over']); $this->settings(['whatsapp_number' => '12345'])->assertSessionHasErrors('whatsapp_number'); $this->assertNull($this->store->fresh()->whatsapp_number); } public function test_east_malaysia_pays_the_east_fee_and_the_peninsula_the_west_fee() { foreach (['Sabah' => 1500, 'Sarawak' => 1500, 'Labuan' => 1500, 'Selangor' => 800, 'Johor' => 800, 'Kuala Lumpur' => 800] as $state => $fee) { $order = $this->checkout($state); $this->assertSame($fee, $order->shipping_total, "Wrong fee for [$state]."); $this->assertSame(1000 + $fee, $order->total); } } public function test_orders_over_the_free_shipping_amount_ship_free() { $this->settings(['free_shipping_over' => '50']); $this->assertSame(800, $this->checkout('Selangor', price: 4999)->shipping_total); $this->assertSame(0, $this->checkout('Sabah', price: 5000)->shipping_total); $this->assertSame(0, $this->checkout('Selangor', price: 2500, quantity: 2)->shipping_total); $this->settings(['free_shipping_over' => '0']); $this->assertSame(0, $this->checkout('Sarawak')->shipping_total); } public function test_the_checkout_page_shows_the_fee_for_each_state() { $this->settings(['free_shipping_over' => '100']); $product = Product::factory()->for($this->store)->create(['price' => 2000, 'stock' => 5]); $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.shippingByState.Selangor', 8) ->where('view.shippingByState.Sabah', 15) ->has('view.shippingByState', 16) ->where('shop.free_shipping_over', 100) ->where('shop.whatsapp', null)); } public function test_a_paid_order_includes_shipping_in_what_must_be_paid_and_in_the_email() { $order = $this->checkout('Sabah', price: 2580); // A payment that covers only the products is not enough. $this->gateway->pay($order, amount: 2580); $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])); $this->assertFalse($order->fresh()->isPaid()); $this->gateway->pay($order, amount: 4080); $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])) ->assertInertia(fn (Assert $page) => $page ->where('view.order.status', 'paid') ->where('view.order.shipping_total', 15) ->where('view.order.total', 40.8)); $mail = new OrderConfirmation($order->fresh()); $mail->assertSeeInHtml('RM 15.00'); $mail->assertSeeInHtml('RM 40.80'); } public function test_the_merchant_can_mark_a_paid_order_as_shipped_and_the_shopper_is_emailed_once() { $order = $this->checkout('Selangor'); $ship = fn (array $data) => $this->post(route('stores.orders.ship', [$this->store->slug, $order->id]), $data); // Not paid yet. $ship(['shipping_courier' => 'J&T Express', 'tracking_number' => 'JT123'])->assertNotFound(); $this->gateway->pay($order); $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])); $ship(['shipping_courier' => 'Teleport', 'tracking_number' => 'x'])->assertSessionHasErrors('shipping_courier'); $ship(['shipping_courier' => 'Pos Laju', 'tracking_number' => '