This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Esun\OceanpaymentKlarna\Controller\Payment;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
abstract class AbstractCallback extends Action implements CsrfAwareActionInterface
{
public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
{
return null;
}
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
}
@@ -0,0 +1,55 @@
<?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');
}
}
@@ -0,0 +1,44 @@
<?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\HttpPostActionInterface;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\Result\RawFactory;
use Psr\Log\LoggerInterface;
class Notify extends AbstractCallback implements HttpPostActionInterface
{
public function __construct(
Context $context,
private readonly Parser $parser,
private readonly Processor $processor,
private readonly RawFactory $rawFactory,
private readonly LoggerInterface $logger
) {
parent::__construct($context);
}
public function execute(): Raw
{
$result = $this->rawFactory->create();
try {
$data = $this->parser->parse(
(string)$this->getRequest()->getContent(),
(array)$this->getRequest()->getParams()
);
$this->processor->process($data, true);
return $result->setContents('receive-ok');
} catch (\Throwable $exception) {
$this->logger->warning('Oceanpayment Klarna asynchronous callback was rejected', [
'exception' => $exception,
]);
$this->getResponse()->setHttpResponseCode(400);
return $result->setContents('invalid');
}
}
}
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Esun\OceanpaymentKlarna\Controller\Payment;
use Esun\OceanpaymentKlarna\Model\Config;
use Esun\OceanpaymentKlarna\Model\Gateway\Client;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
use Magento\Sales\Api\OrderRepositoryInterface;
use Psr\Log\LoggerInterface;
class Redirect extends Action implements HttpGetActionInterface
{
public function __construct(
Context $context,
private readonly CheckoutSession $checkoutSession,
private readonly Client $client,
private readonly OrderRepositoryInterface $orderRepository,
private readonly LoggerInterface $logger
) {
parent::__construct($context);
}
public function execute(): ResultRedirect
{
/** @var ResultRedirect $result */
$result = $this->resultRedirectFactory->create();
$order = $this->checkoutSession->getLastRealOrder();
if (!$order->getEntityId()
|| (string)$order->getPayment()->getMethod() !== Config::METHOD_CODE
) {
$this->messageManager->addErrorMessage(__('The Oceanpayment Klarna order is no longer available.'));
return $result->setPath('checkout/cart');
}
try {
$cachedUrl = (string)$order->getPayment()
->getAdditionalInformation('oceanpayment_klarna_pay_url');
$paymentUrl = $cachedUrl !== '' && $this->client->isAllowedPaymentUrl($cachedUrl)
? $cachedUrl
: $this->client->createPaymentUrl($order);
return $result->setUrl($paymentUrl);
} catch (\Throwable $exception) {
$this->logger->error('Oceanpayment Klarna initialization failed', [
'order_number' => $order->getIncrementId(),
'exception' => $exception,
]);
if ($order->canCancel()) {
$order->cancel();
$order->addCommentToStatusHistory(__('Oceanpayment Klarna initialization failed.'));
$this->orderRepository->save($order);
}
$this->checkoutSession->restoreQuote();
$this->messageManager->addErrorMessage(
__('Klarna payment initialization failed. Please try again or choose another payment method.')
);
return $result->setPath('checkout/cart');
}
}
}