generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from creagia/fix/amount-zero
fix: Allow requests with amount 0
- Loading branch information
Showing
3 changed files
with
151 additions
and
39 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
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); | ||
} | ||
} |
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
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 | ||
); | ||
}, | ||
], | ||
]); |