init
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\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
|
||||
{
|
||||
// Oceanpayment cannot know the Magento form key; authenticity is checked by SHA-256 signature.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Controller\Payment;
|
||||
|
||||
use Esun\Oceanpayment\Model\Callback\Parser;
|
||||
use Esun\Oceanpayment\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(
|
||||
__('Payment was not completed: %1', $data['payment_details'])
|
||||
);
|
||||
} elseif ($paymentResult === Processor::PENDING) {
|
||||
$this->messageManager->addNoticeMessage(
|
||||
__('Your payment is being confirmed. The order will update automatically.')
|
||||
);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->logger->warning('Oceanpayment browser callback was rejected', ['exception' => $exception]);
|
||||
$this->messageManager->addErrorMessage(
|
||||
__('The payment result could not be verified. Please contact customer service if you were charged.')
|
||||
);
|
||||
}
|
||||
|
||||
return $result->setPath('checkout/onepage/success');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Controller\Payment;
|
||||
|
||||
use Esun\Oceanpayment\Model\Callback\Parser;
|
||||
use Esun\Oceanpayment\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 asynchronous callback was rejected', [
|
||||
'exception' => $exception,
|
||||
]);
|
||||
$this->getResponse()->setHttpResponseCode(400);
|
||||
return $result->setContents('invalid');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?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]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Controller\Payment;
|
||||
|
||||
use Esun\Oceanpayment\Model\Callback\Parser;
|
||||
use Esun\Oceanpayment\Model\Callback\Processor;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Controller\Result\Json;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class OnepageResult extends AbstractCallback implements HttpPostActionInterface
|
||||
{
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly Parser $parser,
|
||||
private readonly Processor $processor,
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute(): Json
|
||||
{
|
||||
$result = $this->jsonFactory->create();
|
||||
try {
|
||||
$data = $this->parser->parse(
|
||||
(string)$this->getRequest()->getContent(),
|
||||
(array)$this->getRequest()->getParams()
|
||||
);
|
||||
$paymentResult = $this->processor->process($data, false);
|
||||
return $result->setData([
|
||||
'success' => $paymentResult !== Processor::FAILURE,
|
||||
'status' => $paymentResult,
|
||||
'message' => $paymentResult === Processor::FAILURE
|
||||
? (string)__('Payment was not completed: %1', $data['payment_details'])
|
||||
: '',
|
||||
]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->logger->warning('Oceanpayment OnePage result was rejected', ['exception' => $exception]);
|
||||
$this->getResponse()->setHttpResponseCode(400);
|
||||
return $result->setData([
|
||||
'success' => false,
|
||||
'status' => Processor::FAILURE,
|
||||
'message' => (string)__('The payment result could not be verified.'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Controller\Payment;
|
||||
|
||||
use Esun\Oceanpayment\Model\Config;
|
||||
use Esun\Oceanpayment\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()
|
||||
|| !Config::isSupportedMethod((string)$order->getPayment()->getMethod())
|
||||
) {
|
||||
$this->messageManager->addErrorMessage(__('The Oceanpayment order is no longer available.'));
|
||||
return $result->setPath('checkout/cart');
|
||||
}
|
||||
|
||||
try {
|
||||
$cachedUrl = (string)$order->getPayment()->getAdditionalInformation('qianhai_pay_url');
|
||||
$paymentUrl = $cachedUrl !== '' && $this->client->isAllowedPaymentUrl($cachedUrl)
|
||||
? $cachedUrl
|
||||
: $this->client->createPaymentUrl($order);
|
||||
|
||||
return $result->setUrl($paymentUrl);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->logger->error('Oceanpayment initialization failed', [
|
||||
'order_number' => $order->getIncrementId(),
|
||||
'exception' => $exception,
|
||||
]);
|
||||
if ($order->canCancel()) {
|
||||
$order->cancel();
|
||||
$order->addCommentToStatusHistory(__('Oceanpayment initialization failed.'));
|
||||
$this->orderRepository->save($order);
|
||||
}
|
||||
$this->checkoutSession->restoreQuote();
|
||||
$this->messageManager->addErrorMessage(
|
||||
__('Payment initialization failed. Please try again or choose another payment method.')
|
||||
);
|
||||
return $result->setPath('checkout/cart');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user