Files
Nyan Lin Paing 4737838021 Add Payment module: contracts, payments/refunds tables, KBZ gateway, factory, orchestrator (T5.1-T5.7)
- T5.1 PaymentGatewayInterface, DTOs, PaymentMethod/PaymentStatus/RefundStatus enums
- T5.2 payments/refunds tables, models, factories
- T5.3-T5.5 KbzMiniAppGateway: initiate()/verify()/refund(), ported KBZ signing scheme,
  wired refund_amount through for partial refunds, mTLS options for refund
- T5.6 PaymentGatewayFactory resolving gateways by PaymentMethod
- T5.7 PaymentService orchestrator delegating to the resolved gateway
2026-08-08 22:42:16 +07:00

73 lines
3.1 KiB
PHP

<?php
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Modules\Payment\Enums\PaymentStatus;
use Modules\Payment\Gateways\KbzMiniAppGateway;
use Modules\Payment\Support\KbzSignature;
$config = [
'app_id' => 'APPID123',
'merchant_code' => 'MERCH001',
'merchant_key' => 'test-merchant-key',
'base_url' => 'https://kbz.test/gateway',
'notify_url' => 'https://app.test/api/v1/webhooks/kbz',
];
test('verify posts a correctly signed queryorder request keyed by our own merch_order_id', function () use ($config) {
Http::fake(['kbz.test/*' => Http::response(['Response' => ['trade_status' => 'PAY_SUCCESS']])]);
(new KbzMiniAppGateway($config))->verify('EVB-FIXTURE-001');
Http::assertSent(function ($request) {
$body = $request->data()['Request'];
return $request->url() === 'https://kbz.test/gateway'
&& $body['method'] === 'kbz.payment.queryorder'
&& $body['sign_type'] === 'SHA256'
&& $body['biz_content']['appid'] === 'APPID123'
&& $body['biz_content']['merch_code'] === 'MERCH001'
&& $body['biz_content']['merch_order_id'] === 'EVB-FIXTURE-001'
&& ! array_key_exists('total_amount', $body['biz_content'])
&& $body['sign'] === KbzSignature::sign($body, 'test-merchant-key');
});
});
test('verify maps PAY_SUCCESS to a completed PaymentResultData', function () use ($config) {
Http::fake(['kbz.test/*' => Http::response(['Response' => ['trade_status' => 'PAY_SUCCESS', 'mm_order_id' => 'MM123']])]);
$result = (new KbzMiniAppGateway($config))->verify('EVB-FIXTURE-001');
expect($result->status)->toBe(PaymentStatus::Completed)
->and($result->gatewayTransactionId)->toBe('EVB-FIXTURE-001')
->and($result->gatewayPayload)->toBe(['trade_status' => 'PAY_SUCCESS', 'mm_order_id' => 'MM123']);
});
test('verify maps WAIT_PAY to a pending PaymentResultData', function () use ($config) {
Http::fake(['kbz.test/*' => Http::response(['Response' => ['trade_status' => 'WAIT_PAY']])]);
$result = (new KbzMiniAppGateway($config))->verify('EVB-FIXTURE-001');
expect($result->status)->toBe(PaymentStatus::Pending);
});
test('verify maps any other/unrecognized trade_status to a failed PaymentResultData', function () use ($config) {
Http::fake(['kbz.test/*' => Http::response(['Response' => ['trade_status' => 'PAY_ERROR']])]);
$result = (new KbzMiniAppGateway($config))->verify('EVB-FIXTURE-001');
expect($result->status)->toBe(PaymentStatus::Failed)
->and($result->message)->toBe('PAY_ERROR');
});
test('verify returns a failed PaymentResultData when the connection fails', function () use ($config) {
Http::fake(['kbz.test/*' => fn () => throw new ConnectionException('Connection refused')]);
$result = (new KbzMiniAppGateway($config))->verify('EVB-FIXTURE-001');
expect($result->status)->toBe(PaymentStatus::Failed)
->and($result->gatewayTransactionId)->toBe('EVB-FIXTURE-001')
->and($result->gatewayPayload)->toBe([])
->and($result->message)->toBe('Connection refused');
});