Skip to content

Commit

Permalink
feat: adds charge profile
Browse files Browse the repository at this point in the history
  • Loading branch information
kajetan-nobel committed Sep 21, 2023
1 parent 875c6fd commit 4c7175e
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 24 deletions.
24 changes: 5 additions & 19 deletions src/DTO/BaseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Routegroup\Imoje\Payment\DTO;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Fluent;
use Routegroup\Imoje\Payment\Lib\Utils;

/**
* @template TKey of array-key
Expand Down Expand Up @@ -44,7 +44,9 @@ public function castAttribute(string $castType, mixed $value): mixed
}

if (enum_exists($castType)) {
return $castType::from($value);
return $value instanceof $castType
? $value
: $castType::from($value);
}

return match ($castType) {
Expand All @@ -61,23 +63,7 @@ public function castAttribute(string $castType, mixed $value): mixed
*/
public function toArray(): array
{
$computedAttributes = [];

foreach ($this->attributes as $key => $attribute) {
$value = $attribute;

if ($value instanceof Arrayable) {
$value = $value->toArray();
}

if (is_object($value) && enum_exists(get_class($value))) {
$value = $value->value;
}

$computedAttributes[$key] = $value;
}

return $computedAttributes;
return app(Utils::class)->transformValues($this->attributes);
}

/**
Expand Down
17 changes: 12 additions & 5 deletions src/DTO/Request/ChargeProfileRequestDto.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Request;

use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\Lib\Utils;
use Routegroup\Imoje\Payment\Types\Currency;

/**
* @property-read string $serviceId
* @property-read string $paymentProfileId
* @property-read int $amount
* @property-read string $currency
* @property-read string $orderID
* @property-read Currency $currency
* @property-read string $orderId
* @property-read string $title
*/
class ChargeProfileRequestDto extends BaseDto
{
protected array $casts = [
'amount' => 'int',
'currency' => Currency::class,
];

public static function make(
#[ArrayShape([
// Required
'serviceId' => 'string',
'paymentProfileId' => 'string',
'amount' => 'int',
'currency' => 'string',
'orderID' => 'string',
'currency' => Currency::class,
'orderId' => 'string',
// Optional
'title' => 'string',
])] array $attributes
): BaseDto {
$utils = app(Utils::class);

$attributes = array_merge_recursive([
'type' => 'refund',
'serviceId' => $utils->serviceId,
], $attributes);

Expand Down
5 changes: 5 additions & 0 deletions src/DTO/Request/OneClickRequestDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
*/
class OneClickRequestDto extends BaseDto
{
protected array $casts = [
'amount' => 'int',
'validTo' => 'int',
];

public static function make(
#[ArrayShape([
// Required
Expand Down
4 changes: 4 additions & 0 deletions src/DTO/Request/RefundRequestDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
class RefundRequestDto extends BaseDto
{
protected array $casts = [
'amount' => 'int',
];

public static function make(
#[ArrayShape([
// Required
Expand Down
2 changes: 2 additions & 0 deletions src/DTO/Response/CancelledResponseDto.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Response;

use Routegroup\Imoje\Payment\DTO\BaseDto;
Expand Down
21 changes: 21 additions & 0 deletions src/DTO/Response/ChargeProfileResponseDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Response;

use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\DTO\Response\Api\TransactionResponseDto;

/**
* @property-read TransactionResponseDto $transaction
*/
class ChargeProfileResponseDto extends BaseDto
{
/**
* {@inheritdoc}
*/
protected array $casts = [
'transaction' => TransactionResponseDto::class,
];
}
11 changes: 11 additions & 0 deletions src/DTO/Response/NotificationDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public static function make(array $attributes): BaseDto
return CancelledResponseDto::make($attributes);
}

if (static::isChargeProfile($attributes)) {
return ChargeProfileResponseDto::make($attributes);
}

throw new NotImplementedException();
}

Expand All @@ -43,4 +47,11 @@ public static function isCancelled(array $attributes): bool
return array_key_exists('payment', $attributes)
&& $attributes['payment']['status'] === 'cancelled';
}

public static function isChargeProfile(array $attributes): bool
{
return array_key_exists('transaction', $attributes)
&& $attributes['transaction']['paymentMethod'] === 'card'
&& $attributes['transaction']['source'] === 'api';
}
}
11 changes: 11 additions & 0 deletions src/Lib/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Routegroup\Imoje\Payment\DTO\Request\ChargeProfileRequestDto;
use Routegroup\Imoje\Payment\DTO\Request\RefundRequestDto;

class Api
Expand All @@ -27,6 +28,16 @@ public function createRefund(
->post($url, $dto);
}

public function chargeProfile(
ChargeProfileRequestDto $dto
): Response {
$url = $this->utils->createChargeProfileUrl();

return $this
->request()
->post($url, $dto);
}

public function deactivateProfile(
string $profileId
): Response {
Expand Down
24 changes: 24 additions & 0 deletions src/Lib/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Routegroup\Imoje\Payment\Lib;

use Illuminate\Contracts\Support\Arrayable;
use Routegroup\Imoje\Payment\Types\Environment;
use Routegroup\Imoje\Payment\Types\HashMethod;

Expand Down Expand Up @@ -31,14 +32,37 @@ public function buildQuery(array $orderData): string
{
ksort($orderData);

$orderData = $this->transformValues($orderData);
$data = [];

foreach ($orderData as $key => $value) {
$data[] = $key.'='.$value;
}

return implode('&', $data);
}

public function transformValues(array $values): array
{
$computed = [];

foreach ($values as $key => $value) {
$result = $value;

if ($result instanceof Arrayable) {
$result = $result->toArray();
}

if (is_object($result) && enum_exists(get_class($result))) {
$result = $result->value;
}

$computed[$key] = $result;
}

return $computed;
}

public function createRefundUrl(string $transactionId): string
{
return implode('/', [
Expand Down
20 changes: 20 additions & 0 deletions tests/Lib/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Routegroup\Imoje\Payment\DTO\Request\ChargeProfileRequestDto;
use Routegroup\Imoje\Payment\DTO\Request\RefundRequestDto;
use Routegroup\Imoje\Payment\Lib\Api;
use Routegroup\Imoje\Payment\Lib\Utils;
use Routegroup\Imoje\Payment\Types\Currency;

beforeEach(function (): void {
Http::fake();
Expand All @@ -24,6 +26,24 @@
});
});

it('makes charge profile request', function (): void {
$dto = ChargeProfileRequestDto::make([
'amount' => 1000 * 100,
'currency' => Currency::PLN,
'paymentProfileId' => '123456789',
'orderId' => '1234',
]);

$this->api->chargeProfile($dto);

Http::assertSent(function (Request $request) {
return $request->url() === $this->utils->createChargeProfileUrl()
&& $request->isJson()
&& $request->method() === 'POST'
&& array_keys($request->data()) === ['serviceId', 'amount', 'currency', 'paymentProfileId', 'orderId'];
});
});

it('makes deactivate profile request', function (): void {
$this->api->deactivateProfile('$profile_id$');

Expand Down

0 comments on commit 4c7175e

Please sign in to comment.