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
This commit is contained in:
Nyan Lin Paing
2026-08-08 22:42:16 +07:00
parent e0bcc5f81a
commit 4737838021
28 changed files with 1407 additions and 1 deletions
@@ -0,0 +1,54 @@
<?php
namespace Modules\Payment\Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Modules\Booking\Models\Booking;
use Modules\Payment\Enums\PaymentMethod;
use Modules\Payment\Enums\PaymentStatus;
use Modules\Payment\Models\Payment;
/**
* @extends Factory<Payment>
*/
class PaymentFactory extends Factory
{
protected $model = Payment::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'booking_id' => Booking::factory(),
'gateway' => PaymentMethod::KbzMiniApp,
'status' => PaymentStatus::Pending,
'amount' => $this->faker->randomFloat(2, 5000, 50000),
'currency' => 'MMK',
'gateway_transaction_id' => Str::uuid()->toString(),
'gateway_payload' => null,
'initiated_at' => now(),
'completed_at' => null,
];
}
public function completed(): static
{
return $this->state(fn (array $attributes): array => [
'status' => PaymentStatus::Completed,
'completed_at' => now(),
]);
}
public function failed(): static
{
return $this->state(fn (array $attributes): array => [
'status' => PaymentStatus::Failed,
'completed_at' => now(),
]);
}
}
@@ -0,0 +1,52 @@
<?php
namespace Modules\Payment\Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Modules\Payment\Enums\RefundStatus;
use Modules\Payment\Models\Payment;
use Modules\Payment\Models\Refund;
/**
* @extends Factory<Refund>
*/
class RefundFactory extends Factory
{
protected $model = Refund::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'payment_id' => Payment::factory()->completed(),
'status' => RefundStatus::Pending,
'amount' => $this->faker->randomFloat(2, 1000, 50000),
'reason' => $this->faker->sentence(),
'gateway_refund_id' => null,
'gateway_payload' => null,
'requested_by' => null,
'requested_at' => now(),
'completed_at' => null,
];
}
public function completed(): static
{
return $this->state(fn (array $attributes): array => [
'status' => RefundStatus::Completed,
'completed_at' => now(),
]);
}
public function failed(): static
{
return $this->state(fn (array $attributes): array => [
'status' => RefundStatus::Failed,
'completed_at' => now(),
]);
}
}
@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* One row per payment attempt against a Booking a Booking can have
* more than one row here if an earlier attempt failed and the customer
* retried (domain.md §1, §6).
*/
public function up(): void
{
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->foreignId('booking_id')->constrained('bookings')->cascadeOnDelete();
$table->string('gateway');
$table->string('status')->default('pending');
$table->decimal('amount', 10, 2);
$table->string('currency')->default('MMK');
$table->string('gateway_transaction_id')->nullable()->index();
$table->jsonb('gateway_payload')->nullable();
$table->timestamp('initiated_at')->nullable();
$table->timestamp('completed_at')->nullable();
$table->timestamps();
$table->index(['booking_id', 'status']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('payments');
}
};
@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* A Refund reverses a specific successful Payment, not the Booking
* directly a Payment can have more than one Refund row for partial
* refunds (domain.md §1, §6).
*/
public function up(): void
{
Schema::create('refunds', function (Blueprint $table) {
$table->id();
$table->foreignId('payment_id')->constrained('payments')->cascadeOnDelete();
$table->string('status')->default('pending');
$table->decimal('amount', 10, 2);
$table->text('reason');
$table->string('gateway_refund_id')->nullable()->index();
$table->jsonb('gateway_payload')->nullable();
$table->foreignId('requested_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamp('requested_at')->nullable();
$table->timestamp('completed_at')->nullable();
$table->timestamps();
$table->index(['payment_id', 'status']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('refunds');
}
};