Skip to content

Commit

Permalink
Add logs to the capture notifications (#23368)
Browse files Browse the repository at this point in the history
  • Loading branch information
lamasfoker authored and lruozzi9 committed Jun 28, 2022
1 parent ce77890 commit 2c201fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/Payum/Nexi/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Payum\Core\Request\Capture;
use Payum\Core\Request\GetHttpRequest;
use Payum\Core\Security\TokenInterface;
use Psr\Log\LoggerInterface;
use Sylius\Bundle\PayumBundle\Request\GetStatus;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
Expand All @@ -42,7 +43,8 @@ final class CaptureAction implements ActionInterface, ApiAwareInterface, Gateway
public function __construct(
private Signer $signer,
private Checker $checker,
private RequestParamsDecoderInterface $decoder
private RequestParamsDecoderInterface $decoder,
private LoggerInterface $logger
) {
}

Expand All @@ -55,6 +57,11 @@ public function setApi($api): void
$this->api = $api;
}

/**
* This action handle 2 requests: the POST is the server2server from the payment gateway to sylius
* and the GET is from the client browser to sylius.
* The latter contains the information to handle the request from the client browser to the payment gateway
*/
public function execute($request): void
{
RequestNotSupportedException::assertSupports($this, $request);
Expand All @@ -65,22 +72,25 @@ public function execute($request): void
/** @var PaymentInterface $payment */
$payment = $request->getFirstModel();

$isPost = false;
$isS2S = false;
/** @var array<string, string> $requestParams */
$requestParams = $httpRequest->query;
if (count($requestParams) === 0) {
/** @var array<string, string> $requestParams */
$requestParams = $httpRequest->request;
$isPost = true;
$isS2S = true;
}

$requestParams = $this->decoder->decode($requestParams);
$this->logger->debug('Nexi payment request parameters', ['parameter' => $requestParams, 'isS2S' => $isS2S]);

if (isset($requestParams['esito'])) {
/** @var ArrayObject $details */
$details = $request->getModel();
$this->logger->debug('Nexi payment request details', ['details' => $details, 'isS2S' => $isS2S]);

if ($requestParams['esito'] === Result::OUTCOME_ANNULLO) {
$this->logger->notice('Nexi payment status from http request is cancelled.');
$details->replace($requestParams);

return;
Expand All @@ -94,7 +104,7 @@ public function execute($request): void
);
$details->replace($requestParams);

if ($isPost) {
if ($isS2S) {
$this->gateway->execute(new GetStatus($payment));

throw new HttpResponse('200');
Expand Down Expand Up @@ -133,6 +143,7 @@ public function execute($request): void
);

$this->signer->sign($nexiRequest, $this->api->getMacKey(), SignatureMethod::SHA1_METHOD);
$this->logger->debug('Nexi payment request prepared for the client browser', ['request' => $nexiRequest->getParams()]);

throw new HttpPostRedirect(
$this->api->getApiEndpoint(),
Expand Down
17 changes: 15 additions & 2 deletions src/Payum/Nexi/Action/StatusAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,33 @@ public function execute($request): void
$details = ArrayObject::ensureArrayObject($request->getModel());

if (count($details->getArrayCopy()) === 0) {
$this->logger->warning('HTTP Request has not payment details');

return;
}

if ($details->get('esito') === Result::OUTCOME_OK) {
$this->logger->info(
'Nexi payment status is ok.',
$details->getArrayCopy()
);
$request->markCaptured();

return;
}

if ($details->get('esito') === Result::OUTCOME_ANNULLO) {
$this->logger->notice(
'Nexi payment status is cancelled.',
$details->getArrayCopy()
);
$request->markCanceled();

return;
}

if (in_array($details->get('esito'), [Result::OUTCOME_KO, Result::OUTCOME_ERRORE], true)) {
$this->logger->error(
$this->logger->warning(
'Nexi payment status is not ok or canceled and will be marked as failed.',
$details->getArrayCopy()
);
Expand All @@ -49,7 +59,10 @@ public function execute($request): void
return;
}

$this->logger->error('Nexi payment status is invalid and will be marked as unknown.', $details->getArrayCopy());
$this->logger->warning(
'Nexi payment status is invalid and will be marked as unknown.',
$details->getArrayCopy()
);
$request->markUnknown();
}

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<argument type="service" id="webgriffe_sylius_nexi.lib.signer" />
<argument type="service" id="webgriffe_sylius_nexi.lib.checker" />
<argument type="service" id="webgriffe_sylius_nexi.decoder.request_params"/>
<argument type="service" id="webgriffe_sylius_nexi.logger" />
</service>

<service id="webgriffe_sylius_nexi.lib.signer" class="Webgriffe\LibQuiPago\Signature\DefaultSigner">
Expand Down

0 comments on commit 2c201fe

Please sign in to comment.