What the fake HitPay says about the payment page. */ private array $hitpay = []; private bool $hitpayFaked = false; /** @var list Methods the fake HitPay says are switched off in the merchant's account. */ private array $switchedOff = []; protected function setUp(): void { parent::setUp(); Mail::fake(); Http::preventStrayRequests(); config(['services.hitpay.sandbox' => true]); $this->user = User::factory()->create(); $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']); $this->store->plan = SamplePlan::for($this->store); $this->store->save(); $this->actingAs($this->user); } private function connectHitPay(array $methods = HitPayAccount::METHODS): HitPayAccount { return $this->store->hitpayAccount()->create(['api_key' => 'hp_test_key_123456', 'methods' => $methods]); } /** * HitPay's answers: a new payment page, and that page with whatever payments were made on it. * Can be called again to change the answers. * * @param list> $payments */ private function fakeHitPay(string $status = 'pending', array $payments = [], string $amount = '50.00', string $currency = 'myr'): void { $this->hitpay = ['status' => $status, 'payments' => $payments, 'amount' => $amount, 'currency' => $currency]; if ($this->hitpayFaked) { return; } $this->hitpayFaked = true; Http::fake(function (HttpRequest $request) { if ($request->url() === self::API.'/payment-requests') { // Like HitPay: one error per method that is switched off, by its place in the list. $errors = []; foreach (array_values((array) $request['payment_methods']) as $i => $method) { if (in_array($method, $this->switchedOff, true)) { $errors["payment_methods.{$i}"] = ["The selected payment method ({$method}) is unavailable for your account or the currency is invalid. Please check the payment method you selected."]; } } if ($errors !== []) { return Http::response(['error_code' => 'validation_error', 'message' => 'The given data was invalid.', 'errors' => $errors], 422); } return Http::response([ 'id' => 'hp_req_1', 'url' => 'https://securecheckout.sandbox.hit-pay.com/payment-request/@kedai-ali/hp_req_1/checkout', 'status' => 'pending', 'amount' => $this->hitpay['amount'], 'currency' => $this->hitpay['currency'], ]); } if ($request->url() === self::API.'/payment-requests/hp_req_1') { return Http::response(['id' => 'hp_req_1', ...$this->hitpay]); } return Http::response(['message' => 'Not found.'], 404); }); } /** RM 42 of products and the default RM 8 shipping to Selangor: RM 50. */ private function placeOrder(string $payWith = 'hitpay') { $product = Product::factory()->for($this->store)->create(['name' => 'Sambal', 'price' => 4200, 'stock' => 10]); $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]); return $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', 'pay_with' => $payWith, ]); } public function test_a_merchant_connects_hitpay_with_a_key_that_hitpay_accepts() { // HitPay refuses the first key and accepts the second. Http::fake([self::API.'/account-status' => Http::sequence() ->push(['message' => 'Unauthenticated.'], 401) ->push(['verification' => ['overall_status' => 'verified']])]); $this->post(route('stores.hitpay.connect', $this->store->slug), ['api_key' => 'wrong_key_123456']) ->assertSessionHasErrors('api_key'); $this->assertNull($this->store->fresh()->hitpayAccount); $this->post(route('stores.hitpay.connect', $this->store->slug), ['api_key' => ' hp_test_key_123456 ']) ->assertSessionHasNoErrors() ->assertRedirect(route('stores.settings', $this->store->slug)); $account = $this->store->fresh()->hitpayAccount; $this->assertSame('hp_test_key_123456', $account->api_key); $this->assertSame(['duitnow', 'touch_n_go', 'shopee_pay'], $account->methods); // Saved encrypted. $this->assertStringNotContainsString('hp_test_key_123456', (string) DB::table('hitpay_accounts')->value('api_key')); Http::assertSent(fn (HttpRequest $request) => $request->url() === self::API.'/account-status' && $request->hasHeader('X-BUSINESS-API-KEY', 'hp_test_key_123456')); // Settings says it is connected, and never sends the key back to the page. $this->get(route('stores.settings', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('hitpay.connected', true) ->where('hitpay.methods', ['duitnow', 'touch_n_go', 'shopee_pay']) ->where('hitpay.sandbox', true)) ->assertDontSee('hp_test_key_123456'); } public function test_hitpay_being_down_while_connecting_says_so() { Http::fake([self::API.'/account-status' => Http::failedConnection()]); $this->post(route('stores.hitpay.connect', $this->store->slug), ['api_key' => 'hp_test_key_123456']) ->assertSessionHasErrors(['api_key' => 'HitPay could not be reached. Please try again in a moment.']); $this->assertNull($this->store->fresh()->hitpayAccount); } public function test_the_merchant_chooses_which_hitpay_methods_shoppers_see_and_can_disconnect() { $this->connectHitPay(); $this->put(route('stores.hitpay.update', $this->store->slug), ['methods' => ['shopee_pay', 'duitnow', 'duitnow']]) ->assertSessionHasNoErrors(); $this->assertSame(['duitnow', 'shopee_pay'], $this->store->fresh()->hitpayAccount->methods); $this->put(route('stores.hitpay.update', $this->store->slug), ['methods' => []])->assertSessionHasErrors('methods'); $this->put(route('stores.hitpay.update', $this->store->slug), ['methods' => ['card']])->assertSessionHasErrors('methods.0'); $this->assertSame(['duitnow', 'shopee_pay'], $this->store->fresh()->hitpayAccount->methods); $this->delete(route('stores.hitpay.disconnect', $this->store->slug))->assertRedirect(route('stores.settings', $this->store->slug)); $this->assertNull($this->store->fresh()->hitpayAccount); } public function test_another_merchants_hitpay_cannot_be_changed() { $other = Store::factory()->create(); $other->hitpayAccount()->create(['api_key' => 'theirs_123456', 'methods' => HitPayAccount::METHODS]); $this->post(route('stores.hitpay.connect', $other->slug), ['api_key' => 'hp_test_key_123456'])->assertNotFound(); $this->put(route('stores.hitpay.update', $other->slug), ['methods' => ['duitnow']])->assertNotFound(); $this->delete(route('stores.hitpay.disconnect', $other->slug))->assertNotFound(); $this->assertNotNull($other->fresh()->hitpayAccount); } public function test_shoppers_choose_between_stripe_and_hitpay_at_checkout() { $this->connectHitPay(['duitnow', 'touch_n_go']); $this->post(route('stores.preview.cart.add', $this->store->slug), [ 'product_id' => Product::factory()->for($this->store)->create(['price' => 4200])->id, 'quantity' => 1, ]); $this->get(route('stores.preview.checkout', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('view.takingOrders', true) ->where('view.payWays', [ ['key' => 'fake', 'methods' => ['card', 'fpx', 'grabpay']], ['key' => 'hitpay', 'methods' => ['duitnow', 'touch_n_go']], ])); // A store with only HitPay can take orders too. $stripe = app(PaymentGateway::class); $this->assertInstanceOf(FakeGateway::class, $stripe); $stripe->takingPayments = false; $this->get(route('stores.preview.checkout', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('view.takingOrders', true) ->where('view.payWays', [['key' => 'hitpay', 'methods' => ['duitnow', 'touch_n_go']]])); } public function test_paying_with_hitpay_opens_a_hitpay_page_in_the_merchants_account() { $this->connectHitPay(); $this->fakeHitPay(); $response = $this->placeOrder(); $order = Order::firstOrFail(); $response->assertRedirect('https://securecheckout.sandbox.hit-pay.com/payment-request/@kedai-ali/hp_req_1/checkout'); $this->assertSame('hitpay', $order->payment_provider); $this->assertSame('hp_req_1', $order->payment_checkout_id); $this->assertSame(5000, $order->total); Http::assertSent(function (HttpRequest $request) use ($order) { return $request->url() === self::API.'/payment-requests' && $request->method() === 'POST' && $request->hasHeader('X-BUSINESS-API-KEY', 'hp_test_key_123456') && $request['amount'] === '50.00' && $request['currency'] === 'MYR' && $request['payment_methods'] === ['duitnow', 'touch_n_go', 'shopee_pay'] && $request['phone'] === '+60123456789' && $request['reference_number'] === '1001' && $request['purpose'] === 'Kedai Ali order #1001' && $request['redirect_url'] === route('stores.preview.order', [$this->store->slug, $order->public_id]) && $request['send_email'] === 'false' // A development machine has no public https address for HitPay to call. && ! isset($request['webhook']); }); } public function test_on_a_public_server_hitpay_is_told_where_to_send_news_about_the_order() { config(['app.url' => 'https://shop.example.com']); $this->connectHitPay(); $this->fakeHitPay(); $this->placeOrder(); $order = Order::firstOrFail(); Http::assertSent(fn (HttpRequest $request) => $request->url() === self::API.'/payment-requests' && $request['webhook'] === 'https://shop.example.com/hitpay/webhook/'.$order->public_id); } public function test_hitpay_being_down_leaves_no_order_behind() { $this->connectHitPay(); Http::fake([self::API.'/payment-requests' => Http::response(['message' => 'The selected payment methods is invalid.'], 422)]); $this->placeOrder()->assertSessionHasErrors('cart'); $this->assertSame(0, Order::count()); } public function test_a_way_that_is_not_offered_falls_back_to_stripe() { // HitPay is not connected, so the choice is ignored. $this->placeOrder('hitpay')->assertRedirect('https://pay.test/cs_fake_1'); $this->assertSame('fake', Order::firstOrFail()->payment_provider); } public function test_the_order_is_paid_once_hitpay_says_so_when_the_shopper_comes_back() { $this->connectHitPay(); $this->fakeHitPay(); $this->placeOrder(); $order = Order::firstOrFail(); // Not paid yet. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])) ->assertInertia(fn (Assert $page) => $page->where('view.order.status', 'pending')); $this->assertFalse($order->fresh()->isPaid()); $this->fakeHitPay('completed', [['id' => 'hp_pay_9', 'status' => 'succeeded', 'amount' => '50.00', 'payment_type' => 'duitnow']]); $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])) ->assertInertia(fn (Assert $page) => $page->where('view.order.status', 'paid')->where('cart.count', 0)); $order->refresh(); $this->assertTrue($order->isPaid()); $this->assertSame('hp_pay_9', $order->payment_reference); $this->assertSame(9, Product::firstOrFail()->stock); Mail::assertSent(OrderConfirmation::class, 1); } public function test_a_payment_of_the_wrong_amount_or_currency_does_not_count() { $this->connectHitPay(); $this->fakeHitPay(); $this->placeOrder(); $order = Order::firstOrFail(); $this->fakeHitPay('completed', [['id' => 'hp_pay_9', 'status' => 'succeeded', 'amount' => '5.00']], amount: '5.00'); $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])); $this->assertFalse($order->fresh()->isPaid()); $this->fakeHitPay('completed', [['id' => 'hp_pay_9', 'status' => 'succeeded', 'amount' => '50.00']], currency: 'sgd'); $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])); $this->assertFalse($order->fresh()->isPaid()); Mail::assertNothingSent(); } public function test_a_message_from_hitpay_is_checked_with_hitpay_before_the_order_counts_as_paid() { $this->connectHitPay(); $this->fakeHitPay(); $this->placeOrder(); $order = Order::firstOrFail(); // A message claiming the order was paid, while HitPay itself says it was not. $this->post(route('hitpay.webhook', $order->public_id), ['status' => 'completed', 'payment_request_id' => 'hp_req_1']) ->assertOk(); $this->assertFalse($order->fresh()->isPaid()); // The shopper paid and closed the browser: HitPay's message gets the order marked paid. $this->fakeHitPay('completed', [['id' => 'hp_pay_9', 'status' => 'succeeded', 'amount' => '50.00']]); $this->post(route('hitpay.webhook', $order->public_id))->assertOk(); $this->post(route('hitpay.webhook', $order->public_id))->assertOk(); $this->assertTrue($order->fresh()->isPaid()); Mail::assertSent(OrderConfirmation::class, 1); // Unknown orders are ignored. $this->post(route('hitpay.webhook', 'not-an-order'))->assertOk(); } public function test_methods_switched_off_in_hitpay_are_left_out_and_the_merchant_is_told() { $this->connectHitPay(); $this->fakeHitPay(); $this->switchedOff = ['duitnow', 'touch_n_go']; // The shopper still gets a payment page, with the method that works. $this->placeOrder()->assertRedirect('https://securecheckout.sandbox.hit-pay.com/payment-request/@kedai-ali/hp_req_1/checkout'); Http::assertSent(fn (HttpRequest $request) => $request->url() === self::API.'/payment-requests' && $request['payment_methods'] === ['shopee_pay']); $account = $this->store->fresh()->hitpayAccount; $this->assertSame(['duitnow', 'touch_n_go'], $account->refused_methods); $this->assertNotNull($account->refused_at); // The next shoppers are only offered what works, and the merchant sees why. $this->get(route('stores.preview.checkout', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('view.payWays.1', ['key' => 'hitpay', 'methods' => ['shopee_pay']])); $this->get(route('stores.settings', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('hitpay.refused', ['duitnow', 'touch_n_go'])->whereNot('hitpay.refused_at', null)); // An hour later they are tried again, in case the merchant switched them on in HitPay. $this->travel(61)->minutes(); $this->switchedOff = []; $this->get(route('stores.preview.checkout', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('view.payWays.1.methods', ['duitnow', 'touch_n_go', 'shopee_pay'])); // This time HitPay takes them all, so the warning goes away. $this->placeOrder(); $this->assertNull($this->store->fresh()->hitpayAccount->refused_methods); } public function test_saving_the_methods_tries_refused_ones_again_at_once() { $this->connectHitPay()->refused(['duitnow']); $this->put(route('stores.hitpay.update', $this->store->slug), ['methods' => ['duitnow', 'shopee_pay']]); $account = $this->store->fresh()->hitpayAccount; $this->assertNull($account->refused_methods); $this->assertSame(['duitnow', 'shopee_pay'], $account->offeredMethods()); } public function test_when_hitpay_refuses_every_method_the_shopper_is_told_and_hitpay_is_hidden_for_a_while() { $this->connectHitPay(['duitnow']); $this->fakeHitPay(); $this->switchedOff = ['duitnow']; $this->placeOrder()->assertSessionHasErrors('cart'); $this->assertSame(0, Order::count()); // Only Stripe is offered until the merchant fixes it (or an hour passes). $this->get(route('stores.preview.checkout', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('view.payWays', [['key' => 'fake', 'methods' => ['card', 'fpx', 'grabpay']]])); } }