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 4a4f17a commit da8f834
Show file tree
Hide file tree
Showing 17 changed files with 390 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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);

use CashierProvider\Core\Concerns\Migrations\PrivateMigration;
use Illuminate\Database\Schema\Blueprint;

new class extends PrivateMigration {
public function up(): void
{
$this->connection()->table($this->table(), function (Blueprint $table) {
$table->string('status')->nullable()->after('operation_id');
});
}
};
48 changes: 48 additions & 0 deletions src/Casts/InfoCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Casts;

use CashierProvider\Core\Concerns\Config\Payment\Drivers;
use CashierProvider\Core\Data\Models\InfoData;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

class InfoCast implements CastsAttributes
{
use Drivers;

public function get(Model $model, string $key, mixed $value, array $attributes): InfoData
{
$instance = static::driverByModel($model->parent)->details;

return call_user_func([$instance, 'from'], json_decode($value, true));
}

/**
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param \Spatie\LaravelData\Data $value
* @param array $attributes
*
* @return string
*/
public function set(Model $model, string $key, mixed $value, array $attributes): string
{
return $value->toJson(JSON_UNESCAPED_SLASHES ^ JSON_UNESCAPED_UNICODE);
}
}
13 changes: 13 additions & 0 deletions src/Concerns/Config/Payment/Drivers.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace CashierProvider\Core\Concerns\Config\Payment;

use CashierProvider\Core\Concerns\Transformers\EnumsTransformer;
use CashierProvider\Core\Data\Config\DriverData;
use CashierProvider\Core\Exceptions\Internal\UnknownDriverConfigException;
use CashierProvider\Core\Facades\Config;
Expand All @@ -25,6 +26,9 @@

trait Drivers
{
use Attributes;
use EnumsTransformer;

protected static function drivers(): Collection
{
return Config::payment()->drivers;
Expand All @@ -38,4 +42,13 @@ protected static function driver(int|string $name, Model $payment): DriverData

throw new UnknownDriverConfigException($name, $payment->getKey());
}

protected static function driverByModel(Model $payment): DriverData
{
$name = $payment->getAttribute(
static::attribute()->type
);

return static::driver(static::transformFromEnum($name), $payment);
}
}
25 changes: 25 additions & 0 deletions src/Concerns/Events/Notifiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Concerns\Events;

use Illuminate\Database\Eloquent\Model;

trait Notifiable
{
protected static function event(Model $payment): void {}
}
13 changes: 12 additions & 1 deletion src/Data/Config/Payment/StatusData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StatusData extends Data

public mixed $failed;

public function get(StatusEnum $status): mixed
public function fromEnum(StatusEnum $status): mixed
{
return match ($status) {
StatusEnum::new => $this->new,
Expand All @@ -46,6 +46,17 @@ public function get(StatusEnum $status): mixed
};
}

public function toEnum(int|string $status): StatusEnum
{
return match ($status) {
$this->new => StatusEnum::new,
$this->success => StatusEnum::success,
$this->refund => StatusEnum::refund,
$this->waitRefund => StatusEnum::waitRefund,
$this->failed => StatusEnum::failed,
};
}

public function inProgress(): array
{
return [
Expand Down
37 changes: 37 additions & 0 deletions src/Data/Models/InfoData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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
*/

namespace CashierProvider\Core\Data\Models;

use CashierProvider\Core\Concerns\Config\Payment\Payments;
use CashierProvider\Core\Concerns\Transformers\EnumsTransformer;
use CashierProvider\Core\Enums\StatusEnum;
use Spatie\LaravelData\Data;
use UnitEnum;

abstract class InfoData extends Data
{
use EnumsTransformer;
use Payments;

public UnitEnum|int|string|null $status;

public function statusToEnum(): StatusEnum
{
return static::payment()->status->toEnum(
static::transformFromEnum($this->status)
);
}
}
27 changes: 27 additions & 0 deletions src/Events/BaseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Events;

use Illuminate\Database\Eloquent\Model;

abstract class BaseEvent
{
public function __construct(
public Model $payment
) {}
}
22 changes: 22 additions & 0 deletions src/Events/CreatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Events;

class CreatedEvent extends BaseEvent
{
}
22 changes: 22 additions & 0 deletions src/Events/FailedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Events;

class FailedEvent extends BaseEvent
{
}
22 changes: 22 additions & 0 deletions src/Events/RefundedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Events;

class RefundedEvent extends BaseEvent
{
}
22 changes: 22 additions & 0 deletions src/Events/SuccessEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Events;

class SuccessEvent extends BaseEvent
{
}
22 changes: 22 additions & 0 deletions src/Events/WaitRefundEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Events;

class WaitRefundEvent extends BaseEvent
{
}
6 changes: 5 additions & 1 deletion src/Models/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

namespace CashierProvider\Core\Models;

use CashierProvider\Core\Casts\InfoCast;
use CashierProvider\Core\Concerns\Config\Details as DetailsConcern;
use CashierProvider\Core\Enums\StatusEnum;
use CashierProvider\Core\Facades\Config;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
Expand All @@ -30,11 +32,13 @@ class Details extends Model
'payment_id',
'external_id',
'operation_id',
'status',
'info',
];

protected $casts = [
'info' => 'json',
'info' => InfoCast::class,
'status' => StatusEnum::class,
];

protected $touches = [
Expand Down
Loading

0 comments on commit da8f834

Please sign in to comment.