Files
Moodle/classes/gateway.php
T

132 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace paygw_moneta;
use core_payment\form\account_gateway;
use paygw_moneta\local\protocol\AssistantProtocol;
use paygw_moneta\local\protocol\PaymentServer;
use paygw_moneta\local\receipt\Client;
use paygw_moneta\local\receipt\Vat;
use stdClass;
/**
* Платёжный шлюз Moneta для Payment API Moodle: валюта и форма настроек
* платёжного аккаунта. Имя класса и методов задаёт ядро (`\paygw_<name>\gateway`).
*
* @package paygw_moneta
* @copyright 2026 Moneta Labs {@link https://moneta.ru/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gateway extends \core_payment\gateway
{
public static function get_supported_currencies(): array
{
return [AssistantProtocol::CURRENCY];
}
public static function add_configuration_to_gateway_form(account_gateway $form): void
{
$mform = $form->get_mform();
$mform->addElement('text', 'accountnumber', get_string('accountnumber', 'paygw_moneta'));
$mform->setType('accountnumber', PARAM_ALPHANUMEXT);
$mform->addRule('accountnumber', null, 'required', null, 'client');
$mform->addHelpButton('accountnumber', 'accountnumber', 'paygw_moneta');
$mform->addElement('passwordunmask', 'integritycode', get_string('integritycode', 'paygw_moneta'));
$mform->setType('integritycode', PARAM_RAW_TRIMMED);
$mform->addRule('integritycode', null, 'required', null, 'client');
$mform->addHelpButton('integritycode', 'integritycode', 'paygw_moneta');
$servers = [];
foreach (PaymentServer::cases() as $server) {
$servers[$server->value] = get_string($server->label(), 'paygw_moneta');
}
$mform->addElement('select', 'paymentserver', get_string('paymentserver', 'paygw_moneta'), $servers);
$mform->setDefault('paymentserver', PaymentServer::Prod->value);
$mform->addHelpButton('paymentserver', 'paymentserver', 'paygw_moneta');
$mform->addElement('advcheckbox', 'testmode', get_string('testmode', 'paygw_moneta'));
$mform->addHelpButton('testmode', 'testmode', 'paygw_moneta');
$mform->addElement('text', 'receiptemail', get_string('receiptemail', 'paygw_moneta'));
$mform->setType('receiptemail', PARAM_RAW_TRIMMED);
$mform->addRule('receiptemail', null, 'required', null, 'client');
$mform->addHelpButton('receiptemail', 'receiptemail', 'paygw_moneta');
$mform->addElement('advcheckbox', 'fiscalization', get_string('fiscalization', 'paygw_moneta'));
$mform->addHelpButton('fiscalization', 'fiscalization', 'paygw_moneta');
$vatOptions = [];
foreach (Vat::cases() as $vat) {
$vatOptions[$vat->value] = get_string($vat->langKey(), 'paygw_moneta');
}
$mform->addElement('select', 'vat', get_string('vat', 'paygw_moneta'), $vatOptions);
$mform->addHelpButton('vat', 'vat', 'paygw_moneta');
$mform->setDefault('vat', Vat::None->value);
$mform->addElement('advcheckbox', 'sendpaymentlink', get_string('sendpaymentlink', 'paygw_moneta'));
$mform->addHelpButton('sendpaymentlink', 'sendpaymentlink', 'paygw_moneta');
$mform->setDefault('sendpaymentlink', 1);
$mform->addElement('static', 'callbackurl', get_string('callbackurl', 'paygw_moneta'), self::callbackUrlHtml());
$mform->addHelpButton('callbackurl', 'callbackurl', 'paygw_moneta');
}
public static function validate_gateway_form(
account_gateway $form,
stdClass $data,
array $files,
array &$errors,
): void {
$accountNumber = trim((string) ($data->accountnumber ?? ''));
if ($accountNumber === '') {
$errors['accountnumber'] = get_string('required');
} elseif (preg_match('/\A[0-9]{1,20}\z/', $accountNumber) !== 1) {
$errors['accountnumber'] = get_string('error:accountnumber', 'paygw_moneta');
}
$receiptEmail = trim((string) ($data->receiptemail ?? ''));
if (Client::normalizeEmail($receiptEmail) === null) {
$errors['receiptemail'] = get_string('error:receiptemail', 'paygw_moneta');
}
if (trim((string) ($data->integritycode ?? '')) === '') {
$errors['integritycode'] = get_string('required');
}
}
/**
* Адрес Check URL / Pay URL только для чтения и кнопка «Скопировать»
* (`core/copy_to_clipboard`: сам вешает обработчик на `data-action`).
*/
private static function callbackUrlHtml(): string
{
global $PAGE;
$PAGE->requires->js_amd_inline("require(['core/copy_to_clipboard']);");
$id = \html_writer::random_id('paygw-moneta-callbackurl');
return \html_writer::div(
\html_writer::empty_tag('input', [
'type' => 'text',
'id' => $id,
'class' => 'form-control',
'value' => (new \moodle_url('/payment/gateway/moneta/callback.php'))->out(false),
'readonly' => 'readonly',
'size' => 60,
])
. \html_writer::tag('button', get_string('copytoclipboard'), [
'type' => 'button',
'class' => 'btn btn-secondary',
'data-action' => 'copytoclipboard',
'data-clipboard-target' => '#' . $id,
'data-clipboard-success-message' => get_string('callbackurl:copied', 'paygw_moneta'),
]),
'input-group',
);
}
}