generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88370a4
commit 4d650f1
Showing
9 changed files
with
155 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Factories/Notifications/OneClickNotificationDtoFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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\Notifications\OneClickNotificationDto; | ||
use Routegroup\Imoje\Payment\Factories\Factory; | ||
|
||
class OneClickNotificationDtoFactory extends Factory | ||
{ | ||
protected $model = OneClickNotificationDto::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'transaction' => TransactionDto::factory()->asOneClick(), | ||
'payment' => PaymentDto::factory(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Routegroup\Imoje\Payment\Services; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Arr; | ||
use Routegroup\Imoje\Payment\DTO\BaseDto; | ||
use Routegroup\Imoje\Payment\DTO\Notifications\OneClickNotificationDto; | ||
use Routegroup\Imoje\Payment\Exceptions\NotImplementedException; | ||
use Routegroup\Imoje\Payment\Exceptions\SchemaValidationException; | ||
use Routegroup\Imoje\Payment\Lib\Validator; | ||
use Routegroup\Imoje\Payment\Types\PaymentMethod; | ||
use Routegroup\Imoje\Payment\Types\PaymentMethodCode; | ||
use Routegroup\Imoje\Payment\Types\TransactionSource; | ||
use Routegroup\Imoje\Payment\Types\TransactionType; | ||
|
||
class NotificationService | ||
{ | ||
public function __construct( | ||
public readonly Validator $validator | ||
) { | ||
} | ||
|
||
/** | ||
* @throws SchemaValidationException | ||
* @throws NotImplementedException | ||
*/ | ||
public function resolve(Request $request): BaseDto | ||
{ | ||
$data = $request->toArray(); | ||
|
||
$this->validator->fromNotification($data); | ||
|
||
if ($this->isOneClick($data)) { | ||
return new OneClickNotificationDto($data); | ||
} | ||
|
||
throw new NotImplementedException($data); | ||
} | ||
|
||
protected function isOneClick(array $data): bool | ||
{ | ||
return $this->isEqualWithStructure($data, [ | ||
'transaction' => [ | ||
'type' => TransactionType::SALE->value, | ||
'source' => TransactionSource::WEB->value, | ||
'paymentMethod' => PaymentMethod::CARD->value, | ||
'paymentMethodCode' => PaymentMethodCode::ONECLICK->value, | ||
], | ||
]); | ||
} | ||
|
||
protected function isEqualWithStructure(array $data, array $structure): bool | ||
{ | ||
$structure = Arr::dot($structure); | ||
$data = Arr::dot($data); | ||
|
||
return count($structure) === count(array_intersect($structure, $data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
use Illuminate\Http\Request; | ||
use Routegroup\Imoje\Payment\DTO\BaseDto; | ||
use Routegroup\Imoje\Payment\DTO\Notifications\OneClickNotificationDto; | ||
use Routegroup\Imoje\Payment\Services\NotificationService; | ||
|
||
beforeEach(function (): void { | ||
$this->service = app(NotificationService::class); | ||
}); | ||
|
||
function mockRequest(BaseDto $dto): Request | ||
{ | ||
return new Request($dto->toArray()); | ||
} | ||
|
||
it('returns OneClickNotification', function (): void { | ||
$request = mockRequest(OneClickNotificationDto::factory()->make()); | ||
$result = $this->service->resolve($request); | ||
expect($result)->toBeInstanceOf(OneClickNotificationDto::class); | ||
}); |