Skip to content

Commit

Permalink
Merge pull request #34 from creagia/fix/amount-zero
Browse files Browse the repository at this point in the history
fix: Allow requests with amount 0
  • Loading branch information
dtorras authored Jul 9, 2024
2 parents 84a7029 + 8179b2e commit 60b1439
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 39 deletions.
61 changes: 61 additions & 0 deletions src/Support/Arr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Creagia\Redsys\Support;

use Countable;
use Stringable;

class Arr
{
/**
* Determine if the given value is "blank".
* https://github.com/laravel/framework/blob/11.x/src/Illuminate/Support/helpers.php#L49
*
* @phpstan-assert-if-false !=null|'' $value
*
* @phpstan-assert-if-true !=numeric|bool $value
*
* @param mixed $value
* @return bool
*/
public static function blank(mixed $value): bool
{
if (is_null($value)) {
return true;
}

if (is_string($value)) {
return trim($value) === '';
}

if (is_numeric($value) || is_bool($value)) {
return false;
}

if ($value instanceof Countable) {
return count($value) === 0;
}

if ($value instanceof Stringable) {
return trim((string) $value) === '';
}

return empty($value);
}

/**
* Determine if a value is "filled".
* https://github.com/laravel/framework/blob/11.x/src/Illuminate/Support/helpers.php#L164
*
* @phpstan-assert-if-true !=null|'' $value
*
* @phpstan-assert-if-false !=numeric|bool $value
*
* @param mixed $value
* @return bool
*/
public static function filled(mixed $value): bool
{
return ! self::blank($value);
}
}
2 changes: 1 addition & 1 deletion src/Support/RequestParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,6 @@ public function toEncodedString(): string

public function toArray(): array
{
return array_filter(parent::toArray());
return array_filter(parent::toArray(), [Arr::class, 'filled']);
}
}
127 changes: 89 additions & 38 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,106 @@
<?php

beforeEach(function () {
$this->redsysClient = new \Creagia\Redsys\RedsysClient(
merchantCode: 123123,
secretKey: 123123,
terminal: 1,
environment: \Creagia\Redsys\Enums\Environment::Production,
);

$redsysRequest = \Creagia\Redsys\RedsysRequest::create(
$this->redsysClient,
new \Creagia\Redsys\Support\RequestParameters(
amountInCents: $this->paymentRequestAmount = 123_12,
order: 9999,
currency: \Creagia\Redsys\Enums\Currency::EUR,
transactionType: \Creagia\Redsys\Enums\TransactionType::Autorizacion,
)
);

$this->redsysFakeGateway = new \Creagia\Redsys\RedsysFakeGateway($redsysRequest->getRequestFieldsArray(), 123123);
});

it('can validate a valid signature', function () {
$responsePost = $this->redsysFakeGateway->getResponse('0000');

$redsysNotification = new \Creagia\Redsys\RedsysResponse($this->redsysClient);
it('can validate a valid signature', function (
\Creagia\Redsys\RedsysClient $redsysClient,
int $paymentRequestAmount,
\Creagia\Redsys\RedsysFakeGateway $redsysFakeGateway
) {
$responsePost = $redsysFakeGateway->getResponse('0000');

$redsysNotification = new \Creagia\Redsys\RedsysResponse($redsysClient);
$redsysNotification->setParametersFromResponse($responsePost);
$result = $redsysNotification->checkResponse();

expect($result->amount)->toBe($this->paymentRequestAmount);
});
expect($result->amount)->toBe($paymentRequestAmount);
})->with('gateway');

