Files
Moodle/classes/local/PaymentFormPage.php
T

94 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace paygw_moneta\local;
use core_payment\helper;
use moodle_url;
use paygw_moneta\local\order\CurrentPrice;
use paygw_moneta\local\order\Order;
use paygw_moneta\local\protocol\AssistantProtocol;
use paygw_moneta\local\protocol\AssistantTransactionId;
use paygw_moneta\local\protocol\PaymentRequest;
/**
* Контекст шаблона `paygw_moneta/payment_form` — подписанная форма перехода на
* MONETA.Assistant для заказа. Общий для `pay.php` (новый заказ из модального
* окна) и `checkout.php` (ссылка на оплату из письма).
*
* @package paygw_moneta
* @copyright 2026 Moneta Labs {@link https://moneta.ru/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class PaymentFormPage
{
private const RETURN_PATH = '/payment/gateway/moneta/return.php';
private function __construct() {}
/**
* @return array{action: string, cost: string, description: string, iscourse: bool, logourl: string, fields: list<array{name: string, value: string}>}
*/
public static function context(Order $order, GatewayConfig $config): array
{
global $CFG, $OUTPUT;
$request = new PaymentRequest(
accountId: $config->accountNumber,
transactionId: AssistantTransactionId::build($order->merchantOrderId),
amount: $order->amount,
subscriberId: $order->subscriberId(),
testMode: $order->snapshot->testMode,
signature: $config->signature(),
description: $order->snapshot->description,
successUrl: (new moodle_url(self::RETURN_PATH, ['outcome' => 'success']))->out(false),
failUrl: (new moodle_url(self::RETURN_PATH, ['outcome' => 'fail']))->out(false),
returnUrl: (new moodle_url(self::RETURN_PATH, ['outcome' => 'cancel']))->out(false),
server: $config->paymentServer,
locale: AssistantProtocol::normalizeLocale(current_language()),
cms: CmsInfo::getCmsModuleVersion(),
);
$fields = [];
foreach ($request->getFields() as $name => $value) {
$fields[] = ['name' => $name, 'value' => $value];
}
return [
'action' => empty($CFG->paygw_moneta_assistant_url)
? $request->getActionUrl()
: (string) $CFG->paygw_moneta_assistant_url,
// Сумма для покупателя — как в остальном Moodle: «120,25 ₽».
'cost' => helper::get_cost_as_string((float) $order->amount->toDecimal(), $order->currency),
'description' => $order->snapshot->description,
'iscourse' => $order->component === 'enrol_fee',
'logourl' => $OUTPUT->image_url('img', 'paygw_moneta')->out(false),
'fields' => $fields,
];
}
/**
* Можно ли продолжить оплату открытого заказа по ссылке из письма: реквизиты,
* тестовый режим и цена те же, что при создании, — иначе Pay URL всё равно
* отклонит оплату (`PayHandler`).
*/
public static function canResume(Order $order, ?GatewayConfig $config, CurrentPrice $currentPrice): bool
{
return $order->status->isOpen()
&& $config !== null
&& $config->acceptsNewOrders()
&& $config->accountNumber === $order->snapshot->accountNumber
&& $config->testMode === $order->snapshot->testMode
&& $currentPrice->matches($order);
}
/**
* Адрес страницы продолжения оплаты — его получает покупатель в письме.
*/
public static function resumeUrl(Order $order): moodle_url
{
return new moodle_url('/payment/gateway/moneta/checkout.php', ['order' => $order->merchantOrderId]);
}
}