diff --git a/packages/payment/composer.json b/packages/payment/composer.json index b22d4c50..a01d11ee 100644 --- a/packages/payment/composer.json +++ b/packages/payment/composer.json @@ -27,7 +27,8 @@ "extra": { "laravel": { "providers": [ - "RedJasmine\\Payment\\PaymentPackageServiceProvider" + "RedJasmine\\Payment\\PaymentPackageServiceProvider", + "RedJasmine\\Payment\\Application\\PaymentApplicationServiceProvider" ], "aliases": { diff --git a/packages/payment/database/migrations/create_payment_apps_table.php b/packages/payment/database/migrations/create_payment_apps_table.php index db8a0839..15a57ff6 100644 --- a/packages/payment/database/migrations/create_payment_apps_table.php +++ b/packages/payment/database/migrations/create_payment_apps_table.php @@ -7,7 +7,7 @@ return new class extends Migration { public function up() : void { - Schema::create(config('red-jasmine-product.tables.prefix') . 'payment_apps', function (Blueprint $table) { + Schema::create(config('red-jasmine-payment.tables.prefix') . 'payment_apps', function (Blueprint $table) { $table->unsignedBigInteger('id')->primary()->comment('ID'); $table->unsignedBigInteger('merchant_id')->comment('商户ID'); $table->string('name')->comment('名称'); @@ -23,6 +23,6 @@ public function up() : void public function down() : void { - Schema::dropIfExists(config('red-jasmine-product.tables.prefix') . 'payment_apps'); + Schema::dropIfExists(config('red-jasmine-payment.tables.prefix') . 'payment_apps'); } }; diff --git a/packages/payment/database/migrations/create_payment_merchants_table.php b/packages/payment/database/migrations/create_payment_merchants_table.php index e333d192..4fcd55a3 100644 --- a/packages/payment/database/migrations/create_payment_merchants_table.php +++ b/packages/payment/database/migrations/create_payment_merchants_table.php @@ -4,6 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use RedJasmine\Payment\Domain\Models\Enums\MerchantTypeEnum; +use RedJasmine\Payment\Domain\Models\Enums\MerchantStatusEnum; return new class extends Migration { public function up() : void @@ -15,8 +16,8 @@ public function up() : void $table->string('short_name')->comment('短名称'); $table->string('type')->comment(MerchantTypeEnum::comments('类型')); $table->unsignedBigInteger('isv_id')->nullable()->comment('服务商ID'); - $table->string('status')->comment('状态'); - $table->string('remarks')->comment('备注'); + $table->string('status')->comment(MerchantStatusEnum::comments('状态')); + $table->string('remarks')->nullable()->comment('备注'); $table->nullableMorphs('creator'); $table->nullableMorphs('updater'); $table->timestamps(); diff --git a/packages/payment/database/migrations/create_payment_refunds_table.php b/packages/payment/database/migrations/create_payment_refunds_table.php index 67cf28d2..518680eb 100644 --- a/packages/payment/database/migrations/create_payment_refunds_table.php +++ b/packages/payment/database/migrations/create_payment_refunds_table.php @@ -7,7 +7,7 @@ return new class extends Migration { public function up() : void { - Schema::create(config('red-jasmine-payment.tables.prefix') .'payment_refunds', function (Blueprint $table) { + Schema::create(config('red-jasmine-payment.tables.prefix') . 'payment_refunds', function (Blueprint $table) { $table->id(); $table->string('currency')->comment('货币'); @@ -20,6 +20,6 @@ public function up() : void public function down() : void { - Schema::dropIfExists(config('red-jasmine-payment0000000000000000.tables.prefix') .'payment_refunds'); + Schema::dropIfExists(config('red-jasmine-payment.tables.prefix') . 'payment_refunds'); } }; diff --git a/packages/payment/src/Application/Commands/Merchant/MerchantCreateCommand.php b/packages/payment/src/Application/Commands/Merchant/MerchantCreateCommand.php new file mode 100644 index 00000000..12d41c81 --- /dev/null +++ b/packages/payment/src/Application/Commands/Merchant/MerchantCreateCommand.php @@ -0,0 +1,10 @@ +app->bind(MerchantRepositoryInterface::class, MerchantRepository::class); + $this->app->bind(MerchantReadRepositoryInterface::class, MerchantReadRepository::class); + + } + +} diff --git a/packages/payment/src/Application/Services/CommandHandlers/MerchantCreateCommandHandle.php b/packages/payment/src/Application/Services/CommandHandlers/MerchantCreateCommandHandle.php new file mode 100644 index 00000000..bfe5b84a --- /dev/null +++ b/packages/payment/src/Application/Services/CommandHandlers/MerchantCreateCommandHandle.php @@ -0,0 +1,48 @@ +beginDatabaseTransaction(); + + try { + + $model = app(MerchantTransformer::class)->transform($command); + + $this->repository->store($model); + + $this->commitDatabaseTransaction(); + } catch (AbstractException $exception) { + $this->rollBackDatabaseTransaction(); + throw $exception; + } catch (Throwable $throwable) { + $this->rollBackDatabaseTransaction(); + throw $throwable; + } + + return $model; + + } + +} diff --git a/packages/payment/src/Application/Services/CommandHandlers/MerchantSetStatusCommandHandle.php b/packages/payment/src/Application/Services/CommandHandlers/MerchantSetStatusCommandHandle.php new file mode 100644 index 00000000..04d6da1c --- /dev/null +++ b/packages/payment/src/Application/Services/CommandHandlers/MerchantSetStatusCommandHandle.php @@ -0,0 +1,45 @@ +beginDatabaseTransaction(); + + try { + + $model = $this->repository->find($command->id); + $model->setStatus($command->status); + $this->repository->update($model); + + $this->commitDatabaseTransaction(); + } catch (AbstractException $exception) { + $this->rollBackDatabaseTransaction(); + throw $exception; + } catch (Throwable $throwable) { + $this->rollBackDatabaseTransaction(); + throw $throwable; + } + + + } + +} diff --git a/packages/payment/src/Application/Services/CommandHandlers/MerchantUpdateCommandHandle.php b/packages/payment/src/Application/Services/CommandHandlers/MerchantUpdateCommandHandle.php new file mode 100644 index 00000000..4442dee3 --- /dev/null +++ b/packages/payment/src/Application/Services/CommandHandlers/MerchantUpdateCommandHandle.php @@ -0,0 +1,50 @@ +beginDatabaseTransaction(); + + try { + $model = $this->repository->find($command->id); + + $model = app(MerchantTransformer::class)->transform($command, $model); + + $this->repository->update($model); + + $this->commitDatabaseTransaction(); + } catch (AbstractException $exception) { + $this->rollBackDatabaseTransaction(); + throw $exception; + } catch (Throwable $throwable) { + $this->rollBackDatabaseTransaction(); + throw $throwable; + } + + return $model; + + } + +} diff --git a/packages/payment/src/Application/Services/MerchantCommandService.php b/packages/payment/src/Application/Services/MerchantCommandService.php new file mode 100644 index 00000000..1d54a3e1 --- /dev/null +++ b/packages/payment/src/Application/Services/MerchantCommandService.php @@ -0,0 +1,38 @@ + MerchantCreateCommandHandle::class, + 'setStatus' => MerchantSetStatusCommandHandle::class, + 'update' => MerchantUpdateCommandHandle::class, + ]; + + +} diff --git a/packages/payment/src/Domain/Data/MerchantData.php b/packages/payment/src/Domain/Data/MerchantData.php index 1b29f7d6..89c60682 100644 --- a/packages/payment/src/Domain/Data/MerchantData.php +++ b/packages/payment/src/Domain/Data/MerchantData.php @@ -12,8 +12,8 @@ class MerchantData extends Data public UserInterface $owner; - public string $name; + public string $shortName; public MerchantTypeEnum $type = MerchantTypeEnum::GENERAL; @@ -21,7 +21,7 @@ class MerchantData extends Data public ?int $isvId = null; - public MerchantStatusEnum $status; + public MerchantStatusEnum $status = MerchantStatusEnum::ENABLE; } diff --git a/packages/payment/src/Domain/Models/Enums/MerchantStatusEnum.php b/packages/payment/src/Domain/Models/Enums/MerchantStatusEnum.php index f4498887..85b85270 100644 --- a/packages/payment/src/Domain/Models/Enums/MerchantStatusEnum.php +++ b/packages/payment/src/Domain/Models/Enums/MerchantStatusEnum.php @@ -9,15 +9,16 @@ enum MerchantStatusEnum: string use EnumsHelper; - case Enable = 'Enable';// 启用 - case Disabled = 'disabled';// 禁用 + case ENABLE = 'enable';// 启用 + + case DISABLED = 'disabled';// 禁用 public static function labels() : array { return [ - self::Enable->value => '启用', - self::Disabled->value => '禁用', + self::ENABLE->value => '启用', + self::DISABLED->value => '禁用', ]; } diff --git a/packages/payment/src/Domain/Models/PaymentMerchant.php b/packages/payment/src/Domain/Models/PaymentMerchant.php index ee3c334d..a7871f08 100644 --- a/packages/payment/src/Domain/Models/PaymentMerchant.php +++ b/packages/payment/src/Domain/Models/PaymentMerchant.php @@ -4,13 +4,50 @@ use Illuminate\Database\Eloquent\SoftDeletes; +use RedJasmine\Payment\Domain\Models\Enums\MerchantStatusEnum; use RedJasmine\Support\Domain\Models\Traits\HasOwner; +use RedJasmine\Support\Domain\Models\Traits\HasSnowflakeId; class PaymentMerchant extends Model { + + public $incrementing = false; use HasOwner; + use HasSnowflakeId; + use SoftDeletes; + + + public $casts = [ + 'status' => MerchantStatusEnum::class + ]; + + + + public function getTable() : string + { + return config('red-jasmine-payment.tables.prefix', 'jasmine_') . 'payment_merchants'; + } + + + public static function newModel() : static + { + $model = new static(); + + + return $model; + } + + + public function setStatus(MerchantStatusEnum $status) : void + { + + $this->status = $status; + + $this->fireModelEvent('changeStatus', false); + + } } diff --git a/packages/payment/src/Domain/Repositories/MerchantReadRepositoryInterface.php b/packages/payment/src/Domain/Repositories/MerchantReadRepositoryInterface.php new file mode 100644 index 00000000..25246341 --- /dev/null +++ b/packages/payment/src/Domain/Repositories/MerchantReadRepositoryInterface.php @@ -0,0 +1,10 @@ +owner = $data->owner; + $merchant->name = $data->name; + $merchant->short_name = $data->shortName; + $merchant->type = $data->type; + $merchant->isv_id = $data->isvId; + $merchant->status = $data->status; + + return $merchant; + } +} diff --git a/packages/payment/src/Infrastructure/ReadRepositories/Mysql/MerchantReadRepository.php b/packages/payment/src/Infrastructure/ReadRepositories/Mysql/MerchantReadRepository.php new file mode 100644 index 00000000..7efc90e4 --- /dev/null +++ b/packages/payment/src/Infrastructure/ReadRepositories/Mysql/MerchantReadRepository.php @@ -0,0 +1,18 @@ +paymentMerchantRepository = app(MerchantRepositoryInterface::class); + $this->paymentMerchantCommandService = app(MerchantCommandService::class); + + + // +}); + + +test('can create merchant', function () { + + $command = new MerchantCreateCommand(); + + + $command->owner = UserData::from([ 'type' => 'user', 'id' => 1 ]); + + $command->name = 'XXX有限公司'; + $command->shortName = '测试'; + + + $merchant = $this->paymentMerchantCommandService->create($command); + + + $this->assertEquals($command->name, $merchant->name, '商户名称'); + + return $merchant; +}); + +test('can set status', function (PaymentMerchant $merchant) { + $command = new MerchantSetStatusCommand(); + $command->id = $merchant->id; + $command->status = MerchantStatusEnum::DISABLED; + $this->paymentMerchantCommandService->setStatus($command); + + $merchant = $this->paymentMerchantRepository->find($command->id); + + $this->assertEquals($command->status->value, $merchant->status->value, '商户状态'); + return $merchant; +})->depends('can create merchant'); + + +test('can update merchant', function (PaymentMerchant $merchant) { + + $command = new MerchantUpdateCommand(); + $command->id = $merchant->id; + + $command->owner = UserData::from([ 'type' => 'user', 'id' => 1 ]); + + $command->name = 'XXXX有限公司'; + $command->shortName = '测试'; + + + $merchant = $this->paymentMerchantCommandService->update($command); + + $this->assertEquals($command->name, $merchant->name, '商户名称'); + + return $merchant; +})->depends('can create merchant'); +