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.

ShippingTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace Tests\Feature\Malaysia;
  3. use App\Commerce\Payments\PaymentGateway;
  4. use App\Commerce\Payments\StripeGateway;
  5. use App\Mail\OrderConfirmation;
  6. use App\Mail\OrderShipped;
  7. use App\Models\Order;
  8. use App\Models\Product;
  9. use App\Models\Store;
  10. use App\Models\User;
  11. use App\StorePlan\SamplePlan;
  12. use App\Support\MalaysianPhone;
  13. use Illuminate\Foundation\Testing\RefreshDatabase;
  14. use Illuminate\Support\Facades\Mail;
  15. use Inertia\Testing\AssertableInertia as Assert;
  16. use Tests\Support\FakeGateway;
  17. use Tests\TestCase;
  18. class ShippingTest extends TestCase
  19. {
  20. use RefreshDatabase;
  21. private User $user;
  22. private Store $store;
  23. private FakeGateway $gateway;
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. Mail::fake();
  28. $this->gateway = new FakeGateway;
  29. $this->app->instance(PaymentGateway::class, $this->gateway);
  30. $this->user = User::factory()->create();
  31. $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
  32. $this->store->plan = SamplePlan::for($this->store);
  33. $this->store->save();
  34. $this->actingAs($this->user);
  35. }
  36. private function checkout(string $state, int $price = 1000, int $quantity = 1): Order
  37. {
  38. $product = Product::factory()->for($this->store)->create(['price' => $price, 'stock' => 50]);
  39. // Start every order with an empty cart, since an unpaid order keeps its cart.
  40. session()->forget('cart.'.$this->store->id);
  41. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => $quantity]);
  42. $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  43. 'name' => 'Aminah binti Ali',
  44. 'email' => 'aminah@example.com',
  45. 'phone' => '012-345 6789',
  46. 'line1' => '12 Jalan Mawar',
  47. 'city' => 'Kota Kinabalu',
  48. 'postcode' => '88000',
  49. 'state' => $state,
  50. ])->assertRedirect();
  51. return Order::latest('id')->firstOrFail();
  52. }
  53. private function settings(array $overrides = [])
  54. {
  55. return $this->put(route('stores.settings.update', $this->store->slug), [
  56. 'whatsapp_number' => '',
  57. 'shipping_west' => '8.00',
  58. 'shipping_east' => '15.00',
  59. 'free_shipping_over' => '',
  60. ...$overrides,
  61. ]);
  62. }
  63. public function test_a_new_store_has_sensible_starting_shipping_fees()
  64. {
  65. $this->get(route('stores.settings', $this->store->slug))
  66. ->assertOk()
  67. ->assertInertia(fn (Assert $page) => $page
  68. ->component('stores/settings')
  69. ->where('settings.shipping_west', '8.00')
  70. ->where('settings.shipping_east', '15.00')
  71. ->where('settings.free_shipping_over', null)
  72. ->where('settings.whatsapp_number', null));
  73. }
  74. public function test_the_merchant_can_change_shipping_fees_and_whatsapp_number()
  75. {
  76. $this->settings([
  77. 'whatsapp_number' => '+60 12-345 6789',
  78. 'shipping_west' => '6.5',
  79. 'shipping_east' => '18',
  80. 'free_shipping_over' => '150',
  81. ])->assertRedirect(route('stores.settings', $this->store->slug));
  82. $this->store->refresh();
  83. $this->assertSame('60123456789', $this->store->whatsapp_number);
  84. $this->assertSame(650, $this->store->shipping_west);
  85. $this->assertSame(1800, $this->store->shipping_east);
  86. $this->assertSame(15000, $this->store->free_shipping_over);
  87. // Clearing the fields removes WhatsApp and free shipping.
  88. $this->settings();
  89. $this->store->refresh();
  90. $this->assertNull($this->store->whatsapp_number);
  91. $this->assertNull($this->store->free_shipping_over);
  92. }
  93. public function test_settings_are_checked()
  94. {
  95. $this->settings(['whatsapp_number' => '12345', 'shipping_west' => '-1', 'shipping_east' => 'abc', 'free_shipping_over' => '1.234'])
  96. ->assertSessionHasErrors(['shipping_west', 'shipping_east', 'free_shipping_over']);
  97. $this->settings(['whatsapp_number' => '12345'])->assertSessionHasErrors('whatsapp_number');
  98. $this->assertNull($this->store->fresh()->whatsapp_number);
  99. }
  100. public function test_east_malaysia_pays_the_east_fee_and_the_peninsula_the_west_fee()
  101. {
  102. foreach (['Sabah' => 1500, 'Sarawak' => 1500, 'Labuan' => 1500, 'Selangor' => 800, 'Johor' => 800, 'Kuala Lumpur' => 800] as $state => $fee) {
  103. $order = $this->checkout($state);
  104. $this->assertSame($fee, $order->shipping_total, "Wrong fee for [$state].");
  105. $this->assertSame(1000 + $fee, $order->total);
  106. }
  107. }
  108. public function test_orders_over_the_free_shipping_amount_ship_free()
  109. {
  110. $this->settings(['free_shipping_over' => '50']);
  111. $this->assertSame(800, $this->checkout('Selangor', price: 4999)->shipping_total);
  112. $this->assertSame(0, $this->checkout('Sabah', price: 5000)->shipping_total);
  113. $this->assertSame(0, $this->checkout('Selangor', price: 2500, quantity: 2)->shipping_total);
  114. $this->settings(['free_shipping_over' => '0']);
  115. $this->assertSame(0, $this->checkout('Sarawak')->shipping_total);
  116. }
  117. public function test_the_checkout_page_shows_the_fee_for_each_state()
  118. {
  119. $this->settings(['free_shipping_over' => '100']);
  120. $product = Product::factory()->for($this->store)->create(['price' => 2000, 'stock' => 5]);
  121. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]);
  122. $this->get(route('stores.preview.checkout', $this->store->slug))
  123. ->assertInertia(fn (Assert $page) => $page
  124. ->where('view.shippingByState.Selangor', 8)
  125. ->where('view.shippingByState.Sabah', 15)
  126. ->has('view.shippingByState', 16)
  127. ->where('shop.free_shipping_over', 100)
  128. ->where('shop.whatsapp', null));
  129. }
  130. public function test_a_paid_order_includes_shipping_in_what_must_be_paid_and_in_the_email()
  131. {
  132. $order = $this->checkout('Sabah', price: 2580);
  133. // A payment that covers only the products is not enough.
  134. $this->gateway->pay($order, amount: 2580);
  135. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  136. $this->assertFalse($order->fresh()->isPaid());
  137. $this->gateway->pay($order, amount: 4080);
  138. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  139. ->assertInertia(fn (Assert $page) => $page
  140. ->where('view.order.status', 'paid')
  141. ->where('view.order.shipping_total', 15)
  142. ->where('view.order.total', 40.8));
  143. $mail = new OrderConfirmation($order->fresh());
  144. $mail->assertSeeInHtml('RM 15.00');
  145. $mail->assertSeeInHtml('RM 40.80');
  146. }
  147. public function test_the_merchant_can_mark_a_paid_order_as_shipped_and_the_shopper_is_emailed_once()
  148. {
  149. $order = $this->checkout('Selangor');
  150. $ship = fn (array $data) => $this->post(route('stores.orders.ship', [$this->store->slug, $order->id]), $data);
  151. // Not paid yet.
  152. $ship(['shipping_courier' => 'J&T Express', 'tracking_number' => 'JT123'])->assertNotFound();
  153. $this->gateway->pay($order);
  154. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  155. $ship(['shipping_courier' => 'Teleport', 'tracking_number' => 'x'])->assertSessionHasErrors('shipping_courier');
  156. $ship(['shipping_courier' => 'Pos Laju', 'tracking_number' => '<script>'])->assertSessionHasErrors('tracking_number');
  157. $ship(['shipping_courier' => 'J&T Express', 'tracking_number' => ' 630012345678 '])
  158. ->assertRedirect(route('stores.orders.show', [$this->store->slug, $order->id]));
  159. $order->refresh();
  160. $this->assertNotNull($order->shipped_at);
  161. $this->assertSame('J&T Express', $order->shipping_courier);
  162. $this->assertSame('630012345678', $order->tracking_number);
  163. Mail::assertSent(OrderShipped::class, fn (OrderShipped $mail) => $mail->hasTo('aminah@example.com'));
  164. // Correcting a typo saves it without emailing again.
  165. $shippedAt = $order->shipped_at;
  166. $ship(['shipping_courier' => 'Ninja Van', 'tracking_number' => 'NV999']);
  167. $order->refresh();
  168. $this->assertSame('Ninja Van', $order->shipping_courier);
  169. $this->assertEquals($shippedAt, $order->shipped_at);
  170. Mail::assertSent(OrderShipped::class, 1);
  171. // The shopper sees it on their order page.
  172. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  173. ->assertInertia(fn (Assert $page) => $page
  174. ->where('view.order.shipping_courier', 'Ninja Van')
  175. ->where('view.order.tracking_number', 'NV999'));
  176. }
  177. public function test_the_shipped_email_is_in_the_store_language()
  178. {
  179. $order = Order::factory()->for($this->store)->paid()->create([
  180. 'number' => 1009, 'shipped_at' => now(), 'shipping_courier' => 'Pos Laju', 'tracking_number' => 'EP123MY',
  181. ]);
  182. $order->items()->create(['store_id' => $this->store->id, 'name' => 'Sambal Hijau', 'unit_price' => 1290, 'quantity' => 2, 'line_total' => 2580]);
  183. $mail = new OrderShipped($order);
  184. $mail->assertHasSubject('Order #1009 is on its way - Kedai Ali');
  185. $mail->assertSeeInHtml('EP123MY');
  186. $mail->assertSeeInHtml('Pos Laju');
  187. $this->store->update(['language' => 'ms']);
  188. $mail = new OrderShipped($order->fresh());
  189. $mail->assertHasSubject('Pesanan #1009 telah dihantar - Kedai Ali');
  190. $mail->assertSeeInHtml('Nombor penjejakan');
  191. }
  192. public function test_another_merchant_cannot_ship_or_change_settings()
  193. {
  194. $order = $this->checkout('Selangor');
  195. $this->gateway->pay($order);
  196. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  197. $this->actingAs(User::factory()->create());
  198. $this->post(route('stores.orders.ship', [$this->store->slug, $order->id]), ['shipping_courier' => 'Pos Laju'])->assertNotFound();
  199. $this->get(route('stores.settings', $this->store->slug))->assertNotFound();
  200. $this->settings(['shipping_west' => '0'])->assertNotFound();
  201. $this->assertNull($order->fresh()->shipped_at);
  202. $this->assertSame(800, $this->store->fresh()->shipping_west);
  203. }
  204. public function test_the_whatsapp_number_reaches_the_store_and_the_merchant_gets_a_chat_link_to_the_shopper()
  205. {
  206. $this->settings(['whatsapp_number' => '0123456789']);
  207. $order = $this->checkout('Selangor');
  208. $this->get(route('stores.preview', $this->store->slug))
  209. ->assertInertia(fn (Assert $page) => $page->where('shop.whatsapp', '60123456789'));
  210. $this->get(route('stores.orders.show', [$this->store->slug, $order->id]))
  211. ->assertInertia(fn (Assert $page) => $page
  212. ->has('couriers', 10)
  213. ->where('customer_whatsapp_url', fn (string $url) => str_starts_with($url, 'https://wa.me/60123456789?text=')
  214. && str_contains(rawurldecode($url), 'order #'.$order->number)));
  215. }
  216. public function test_malaysian_phone_numbers_are_normalised_for_whatsapp()
  217. {
  218. $this->assertSame('60123456789', MalaysianPhone::normalize('012-345 6789'));
  219. $this->assertSame('60123456789', MalaysianPhone::normalize('+60 12 345 6789'));
  220. $this->assertSame('601123456789', MalaysianPhone::normalize('011-2345 6789'));
  221. $this->assertSame('60123456789', MalaysianPhone::normalize('123456789'));
  222. $this->assertNull(MalaysianPhone::normalize('12345'));
  223. $this->assertNull(MalaysianPhone::normalize('+65 9123 4567'));
  224. $this->assertNull(MalaysianPhone::normalize(''));
  225. $this->assertSame('https://wa.me/60123456789?text=Hi%20there%20%231', MalaysianPhone::whatsappLink('60123456789', 'Hi there #1'));
  226. }
  227. public function test_checkout_messages_follow_the_store_language()
  228. {
  229. $this->store->update(['language' => 'ms']);
  230. $product = Product::factory()->for($this->store)->create(['price' => 1000, 'stock' => 5]);
  231. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]);
  232. $response = $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  233. 'name' => '', 'email' => 'nope', 'phone' => 'x', 'line1' => 'ab', 'city' => 'KL', 'postcode' => '123', 'state' => 'Atlantis',
  234. ]);
  235. $response->assertSessionHasErrors([
  236. 'name' => 'Nama penuh diperlukan.',
  237. 'email' => 'E-mel mestilah alamat e-mel yang sah.',
  238. 'phone' => 'Sila masukkan nombor telefon yang sah.',
  239. 'line1' => 'Alamat mestilah sekurang-kurangnya 3 aksara.',
  240. 'postcode' => 'Poskod mestilah 5 digit.',
  241. 'state' => 'Sila pilih negeri.',
  242. ]);
  243. $soldOut = Product::factory()->for($this->store)->soldOut()->create();
  244. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $soldOut->id, 'quantity' => 1])
  245. ->assertSessionHasErrors(['quantity' => 'Produk ini telah habis dijual.']);
  246. }
  247. public function test_stripe_is_given_the_shipping_fee_as_its_own_line()
  248. {
  249. $order = Order::factory()->for($this->store)->create(['shipping_total' => 1500]);
  250. $method = new \ReflectionMethod(StripeGateway::class, 'shippingLabel');
  251. $this->assertSame('Shipping', $method->invoke(new StripeGateway, $order));
  252. $this->assertSame('Free shipping', $method->invoke(new StripeGateway, $order->replicate()->fill([])->forceFill(['shipping_total' => 0])));
  253. $this->store->update(['language' => 'ms']);
  254. $this->assertSame('Penghantaran', $method->invoke(new StripeGateway, $order->fresh()));
  255. }
  256. }