91 lines
2.9 KiB
PHP
Executable File
91 lines
2.9 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Opencart\Catalog\Library\Extension\PayAnyWay\Traits;
|
|
|
|
trait PayAnyWay
|
|
{
|
|
private function initializeModule(): void
|
|
{
|
|
$this->mntId = (int)$this->config->get('payment_payanyway_account');
|
|
$this->integrityCode = (string)$this->config->get('payment_payanyway_integrity_code');
|
|
$this->server = (string)$this->config->get('payment_payanyway_server');
|
|
$this->vatRate = (string)$this->config->get('payment_payanyway_vat_rate');
|
|
$this->currency = $this->config->get('payment_payanyway_currency');
|
|
$this->pendingOrderStatusId = (int)$this->config->get('payment_payanyway_pending_order_status_id');
|
|
$this->successOrderStatusId = (int)$this->config->get('payment_payanyway_success_order_status_id');
|
|
$this->errorOrderStatusId = (int)$this->config->get('payment_payanyway_error_order_status_id');
|
|
$this->cmsModuleVersion = (string)$this->config->get('payment_payanyway_cms_module_version');
|
|
}
|
|
|
|
private function validateOrder(): bool
|
|
{
|
|
$this->orderId = $this->getOrderIdFromTransactionId() ?? $this->getOrderIdFromSession();
|
|
if (null === $this->orderId) {
|
|
return false;
|
|
}
|
|
|
|
$this->load->model('checkout/order');
|
|
$this->orderInfo = $this->model_checkout_order->getOrder($this->orderId);
|
|
|
|
return ([] !== $this->orderInfo);
|
|
}
|
|
|
|
private string $transactionIdDelimiter = '|';
|
|
|
|
private function validateString(string $value, int $maxLength): string
|
|
{
|
|
$maxLength = max(0, $maxLength);
|
|
$value = $this->sanitizeString($value);
|
|
|
|
if (mb_strlen($value, 'UTF-8') <= $maxLength) {
|
|
return $value;
|
|
}
|
|
|
|
$trimLength = max(0, $maxLength - 3);
|
|
return mb_substr($value, 0, $trimLength, 'UTF-8') . ($maxLength > 3 ? '...' : '');
|
|
}
|
|
|
|
private function sanitizeString(string $value): string
|
|
{
|
|
$decoded = html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
|
$cleaned = preg_replace('/[^\p{L}\p{N}\s.,()_№+-]/u', '', $decoded);
|
|
$cleaned = str_replace(['&', '/', '\\', ';', '%', '#', '"', "'"], '', $cleaned);
|
|
|
|
return trim(preg_replace('/\s+/', ' ', $cleaned));
|
|
}
|
|
|
|
private function formatPrice(float $price): string
|
|
{
|
|
return number_format($price, 2, '.', '');
|
|
}
|
|
|
|
private function getOrderIdFromTransactionId(): ?int
|
|
{
|
|
if (!isset($_REQUEST['MNT_TRANSACTION_ID'])) {
|
|
return null;
|
|
}
|
|
$transactionIdData = explode($this->transactionIdDelimiter, $_REQUEST['MNT_TRANSACTION_ID'], 2);
|
|
|
|
return isset($transactionIdData[0]) ? (int)$transactionIdData[0] : null;
|
|
}
|
|
|
|
private function getOrderIdFromSession(): ?int
|
|
{
|
|
return isset($this->session->data['order_id']) ? (int)$this->session->data['order_id'] : null;
|
|
}
|
|
|
|
private function createTransactionId(int $orderId): string
|
|
{
|
|
$date = (new \DateTimeImmutable())->format('YmdHis');
|
|
return $orderId . $this->transactionIdDelimiter . $date;
|
|
}
|
|
|
|
private function addOrderHistory(int $orderId, int $orderStatusId, string $comment): void
|
|
{
|
|
$this->load->model('checkout/order');
|
|
$this->model_checkout_order->addHistory($orderId, $orderStatusId, $comment);
|
|
}
|
|
}
|