84 lines
3.4 KiB
PHP
84 lines
3.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Customer\Controller\Order;
|
|
|
|
use Esun\Oceanpayment\Model\Config as OceanpaymentConfig;
|
|
use Esun\Oceanpayment\Model\Gateway\Client;
|
|
use Magento\Checkout\Model\Session as CheckoutSession;
|
|
use Magento\Customer\Controller\AbstractAccount;
|
|
use Magento\Customer\Model\Session as CustomerSession;
|
|
use Magento\Framework\App\Action\Context;
|
|
use Magento\Framework\App\Action\HttpPostActionInterface;
|
|
use Magento\Framework\Controller\Result\Redirect;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Sales\Api\OrderRepositoryInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class Repay extends AbstractAccount implements HttpPostActionInterface
|
|
{
|
|
public function __construct(
|
|
Context $context,
|
|
private readonly CustomerSession $customerSession,
|
|
private readonly CheckoutSession $checkoutSession,
|
|
private readonly OrderRepositoryInterface $orderRepository,
|
|
private readonly Client $client,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute(): Redirect
|
|
{
|
|
/** @var Redirect $result */
|
|
$result = $this->resultRedirectFactory->create();
|
|
$orderId = (int)$this->getRequest()->getParam('order_id');
|
|
|
|
try {
|
|
$order = $this->orderRepository->get($orderId);
|
|
$payment = $order->getPayment();
|
|
$method = $payment ? (string)$payment->getMethod() : '';
|
|
|
|
if ((int)$order->getCustomerId() !== (int)$this->customerSession->getCustomerId()) {
|
|
throw new LocalizedException(__('The requested order is not available.'));
|
|
}
|
|
if ((string)$order->getStatus() !== 'pending' || (float)$order->getTotalPaid() > 0.0) {
|
|
throw new LocalizedException(__('This order is no longer awaiting payment.'));
|
|
}
|
|
if (!in_array(
|
|
$method,
|
|
[OceanpaymentConfig::METHOD_CODE, OceanpaymentConfig::LEGACY_METHOD_CODE],
|
|
true
|
|
)) {
|
|
throw new LocalizedException(__('Online repayment is not available for this payment method.'));
|
|
}
|
|
|
|
$this->checkoutSession
|
|
->setLastQuoteId((int)$order->getQuoteId())
|
|
->setLastSuccessQuoteId((int)$order->getQuoteId())
|
|
->setLastOrderId((int)$order->getEntityId())
|
|
->setLastRealOrderId((string)$order->getIncrementId());
|
|
|
|
$cachedUrl = (string)$payment->getAdditionalInformation('qianhai_pay_url');
|
|
$paymentUrl = $cachedUrl !== '' && $this->client->isAllowedPaymentUrl($cachedUrl)
|
|
? $cachedUrl
|
|
: $this->client->createPaymentUrl($order);
|
|
|
|
return $result->setUrl($paymentUrl);
|
|
} catch (LocalizedException $exception) {
|
|
$this->messageManager->addErrorMessage($exception->getMessage());
|
|
} catch (\Throwable $exception) {
|
|
$this->logger->error('Customer order repayment initialization failed', [
|
|
'order_id' => $orderId,
|
|
'customer_id' => (int)$this->customerSession->getCustomerId(),
|
|
'exception' => $exception,
|
|
]);
|
|
$this->messageManager->addErrorMessage(
|
|
__('Payment initialization failed. Please try again or contact customer service.')
|
|
);
|
|
}
|
|
|
|
return $result->setPath('sales/order/history', ['status' => 'pending']);
|
|
}
|
|
}
|