AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PaymentDoubleCheckTest.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Commerce\PaymentDoubleCheck;
  4. use App\Commerce\Payments\PaymentGateway;
  5. use App\Enums\OrderStatus;
  6. use App\Mail\OrderConfirmation;
  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. * The double-check every 5 minutes for orders whose payment news was missed.
  19. */
  20. class PaymentDoubleCheckTest extends TestCase
  21. {
  22. use RefreshDatabase;
  23. private Store $store;
  24. private FakeGateway $gateway;
  25. protected function setUp(): void
  26. {
  27. parent::setUp();
  28. Mail::fake();
  29. $this->gateway = new FakeGateway;
  30. $this->app->instance(PaymentGateway::class, $this->gateway);
  31. $user = User::factory()->create();
  32. $this->store = Store::factory()->for($user)->create();
  33. $this->store->plan = SamplePlan::for($this->store);
  34. $this->store->save();
  35. $this->actingAs($user);
  36. }
  37. /** A shopper goes to the payment page and does not come back. */
  38. private function placeOrder(): Order
  39. {
  40. $product = Product::factory()->for($this->store)->create(['price' => 4200, 'stock' => 10]);
  41. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]);
  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' => 'Shah Alam',
  48. 'postcode' => '40000',
  49. 'state' => 'Selangor',
  50. ]);
  51. return Order::latest('id')->firstOrFail();
  52. }
  53. private function runCheck(): array
  54. {
  55. return app(PaymentDoubleCheck::class)->run();
  56. }
  57. public function test_a_payment_whose_news_was_missed_is_found_and_the_order_marked_paid()
  58. {
  59. $order = $this->placeOrder();
  60. // The shopper paid, then closed the phone; no webhook came.
  61. $this->gateway->pay($order);
  62. // Too new to ask about yet.
  63. $this->assertSame(0, $this->runCheck()['checked']);
  64. $this->assertFalse($order->fresh()->isPaid());
  65. $this->travel(3)->minutes();
  66. $this->artisan('orders:check-payments')
  67. ->expectsOutputToContain('Checked 1 unpaid orders: 1 were paid')
  68. ->assertSuccessful();
  69. $order->refresh();
  70. $this->assertTrue($order->isPaid());
  71. $this->assertNotNull($order->payment_checked_at);
  72. $this->assertSame(9, Product::firstOrFail()->stock);
  73. Mail::assertSent(OrderConfirmation::class, 1);
  74. // Paid orders are never asked about again.
  75. $this->travel(10)->minutes();
  76. $this->assertSame(0, $this->runCheck()['checked']);
  77. }
  78. public function test_new_orders_are_asked_about_often_and_older_ones_rarely()
  79. {
  80. $order = $this->placeOrder();
  81. $this->travel(3)->minutes();
  82. $this->assertSame(1, $this->runCheck()['checked']);
  83. // Every 5 minutes in the first quarter of an hour.
  84. $this->travel(2)->minutes();
  85. $this->assertSame(0, $this->runCheck()['checked']);
  86. $this->travel(3)->minutes();
  87. $this->assertSame(1, $this->runCheck()['checked']);
  88. // Every 15 minutes until an hour.
  89. $this->travel(22)->minutes(); // 30 minutes old
  90. $this->assertSame(1, $this->runCheck()['checked']);
  91. $this->travel(10)->minutes();
  92. $this->assertSame(0, $this->runCheck()['checked']);
  93. $this->travel(5)->minutes();
  94. $this->assertSame(1, $this->runCheck()['checked']);
  95. // Then every hour.
  96. $this->travel(2)->hours();
  97. $this->assertSame(1, $this->runCheck()['checked']);
  98. $this->travel(30)->minutes();
  99. $this->assertSame(0, $this->runCheck()['checked']);
  100. $this->assertFalse($order->fresh()->isPaid());
  101. }
  102. public function test_a_day_later_an_unpaid_order_becomes_not_paid_and_is_left_alone()
  103. {
  104. $order = $this->placeOrder();
  105. $this->travel(26)->hours();
  106. $result = $this->runCheck();
  107. $this->assertSame(0, $result['checked']);
  108. $this->assertSame(1, $result['given_up']);
  109. $this->assertSame(OrderStatus::Unpaid, $order->fresh()->status);
  110. $this->assertSame(0, $this->runCheck()['given_up']);
  111. $this->get(route('stores.orders.index', $this->store->slug))
  112. ->assertInertia(fn (Assert $page) => $page->where('orders.0.status', 'unpaid')->where('orders.0.status_label', 'Not paid'));
  113. // A payment that still arrives (the shopper opens the order page) counts.
  114. $this->gateway->pay($order);
  115. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  116. ->assertInertia(fn (Assert $page) => $page->where('view.order.status', 'paid'));
  117. $this->assertTrue($order->fresh()->isPaid());
  118. }
  119. public function test_a_provider_that_cannot_be_reached_does_not_stop_the_other_orders()
  120. {
  121. $first = $this->placeOrder();
  122. $second = $this->placeOrder();
  123. $this->gateway->pay($second);
  124. // The provider no longer knows the first payment page.
  125. unset($this->gateway->payments[$first->payment_checkout_id]);
  126. $this->travel(3)->minutes();
  127. $result = $this->runCheck();
  128. $this->assertSame(['checked' => 2, 'paid' => 1, 'failed' => 1, 'reminded' => 0, 'given_up' => 0], $result);
  129. $this->assertTrue($second->fresh()->isPaid());
  130. // Not asked again on the very next run.
  131. $this->travel(1)->minutes();
  132. $this->assertSame(0, $this->runCheck()['checked']);
  133. }
  134. public function test_the_check_is_scheduled_every_five_minutes()
  135. {
  136. $this->artisan('schedule:list')
  137. ->expectsOutputToContain('*/5 * * * * php artisan orders:check-payments')
  138. ->assertSuccessful();
  139. }
  140. }