diff --git a/composer.json b/composer.json index 4ccce836..8dd8b8de 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ "require": { "php": "^8.1", "archtechx/enums": "^0.3", - "dragon-code/laravel-cache": "^3.8", + "dragon-code/support": "^6.11", "illuminate/console": "^10.0", "illuminate/database": "^10.0", "illuminate/support": "^10.0", diff --git a/config/cashier.php b/config/cashier.php index 5847749b..8b9d555a 100644 --- a/config/cashier.php +++ b/config/cashier.php @@ -15,7 +15,6 @@ declare(strict_types=1); -use CashierProvider\Core\Enums\AttributeEnum; use CashierProvider\Core\Enums\StatusEnum; return [ @@ -66,9 +65,9 @@ */ 'attribute' => [ - AttributeEnum::type() => 'type_id', - AttributeEnum::status() => 'status_id', - AttributeEnum::createdAt() => 'created_at', + 'type' => 'type_id', + 'status' => 'status_id', + 'created_at' => 'created_at', ], /* @@ -234,21 +233,21 @@ /* |-------------------------------------------------------------------------- - | Check Requests + | Verify Requests |-------------------------------------------------------------------------- | | This parameter of settings is responsible for the duration of the requests. | */ - 'check' => [ + 'verify' => [ /* |-------------------------------------------------------------------------- | Delay |-------------------------------------------------------------------------- | | This setting determines the number of seconds to pause before - | re-checking the payment status. + | re-verifying the payment status. | */ @@ -260,7 +259,7 @@ |-------------------------------------------------------------------------- | | This setting determines the number of seconds after which you need to - | stop trying to check the status of the payment. + | stop trying to verify the status of the payment. | */ @@ -340,7 +339,7 @@ // // 'queue' => [ // 'start' => env('CASHIER_QUEUE'), - // 'check' => env('CASHIER_QUEUE'), + // 'verify' => env('CASHIER_QUEUE'), // 'refund' => env('CASHIER_QUEUE'), // ], // ], diff --git a/src/Concerns/Cache.php b/src/Concerns/Cache.php deleted file mode 100644 index 399bc75d..00000000 --- a/src/Concerns/Cache.php +++ /dev/null @@ -1,29 +0,0 @@ - - * @copyright 2023 Andrey Helldar - * @license MIT - * - * @see https://github.com/cashier-provider - */ - -declare(strict_types=1); - -namespace CashierProvider\Core\Concerns; - -use DragonCode\Cache\Services\Cache as DC; -use Illuminate\Database\Eloquent\Model; - -trait Cache -{ - protected function cache(Model $payment) - { - return DC::make()->key(static::class, $payment->getKey()); - } -} diff --git a/src/Console/Commands/Command.php b/src/Console/Commands/Command.php index d95c7a82..0dcadd84 100644 --- a/src/Console/Commands/Command.php +++ b/src/Console/Commands/Command.php @@ -17,7 +17,6 @@ namespace CashierProvider\Core\Console\Commands; -use CashierProvider\Core\Concerns\Cache; use CashierProvider\Core\Concerns\Config\Payment\Attributes; use CashierProvider\Core\Concerns\Config\Payment\Drivers; use CashierProvider\Core\Concerns\Config\Payment\Payments; @@ -32,7 +31,6 @@ abstract class Command extends BaseCommand { use Attributes; - use Cache; use Drivers; use Payments; use Statuses; diff --git a/src/Data/Config/ConfigData.php b/src/Data/Config/ConfigData.php index d97bf354..7342d58f 100644 --- a/src/Data/Config/ConfigData.php +++ b/src/Data/Config/ConfigData.php @@ -35,7 +35,7 @@ class ConfigData extends Data public QueueData $queue; - public CheckData $check; + public VerifyData $verify; #[MapInputName('auto_refund')] public RefundData $refund; @@ -58,9 +58,9 @@ public function queue(): QueueData return $this->queue; } - public function check(): CheckData + public function verify(): VerifyData { - return $this->check; + return $this->verify; } public function refund(): RefundData diff --git a/src/Data/Config/Payment/AttributeData.php b/src/Data/Config/Payment/AttributeData.php index be277669..756a3bda 100644 --- a/src/Data/Config/Payment/AttributeData.php +++ b/src/Data/Config/Payment/AttributeData.php @@ -28,5 +28,6 @@ class AttributeData extends Data public string $status; + #[MapInputName('created_at')] public string $createdAt; } diff --git a/src/Data/Config/CheckData.php b/src/Data/Config/VerifyData.php similarity index 95% rename from src/Data/Config/CheckData.php rename to src/Data/Config/VerifyData.php index 1f21b825..52667c22 100644 --- a/src/Data/Config/CheckData.php +++ b/src/Data/Config/VerifyData.php @@ -21,7 +21,7 @@ use Spatie\LaravelData\Attributes\WithCast; use Spatie\LaravelData\Data; -class CheckData extends Data +class VerifyData extends Data { #[WithCast(NumberCast::class, min: 0, default: 60)] public int $delay; diff --git a/src/Enums/AttributeEnum.php b/src/Enums/AttributeEnum.php deleted file mode 100644 index cc0e2046..00000000 --- a/src/Enums/AttributeEnum.php +++ /dev/null @@ -1,34 +0,0 @@ - - * @copyright 2023 Andrey Helldar - * @license MIT - * - * @see https://github.com/cashier-provider - */ - -declare(strict_types=1); - -namespace CashierProvider\Core\Enums; - -use ArchTech\Enums\Values; - -/** - * @method string createdAt() - * @method string status() - * @method string type() - */ -enum AttributeEnum: string -{ - use Values; - - case type = 'type'; - case status = 'status'; - case createdAt = 'created_at'; -} diff --git a/src/Facades/Config.php b/src/Facades/Config.php index 3bbb6ab0..5de49597 100644 --- a/src/Facades/Config.php +++ b/src/Facades/Config.php @@ -17,23 +17,23 @@ namespace CashierProvider\Core\Facades; -use CashierProvider\Core\Data\Config\CheckData; use CashierProvider\Core\Data\Config\ConfigData; use CashierProvider\Core\Data\Config\DetailsData; use CashierProvider\Core\Data\Config\DriverData; use CashierProvider\Core\Data\Config\Payment\PaymentData; use CashierProvider\Core\Data\Config\Queue\QueueData; use CashierProvider\Core\Data\Config\RefundData; +use CashierProvider\Core\Data\Config\VerifyData; use Illuminate\Support\Facades\Facade; /** * @method static bool isProduction() - * @method static CheckData check() * @method static DetailsData details() * @method static DriverData driver(int|string $name) * @method static PaymentData payment() * @method static QueueData queue() * @method static RefundData refund() + * @method static VerifyData verify() */ class Config extends Facade { diff --git a/src/Helpers/Access.php b/src/Helpers/Access.php new file mode 100644 index 00000000..110a0b74 --- /dev/null +++ b/src/Helpers/Access.php @@ -0,0 +1,60 @@ + + * @copyright 2023 Andrey Helldar + * @license MIT + * + * @see https://github.com/cashier-provider + */ + +declare(strict_types=1); + +namespace CashierProvider\Core\Helpers; + +use CashierProvider\Core\Data\Config\Payment\AttributeData; +use CashierProvider\Core\Facades\Config; +use Illuminate\Database\Eloquent\Model; + +class Access +{ + public static function toStart(Model $payment): bool + { + return static::allowType($payment); + } + + public static function toVerify(Model $payment): bool + { + return static::allowType($payment); + } + + public static function toRefund(Model $payment): bool + { + return static::allowType($payment); + } + + protected static function allowType(Model $payment): bool + { + return in_array(static::paymentType($payment), static::paymentTypes(), true); + } + + protected static function paymentTypes(): array + { + return Config::payment()->drivers->keys()->toArray(); + } + + protected static function paymentType(Model $payment): mixed + { + return $payment->getAttribute(static::attribute()->type); + } + + protected static function attribute(): AttributeData + { + return Config::payment()->attribute; + } +} diff --git a/src/Helpers/Permission.php b/src/Helpers/Permission.php deleted file mode 100644 index 8cc417b8..00000000 --- a/src/Helpers/Permission.php +++ /dev/null @@ -1,35 +0,0 @@ - - * @copyright 2023 Andrey Helldar - * @license MIT - * - * @see https://github.com/cashier-provider - */ - -declare(strict_types=1); - -namespace CashierProvider\Core\Helpers; - -use CashierProvider\Core\Facades\Config; -use Illuminate\Database\Eloquent\Model; - -class Permission -{ - public static function allowToStart(Model $payment): bool {} - - public static function allowToVerify(Model $payment): bool {} - - public static function allowToRefund(Model $payment): bool {} - - public static function allowToAutoRefund(): bool - { - return Config::refund()->enabled; - } -} diff --git a/src/Services/Job.php b/src/Services/Job.php index 5a20dc47..1b42fb83 100644 --- a/src/Services/Job.php +++ b/src/Services/Job.php @@ -19,7 +19,8 @@ use CashierProvider\Core\Concerns\Config\Queue; use CashierProvider\Core\Concerns\Config\Refund; -use CashierProvider\Core\Helpers\Permission; +use CashierProvider\Core\Helpers\Access; +use CashierProvider\Core\Helpers\Validator; use CashierProvider\Core\Jobs\RefundJob; use CashierProvider\Core\Jobs\StartJob; use CashierProvider\Core\Jobs\VerifyJob; @@ -34,7 +35,9 @@ class Job public function __construct( protected Model $payment - ) {} + ) { + $this->validateModel($this->payment); + } public static function model(Model $payment): self { @@ -46,7 +49,7 @@ public function start(): void if ($this->allowToStart()) { $this->dispatch(StartJob::class, $this->queue()->name->start); - if ($this->allowToAutoRefund()) { + if ($this->autoRefund()->enabled) { $this->refund($this->autoRefund()->delay); } } @@ -66,6 +69,12 @@ public function refund(?int $delay = null): void } } + public function retry(): void + { + $this->start(); + $this->verify(); + } + public function force(bool $force = true): self { $this->force = $force; @@ -78,26 +87,27 @@ protected function dispatch(string $job, ?string $queue, ?int $delay = null): vo dispatch(new $job($this->payment, $this->force)) ->onConnection($this->queue()->connection) ->onQueue($queue) + ->afterCommit() ->delay($delay); } protected function allowToStart(): bool { - return Permission::allowToStart($this->payment); + return Access::toStart($this->payment); } protected function allowToVerify(): bool { - return Permission::allowToVerify($this->payment); + return Access::toVerify($this->payment); } protected function allowToRefund(): bool { - return Permission::allowToRefund($this->payment); + return Access::toRefund($this->payment); } - protected function allowToAutoRefund(): bool + protected function validateModel(Model $model): void { - return Permission::allowToAutoRefund(); + Validator::model($model); } }