Skip to content

Commit

Permalink
Cast amount from Redsys notification to float
Browse files Browse the repository at this point in the history
  • Loading branch information
dtorras committed Jan 29, 2022
1 parent 33e8b81 commit 87b7335
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
13 changes: 13 additions & 0 deletions src/Casters/NotificationAmountCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Creagia\Redsys\Casters;

use Spatie\DataTransferObject\Caster;

class NotificationAmountCaster implements Caster
{
public function cast(mixed $value): float
{
return floatval($value / 100);
}
}
19 changes: 12 additions & 7 deletions src/RedsysRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function createPaymentRequest(
?RequestParameters $requestParameters = null
): array {
$this->parameters = $requestParameters ?? new RequestParameters();
$this->parameters->amount = number_format(round($amount, 2) * 100, 0, '', '');
$this->parameters->amount = $this->formatAmount($amount);
$this->parameters->currency = $currency->value;
$this->parameters->merchantCode = $this->redsysClient->merchantCode;
$this->parameters->order = $orderNumber;
Expand All @@ -49,6 +49,11 @@ public function createPaymentRequest(
return $this->getRequestFieldsArray();
}

private function formatAmount(float $amount): int
{
return number_format(round($amount, 2) * 100, 0, '', '');
}

#[ArrayShape(['Ds_SignatureVersion' => "string", 'Ds_MerchantParameters' => "string", 'Ds_Signature' => "string"])]
public function getRequestFieldsArray(): array
{
Expand All @@ -67,13 +72,13 @@ public function getFormHtml(): string

$formFields = $this->getRequestFieldsArray();

return '
<form action="' . $this->redsysClient->getBaseUrl() . '/realizarPago" method="post">
<input type="hidden" name="Ds_SignatureVersion" value="' . $formFields['Ds_SignatureVersion'] . '"/>
<input type="hidden" name="Ds_MerchantParameters" value="' . $formFields['Ds_MerchantParameters'] . '"/>
<input type="hidden" name="Ds_Signature" value="' . $formFields['Ds_Signature'] . '"/>
return <<<HTML
<form action="{$this->redsysClient->getBaseUrl()}/realizarPago" method="post">
<input type="hidden" name="Ds_SignatureVersion" value="{$formFields['Ds_SignatureVersion']}"/>
<input type="hidden" name="Ds_MerchantParameters" value="{$formFields['Ds_MerchantParameters']}"/>
<input type="hidden" name="Ds_Signature" value="{$formFields['Ds_Signature']}"/>
</form>
<script>document.forms[0].submit();</script>
';
HTML;
}
}
10 changes: 4 additions & 6 deletions src/Support/NotificationParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Creagia\Redsys\Support;

use Creagia\Redsys\Casters\NotificationAmountCaster;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\DataTransferObject;

class NotificationParameters extends DataTransferObject
Expand All @@ -13,7 +15,8 @@ class NotificationParameters extends DataTransferObject
public string $hour;

#[MapTo('DS_AMOUNT')]
public int $amount;
#[CastWith(NotificationAmountCaster::class)]
public float $amount;

#[MapTo('DS_CURRENCY')]
public int $currency;
Expand Down Expand Up @@ -85,9 +88,4 @@ public function toArray(): array
{
return array_filter(parent::toArray());
}

public function getAmount(): float
{
return (float) $this->amount / 100;
}
}
2 changes: 1 addition & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$redsysNotification->setParametersFromResponse($responsePost);
$result = $redsysNotification->checkResponse();

expect($result->getAmount())->toBe($this->paymentRequestAmount);
expect($result->amount)->toBe($this->paymentRequestAmount);
});

it('fails if signatures differ', function () {
Expand Down

0 comments on commit 87b7335

Please sign in to comment.