From f8c810fdfe77ff3419e8d99fb48928fa9ccb5d67 Mon Sep 17 00:00:00 2001 From: Kajetan Nobel Date: Tue, 3 Oct 2023 23:08:46 +0200 Subject: [PATCH] feat: add createPayment method --- src/DTO/Api/PaymentDto.php | 77 +++++++++++++++++++ src/DTO/BaseDto.php | 4 +- src/DTO/Casts/CustomerDto.php | 2 +- src/DTO/Casts/PaymentDto.php | 25 +++--- src/DTO/Casts/TransactionDto.php | 2 + src/DTO/Casts/TransactionPaymentDto.php | 43 +++++++++++ .../Paywall/OneClickNotificationDto.php | 6 +- .../Paywall/TransactionNotificationDto.php | 6 +- .../Notifications/RefundNotificationDto.php | 6 +- src/DTO/Responses/PaymentResponseDto.php | 26 +++++++ src/Factories/Api/PaymentDtoFactory.php | 28 +++++++ src/Factories/Casts/PaymentDtoFactory.php | 18 +++-- .../Casts/TransactionPaymentDtoFactory.php | 31 ++++++++ .../OneClickNotificationDtoFactory.php | 4 +- .../TransactionNotificationDtoFactory.php | 4 +- .../RefundNotificationDtoFactory.php | 4 +- src/Lib/Api.php | 12 +++ src/Lib/Url.php | 10 +++ 18 files changed, 273 insertions(+), 35 deletions(-) create mode 100644 src/DTO/Api/PaymentDto.php create mode 100644 src/DTO/Casts/TransactionPaymentDto.php create mode 100644 src/DTO/Responses/PaymentResponseDto.php create mode 100644 src/Factories/Api/PaymentDtoFactory.php create mode 100644 src/Factories/Casts/TransactionPaymentDtoFactory.php diff --git a/src/DTO/Api/PaymentDto.php b/src/DTO/Api/PaymentDto.php new file mode 100644 index 0000000..8bf24ad --- /dev/null +++ b/src/DTO/Api/PaymentDto.php @@ -0,0 +1,77 @@ + '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(); + } +} diff --git a/src/DTO/BaseDto.php b/src/DTO/BaseDto.php index 0bd7046..16c396a 100644 --- a/src/DTO/BaseDto.php +++ b/src/DTO/BaseDto.php @@ -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)) { diff --git a/src/DTO/Casts/CustomerDto.php b/src/DTO/Casts/CustomerDto.php index 1b955be..bdb6616 100644 --- a/src/DTO/Casts/CustomerDto.php +++ b/src/DTO/Casts/CustomerDto.php @@ -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 { diff --git a/src/DTO/Casts/PaymentDto.php b/src/DTO/Casts/PaymentDto.php index e4165ed..a277aae 100644 --- a/src/DTO/Casts/PaymentDto.php +++ b/src/DTO/Casts/PaymentDto.php @@ -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 diff --git a/src/DTO/Casts/TransactionDto.php b/src/DTO/Casts/TransactionDto.php index c71e0ab..51c3bd4 100644 --- a/src/DTO/Casts/TransactionDto.php +++ b/src/DTO/Casts/TransactionDto.php @@ -37,6 +37,8 @@ class TransactionDto extends BaseDto { use HasFactory; + protected bool $allowNull = true; + protected array $casts = [ 'id' => 'string', 'type' => TransactionType::class, diff --git a/src/DTO/Casts/TransactionPaymentDto.php b/src/DTO/Casts/TransactionPaymentDto.php new file mode 100644 index 0000000..395bc95 --- /dev/null +++ b/src/DTO/Casts/TransactionPaymentDto.php @@ -0,0 +1,43 @@ + '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(); + } +} diff --git a/src/DTO/Notifications/Paywall/OneClickNotificationDto.php b/src/DTO/Notifications/Paywall/OneClickNotificationDto.php index a28dce0..3c3cbc7 100644 --- a/src/DTO/Notifications/Paywall/OneClickNotificationDto.php +++ b/src/DTO/Notifications/Paywall/OneClickNotificationDto.php @@ -6,13 +6,13 @@ 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 { @@ -20,7 +20,7 @@ class OneClickNotificationDto extends BaseDto protected array $casts = [ 'transaction' => TransactionDto::class, - 'payment' => PaymentDto::class, + 'payment' => TransactionPaymentDto::class, ]; protected static function newFactory(): OneClickNotificationDtoFactory diff --git a/src/DTO/Notifications/Paywall/TransactionNotificationDto.php b/src/DTO/Notifications/Paywall/TransactionNotificationDto.php index efdea4a..25f881c 100644 --- a/src/DTO/Notifications/Paywall/TransactionNotificationDto.php +++ b/src/DTO/Notifications/Paywall/TransactionNotificationDto.php @@ -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 @@ -22,7 +22,7 @@ class TransactionNotificationDto extends BaseDto protected array $casts = [ 'transaction' => TransactionDto::class, - 'payment' => PaymentDto::class, + 'payment' => TransactionPaymentDto::class, 'action' => ActionDto::class, ]; diff --git a/src/DTO/Notifications/RefundNotificationDto.php b/src/DTO/Notifications/RefundNotificationDto.php index e00e7e9..543a61a 100644 --- a/src/DTO/Notifications/RefundNotificationDto.php +++ b/src/DTO/Notifications/RefundNotificationDto.php @@ -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, ]; } diff --git a/src/DTO/Responses/PaymentResponseDto.php b/src/DTO/Responses/PaymentResponseDto.php new file mode 100644 index 0000000..8d36057 --- /dev/null +++ b/src/DTO/Responses/PaymentResponseDto.php @@ -0,0 +1,26 @@ + PaymentDto::class, + ]; + + protected static function newFactory(): PaymentDtoFactory + { + return PaymentDtoFactory::new(); + } +} diff --git a/src/Factories/Api/PaymentDtoFactory.php b/src/Factories/Api/PaymentDtoFactory.php new file mode 100644 index 0000000..4c5fb81 --- /dev/null +++ b/src/Factories/Api/PaymentDtoFactory.php @@ -0,0 +1,28 @@ + $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', + ]; + } +} diff --git a/src/Factories/Casts/PaymentDtoFactory.php b/src/Factories/Casts/PaymentDtoFactory.php index 9b48163..f108f18 100644 --- a/src/Factories/Casts/PaymentDtoFactory.php +++ b/src/Factories/Casts/PaymentDtoFactory.php @@ -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 { @@ -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'), ]; } } diff --git a/src/Factories/Casts/TransactionPaymentDtoFactory.php b/src/Factories/Casts/TransactionPaymentDtoFactory.php new file mode 100644 index 0000000..40d2687 --- /dev/null +++ b/src/Factories/Casts/TransactionPaymentDtoFactory.php @@ -0,0 +1,31 @@ + $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'), + ]; + } +} diff --git a/src/Factories/Notifications/Paywall/OneClickNotificationDtoFactory.php b/src/Factories/Notifications/Paywall/OneClickNotificationDtoFactory.php index 804fe25..d424b5c 100644 --- a/src/Factories/Notifications/Paywall/OneClickNotificationDtoFactory.php +++ b/src/Factories/Notifications/Paywall/OneClickNotificationDtoFactory.php @@ -4,8 +4,8 @@ namespace Routegroup\Imoje\Payment\Factories\Notifications\Paywall; -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\DTO\Notifications\Paywall\OneClickNotificationDto; use Routegroup\Imoje\Payment\Factories\Factory; @@ -17,7 +17,7 @@ public function definition(): array { return [ 'transaction' => TransactionDto::factory()->asOneClick(), - 'payment' => PaymentDto::factory(), + 'payment' => TransactionPaymentDto::factory(), ]; } } diff --git a/src/Factories/Notifications/Paywall/TransactionNotificationDtoFactory.php b/src/Factories/Notifications/Paywall/TransactionNotificationDtoFactory.php index cb395d3..d0faca9 100644 --- a/src/Factories/Notifications/Paywall/TransactionNotificationDtoFactory.php +++ b/src/Factories/Notifications/Paywall/TransactionNotificationDtoFactory.php @@ -5,8 +5,8 @@ namespace Routegroup\Imoje\Payment\Factories\Notifications\Paywall; 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\DTO\Notifications\Paywall\TransactionNotificationDto; use Routegroup\Imoje\Payment\Factories\Factory; @@ -18,7 +18,7 @@ public function definition(): array { return [ 'transaction' => TransactionDto::factory(), - 'payment' => PaymentDto::factory(), + 'payment' => TransactionPaymentDto::factory(), 'action' => ActionDto::factory(), ]; } diff --git a/src/Factories/Notifications/RefundNotificationDtoFactory.php b/src/Factories/Notifications/RefundNotificationDtoFactory.php index 861c3ab..496187a 100644 --- a/src/Factories/Notifications/RefundNotificationDtoFactory.php +++ b/src/Factories/Notifications/RefundNotificationDtoFactory.php @@ -4,8 +4,8 @@ namespace Routegroup\Imoje\Payment\Factories\Notifications; -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\DTO\Notifications\RefundNotificationDto; use Routegroup\Imoje\Payment\Factories\Factory; @@ -18,7 +18,7 @@ public function definition(): array return [ 'transaction' => TransactionDto::factory(), 'originalTransactionId' => $this->faker->unique()->uuid, - 'payment' => PaymentDto::factory(), + 'payment' => TransactionPaymentDto::factory(), ]; } } diff --git a/src/Lib/Api.php b/src/Lib/Api.php index a918737..bdc7d2f 100644 --- a/src/Lib/Api.php +++ b/src/Lib/Api.php @@ -8,9 +8,11 @@ use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Http; use Routegroup\Imoje\Payment\DTO\Api\ChargeProfileDto; +use Routegroup\Imoje\Payment\DTO\Api\PaymentDto; use Routegroup\Imoje\Payment\DTO\Api\RefundDto; use Routegroup\Imoje\Payment\DTO\Api\TransactionDto; use Routegroup\Imoje\Payment\DTO\Responses\ChargeProfileResponseDto; +use Routegroup\Imoje\Payment\DTO\Responses\PaymentResponseDto; use Routegroup\Imoje\Payment\DTO\Responses\ProfileResponseDto; use Routegroup\Imoje\Payment\DTO\Responses\RefundResponseDto; use Routegroup\Imoje\Payment\DTO\Responses\TransactionResponseDto; @@ -34,6 +36,16 @@ public function createTransaction( return new TransactionResponseDto($response); } + public function createPayment( + PaymentDto $dto + ): PaymentResponseDto { + $url = $this->url->createPaymentUrl(); + $response = $this->request()->post($url, $dto); + $this->validateResponse($response, $dto->toArray()); + + return new PaymentResponseDto($response); + } + public function createRefund( RefundDto $dto, string $transactionId diff --git a/src/Lib/Url.php b/src/Lib/Url.php index 00742c0..5995f94 100644 --- a/src/Lib/Url.php +++ b/src/Lib/Url.php @@ -21,6 +21,16 @@ public function createTransactionUrl(): string ]); } + public function createPaymentUrl(): string + { + return implode('/', [ + $this->config->env->apiUrl(), + 'merchant', + $this->config->merchantId, + 'payment', + ]); + } + public function createRefundUrl(string $transactionId): string { return implode('/', [