Skip to content

Commit

Permalink
feat(payment): 创建商户相关的命令和服务
Browse files Browse the repository at this point in the history
- 新增商户创建、更新和设置状态的命令类
- 实现商户命令的处理逻辑- 添加商户相关的服务提供者- 更新数据库迁移文件,修正表名和字段
- 新增商户模型和转换器
- 实现商户读取和写入的仓库接口和具体实现
  • Loading branch information
liushoukun committed Nov 18, 2024
1 parent be58d5f commit e03f2c9
Show file tree
Hide file tree
Showing 22 changed files with 446 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/payment/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"extra": {
"laravel": {
"providers": [
"RedJasmine\\Payment\\PaymentPackageServiceProvider"
"RedJasmine\\Payment\\PaymentPackageServiceProvider",
"RedJasmine\\Payment\\Application\\PaymentApplicationServiceProvider"
],
"aliases": {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('名称');
Expand All @@ -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');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('货币');
Expand All @@ -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');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace RedJasmine\Payment\Application\Commands\Merchant;

use RedJasmine\Payment\Domain\Data\MerchantData;

class MerchantCreateCommand extends MerchantData
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace RedJasmine\Payment\Application\Commands\Merchant;

use RedJasmine\Payment\Domain\Models\Enums\MerchantStatusEnum;
use RedJasmine\Support\Data\Data;

class MerchantSetStatusCommand extends Data
{


public int $id;

public MerchantStatusEnum $status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace RedJasmine\Payment\Application\Commands\Merchant;

use RedJasmine\Payment\Domain\Data\MerchantData;

class MerchantUpdateCommand extends MerchantData
{
public int $id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace RedJasmine\Payment\Application;

use Illuminate\Support\ServiceProvider;
use RedJasmine\Payment\Domain\Repositories\MerchantReadRepositoryInterface;
use RedJasmine\Payment\Domain\Repositories\MerchantRepositoryInterface;
use RedJasmine\Payment\Infrastructure\ReadRepositories\Mysql\MerchantReadRepository;
use RedJasmine\Payment\Infrastructure\Repositories\Eloquent\MerchantRepository;

class PaymentApplicationServiceProvider extends ServiceProvider
{

public function register() : void
{

$this->app->bind(MerchantRepositoryInterface::class, MerchantRepository::class);
$this->app->bind(MerchantReadRepositoryInterface::class, MerchantReadRepository::class);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace RedJasmine\Payment\Application\Services\CommandHandlers;

use RedJasmine\Payment\Application\Commands\Merchant\MerchantCreateCommand;
use RedJasmine\Payment\Domain\Models\PaymentMerchant;
use RedJasmine\Payment\Domain\Repositories\MerchantRepositoryInterface;
use RedJasmine\Payment\Domain\Transformer\MerchantTransformer;
use RedJasmine\Support\Application\CommandHandlers\CommandHandler;
use Throwable;

class MerchantCreateCommandHandle extends CommandHandler
{


public function __construct(protected MerchantRepositoryInterface $repository)
{
}

/**
* @param MerchantCreateCommand $command
* @return PaymentMerchant
* @throws Throwable
*/
public function handle(MerchantCreateCommand $command) : PaymentMerchant
{
$this->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;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace RedJasmine\Payment\Application\Services\CommandHandlers;

use RedJasmine\Payment\Application\Commands\Merchant\MerchantSetStatusCommand;
use RedJasmine\Payment\Domain\Repositories\MerchantRepositoryInterface;
use RedJasmine\Support\Application\CommandHandlers\CommandHandler;
use Throwable;

class MerchantSetStatusCommandHandle extends CommandHandler
{


public function __construct(protected MerchantRepositoryInterface $repository)
{
}

/**
* @param MerchantSetStatusCommand $command
* @return void
* @throws Throwable
*/
public function handle(MerchantSetStatusCommand $command):void
{
$this->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;
}


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace RedJasmine\Payment\Application\Services\CommandHandlers;

use RedJasmine\Payment\Application\Commands\Merchant\MerchantCreateCommand;
use RedJasmine\Payment\Application\Commands\Merchant\MerchantUpdateCommand;
use RedJasmine\Payment\Domain\Models\PaymentMerchant;
use RedJasmine\Payment\Domain\Repositories\MerchantRepositoryInterface;
use RedJasmine\Payment\Domain\Transformer\MerchantTransformer;
use RedJasmine\Support\Application\CommandHandlers\CommandHandler;
use Throwable;

class MerchantUpdateCommandHandle extends CommandHandler
{


public function __construct(protected MerchantRepositoryInterface $repository)
{
}

/**
* @param MerchantUpdateCommand $command
* @return PaymentMerchant
* @throws Throwable
*/
public function handle(MerchantUpdateCommand $command) : PaymentMerchant
{
$this->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;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace RedJasmine\Payment\Application\Services;

use RedJasmine\Payment\Application\Commands\Merchant\MerchantCreateCommand;
use RedJasmine\Payment\Application\Commands\Merchant\MerchantSetStatusCommand;
use RedJasmine\Payment\Application\Commands\Merchant\MerchantUpdateCommand;
use RedJasmine\Payment\Application\Services\CommandHandlers\MerchantCreateCommandHandle;
use RedJasmine\Payment\Application\Services\CommandHandlers\MerchantSetStatusCommandHandle;
use RedJasmine\Payment\Application\Services\CommandHandlers\MerchantUpdateCommandHandle;
use RedJasmine\Payment\Domain\Models\PaymentMerchant;
use RedJasmine\Support\Application\ApplicationCommandService;

/**
* @method PaymentMerchant create(MerchantCreateCommand $command)
* @method PaymentMerchant update(MerchantUpdateCommand $command)
* @method void setStatus(MerchantSetStatusCommand $command)
*/
class MerchantCommandService extends ApplicationCommandService
{

/**
* 钩子前缀
* @var string
*/
public static string $hookNamePrefix = 'payment.application.merchant.command';

protected static string $modelClass = PaymentMerchant::class;


protected static $macros = [
'create' => MerchantCreateCommandHandle::class,
'setStatus' => MerchantSetStatusCommandHandle::class,
'update' => MerchantUpdateCommandHandle::class,
];


}
4 changes: 2 additions & 2 deletions packages/payment/src/Domain/Data/MerchantData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ class MerchantData extends Data

public UserInterface $owner;


public string $name;
public string $shortName;

public MerchantTypeEnum $type = MerchantTypeEnum::GENERAL;


public ?int $isvId = null;


public MerchantStatusEnum $status;
public MerchantStatusEnum $status = MerchantStatusEnum::ENABLE;


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 => '禁用',
];

}
Expand Down
37 changes: 37 additions & 0 deletions packages/payment/src/Domain/Models/PaymentMerchant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
Loading

0 comments on commit e03f2c9

Please sign in to comment.