Skip to content

Commit

Permalink
feat: add createPayment method
Browse files Browse the repository at this point in the history
  • Loading branch information
kajetan-nobel committed Oct 3, 2023
1 parent d34d7a3 commit f8c810f
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 35 deletions.
77 changes: 77 additions & 0 deletions src/DTO/Api/PaymentDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Api;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use JetBrains\PhpStorm\ArrayShape;
use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\DTO\Casts\CustomerDto;
use Routegroup\Imoje\Payment\Factories\Api\PaymentDtoFactory;
use Routegroup\Imoje\Payment\Lib\Config;
use Routegroup\Imoje\Payment\Types\Currency;

/**
* @property-read string $serviceId
* @property-read int $amount
* @property-read Currency $currency
* @property-read string $orderId
* @property-read CustomerDto $customer
* @property-read string $title
* @property-read string $visibleMethod
* @property-read string $preselectMethodCode
* @property-read string $returnUrl
* @property-read string $successReturnUrl
* @property-read string $failureReturnUrl
* @property-read string $simp
* @property-read int $validTo
* @property-read array $cart
*/
class PaymentDto extends BaseDto
{
use HasFactory;

protected bool $allowNull = true;

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

public function __construct(
#[ArrayShape([
// Required
'amount' => 'int',
'currency' => 'string',
'orderId' => 'string',
'customer' => 'object',
// Required but provided,
'serviceId' => 'string',
// Optional
'title' => 'string',
'visibleMethod' => 'string',
'preselectMethodCode' => 'string',
'returnUrl' => 'string',
'successReturnUrl' => 'string',
'failureReturnUrl' => 'string',
'simp' => 'string',
'validTo' => 'int',
'cart' => 'array',
])] array $attributes = []
) {
$config = app(Config::class);

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

parent::__construct($attributes);
}

protected static function newFactory(): PaymentDtoFactory
{
return PaymentDtoFactory::new();
}
}
4 changes: 2 additions & 2 deletions src/DTO/BaseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ private function castAttributes(array $attributes): array

