Skip to content

Commit

Permalink
feat: simplify generic notification factory, usage fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kajetan-nobel committed Oct 4, 2023
1 parent 0842698 commit 75e0e05
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
36 changes: 19 additions & 17 deletions src/Factories/Notifications/GenericNotificationDtoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/Lib/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Types/TransactionStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/PlaygroundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand All @@ -18,4 +19,6 @@
$api = app(Api::class);
/** @var Paywall $paywall */
$paywall = app(Paywall::class);
/** @var Utils $utils */
$utils = app(Utils::class);
});

0 comments on commit 75e0e05

Please sign in to comment.