Skip to content

Commit

Permalink
feat: introduces factories and different types of responses
Browse files Browse the repository at this point in the history
  • Loading branch information
kajetan-nobel committed Sep 26, 2023
1 parent 2f763c4 commit c898482
Show file tree
Hide file tree
Showing 42 changed files with 824 additions and 407 deletions.
74 changes: 0 additions & 74 deletions src/Client/Response.php

This file was deleted.

9 changes: 3 additions & 6 deletions src/DTO/BaseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private function castAttributes(array $attributes): array
public function castAttribute(string $castType, mixed $value): mixed
{
if (is_a($castType, BaseDto::class, true)) {
return $castType::make($value ?? []);
return new $castType($value ?? []);
}

if (enum_exists($castType)) {
Expand All @@ -49,11 +49,8 @@ public function castAttribute(string $castType, mixed $value): mixed

public function toArray(): array
{
return app(Utils::class)->transformValues($this->attributes);
}
unset($this->attributes['getKey']);

public static function make(array $attributes): BaseDto
{
return new static($attributes);
return app(Utils::class)->transformValues($this->attributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Response\Api;
namespace Routegroup\Imoje\Payment\DTO\Casts;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\Factories\Casts\PaymentDtoFactory;
use Routegroup\Imoje\Payment\Types\Currency;
use Routegroup\Imoje\Payment\Types\TransactionStatus;

/**
* @property-read string $id
* @property-read string $title
* @property-read string|null $title
* @property-read int $amount
* @property-read TransactionStatus $status
* @property-read int $created
* @property-read string $orderId
* @property-read Currency $currency
* @property-read int $modified
* @property-read string $serviceId
* @property-read string $notificationUrl
*/
class PaymentResponseDto extends BaseDto
class PaymentDto extends BaseDto
{
use HasFactory;

protected array $casts = [
'id' => 'string',
'amount' => 'int',
'status' => TransactionStatus::class,
'created' => 'int',
'modified' => 'int',
'orderId' => 'string',
'currency' => Currency::class,
'status' => TransactionStatus::class,
'modified' => 'int',
'serviceId' => 'string',
];

protected static function newFactory(): PaymentDtoFactory
{
return PaymentDtoFactory::new();
}
}
52 changes: 52 additions & 0 deletions src/DTO/Casts/PaymentProfileDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Casts;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\Factories\Casts\PaymentProfileDtoFactory;

/**
* @property-read string|null $id
* @property-read string $merchantMid
* @property-read string|null $merchantCustomerId
* @property-read string|null $firstName
* @property-read string|null $lastName
* @property-read string|null $maskedNumber
* @property-read int|null $month
* @property-read int|null $year
* @property-read string|null $organization
* @property-read bool|null $isActive
* @property-read string|null $profile
*/
class PaymentProfileDto extends BaseDto
{
use HasFactory;

protected $attributes = [
'id' => null,
'merchantMid' => null,
'merchantCustomerId' => null,
'firstName' => null,
'lastName' => null,
'maskedNumber' => null,
'month' => null,
'year' => null,
'organization' => null,
'isActive' => null,
'profile' => null,
];

protected array $casts = [
'month' => 'int',
'year' => 'int',
'isActive' => 'bool',
];

protected static function newFactory(): PaymentProfileDtoFactory
{
return PaymentProfileDtoFactory::new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,57 @@

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Response\Api;
namespace Routegroup\Imoje\Payment\DTO\Casts;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\Factories\Casts\TransactionDtoFactory;
use Routegroup\Imoje\Payment\Types\Currency;
use Routegroup\Imoje\Payment\Types\PaymentMethod;
use Routegroup\Imoje\Payment\Types\PaymentMethodCode;
use Routegroup\Imoje\Payment\Types\TransactionSource;
use Routegroup\Imoje\Payment\Types\TransactionStatus;
use Routegroup\Imoje\Payment\Types\TransactionType;

/**
* @property-read int $amount
* @property-read string $id
* @property-read TransactionType $type
* @property-read TransactionStatus $status
* @property-read TransactionSource $source
* @property-read int $created
* @property-read int $modified
* @property-read Currency $currency
* @property-read string $id
* @property-read string $notificationUrl
* @property-read string $serviceId
* @property-read int $amount
* @property-read Currency $currency
* @property-read string $title
* @property-read string $orderId
* @property-read PaymentMethod $paymentMethod
* @property-read PaymentMethodCode $paymentMethodCode
* @property-read PaymentProfileResponseDto $paymentProfile
* @property-read string $serviceId
* @property-read string $source
* @property-read TransactionStatus $status
* @property-read string $statusCode
* @property-read string $statusCodeDescription
* @property-read string $title
* @property-read string $type
* @property-read PaymentProfileDto|null $paymentProfile
*/
class TransactionResponseDto extends BaseDto
class TransactionDto extends BaseDto
{
use HasFactory;

protected array $casts = [
'amount' => 'int',
'id' => 'string',
'type' => TransactionType::class,
'status' => TransactionStatus::class,
'source' => TransactionSource::class,
'created' => 'int',
'modified' => 'int',
'serviceId' => 'string',
'amount' => 'int',
'currency' => Currency::class,
'orderId' => 'string',
'paymentMethod' => PaymentMethod::class,
'paymentMethodCode' => PaymentMethodCode::class,
'paymentProfile' => PaymentProfileResponseDto::class,
'status' => TransactionStatus::class,
'paymentProfile' => PaymentProfileDto::class,
];

protected static function newFactory(): TransactionDtoFactory
{
return TransactionDtoFactory::new();
}
}
23 changes: 23 additions & 0 deletions src/DTO/Notifications/RefundNotificationDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Notifications;

use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\DTO\Casts\PaymentDto;
use Routegroup\Imoje\Payment\DTO\Casts\TransactionDto;

/**
* @property-read TransactionDto $transaction
* @property-read string $originalTransactionId
* @property-read PaymentDto $payment
*/
class RefundNotificationDto extends BaseDto
{
protected array $casts = [
'transaction' => TransactionDto::class,
'originalTransactionId' => 'string',
'payment' => PaymentDto::class,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Request;
namespace Routegroup\Imoje\Payment\DTO\Requests;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\Factories\Requests\ChargeProfileRequestDtoFactory;
use Routegroup\Imoje\Payment\Lib\Config;
use Routegroup\Imoje\Payment\Types\Currency;

Expand All @@ -18,29 +20,37 @@
*/
class ChargeProfileRequestDto extends BaseDto
{
use HasFactory;

protected array $casts = [
'amount' => 'int',
'currency' => Currency::class,
];

public static function make(
public function __construct(
#[ArrayShape([
// Required
'serviceId' => 'string',
'paymentProfileId' => 'string',
'amount' => 'int',
'currency' => Currency::class,
'orderId' => 'string',
// Required but provided by default
'serviceId' => 'string',
// Optional
'title' => 'string',
])] array $attributes
): BaseDto {
])] array $attributes = []
) {
$config = app(Config::class);

$attributes = array_merge_recursive([
'serviceId' => $config->serviceId,
], $attributes);

return parent::make($attributes);
parent::__construct($attributes);
}

protected static function newFactory(): ChargeProfileRequestDtoFactory
{
return ChargeProfileRequestDtoFactory::new();
}
}
Loading

0 comments on commit c898482

Please sign in to comment.