public function castAttribute(string $castType, mixed $value): mixed
{
if ($this->allowNull && is_null($value)) {
return $value;
if ($this->allowNull && empty($value)) {
return null;
}

if (is_a($castType, BaseDto::class, true)) {
Expand Down
2 changes: 1 addition & 1 deletion src/DTO/Casts/CustomerDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @property-read string|null $cid
* @property-read string|null $company
* @property-read string|null $phone
* @property-read Lang $locale
* @property-read Lang|null $locale
*/
class CustomerDto extends BaseDto
{
Expand Down
25 changes: 13 additions & 12 deletions src/DTO/Casts/PaymentDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@
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|null $title
* @property-read string $serviceId
* @property-read int $amount
* @property-read TransactionStatus $status
* @property-read int $created
* @property-read string $orderId
* @property-read Currency $currency
* @property-read string $orderId
* @property-read CustomerDto $customer
* @property-read string $id
* @property-read string $url
* @property-read int|null $validTo
* @property-read int $created
* @property-read int $modified
* @property-read string $serviceId
*/
class PaymentDto extends BaseDto
{
use HasFactory;

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

protected static function newFactory(): PaymentDtoFactory
Expand Down
2 changes: 2 additions & 0 deletions src/DTO/Casts/TransactionDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class TransactionDto extends BaseDto
{
use HasFactory;

protected bool $allowNull = true;

protected array $casts = [
'id' => 'string',
'type' => TransactionType::class,
Expand Down
43 changes: 43 additions & 0 deletions src/DTO/Casts/TransactionPaymentDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\TransactionPaymentDtoFactory;
use Routegroup\Imoje\Payment\Types\Currency;
use Routegroup\Imoje\Payment\Types\TransactionStatus;

/**
* @property-read string $id
* @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
*/
class TransactionPaymentDto extends BaseDto
{
use HasFactory;

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

protected static function newFactory(): TransactionPaymentDtoFactory
{
return TransactionPaymentDtoFactory::new();
}
}
6 changes: 3 additions & 3 deletions src/DTO/Notifications/Paywall/OneClickNotificationDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\DTO\Casts\PaymentDto;
use Routegroup\Imoje\Payment\DTO\Casts\TransactionDto;
use Routegroup\Imoje\Payment\DTO\Casts\TransactionPaymentDto;
use Routegroup\Imoje\Payment\Factories\Notifications\Paywall\OneClickNotificationDtoFactory;

/**
* @property-read TransactionDto $transaction
* @property-read PaymentDto $payment
* @property-read TransactionPaymentDto $payment
*/
class OneClickNotificationDto extends BaseDto
{
use HasFactory;

protected array $casts = [
'transaction' => TransactionDto::class,
'payment' => PaymentDto::class,
'payment' => TransactionPaymentDto::class,
];

protected static function newFactory(): OneClickNotificationDtoFactory
Expand Down
6 changes: 3 additions & 3 deletions src/DTO/Notifications/Paywall/TransactionNotificationDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\DTO\Casts\ActionDto;
use Routegroup\Imoje\Payment\DTO\Casts\PaymentDto;
use Routegroup\Imoje\Payment\DTO\Casts\TransactionDto;
use Routegroup\Imoje\Payment\DTO\Casts\TransactionPaymentDto;
use Routegroup\Imoje\Payment\Factories\Notifications\Paywall\TransactionNotificationDtoFactory;

/**
* @property-read TransactionDto $transaction
* @property-read PaymentDto $payment
* @property-read TransactionPaymentDto $payment
* @property-read ActionDto $action
*/
class TransactionNotificationDto extends BaseDto
Expand All @@ -22,7 +22,7 @@ class TransactionNotificationDto extends BaseDto

protected array $casts = [
'transaction' => TransactionDto::class,
'payment' => PaymentDto::class,
'payment' => TransactionPaymentDto::class,
'action' => ActionDto::class,
];

Expand Down
6 changes: 3 additions & 3 deletions src/DTO/Notifications/RefundNotificationDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
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;
use Routegroup\Imoje\Payment\DTO\Casts\TransactionPaymentDto;

/**
* @property-read TransactionDto $transaction
* @property-read string $originalTransactionId
* @property-read PaymentDto $payment
* @property-read TransactionPaymentDto $payment
*/
class RefundNotificationDto extends BaseDto
{
protected array $casts = [
'transaction' => TransactionDto::class,
'originalTransactionId' => 'string',
'payment' => PaymentDto::class,
'payment' => TransactionPaymentDto::class,
];
}
26 changes: 26 additions & 0 deletions src/DTO/Responses/PaymentResponseDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Responses;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Routegroup\Imoje\Payment\DTO\Casts\PaymentDto;
use Routegroup\Imoje\Payment\Factories\Api\PaymentDtoFactory;

/**
* @property-read PaymentDto $payment
*/
class PaymentResponseDto extends ResponseDto
{
use HasFactory;

protected array $casts = [
'payment' => PaymentDto::class,
];

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

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\Factories\Api;

use Routegroup\Imoje\Payment\DTO\Api\PaymentDto;
use Routegroup\Imoje\Payment\DTO\Casts\CustomerDto;
use Routegroup\Imoje\Payment\Factories\Factory;
use Routegroup\Imoje\Payment\Types\Currency;

class PaymentDtoFactory extends Factory
{
protected $model = PaymentDto::class;

public function definition(): array
{
return [
'amount' => $this->faker->numberBetween(1, 100) * 100,
'currency' => Currency::PLN,
'orderId' => $this->faker->uuid,
'customer' => CustomerDto::factory(),
'returnUrl' => 'https://imoje.requestcatcher.com',
'successReturnUrl' => 'https://imoje.requestcatcher.com',
'failureReturnUrl' => 'https://imoje.requestcatcher.com',
];
}
}
18 changes: 13 additions & 5 deletions src/Factories/Casts/PaymentDtoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Routegroup\Imoje\Payment\Factories\Casts;

use Routegroup\Imoje\Payment\DTO\Casts\CustomerDto;
use Routegroup\Imoje\Payment\DTO\Casts\PaymentDto;
use Routegroup\Imoje\Payment\Factories\Factory;
use Routegroup\Imoje\Payment\Types\Currency;
use Routegroup\Imoje\Payment\Types\TransactionStatus;

class PaymentDtoFactory extends Factory
{
Expand All @@ -19,13 +19,21 @@ public function definition(): array

return [
'id' => $this->faker->unique()->uuid,
'amount' => $this->faker->numberBetween(1, 1000) * 100,
'status' => TransactionStatus::SETTLED,
'created' => $now->timestamp,
'url' => '#',
'serviceId' => config('services.imoje.service_key'),
'orderId' => $this->faker->unique()->uuid,
'title' => $this->faker->unique()->uuid,
'simp' => '',
'amount' => $this->faker->numberBetween(1, 1000) * 100,
'currency' => Currency::PLN,
'returnUrl' => '#',
'successReturnUrl' => '#',
'failureReturnUrl' => '#',
'customer' => CustomerDto::factory(),
'isActive' => true,
'validTo' => null,
'created' => $now->timestamp,
'modified' => $now->timestamp,
'serviceId' => config('services.imoje.service_key'),
];
}
}
31 changes: 31 additions & 0 deletions src/Factories/Casts/TransactionPaymentDtoFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\Factories\Casts;

use Routegroup\Imoje\Payment\DTO\Casts\TransactionPaymentDto;
use Routegroup\Imoje\Payment\Factories\Factory;
use Routegroup\Imoje\Payment\Types\Currency;
use Routegroup\Imoje\Payment\Types\TransactionStatus;

class TransactionPaymentDtoFactory extends Factory
{
protected $model = TransactionPaymentDto::class;

public function definition(): array
{
$now = now();

return [
'id' => $this->faker->unique()->uuid,
'amount' => $this->faker->numberBetween(1, 1000) * 100,
'status' => TransactionStatus::SETTLED,
'created' => $now->timestamp,
'orderId' => $this->faker->unique()->uuid,
'currency' => Currency::PLN,
'modified' => $now->timestamp,
'serviceId' => config('services.imoje.service_key'),
];
}
}
Loading

0 comments on commit f8c810f

Please sign in to comment.