119 lines
4.5 KiB
PHP
119 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once dirname(__DIR__) . '/class/autoload.php';
|
|
|
|
use MonetaPhpShop\form\ReturnAccess;
|
|
use MonetaPhpShop\form\ReturnMessage;
|
|
use MonetaPhpShop\host\Database;
|
|
use MonetaPhpShop\host\ErrorLogLogger;
|
|
use MonetaPhpShop\host\HostCharset;
|
|
use MonetaPhpShop\host\OperationRepository;
|
|
use MonetaPhpShop\host\OrderRepository;
|
|
use MonetaPhpShop\host\SettingsRepository;
|
|
use MonetaPhpShop\Settings;
|
|
|
|
/**
|
|
* Ñòðàíèöà /moneta/, êóäà Ìîíåòà âîçâðàùàåò ïîêóïàòåëÿ (`MNT_SUCCESS_URL`, `MNT_FAIL_URL`,
|
|
* `MNT_INPROGRESS_URL`, `MNT_RETURN_URL` ñ `?result=...`; ñåðâèñ äîïèñûâàåò `MNT_TRANSACTION_ID`).
|
|
* ×òî ïîêàçàòü, ðåøàåò {@see ReturnMessage}; çàêàç — òîëüêî âëàäåëüöó ({@see ReturnAccess}).
|
|
* Ïîêà æä¸ì Pay URL, ñòðàíèöà ïåðåçàïðàøèâàåò ñåáÿ ñ `attempt=<íîìåð>`.
|
|
*/
|
|
class PHPShopMoneta extends PHPShopCore
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$result = self::query('result');
|
|
$transactionId = self::query('MNT_TRANSACTION_ID');
|
|
$attempt = max(0, min(ReturnMessage::MAX_ATTEMPTS, (int) self::query('attempt')));
|
|
|
|
$message = $this->returnMessage($result, $transactionId, $attempt);
|
|
if ($message === null) {
|
|
$this->setError404();
|
|
|
|
return;
|
|
}
|
|
|
|
$orderUid = $message->getOrderUid();
|
|
$refreshUrl = $message->isWaiting()
|
|
? '?' . http_build_query([
|
|
'result' => $result,
|
|
'MNT_TRANSACTION_ID' => $transactionId,
|
|
'attempt' => $attempt + 1,
|
|
])
|
|
: '';
|
|
|
|
$this->title = HostCharset::toPage($message->getTitle());
|
|
$this->set('monetaReturnIcon', $message->getIcon());
|
|
$this->set('monetaReturnTitle', self::escape($message->getTitle()));
|
|
$this->set('monetaReturnText', self::escape($message->getText()));
|
|
$this->set('monetaReturnOrderUrl', self::escape('/users/order.html?order_info=' . rawurlencode($orderUid) . '#Order'));
|
|
$this->set('monetaReturnOrderHide', $orderUid === '' ? 'moneta-hide' : '');
|
|
$this->set('monetaReturnRefreshUrl', self::escape($refreshUrl));
|
|
$this->set('monetaReturnRefreshDelay', (string) ReturnMessage::REFRESH_DELAY_MS);
|
|
|
|
// ßäðî ÷èòàåò øàáëîí ìîäóëÿ êàê åñòü (cp1251) è íå ïåðåêîäèðóåò åãî: íà UTF-8-ìàãàçèíå
|
|
// ïåðåìåííûå è òåêñò øàáëîíà ïåðåêîäèðóþòñÿ âìåñòå, îäèí ðàç, óæå ïîñëå ðàçáîðà.
|
|
$this->parseTemplate($GLOBALS['SysValue']['templates']['moneta']['moneta_return_forma'], true);
|
|
$this->Disp = HostCharset::toPage((string) $this->Disp);
|
|
}
|
|
|
|
/**
|
|
* Çàêàç èùåòñÿ ïî íîìåðó èç `MNT_TRANSACTION_ID` è ïîêàçûâàåòñÿ òîëüêî âëàäåëüöó
|
|
* ({@see ReturnAccess::findOrder()}). Ñáîé ÁÄ ñòðàíèöó íå ðîíÿåò: ïîêóïàòåëü óâèäèò òåêñò ïî `result`.
|
|
*/
|
|
private function returnMessage(string $result, string $transactionId, int $attempt): ?ReturnMessage
|
|
{
|
|
$order = null;
|
|
$paidByJournal = false;
|
|
$settings = null;
|
|
$transactionGiven = $transactionId !== '';
|
|
|
|
try {
|
|
$settings = (new SettingsRepository())->load();
|
|
if ($transactionGiven) {
|
|
$order = ReturnAccess::findOrder(
|
|
$transactionId,
|
|
new OrderRepository(),
|
|
(int) ($_SESSION['UsersId'] ?? 0),
|
|
$_SESSION[ReturnAccess::SESSION_KEY] ?? null,
|
|
);
|
|
if ($order !== null) {
|
|
$paidByJournal = (new OperationRepository(Database::fromHost()))->findPaidOperation($order->getId()) !== null;
|
|
}
|
|
}
|
|
} catch (\Throwable $exception) {
|
|
(new ErrorLogLogger())->error(
|
|
'Ñòðàíèöà âîçâðàòà áåç ñòàòóñà çàêàçà: ' . get_class($exception) . ': ' . $exception->getMessage(),
|
|
);
|
|
$order = null;
|
|
$transactionGiven = false;
|
|
}
|
|
|
|
return ReturnMessage::resolve(
|
|
$result,
|
|
$transactionGiven,
|
|
$order,
|
|
$paidByJournal,
|
|
$settings ?? Settings::fromRow([]),
|
|
$attempt,
|
|
);
|
|
}
|
|
|
|
private static function query(string $name): string
|
|
{
|
|
return isset($_GET[$name]) && is_string($_GET[$name]) ? $_GET[$name] : '';
|
|
}
|
|
|
|
private static function escape(string $value): string
|
|
{
|
|
return htmlspecialchars($value, ENT_QUOTES, 'Windows-1251');
|
|
}
|
|
}
|