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

62 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Oceanpayment\Controller\Payment;
use Esun\Oceanpayment\Model\Config;
use Esun\Oceanpayment\Model\Gateway\RequestBuilder;
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\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Psr\Log\LoggerInterface;
class Onepage extends Action implements HttpGetActionInterface
{
public function __construct(
Context $context,
private readonly CheckoutSession $checkoutSession,
private readonly Config $config,
private readonly RequestBuilder $requestBuilder,
private readonly JsonFactory $jsonFactory,
private readonly LoggerInterface $logger
) {
parent::__construct($context);
}
public function execute(): Json
{
$result = $this->jsonFactory->create();
$order = $this->checkoutSession->getLastRealOrder();
if (!$order->getEntityId()
|| (string)$order->getPayment()->getMethod() !== Config::ONEPAGE_METHOD_CODE
) {
return $this->error($result, __('The Oceanpayment OnePage order is no longer available.'), 404);
}
$storeId = (int)$order->getStoreId();
if (!$this->config->isConfigured($storeId, Config::ONEPAGE_METHOD_CODE)) {
return $this->error($result, __('Oceanpayment OnePage is not fully configured.'), 503);
}
try {
return $result->setData(['success' => true, 'payload' => $this->requestBuilder->buildEmbedded($order)]);
} catch (\Throwable $exception) {
$this->logger->error('Oceanpayment OnePage initialization failed', [
'order_number' => $order->getIncrementId(),
'exception' => $exception,
]);
return $this->error($result, __('Payment initialization failed. Please try again.'), 500);
}
}
private function error(Json $result, \Magento\Framework\Phrase $message, int $status): Json
{
$this->getResponse()->setHttpResponseCode($status);
return $result->setData(['success' => false, 'message' => (string)$message]);
}
}