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(): 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' => 'aminah@example.com', '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_a_payment_whose_news_was_missed_is_found_and_the_order_marked_paid() { $order = $this->placeOrder(); // The shopper paid, then closed the phone; no webhook came. $this->gateway->pay($order); // Too new to ask about yet. $this->assertSame(0, $this->runCheck()['checked']); $this->assertFalse($order->fresh()->isPaid()); $this->travel(3)->minutes(); $this->artisan('orders:check-payments') ->expectsOutputToContain('Checked 1 unpaid orders: 1 were paid') ->assertSuccessful(); $order->refresh(); $this->assertTrue($order->isPaid()); $this->assertNotNull($order->payment_checked_at); $this->assertSame(9, Product::firstOrFail()->stock); Mail::assertSent(OrderConfirmation::class, 1); // Paid orders are never asked about again. $this->travel(10)->minutes(); $this->assertSame(0, $this->runCheck()['checked']); } public function test_new_orders_are_asked_about_often_and_older_ones_rarely() { $order = $this->placeOrder(); $this->travel(3)->minutes(); $this->assertSame(1, $this->runCheck()['checked']); // Every 5 minutes in the first quarter of an hour. $this->travel(2)->minutes(); $this->assertSame(0, $this->runCheck()['checked']); $this->travel(3)->minutes(); $this->assertSame(1, $this->runCheck()['checked']); // Every 15 minutes until an hour. $this->travel(22)->minutes(); // 30 minutes old $this->assertSame(1, $this->runCheck()['checked']); $this->travel(10)->minutes(); $this->assertSame(0, $this->runCheck()['checked']); $this->travel(5)->minutes(); $this->assertSame(1, $this->runCheck()['checked']); // Then every hour. $this->travel(2)->hours(); $this->assertSame(1, $this->runCheck()['checked']); $this->travel(30)->minutes(); $this->assertSame(0, $this->runCheck()['checked']); $this->assertFalse($order->fresh()->isPaid()); } public function test_a_day_later_an_unpaid_order_becomes_not_paid_and_is_left_alone() { $order = $this->placeOrder(); $this->travel(26)->hours(); $result = $this->runCheck(); $this->assertSame(0, $result['checked']); $this->assertSame(1, $result['given_up']); $this->assertSame(OrderStatus::Unpaid, $order->fresh()->status); $this->assertSame(0, $this->runCheck()['given_up']); $this->get(route('stores.orders.index', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('orders.0.status', 'unpaid')->where('orders.0.status_label', 'Not paid')); // A payment that still arrives (the shopper opens the order page) counts. $this->gateway->pay($order); $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])) ->assertInertia(fn (Assert $page) => $page->where('view.order.status', 'paid')); $this->assertTrue($order->fresh()->isPaid()); } public function test_a_provider_that_cannot_be_reached_does_not_stop_the_other_orders() { $first = $this->placeOrder(); $second = $this->placeOrder(); $this->gateway->pay($second); // The provider no longer knows the first payment page. unset($this->gateway->payments[$first->payment_checkout_id]); $this->travel(3)->minutes(); $result = $this->runCheck(); $this->assertSame(['checked' => 2, 'paid' => 1, 'failed' => 1, 'reminded' => 0, 'given_up' => 0], $result); $this->assertTrue($second->fresh()->isPaid()); // Not asked again on the very next run. $this->travel(1)->minutes(); $this->assertSame(0, $this->runCheck()['checked']); } public function test_the_check_is_scheduled_every_five_minutes() { $this->artisan('schedule:list') ->expectsOutputToContain('*/5 * * * * php artisan orders:check-payments') ->assertSuccessful(); } }