Files
famous-ly4-ev/app-modules/payment/tests/Feature/KbzMiniAppGatewayHandleWebhookTest.php
Nyan Lin Paing d19a14a45e Complete Payment module: initiate/webhook/confirm/refund actions, Filament resources (T5.8-T5.13)
- InitiatePaymentAction + POST /api/v1/payments/{booking}/initiate
- Generic KBZ webhook (POST /api/v1/webhooks/{method}/{encryptBookingId?}),
  signature verification per KBZ's real callback spec, PaymentGatewayInterface::handleWebhook()
- ConfirmPaymentAction: idempotent confirmation, PaymentCompleted/PaymentFailed events,
  MarkBookingPaid listener
- RefundBookingAction + POST /api/v1/bookings/{booking}/refund: partial refunds validated
  against remaining balance, RefundProcessed event, MarkBookingRefunded listener
- CancelBookingAction now refunds confirmed bookings instead of rejecting; BookingPolicy::cancel
  requires process_refunds for confirmed bookings
- PaymentPlugin + PaymentResource/RefundResource Filament admin UI (read-only payments,
  refund list + Process action)
- Booking detail page now shows related payments
- Fix CACHE_STORE mismatch (database -> redis) so tagged route caching works
- CLAUDE.md: never run migrate:fresh/migrate:refresh/db:wipe on dev without being asked
2026-08-09 16:20:21 +07:00

80 lines
3.4 KiB
PHP

<?php
use Modules\Payment\Enums\PaymentStatus;
use Modules\Payment\Exceptions\InvalidWebhookSignatureException;
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',
];
/**
* @return array<string, mixed>
*/
$signedKbzNotification = function (array $overrides, string $merchantKey): array {
$notification = array_merge([
'appid' => 'APPID123',
'notify_time' => '1576842150',
'merch_code' => 'MERCH001',
'merch_order_id' => 'EVB-FIXTURE-001-1',
'mm_order_id' => '01001814070006560257',
'trans_currency' => 'MMK',
'total_amount' => '15000',
'trade_status' => 'PAY_SUCCESS',
'trans_end_time' => '1576834704',
'nonce_str' => '513ba55344ad44c8b69465aae66f7703',
'sign_type' => 'SHA256',
], $overrides);
$notification['sign'] = KbzSignature::sign($notification, $merchantKey);
return $notification;
};
test('handleWebhook maps a validly signed PAY_SUCCESS notification to a completed PaymentResultData', function () use ($config, $signedKbzNotification) {
$notification = $signedKbzNotification([], $config['merchant_key']);
$result = (new KbzMiniAppGateway($config))->handleWebhook(['Request' => $notification]);
expect($result->status)->toBe(PaymentStatus::Completed)
->and($result->gatewayTransactionId)->toBe('EVB-FIXTURE-001-1')
->and($result->gatewayPayload)->toBe($notification);
});
test('handleWebhook maps a validly signed WAIT_PAY notification to a pending PaymentResultData', function () use ($config, $signedKbzNotification) {
$notification = $signedKbzNotification(['trade_status' => 'WAIT_PAY'], $config['merchant_key']);
$result = (new KbzMiniAppGateway($config))->handleWebhook(['Request' => $notification]);
expect($result->status)->toBe(PaymentStatus::Pending);
});
test('handleWebhook throws InvalidWebhookSignatureException when the sign does not match', function () use ($config, $signedKbzNotification) {
$notification = $signedKbzNotification([], $config['merchant_key']);
$notification['sign'] = 'not-the-real-signature';
expect(fn () => (new KbzMiniAppGateway($config))->handleWebhook(['Request' => $notification]))
->toThrow(InvalidWebhookSignatureException::class);
});
test('handleWebhook throws InvalidWebhookSignatureException when signed with the wrong merchant key', function () use ($config, $signedKbzNotification) {
$notification = $signedKbzNotification([], 'a-different-merchant-key');
expect(fn () => (new KbzMiniAppGateway($config))->handleWebhook(['Request' => $notification]))
->toThrow(InvalidWebhookSignatureException::class);
});
test('handleWebhook throws InvalidWebhookSignatureException when the sign field is missing entirely', function () use ($config) {
expect(fn () => (new KbzMiniAppGateway($config))->handleWebhook(['Request' => ['trade_status' => 'PAY_SUCCESS']]))
->toThrow(InvalidWebhookSignatureException::class);
});
test('handleWebhook throws InvalidWebhookSignatureException when the Request key is missing entirely', function () use ($config) {
expect(fn () => (new KbzMiniAppGateway($config))->handleWebhook([]))
->toThrow(InvalidWebhookSignatureException::class);
});