From 1d976c772cc9635ed616c9ca3dc8db051c0181ab Mon Sep 17 00:00:00 2001 From: Kajetan Nobel Date: Wed, 4 Oct 2023 01:01:29 +0200 Subject: [PATCH] feat: test rest of api --- src/DTO/Api/TransactionDto.php | 2 +- src/DTO/Responses/PaymentResponseDto.php | 6 ++-- src/Factories/Api/TransactionDtoFactory.php | 6 ++-- src/Factories/Casts/ActionDtoFactory.php | 2 +- src/Factories/Casts/CustomerDtoFactory.php | 10 +++++-- src/Factories/Casts/PaymentDtoFactory.php | 8 ++--- src/Factories/Casts/TransactionDtoFactory.php | 17 +++++++++-- .../CancelPaymentResponseDtoFactory.php | 30 ++++++++++++++++++- .../Responses/PaymentResponseDtoFactory.php | 21 +++++++++++++ .../TransactionResponseDtoFactory.php | 2 +- tests/Lib/ApiTest.php | 27 +++++++++++++++++ 11 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 src/Factories/Responses/PaymentResponseDtoFactory.php diff --git a/src/DTO/Api/TransactionDto.php b/src/DTO/Api/TransactionDto.php index ca7b489..131b247 100644 --- a/src/DTO/Api/TransactionDto.php +++ b/src/DTO/Api/TransactionDto.php @@ -53,7 +53,7 @@ class TransactionDto extends BaseDto 'billing' => BillingDto::class, 'shipping' => BillingDto::class, 'card' => CardDto::class, - 'additionalData' => CardAdditionalDataDto::class, + // 'additionalData' => 'array' @todo // 'multipayout' => 'array' @todo // 'invoice' => 'array' @todo ]; diff --git a/src/DTO/Responses/PaymentResponseDto.php b/src/DTO/Responses/PaymentResponseDto.php index 8d36057..e9ea014 100644 --- a/src/DTO/Responses/PaymentResponseDto.php +++ b/src/DTO/Responses/PaymentResponseDto.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Routegroup\Imoje\Payment\DTO\Casts\PaymentDto; -use Routegroup\Imoje\Payment\Factories\Api\PaymentDtoFactory; +use Routegroup\Imoje\Payment\Factories\Responses\PaymentResponseDtoFactory; /** * @property-read PaymentDto $payment @@ -19,8 +19,8 @@ class PaymentResponseDto extends ResponseDto 'payment' => PaymentDto::class, ]; - protected static function newFactory(): PaymentDtoFactory + protected static function newFactory(): PaymentResponseDtoFactory { - return PaymentDtoFactory::new(); + return PaymentResponseDtoFactory::new(); } } diff --git a/src/Factories/Api/TransactionDtoFactory.php b/src/Factories/Api/TransactionDtoFactory.php index 1589531..28d882f 100644 --- a/src/Factories/Api/TransactionDtoFactory.php +++ b/src/Factories/Api/TransactionDtoFactory.php @@ -20,9 +20,9 @@ class TransactionDtoFactory extends Factory public function definition(): array { return [ - 'amount' => $this->faker->numberBetween(1, 100) * 100, + 'amount' => $this->faker->numberBetween(1, 100000) * 100, 'currency' => Currency::PLN, - 'orderId' => $this->faker->uuid, + 'orderId' => $this->faker->unique()->uuid, 'paymentMethod' => PaymentMethod::PAY_BY_LINK, 'paymentMethodCode' => PaymentMethodCode::IPKO, 'successReturnUrl' => 'https://imoje.requestcatcher.com', @@ -30,8 +30,6 @@ public function definition(): array 'customer' => CustomerDto::factory(), 'billing' => BillingDto::factory(), 'shipping' => BillingDto::factory(), - // 'validTo' => '', - // 'title' => '', ]; } diff --git a/src/Factories/Casts/ActionDtoFactory.php b/src/Factories/Casts/ActionDtoFactory.php index e12859e..9a8bfa5 100644 --- a/src/Factories/Casts/ActionDtoFactory.php +++ b/src/Factories/Casts/ActionDtoFactory.php @@ -16,7 +16,7 @@ public function definition(): array { return [ 'type' => ActionType::REDIRECT, - 'url' => '', + 'url' => "https://sandbox.paywall.imoje.pl/redirect/{$this->faker->uuid}/{$this->faker->uuid}", 'method' => 'GET', 'contentType' => '', 'contentBodyRaw' => '', diff --git a/src/Factories/Casts/CustomerDtoFactory.php b/src/Factories/Casts/CustomerDtoFactory.php index 611d34b..2b60392 100644 --- a/src/Factories/Casts/CustomerDtoFactory.php +++ b/src/Factories/Casts/CustomerDtoFactory.php @@ -17,9 +17,15 @@ public function definition(): array 'firstName' => $this->faker->firstName, 'lastName' => $this->faker->lastName, 'email' => $this->faker->email, - 'cid' => $this->faker->numerify('##############'), - 'company' => $this->faker->company, 'phone' => $this->faker->numerify('#########'), ]; } + + public function asCard(): static + { + return $this->state([ + 'cid' => $this->faker->numerify('##############'), + 'company' => $this->faker->company, + ]); + } } diff --git a/src/Factories/Casts/PaymentDtoFactory.php b/src/Factories/Casts/PaymentDtoFactory.php index f108f18..70ab001 100644 --- a/src/Factories/Casts/PaymentDtoFactory.php +++ b/src/Factories/Casts/PaymentDtoFactory.php @@ -19,16 +19,16 @@ public function definition(): array return [ 'id' => $this->faker->unique()->uuid, - 'url' => '#', + 'url' => "https://sandbox.paywall.imoje.pl/pay/{$this->faker->uuid}", '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' => '#', + 'returnUrl' => 'https://imoje.requestcatcher.com', + 'successReturnUrl' => 'https://imoje.requestcatcher.com', + 'failureReturnUrl' => 'https://imoje.requestcatcher.com', 'customer' => CustomerDto::factory(), 'isActive' => true, 'validTo' => null, diff --git a/src/Factories/Casts/TransactionDtoFactory.php b/src/Factories/Casts/TransactionDtoFactory.php index bab1899..8959cb6 100644 --- a/src/Factories/Casts/TransactionDtoFactory.php +++ b/src/Factories/Casts/TransactionDtoFactory.php @@ -29,14 +29,25 @@ public function definition(): array 'created' => $now->timestamp, 'modified' => $now->timestamp, 'serviceId' => config('services.imoje.service_key'), - 'amount' => $this->faker->numberBetween(1, 100) * 100, + 'amount' => $this->faker->numberBetween(1, 1000) * 100, 'currency' => Currency::PLN, 'orderId' => $this->faker->unique()->uuid, - 'paymentMethod' => $this->faker->randomElement(PaymentMethod::cases()), - 'paymentMethodCode' => $this->faker->randomElement(PaymentMethodCode::cases()), + 'paymentMethod' => PaymentMethod::PAY_BY_LINK, + 'paymentMethodCode' => PaymentMethodCode::IPKO, ]; } + public function asApiPending(): static + { + return $this->state([ + 'type' => TransactionType::SALE, + 'status' => TransactionStatus::PENDING, + 'source' => TransactionSource::API, + 'notificationUrl' => 'https://imoje.requestcatcher.com/', + + ]); + } + public function asOneClick(): static { return $this->state([ diff --git a/src/Factories/Responses/CancelPaymentResponseDtoFactory.php b/src/Factories/Responses/CancelPaymentResponseDtoFactory.php index e42282b..0cc8075 100644 --- a/src/Factories/Responses/CancelPaymentResponseDtoFactory.php +++ b/src/Factories/Responses/CancelPaymentResponseDtoFactory.php @@ -4,8 +4,11 @@ namespace Routegroup\Imoje\Payment\Factories\Responses; +use Routegroup\Imoje\Payment\DTO\Casts\CustomerDto; use Routegroup\Imoje\Payment\DTO\Responses\CancelPaymentResponseDto; use Routegroup\Imoje\Payment\Factories\Factory; +use Routegroup\Imoje\Payment\Types\Currency; +use Routegroup\Imoje\Payment\Types\TransactionStatus; class CancelPaymentResponseDtoFactory extends Factory { @@ -13,6 +16,31 @@ class CancelPaymentResponseDtoFactory extends Factory public function definition(): array { - return []; + $now = now(); + + return [ + 'id' => $this->faker->unique()->uuid, + 'url' => "https://sandbox.paywall.imoje.pl/pay/{$this->faker->unique()->uuid}", + 'serviceId' => config('services.imoje.service_key'), + 'orderId' => $this->faker->unique()->uuid, + 'title' => '', + 'simp' => '', + 'amount' => $this->faker->numberBetween(1, 1000) * 100, + 'currency' => Currency::PLN, + 'status' => TransactionStatus::CANCELLED, + 'isActive' => true, + 'validTo' => null, + 'created' => $now->timestamp, + 'modified' => $now->timestamp, + 'isGenerated' => false, + 'isUsed' => false, + 'usedAt' => null, + 'isConfirmVisited' => false, + 'confirmVisitedAt' => null, + 'returnUrl' => 'https://imoje.requestcatcher.com', + 'failureReturnUrl' => 'https://imoje.requestcatcher.com', + 'successReturnUrl' => 'https://imoje.requestcatcher.com', + 'customer' => CustomerDto::factory(), + ]; } } diff --git a/src/Factories/Responses/PaymentResponseDtoFactory.php b/src/Factories/Responses/PaymentResponseDtoFactory.php new file mode 100644 index 0000000..b3eb903 --- /dev/null +++ b/src/Factories/Responses/PaymentResponseDtoFactory.php @@ -0,0 +1,21 @@ + PaymentDto::factory(), + ]; + } +} diff --git a/src/Factories/Responses/TransactionResponseDtoFactory.php b/src/Factories/Responses/TransactionResponseDtoFactory.php index 992d6b3..7b580ae 100644 --- a/src/Factories/Responses/TransactionResponseDtoFactory.php +++ b/src/Factories/Responses/TransactionResponseDtoFactory.php @@ -16,7 +16,7 @@ class TransactionResponseDtoFactory extends Factory public function definition(): array { return [ - 'transaction' => TransactionDto::factory(), + 'transaction' => TransactionDto::factory()->asApiPending(), 'action' => ActionDto::factory(), ]; } diff --git a/tests/Lib/ApiTest.php b/tests/Lib/ApiTest.php index 26bf331..c680231 100644 --- a/tests/Lib/ApiTest.php +++ b/tests/Lib/ApiTest.php @@ -1,11 +1,17 @@ api); expect($response)->toBeInstanceOf($instance); })->with([ + 'successfully calls create transaction' => [ + fn (Api $api) => [ + $api->url->createTransactionUrl() => Http::response(TransactionResponseDto::factory()->make()->toArray()), + ], + fn (Api $api) => $api->createTransaction(TransactionDto::factory()->make()), + TransactionResponseDto::class, + ], + 'successfully calls create payment' => [ + fn (Api $api) => [ + $api->url->createPaymentUrl() => Http::response(PaymentResponseDto::factory()->make()->toArray()), + ], + fn (Api $api) => $api->createPayment(PaymentDto::factory()->make()), + PaymentResponseDto::class, + ], + 'successfully calls cancel payment' => [ + fn (Api $api) => [ + $api->url->createCancelPaymentUrl() => Http::response(CancelPaymentResponseDto::factory()->make()->toArray()), + ], + fn (Api $api) => $api->cancelPayment(CancelPaymentDto::factory()->make()), + CancelPaymentResponseDto::class, + ], 'successfully calls refund' => [ fn (Api $api) => [ $api->url->createRefundUrl('$transaction_id$') => Http::response(RefundResponseDto::factory()->make()->toArray()),