AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CompletePaymentTest.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Commerce\Checkout;
  4. use App\Commerce\PaymentDoubleCheck;
  5. use App\Commerce\Payments\PaymentGateway;
  6. use App\Mail\PaymentReminder;
  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 Illuminate\Foundation\Testing\RefreshDatabase;
  13. use Illuminate\Support\Facades\Mail;
  14. use Inertia\Testing\AssertableInertia as Assert;
  15. use Tests\Support\FakeGateway;
  16. use Tests\TestCase;
  17. /**
  18. * A shopper who left the payment page can come back and pay: from the order page, the cart page, or the
  19. * "Complete your payment" email.
  20. */
  21. class CompletePaymentTest extends TestCase
  22. {
  23. use RefreshDatabase;
  24. private Store $store;
  25. private FakeGateway $gateway;
  26. protected function setUp(): void
  27. {
  28. parent::setUp();
  29. Mail::fake();
  30. $this->gateway = new FakeGateway;
  31. $this->app->instance(PaymentGateway::class, $this->gateway);
  32. $user = User::factory()->create();
  33. $this->store = Store::factory()->for($user)->create();
  34. $this->store->plan = SamplePlan::for($this->store);
  35. $this->store->save();
  36. $this->actingAs($user);
  37. }
  38. /** A shopper goes to the payment page and does not come back. */
  39. private function placeOrder(string $email = 'aminah@example.com'): Order
  40. {
  41. $product = Product::factory()->for($this->store)->create(['price' => 4200, 'stock' => 10]);
  42. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]);
  43. $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  44. 'name' => 'Aminah binti Ali',
  45. 'email' => $email,
  46. 'phone' => '012-345 6789',
  47. 'line1' => '12 Jalan Mawar',
  48. 'city' => 'Shah Alam',
  49. 'postcode' => '40000',
  50. 'state' => 'Selangor',
  51. ]);
  52. return Order::latest('id')->firstOrFail();
  53. }
  54. private function runCheck(): array
  55. {
  56. return app(PaymentDoubleCheck::class)->run();
  57. }
  58. public function test_the_order_keeps_its_payment_page_and_deadline()
  59. {
  60. $this->freezeSecond();
  61. $order = $this->placeOrder();
  62. $this->assertSame('https://pay.test/'.$order->payment_checkout_id, $order->payment_url);
  63. $this->assertTrue($order->payment_expires_at->eq(now()->addHours(Checkout::PAY_WITHIN_HOURS)));
  64. $this->assertTrue($order->canStillPay());
  65. }
  66. public function test_the_order_page_offers_complete_payment_with_the_deadline()
  67. {
  68. $order = $this->placeOrder();
  69. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  70. ->assertInertia(fn (Assert $page) => $page
  71. ->where('view.order.status', 'pending')
  72. ->where('view.payment.can_pay', true)
  73. ->where('view.payment.expires_at', $order->payment_expires_at->toIso8601String()));
  74. // The button goes back to the same payment page.
  75. $this->get(route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
  76. ->assertRedirect($order->payment_url);
  77. }
  78. public function test_complete_payment_shows_the_thank_you_when_the_shopper_already_paid()
  79. {
  80. $order = $this->placeOrder();
  81. $this->gateway->pay($order);
  82. $this->get(route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
  83. ->assertRedirect(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  84. $this->assertTrue($order->fresh()->isPaid());
  85. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  86. ->assertInertia(fn (Assert $page) => $page->where('view.order.status', 'paid')->where('view.payment', null));
  87. }
  88. public function test_after_the_deadline_the_order_is_closed()
  89. {
  90. $order = $this->placeOrder();
  91. $this->travel(Checkout::PAY_WITHIN_HOURS)->hours();
  92. $this->travel(1)->minutes();
  93. $this->get(route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
  94. ->assertRedirect(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  95. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  96. ->assertInertia(fn (Assert $page) => $page->where('view.payment.can_pay', false));
  97. }
  98. public function test_older_orders_without_a_saved_payment_page_show_the_old_notice()
  99. {
  100. $order = $this->placeOrder();
  101. $order->forceFill(['payment_url' => null, 'payment_expires_at' => null])->save();
  102. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  103. ->assertInertia(fn (Assert $page) => $page->where('view.payment', null));
  104. $this->get(route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
  105. ->assertRedirect(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  106. }
  107. public function test_the_cart_page_reminds_this_shopper_of_their_unpaid_order()
  108. {
  109. $order = $this->placeOrder();
  110. $this->get(route('stores.preview.cart', $this->store->slug))
  111. ->assertInertia(fn (Assert $page) => $page
  112. ->where('view.unpaidOrder.number', $order->number)
  113. ->where('view.unpaidOrder.public_id', $order->public_id));
  114. // Someone in another browser does not see it.
  115. $this->flushSession();
  116. $this->get(route('stores.preview.cart', $this->store->slug))
  117. ->assertInertia(fn (Assert $page) => $page->where('view.unpaidOrder', null));
  118. }
  119. public function test_the_cart_page_forgets_the_order_once_paid_or_closed()
  120. {
  121. $order = $this->placeOrder();
  122. $this->gateway->pay($order);
  123. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  124. $this->get(route('stores.preview.cart', $this->store->slug))
  125. ->assertInertia(fn (Assert $page) => $page->where('view.unpaidOrder', null));
  126. $this->placeOrder();
  127. $this->travel(Checkout::PAY_WITHIN_HOURS + 1)->hours();
  128. $this->get(route('stores.preview.cart', $this->store->slug))
  129. ->assertInertia(fn (Assert $page) => $page->where('view.unpaidOrder', null));
  130. }
  131. public function test_the_shopper_is_emailed_once_about_30_minutes_after_leaving_the_payment_page()
  132. {
  133. $order = $this->placeOrder();
  134. $this->travel(20)->minutes();
  135. $this->runCheck();
  136. Mail::assertNothingSent();
  137. $this->travel(11)->minutes();
  138. $this->artisan('orders:check-payments')
  139. ->expectsOutputToContain('1 shoppers reminded by email')
  140. ->assertSuccessful();
  141. Mail::assertSent(PaymentReminder::class, function (PaymentReminder $mail) use ($order) {
  142. $html = $mail->render();
  143. return $mail->hasTo('aminah@example.com')
  144. && $mail->envelope()->subject === "Complete your payment for order #{$order->number} - {$this->store->name}"
  145. && str_contains($html, route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
  146. && str_contains($html, 'You can pay until');
  147. });
  148. $this->assertNotNull($order->fresh()->payment_reminded_at);
  149. // Never twice.
  150. $this->travel(2)->hours();
  151. $this->runCheck();
  152. Mail::assertSent(PaymentReminder::class, 1);
  153. }
  154. public function test_no_reminder_when_the_shopper_paid()
  155. {
  156. $order = $this->placeOrder();
  157. $this->gateway->pay($order);
  158. $this->travel(31)->minutes();
  159. $result = $this->runCheck();
  160. $this->assertSame(1, $result['paid']);
  161. $this->assertSame(0, $result['reminded']);
  162. Mail::assertNotSent(PaymentReminder::class);
  163. }
  164. public function test_no_reminder_when_the_shopper_ordered_again_since()
  165. {
  166. $first = $this->placeOrder();
  167. $this->travel(5)->minutes();
  168. $second = $this->placeOrder();
  169. $this->gateway->pay($second);
  170. $this->get(route('stores.preview.order', [$this->store->slug, $second->public_id]));
  171. $this->travel(40)->minutes();
  172. $this->runCheck();
  173. Mail::assertNotSent(PaymentReminder::class);
  174. $this->assertNull($first->fresh()->payment_reminded_at);
  175. }
  176. public function test_no_reminder_while_the_provider_cannot_say_whether_they_paid()
  177. {
  178. $order = $this->placeOrder();
  179. unset($this->gateway->payments[$order->payment_checkout_id]);
  180. $this->travel(31)->minutes();
  181. $this->assertSame(1, $this->runCheck()['failed']);
  182. Mail::assertNotSent(PaymentReminder::class);
  183. $this->assertNull($order->fresh()->payment_reminded_at);
  184. }
  185. public function test_the_reminder_is_written_in_malay_for_malay_stores()
  186. {
  187. $this->store->forceFill(['language' => 'ms'])->save();
  188. $order = $this->placeOrder();
  189. $this->travel(31)->minutes();
  190. $this->runCheck();
  191. Mail::assertSent(PaymentReminder::class, fn (PaymentReminder $mail) => $mail->envelope()->subject === "Lengkapkan bayaran untuk pesanan #{$order->number} - {$this->store->name}"
  192. && str_contains($mail->render(), 'Anda boleh membayar sehingga'));
  193. }
  194. public function test_merchants_see_when_an_order_is_waiting_for_payment()
  195. {
  196. $order = $this->placeOrder();
  197. $this->get(route('stores.orders.show', [$this->store->slug, $order->id]))
  198. ->assertInertia(fn (Assert $page) => $page
  199. ->where('order.status', 'pending')
  200. ->where('order.payment_expires_at', $order->payment_expires_at->toIso8601String())
  201. ->where('order.payment_reminded_at', null));
  202. }
  203. }