b87a8bf4e13d7476264b9c1da75541a33cf65004
[moodle.git] / payment / amd / src / gateways_modal.js
1 // This file is part of Moodle - http://moodle.org/
2 //
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.
7 //
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.
12 //
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/>.
16 /**
17  * Contain the logic for the gateways modal.
18  *
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
23  */
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 'core_payment/repository';
29 import Selectors from './selectors';
30 import * as ModalEvents from 'core/modal_events';
31 import {add as addToast, addToastRegion} from 'core/toast';
32 import Notification from 'core/notification';
34 /**
35  * Register event listeners for the module.
36  *
37  * @param {string} nodeSelector The root to listen to.
38  */
39 export const registerEventListeners = (nodeSelector) => {
40     const rootNode = document.querySelector(nodeSelector);
42     rootNode.addEventListener('click', (e) => {
43         e.preventDefault();
44         show(rootNode, {focusOnClose: e.target});
45     });
46 };
48 /**
49  * Shows the gateway selector modal.
50  *
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.
54  */
55 const show = (rootNode, {
56     focusOnClose = null,
57 } = {}) => {
58     Templates.render('core_payment/gateways_modal', {})
59     .done(content => {
60         ModalFactory.create({
61             type: ModalFactory.types.SAVE_CANCEL,
62             title: getString('selectpaymenttype', 'core_payment'),
63             body: content,
64         })
65         .done(function(modal) {
66             addToastRegion(modal.getRoot()[0]);
67             const currency = rootNode.dataset.currency;
68             getGatewaysSupportingCurrency(currency)
69             .done(gateways => {
70                 const context = {
71                     gateways: []
72                 };
74                 for (let gateway of gateways) {
75                     context.gateways.push(gateway);
76                 }
78                 Templates.render('core_payment/gateways', context)
79                     .done((html, js) => {
80                         Templates.replaceNodeContents(modal.getRoot().find(Selectors.regions.gatewaysContainer),
81                             html, js);
82                     });
83             });
85             modal.getRoot().on(ModalEvents.hidden, function() {
86                 // Destroy when hidden.
87                 modal.destroy();
88                 try {
89                     focusOnClose.focus();
90                 } catch (e) {
91                     // eslint-disable-line
92                 }
93             });
95             modal.getRoot().on(ModalEvents.save, function(e) {
96                 const root = modal.getRoot()[0];
97                 const gateway = (root.querySelector(Selectors.values.gateway) || {value: ''}).value;
99                         if (gateway) {
100                             processPayment(
101                                 gateway,
102                                 rootNode.dataset.amount,
103                                 rootNode.dataset.currency,
104                                 rootNode.dataset.component,
105                                 rootNode.dataset.componentid,
106                                 rootNode.dataset.description,
107                                 ({success, message = ''}) => {
108                                     modal.hide();
109                                     if (success) {
110                                         Notification.addNotification({
111                                             message: message,
112                                             type: 'success',
113                                         });
114                                         location.reload();
115                                     } else {
116                                         Notification.alert('', message);
117                                     }
118                                 },
119                             );
120                         } else {
121                             getString('nogatewayselected', 'core_payment').then(message => {
122                                 return addToast(message);
123                             });
124                         }
126                 e.preventDefault();
127             });
129             modal.show();
130         });
131     });
132 };
134 /**
135  * Process payment using the selected gateway.
136  *
137  * @param {string} gateway The gateway to be used for payment
138  * @param {number} amount Amount of payment
139  * @param {string} currency The currency in the three-character ISO-4217 format
140  * @param {string} component Name of the component that the componentid belongs to
141  * @param {number} componentid An internal identifier that is used by the component
142  * @param {string} description Description of the payment
143  * @param {processPaymentCallback} callback The callback function to call when processing is finished
144  * @returns {Promise<void>}
145  */
146 const processPayment = async(gateway, amount, currency, component, componentid, description, callback) => {
147     const paymentMethod = await import(`pg_${gateway}/gateways_modal`);
149     paymentMethod.process(amount, currency, component, componentid, description, callback);
150 };
152 /**
153  * The callback definition for processPayment.
154  *
155  * @callback processPaymentCallback
156  * @param {bool} success
157  * @param {string} message
158  */