| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Commerce\Checkout;
- use App\Commerce\PaymentDoubleCheck;
- use App\Commerce\Payments\PaymentGateway;
- use App\Mail\PaymentReminder;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Mail;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\FakeGateway;
- use Tests\TestCase;
-
- /**
- * A shopper who left the payment page can come back and pay: from the order page, the cart page, or the
- * "Complete your payment" email.
- */
- class CompletePaymentTest extends TestCase
- {
- use RefreshDatabase;
-
- private Store $store;
-
- private FakeGateway $gateway;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- Mail::fake();
-
- $this->gateway = new FakeGateway;
- $this->app->instance(PaymentGateway::class, $this->gateway);
-
- $user = User::factory()->create();
- $this->store = Store::factory()->for($user)->create();
- $this->store->plan = SamplePlan::for($this->store);
- $this->store->save();
- $this->actingAs($user);
- }
-
- /** A shopper goes to the payment page and does not come back. */
- private function placeOrder(string $email = 'aminah@example.com'): Order
- {
- $product = Product::factory()->for($this->store)->create(['price' => 4200, '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' => $email,
- 'phone' => '012-345 6789',
- 'line1' => '12 Jalan Mawar',
- 'city' => 'Shah Alam',
- 'postcode' => '40000',
- 'state' => 'Selangor',
- ]);
-
- return Order::latest('id')->firstOrFail();
- }
-
- private function runCheck(): array
- {
- return app(PaymentDoubleCheck::class)->run();
- }
-
- public function test_the_order_keeps_its_payment_page_and_deadline()
- {
- $this->freezeSecond();
- $order = $this->placeOrder();
-
- $this->assertSame('https://pay.test/'.$order->payment_checkout_id, $order->payment_url);
- $this->assertTrue($order->payment_expires_at->eq(now()->addHours(Checkout::PAY_WITHIN_HOURS)));
- $this->assertTrue($order->canStillPay());
- }
-
- public function test_the_order_page_offers_complete_payment_with_the_deadline()
- {
- $order = $this->placeOrder();
-
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('view.order.status', 'pending')
- ->where('view.payment.can_pay', true)
- ->where('view.payment.expires_at', $order->payment_expires_at->toIso8601String()));
-
- // The button goes back to the same payment page.
- $this->get(route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
- ->assertRedirect($order->payment_url);
- }
-
- public function test_complete_payment_shows_the_thank_you_when_the_shopper_already_paid()
- {
- $order = $this->placeOrder();
- $this->gateway->pay($order);
-
- $this->get(route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
- ->assertRedirect(route('stores.preview.order', [$this->store->slug, $order->public_id]));
- $this->assertTrue($order->fresh()->isPaid());
-
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
- ->assertInertia(fn (Assert $page) => $page->where('view.order.status', 'paid')->where('view.payment', null));
- }
-
- public function test_after_the_deadline_the_order_is_closed()
- {
- $order = $this->placeOrder();
- $this->travel(Checkout::PAY_WITHIN_HOURS)->hours();
- $this->travel(1)->minutes();
-
- $this->get(route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
- ->assertRedirect(route('stores.preview.order', [$this->store->slug, $order->public_id]));
-
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
- ->assertInertia(fn (Assert $page) => $page->where('view.payment.can_pay', false));
- }
-
- public function test_older_orders_without_a_saved_payment_page_show_the_old_notice()
- {
- $order = $this->placeOrder();
- $order->forceFill(['payment_url' => null, 'payment_expires_at' => null])->save();
-
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
- ->assertInertia(fn (Assert $page) => $page->where('view.payment', null));
- $this->get(route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
- ->assertRedirect(route('stores.preview.order', [$this->store->slug, $order->public_id]));
- }
-
- public function test_the_cart_page_reminds_this_shopper_of_their_unpaid_order()
- {
- $order = $this->placeOrder();
-
- $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('view.unpaidOrder.number', $order->number)
- ->where('view.unpaidOrder.public_id', $order->public_id));
-
- // Someone in another browser does not see it.
- $this->flushSession();
- $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('view.unpaidOrder', null));
- }
-
- public function test_the_cart_page_forgets_the_order_once_paid_or_closed()
- {
- $order = $this->placeOrder();
- $this->gateway->pay($order);
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
-
- $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('view.unpaidOrder', null));
-
- $this->placeOrder();
- $this->travel(Checkout::PAY_WITHIN_HOURS + 1)->hours();
-
- $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('view.unpaidOrder', null));
- }
-
- public function test_the_shopper_is_emailed_once_about_30_minutes_after_leaving_the_payment_page()
- {
- $order = $this->placeOrder();
-
- $this->travel(20)->minutes();
- $this->runCheck();
- Mail::assertNothingSent();
-
- $this->travel(11)->minutes();
- $this->artisan('orders:check-payments')
- ->expectsOutputToContain('1 shoppers reminded by email')
- ->assertSuccessful();
-
- Mail::assertSent(PaymentReminder::class, function (PaymentReminder $mail) use ($order) {
- $html = $mail->render();
-
- return $mail->hasTo('aminah@example.com')
- && $mail->envelope()->subject === "Complete your payment for order #{$order->number} - {$this->store->name}"
- && str_contains($html, route('stores.preview.order.pay', [$this->store->slug, $order->public_id]))
- && str_contains($html, 'You can pay until');
- });
- $this->assertNotNull($order->fresh()->payment_reminded_at);
-
- // Never twice.
- $this->travel(2)->hours();
- $this->runCheck();
- Mail::assertSent(PaymentReminder::class, 1);
- }
-
- public function test_no_reminder_when_the_shopper_paid()
- {
- $order = $this->placeOrder();
- $this->gateway->pay($order);
-
- $this->travel(31)->minutes();
- $result = $this->runCheck();
-
- $this->assertSame(1, $result['paid']);
- $this->assertSame(0, $result['reminded']);
- Mail::assertNotSent(PaymentReminder::class);
- }
-
- public function test_no_reminder_when_the_shopper_ordered_again_since()
- {
- $first = $this->placeOrder();
- $this->travel(5)->minutes();
- $second = $this->placeOrder();
- $this->gateway->pay($second);
- $this->get(route('stores.preview.order', [$this->store->slug, $second->public_id]));
-
- $this->travel(40)->minutes();
- $this->runCheck();
-
- Mail::assertNotSent(PaymentReminder::class);
- $this->assertNull($first->fresh()->payment_reminded_at);
- }
-
- public function test_no_reminder_while_the_provider_cannot_say_whether_they_paid()
- {
- $order = $this->placeOrder();
- unset($this->gateway->payments[$order->payment_checkout_id]);
-
- $this->travel(31)->minutes();
- $this->assertSame(1, $this->runCheck()['failed']);
-
- Mail::assertNotSent(PaymentReminder::class);
- $this->assertNull($order->fresh()->payment_reminded_at);
- }
-
- public function test_the_reminder_is_written_in_malay_for_malay_stores()
- {
- $this->store->forceFill(['language' => 'ms'])->save();
- $order = $this->placeOrder();
-
- $this->travel(31)->minutes();
- $this->runCheck();
-
- Mail::assertSent(PaymentReminder::class, fn (PaymentReminder $mail) => $mail->envelope()->subject === "Lengkapkan bayaran untuk pesanan #{$order->number} - {$this->store->name}"
- && str_contains($mail->render(), 'Anda boleh membayar sehingga'));
- }
-
- public function test_merchants_see_when_an_order_is_waiting_for_payment()
- {
- $order = $this->placeOrder();
-
- $this->get(route('stores.orders.show', [$this->store->slug, $order->id]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('order.status', 'pending')
- ->where('order.payment_expires_at', $order->payment_expires_at->toIso8601String())
- ->where('order.payment_reminded_at', null));
- }
- }
|