1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Contain the logic for the gateways modal.
19 * @module core_payment/gateways_modal
20 * @package core_payment
21 * @copyright 2019 Shamim Rezaie <shamim@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 import ModalFactory from 'core/modal_factory';
26 import Templates from 'core/templates';
27 import {get_string as getString} from 'core/str';
28 import {getGatewaysSupportingCurrency} from './repository';
29 import Selectors from './selectors';
30 import ModalEvents from 'core/modal_events';
31 import {add as addToast, addToastRegion} from 'core/toast';
32 import Notification from 'core/notification';
35 * Register event listeners for the module.
37 * @param {string} nodeSelector The root to listen to.
39 export const registerEventListeners = (nodeSelector) => {
40 const rootNode = document.querySelector(nodeSelector);
42 rootNode.addEventListener('click', (e) => {
44 show(rootNode, {focusOnClose: e.target});
49 * Shows the gateway selector modal.
51 * @param {HTMLElement} rootNode
52 * @param {Object} options - Additional options
53 * @param {HTMLElement} options.focusOnClose The element to focus on when the modal is closed.
55 const show = async(rootNode, {
58 const modal = await ModalFactory.create({
59 type: ModalFactory.types.SAVE_CANCEL,
60 title: await getString('selectpaymenttype', 'core_payment'),
61 body: await Templates.render('core_payment/gateways_modal', {}),
64 addToastRegion(modal.getRoot()[0]);
68 modal.getRoot().on(ModalEvents.hidden, () => {
69 // Destroy when hidden.
74 // eslint-disable-line
78 modal.getRoot().on(ModalEvents.save, (e) => {
79 const root = modal.getRoot()[0];
80 const gateway = (root.querySelector(Selectors.values.gateway) || {value: ''}).value;
85 rootNode.dataset.amount,
86 rootNode.dataset.currency,
87 rootNode.dataset.component,
88 rootNode.dataset.componentid,
89 rootNode.dataset.description,
90 ({success, message = ''}) => {
93 Notification.addNotification({
99 Notification.alert('', message);
104 // We cannot use await in the following line.
105 // The reason is that we are preventing the default action of the save event being triggered,
106 // therefore we cannot define the event handler function asynchronous.
107 getString('nogatewayselected', 'core_payment').then(message => addToast(message));
113 const currency = rootNode.dataset.currency;
114 const gateways = await getGatewaysSupportingCurrency(currency);
119 const {html, js} = await Templates.renderForPromise('core_payment/gateways', context);
120 Templates.replaceNodeContents(modal.getRoot().find(Selectors.regions.gatewaysContainer), html, js);
124 * Process payment using the selected gateway.
126 * @param {string} gateway The gateway to be used for payment
127 * @param {number} amount Amount of payment
128 * @param {string} currency The currency in the three-character ISO-4217 format
129 * @param {string} component Name of the component that the componentid belongs to
130 * @param {number} componentid An internal identifier that is used by the component
131 * @param {string} description Description of the payment
132 * @param {processPaymentCallback} callback The callback function to call when processing is finished
133 * @returns {Promise<void>}
135 const processPayment = async(gateway, amount, currency, component, componentid, description, callback) => {
136 const paymentMethod = await import(`pg_${gateway}/gateways_modal`);
138 paymentMethod.process(amount, currency, component, componentid, description, callback);
142 * The callback definition for processPayment.
144 * @callback processPaymentCallback
145 * @param {bool} success
146 * @param {string} message