Files
2026-09-15 10:35:56 +08:00

56 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\OceanpaymentKlarna\Controller\Payment;
use Esun\OceanpaymentKlarna\Model\Callback\Parser;
use Esun\OceanpaymentKlarna\Model\Callback\Processor;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
use Psr\Log\LoggerInterface;
class Back extends AbstractCallback implements HttpGetActionInterface, HttpPostActionInterface
{
public function __construct(
Context $context,
private readonly Parser $parser,
private readonly Processor $processor,
private readonly LoggerInterface $logger
) {
parent::__construct($context);
}
public function execute(): ResultRedirect
{
/** @var ResultRedirect $result */
$result = $this->resultRedirectFactory->create();
try {
$data = $this->parser->parse(
(string)$this->getRequest()->getContent(),
(array)$this->getRequest()->getParams()
);
$paymentResult = $this->processor->process($data, false);
if ($paymentResult === Processor::FAILURE) {
$this->messageManager->addErrorMessage(
__('Klarna payment was not completed: %1', $data['payment_details'])
);
} elseif ($paymentResult === Processor::PENDING) {
$this->messageManager->addNoticeMessage(
__('Your Klarna authorization is being confirmed. The order will update automatically.')
);
}
} catch (\Throwable $exception) {
$this->logger->warning('Oceanpayment Klarna browser callback was rejected', [
'exception' => $exception,
]);
$this->messageManager->addErrorMessage(
__('The Klarna payment result could not be verified. Please contact customer service if charged.')
);
}
return $result->setPath('checkout/onepage/success');
}
}