3
0

183 lines
6.2 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace Opencart\Catalog\Controller\Extension\PayAnyWay\Payment;
use Opencart\Catalog\Model\Account\Customer as ModelAccountCustomer;
use Opencart\Catalog\Model\Account\Order as ModelAccountOrder;
use Opencart\Catalog\Model\Checkout\Order as ModelCheckoutOrder;
use Opencart\System\Engine\Config;
use Opencart\System\Engine\Loader;
use Opencart\System\Library\Cart\Currency;
use Opencart\System\Library\Cart\Customer;
use Opencart\System\Library\Document;
use Opencart\System\Library\Language;
use Opencart\System\Library\Request;
use Opencart\System\Library\Response;
use Opencart\System\Library\Session;
use Opencart\System\Library\Url;
require_once DIR_EXTENSION . '/payanyway/catalog/library/payanyway/traits/payanyway.php';
/**
* @property Config $config
* @property Loader $load
* @property Request $request
* @property Response $response
* @property Document $document
* @property Session $session
* @property Language $language
* @property Url $url
* @property Currency $currency
* @property ModelCheckoutOrder $model_checkout_order
* @property ModelAccountOrder $model_account_order
* @property ModelAccountCustomer $model_account_customer
* @property Customer $customer
*/
class PayAnyWay extends \Opencart\System\Engine\Controller
{
use \Opencart\Catalog\Library\Extension\PayAnyWay\Traits\PayAnyWay;
private const PAYMENT_SERVER_PROD = 'prod';
private const PAYMENT_URL_PROD = 'https://www.payanyway.ru/assistant.htm';
private const PAYMENT_URL_DEMO = 'https://demo.moneta.ru/assistant.htm';
public const DESCRIPTION_MAX_LENGTH = 500;
private int $mntId;
private string $integrityCode;
private string $server;
private string $vatRate;
private string $currency;
private int $pendingOrderStatusId;
private int $successOrderStatusId;
private int $errorOrderStatusId;
private int $geoZoneId;
private int $sortOrder;
private string $cmsModuleVersion;
private array $orderInfo;
private ?int $orderId;
public function __construct(\Opencart\System\Engine\Registry $registry)
{
parent::__construct($registry);
if (!$this->validateOrder()) {
$this->response->redirect($this->url->link('account/login', '', true));
}
$this->initializeModule();
}
/**
* @throws \Exception
*/
public function index(): string
{
$this->load->language('extension/payanyway/payment/payanyway');
$this->document->setTitle($this->language->get('heading_title'));
$data['action'] = (self::PAYMENT_SERVER_PROD === $this->server)
? self::PAYMENT_URL_PROD
: self::PAYMENT_URL_DEMO;
$data['MNT_ID'] = $this->mntId;
$data['MNT_TRANSACTION_ID'] = $this->createTransactionId($this->orderId);
$data['MNT_AMOUNT'] = $this->formatPrice((float)($this->orderInfo['total'] ?? 0));
$data['MNT_CURRENCY_CODE'] = $this->currency;
$data['MNT_TEST_MODE'] = '0';
$data['MNT_DESCRIPTION'] = $this->getDescription();
$data['MNT_SUBSCRIBER_ID'] = $this->getSubscriberId();
$signature = md5(
$data['MNT_ID'] .
$data['MNT_TRANSACTION_ID'] .
$data['MNT_AMOUNT'] .
$data['MNT_CURRENCY_CODE'] .
$data['MNT_SUBSCRIBER_ID'] .
$data['MNT_TEST_MODE'] .
$this->integrityCode,
);
$data['MNT_SIGNATURE'] = $signature;
$customerToken = $this->session->data['customer_token'];
$data['MNT_SUCCESS_URL'] = $this->url->link('extension/payanyway/payment/payanyway.success', 'customer_token=' . $customerToken, true);
$data['MNT_FAIL_URL'] = $this->url->link('extension/payanyway/payment/payanyway.fail', 'customer_token=' . $customerToken, true);
$data['MNT_RETURN_URL'] = $this->url->link('checkout/checkout', 'customer_token=' . $customerToken, true);
$data['moneta_locale'] = $this->language->get('text_locale');
$data['MNT_CMS'] = $this->cmsModuleVersion;
$data['btn_confirm'] = $this->language->get('text_paw_pay');
$data['confirm_url'] = $this->url->link('extension/payanyway/payment/payanyway.confirm', 'customer_token=' . $customerToken, true,);
return $this->load->view('extension/payanyway/payment/payanyway', $data);
}
public function confirm(): void
{
$this->load->language('extension/payanyway/payment/payanyway');
$orderStatusId = isset($this->orderInfo['order_status_id']) ? (int)$this->orderInfo['order_status_id'] : null;
if ((null !== $orderStatusId) && ($orderStatusId !== $this->successOrderStatusId)) {
$this->addOrderHistory($this->orderId, $this->pendingOrderStatusId, $this->language->get('text_order_confirmed'),);
}
$json['success'] = true;
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
public function success(): void
{
$this->load->language('extension/payanyway/payment/payanyway');
$orderStatusId = isset($this->orderInfo['order_status_id']) ? (int)$this->orderInfo['order_status_id'] : null;
if ((null !== $orderStatusId) && ($orderStatusId !== $this->successOrderStatusId)) {
$this->addOrderHistory($this->orderId, $this->pendingOrderStatusId, $this->language->get('text_payment_processing'));
}
$customerToken = $this->request->get['customer_token'] ?? '';
$this->response->redirect($this->url->link('checkout/success', 'customer_token=' . $customerToken, true));
}
public function fail(): void
{
$this->load->language('extension/payanyway/payment/payanyway');
$orderStatusId = isset($this->orderInfo['order_status_id']) ? (int)$this->orderInfo['order_status_id'] : null;
if ((null !== $orderStatusId) && ($orderStatusId !== $this->successOrderStatusId)) {
$this->addOrderHistory($this->orderId, $this->errorOrderStatusId, $this->language->get('text_payment_error'));
}
$this->response->redirect($this->url->link('checkout/failure', '', true));
}
private function getDescription(): string
{
$orderId = $this->orderInfo['order_id'] ?? null;
$description = "Оплата заказа" . (!empty($orderId) ? "{$orderId}" : '');
$name = $this->getClientName();
if (null !== $name) {
$description .= " от {$name}";
}
$comment = $this->orderInfo['comment'] ?? '';
if ('' !== $comment) {
$description .= ': ' . htmlspecialchars($comment, ENT_QUOTES, 'UTF-8');
}
return $this->validateString($description, self::DESCRIPTION_MAX_LENGTH);
}
private function getSubscriberId(): string
{
return $this->getClientEmail() ?? $this->getClientPhone() ?? '';
}
}