Merge branch 'MDL-70279-310' of git://github.com/paulholden/moodle into MOODLE_310_STABLE
[moodle.git] / payment / manage_gateway.php
CommitLineData
895f38cc
MG
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Manage one payment gateway
19 *
20 * @package core_payment
21 * @copyright 2020 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once(__DIR__ . '/../config.php');
26require_once($CFG->libdir . '/adminlib.php');
27
28$id = optional_param('id', 0, PARAM_INT);
29$accountid = optional_param('accountid', 0, PARAM_INT);
30$gatewayname = optional_param('gateway', null, PARAM_COMPONENT);
31
32$pageurl = new moodle_url('/payment/manage_gateway.php');
33admin_externalpage_setup('paymentaccounts', '', [], $pageurl);
34
6b3d163a 35$enabledplugins = \core\plugininfo\paygw::get_enabled_plugins();
895f38cc
MG
36
37if ($id) {
38 $gateway = new \core_payment\account_gateway($id);
39 $account = new \core_payment\account($gateway->get('accountid'));
40} else if ($accountid) {
41 $account = new \core_payment\account($accountid);
42 $gateway = $account->get_gateways()[$gatewayname] ?? null;
43}
44
45if (empty($account) || empty($gateway)) {
46 throw new moodle_exception('gatewaynotfound', 'payment');
47}
48require_capability('moodle/payment:manageaccounts', $account->get_context());
49
50$PAGE->set_heading($id ? format_string($account->get('name')) : get_string('createaccount', 'payment'));
51
52$form = new \core_payment\form\account_gateway($pageurl->out(false), ['persistent' => $gateway]);
53
54if ($form->is_cancelled()) {
55 redirect(new moodle_url('/payment/accounts.php'));
56} else if ($data = $form->get_data()) {
57 \core_payment\helper::save_payment_gateway($data);
58 redirect(new moodle_url('/payment/accounts.php'));
59}
60
61echo $OUTPUT->header();
62$form->display();
63echo $OUTPUT->footer();