From 75e0e053e8cb775c23924869034312562d132912 Mon Sep 17 00:00:00 2001 From: Kajetan Nobel Date: Wed, 4 Oct 2023 19:15:46 +0200 Subject: [PATCH] feat: simplify generic notification factory, usage fixes --- .../GenericNotificationDtoFactory.php | 36 ++++++++++--------- src/Lib/Utils.php | 2 +- src/Types/TransactionStatus.php | 4 +++ tests/PlaygroundTest.php | 3 ++ 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/Factories/Notifications/GenericNotificationDtoFactory.php b/src/Factories/Notifications/GenericNotificationDtoFactory.php index a85df14..0033c39 100644 --- a/src/Factories/Notifications/GenericNotificationDtoFactory.php +++ b/src/Factories/Notifications/GenericNotificationDtoFactory.php @@ -4,6 +4,7 @@ namespace Routegroup\Imoje\Payment\Factories\Notifications; +use Illuminate\Database\Eloquent\Model; use Routegroup\Imoje\Payment\DTO\Casts\ActionDto; use Routegroup\Imoje\Payment\DTO\Casts\TransactionDto; use Routegroup\Imoje\Payment\DTO\Casts\TransactionPaymentDto; @@ -18,41 +19,42 @@ class GenericNotificationDtoFactory extends Factory public function definition(): array { - $orderId = $this->faker->unique()->uuid; - $transactionId = $this->faker->unique()->uuid; - return [ - 'transaction' => TransactionDto::factory()->state([ - 'id' => $transactionId, - 'title' => $orderId, - 'orderId' => $orderId, + 'transaction' => TransactionDto::factory()->withPaymentProfile()->state([ 'type' => TransactionType::SALE, 'status' => TransactionStatus::SETTLED, ]), 'payment' => TransactionPaymentDto::factory()->state([ - 'id' => $transactionId, - 'orderId' => $orderId, 'status' => TransactionStatus::SETTLED, ]), ]; } - public function asPending(): static + public function make($attributes = [], Model $parent = null): GenericNotificationDto { - $orderId = $this->faker->unique()->uuid; - $transactionId = $this->faker->unique()->uuid; + $dto = parent::make($attributes, $parent); + + $orderId = $attributes['orderId'] ?? $this->faker->unique()->uuid; + $transactionId = $attributes['transactionId'] ?? $this->faker->unique()->uuid; + + $data = $dto->toArray(); + $data['transaction']['id'] = $transactionId; + $data['transaction']['title'] = $orderId; + $data['transaction']['orderId'] = $orderId; + $data['payment']['id'] = $transactionId; + $data['payment']['orderId'] = $orderId; + return new GenericNotificationDto($data); + } + + public function asPending(): static + { return $this->state([ 'transaction' => TransactionDto::factory()->state([ - 'id' => $transactionId, - 'title' => $orderId, - 'orderId' => $orderId, 'type' => TransactionType::SALE, 'status' => TransactionStatus::PENDING, ]), 'payment' => TransactionPaymentDto::factory()->state([ - 'id' => $transactionId, - 'orderId' => $orderId, 'status' => TransactionStatus::PENDING, ]), 'action' => ActionDto::factory(), diff --git a/src/Lib/Utils.php b/src/Lib/Utils.php index 6b40910..26aa2e3 100644 --- a/src/Lib/Utils.php +++ b/src/Lib/Utils.php @@ -30,7 +30,7 @@ public function verifySignature( array $body, HashMethod $hashMethod ): bool { - $body = json_encode($body, JSON_UNESCAPED_SLASHES); + $body = json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); return $signature === hash($hashMethod->value, $body.$this->config->serviceKey); } diff --git a/src/Types/TransactionStatus.php b/src/Types/TransactionStatus.php index bb377ee..661072e 100644 --- a/src/Types/TransactionStatus.php +++ b/src/Types/TransactionStatus.php @@ -22,6 +22,10 @@ public function canChange(TransactionStatus $newStatus): bool return false; } + if ($this === self::SETTLED && $newStatus === self::REFUND) { + return true; + } + if ( in_array($this, [ self::ERROR, diff --git a/tests/PlaygroundTest.php b/tests/PlaygroundTest.php index ecfa76e..a981448 100644 --- a/tests/PlaygroundTest.php +++ b/tests/PlaygroundTest.php @@ -2,6 +2,7 @@ use Routegroup\Imoje\Payment\Lib\Api; use Routegroup\Imoje\Payment\Lib\Paywall; +use Routegroup\Imoje\Payment\Lib\Utils; beforeEach(function (): void { config()->set('services.imoje', [ @@ -18,4 +19,6 @@ $api = app(Api::class); /** @var Paywall $paywall */ $paywall = app(Paywall::class); + /** @var Utils $utils */ + $utils = app(Utils::class); });