it('fails if signatures differ', function () {
$responsePost = $this->redsysFakeGateway->getResponse('0000');
it('fails if signatures differ', function (
\Creagia\Redsys\RedsysClient $redsysClient,
int $paymentRequestAmount,
\Creagia\Redsys\RedsysFakeGateway $redsysFakeGateway
) {
$responsePost = $redsysFakeGateway->getResponse('0000');
$responsePost['Ds_Signature'] = "123";

$redsysNotification = new \Creagia\Redsys\RedsysResponse($this->redsysClient);
$redsysNotification = new \Creagia\Redsys\RedsysResponse($redsysClient);
$redsysNotification->setParametersFromResponse($responsePost);
$redsysNotification->checkResponse();
})->throws(\Creagia\Redsys\Exceptions\InvalidRedsysResponseException::class, 'does not match');
})
->with('gateway')
->throws(\Creagia\Redsys\Exceptions\InvalidRedsysResponseException::class, 'does not match');

it('throws an exception if invalid response from Redsys', function () {
$redsysNotification = new \Creagia\Redsys\RedsysResponse($this->redsysClient);
it('throws an exception if invalid response from Redsys', function (
\Creagia\Redsys\RedsysClient $redsysClient,
int $paymentRequestAmount,
\Creagia\Redsys\RedsysFakeGateway $redsysFakeGateway
) {
$redsysNotification = new \Creagia\Redsys\RedsysResponse($redsysClient);
$redsysNotification->setParametersFromResponse([]);
$redsysNotification->checkResponse();
})->throws(\Creagia\Redsys\Exceptions\InvalidRedsysResponseException::class);
})
->with('gateway')
->throws(\Creagia\Redsys\Exceptions\InvalidRedsysResponseException::class);

it('throws an exception if denied response from Redsys', function () {
$responsePost = $this->redsysFakeGateway->getResponse('0184');
it('throws an exception if denied response from Redsys', function (
\Creagia\Redsys\RedsysClient $redsysClient,
int $paymentRequestAmount,
\Creagia\Redsys\RedsysFakeGateway $redsysFakeGateway
) {
$responsePost = $redsysFakeGateway->getResponse('0184');

$redsysNotification = new \Creagia\Redsys\RedsysResponse($this->redsysClient);
$redsysNotification = new \Creagia\Redsys\RedsysResponse($redsysClient);
$redsysNotification->setParametersFromResponse($responsePost);
$redsysNotification->checkResponse();
})->throws(\Creagia\Redsys\Exceptions\DeniedRedsysPaymentResponseException::class);
})
->with('gateway')
->throws(\Creagia\Redsys\Exceptions\DeniedRedsysPaymentResponseException::class);

$redsysClient = new \Creagia\Redsys\RedsysClient(
merchantCode: 123123,
secretKey: 123123,
terminal: 1,
environment: \Creagia\Redsys\Enums\Environment::Production,
);

dataset('gateway', [
[
$redsysClient,
$paymentRequestAmount = 123_12,
function () use ($redsysClient, $paymentRequestAmount) {
$redsysRequest = \Creagia\Redsys\RedsysRequest::create(
$redsysClient,
new \Creagia\Redsys\Support\RequestParameters(
amountInCents: $paymentRequestAmount,
transactionType: \Creagia\Redsys\Enums\TransactionType::Autorizacion,
currency: \Creagia\Redsys\Enums\Currency::EUR,
order: 9999,
)
);

return $this->redsysFakeGateway = new \Creagia\Redsys\RedsysFakeGateway(
$redsysRequest->getRequestFieldsArray(),
123123
);
},
],
[
$redsysClient,
$paymentRequestAmount = 0,
function () use ($redsysClient, $paymentRequestAmount) {
$redsysRequest = \Creagia\Redsys\RedsysRequest::create(
$redsysClient,
new \Creagia\Redsys\Support\RequestParameters(
amountInCents: $paymentRequestAmount,
transactionType: \Creagia\Redsys\Enums\TransactionType::Autorizacion,
currency: \Creagia\Redsys\Enums\Currency::EUR,
order: 9999,
)
);

return $this->redsysFakeGateway = new \Creagia\Redsys\RedsysFakeGateway(
$redsysRequest->getRequestFieldsArray(),
123123
);
},
],
]);

0 comments on commit 60b1439

Please sign in to comment.