Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jul 8, 2023
1 parent fa77a57 commit d373c10
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 124 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 8 additions & 9 deletions config/cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

declare(strict_types=1);

use CashierProvider\Core\Enums\AttributeEnum;
use CashierProvider\Core\Enums\StatusEnum;

return [
Expand Down Expand Up @@ -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',
],

/*
Expand Down Expand Up @@ -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.
|
*/

Expand All @@ -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.
|
*/

Expand Down Expand Up @@ -340,7 +339,7 @@
//
// 'queue' => [
// 'start' => env('CASHIER_QUEUE'),
// 'check' => env('CASHIER_QUEUE'),
// 'verify' => env('CASHIER_QUEUE'),
// 'refund' => env('CASHIER_QUEUE'),
// ],
// ],
Expand Down
29 changes: 0 additions & 29 deletions src/Concerns/Cache.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/Console/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +31,6 @@
abstract class Command extends BaseCommand
{
use Attributes;
use Cache;
use Drivers;
use Payments;
use Statuses;
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Config/ConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ConfigData extends Data

public QueueData $queue;

public CheckData $check;
public VerifyData $verify;

#[MapInputName('auto_refund')]
public RefundData $refund;
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Data/Config/Payment/AttributeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ class AttributeData extends Data

public string $status;

#[MapInputName('created_at')]
public string $createdAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 0 additions & 34 deletions src/Enums/AttributeEnum.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
60 changes: 60 additions & 0 deletions src/Helpers/Access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* This file is part of the "cashier-provider/core" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <helldar@dragon-code.pro>
* @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;
}
}
35 changes: 0 additions & 35 deletions src/Helpers/Permission.php

This file was deleted.

26 changes: 18 additions & 8 deletions src/Services/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +35,9 @@ class Job

public function __construct(
protected Model $payment
) {}
) {
$this->validateModel($this->payment);
}

public static function model(Model $payment): self
{
Expand All @@ -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);
}
}
Expand All @@ -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;
Expand All @@ -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);
}
}

0 comments on commit d373c10

Please sign in to comment.