1 |
efrain |
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/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Contain the logic for the gateways modal.
|
|
|
18 |
*
|
|
|
19 |
* @module core_payment/gateways_modal
|
|
|
20 |
* @copyright 2019 Shamim Rezaie <shamim@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import Templates from 'core/templates';
|
|
|
25 |
import {getString} from 'core/str';
|
|
|
26 |
import {getAvailableGateways} from './repository';
|
|
|
27 |
import Selectors from './selectors';
|
|
|
28 |
import ModalEvents from 'core/modal_events';
|
|
|
29 |
import PaymentEvents from 'core_payment/events';
|
|
|
30 |
import {add as addToast, addToastRegion} from 'core/toast';
|
|
|
31 |
import Notification from 'core/notification';
|
|
|
32 |
import ModalGateways from './modal_gateways';
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Register event listeners for the module.
|
|
|
36 |
*/
|
|
|
37 |
const registerEventListeners = () => {
|
|
|
38 |
document.addEventListener('click', e => {
|
|
|
39 |
const gatewayTrigger = e.target.closest('[data-action="core_payment/triggerPayment"]');
|
|
|
40 |
if (gatewayTrigger) {
|
|
|
41 |
e.preventDefault();
|
|
|
42 |
|
|
|
43 |
show(gatewayTrigger, {focusOnClose: e.target});
|
|
|
44 |
}
|
|
|
45 |
});
|
|
|
46 |
};
|
|
|
47 |
|
|
|
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 = async(rootNode, {
|
|
|
56 |
focusOnClose = null,
|
|
|
57 |
} = {}) => {
|
|
|
58 |
|
|
|
59 |
// Load upfront, so we don't try to inject the internal content into a possibly-not-yet-resolved promise.
|
|
|
60 |
const body = await Templates.render('core_payment/gateways_modal', {});
|
|
|
61 |
|
|
|
62 |
const modal = await ModalGateways.create({
|
|
|
63 |
title: getString('selectpaymenttype', 'core_payment'),
|
|
|
64 |
body: body,
|
|
|
65 |
show: true,
|
|
|
66 |
removeOnClose: true,
|
|
|
67 |
});
|
|
|
68 |
|
|
|
69 |
const rootElement = modal.getRoot()[0];
|
|
|
70 |
addToastRegion(rootElement);
|
|
|
71 |
|
|
|
72 |
modal.getRoot().on(ModalEvents.hidden, () => {
|
|
|
73 |
focusOnClose?.focus();
|
|
|
74 |
});
|
|
|
75 |
|
|
|
76 |
modal.getRoot().on(PaymentEvents.proceed, async(e) => {
|
|
|
77 |
e.preventDefault();
|
|
|
78 |
const gateway = (rootElement.querySelector(Selectors.values.gateway) || {value: ''}).value;
|
|
|
79 |
|
|
|
80 |
if (gateway) {
|
|
|
81 |
processPayment(
|
|
|
82 |
gateway,
|
|
|
83 |
rootNode.dataset.component,
|
|
|
84 |
rootNode.dataset.paymentarea,
|
|
|
85 |
rootNode.dataset.itemid,
|
|
|
86 |
rootNode.dataset.description
|
|
|
87 |
).then((message) => {
|
|
|
88 |
modal.hide();
|
|
|
89 |
Notification.addNotification({
|
|
|
90 |
message,
|
|
|
91 |
type: 'success',
|
|
|
92 |
});
|
|
|
93 |
location.href = rootNode.dataset.successurl;
|
|
|
94 |
|
|
|
95 |
return;
|
|
|
96 |
}).catch(message => Notification.alert('', message));
|
|
|
97 |
} else {
|
|
|
98 |
// We cannot use await in the following line.
|
|
|
99 |
// The reason is that we are preventing the default action of the save event being triggered,
|
|
|
100 |
// therefore we cannot define the event handler function asynchronous.
|
|
|
101 |
addToast(getString('nogatewayselected', 'core_payment'), {
|
|
|
102 |
type: 'warning',
|
|
|
103 |
});
|
|
|
104 |
}
|
|
|
105 |
});
|
|
|
106 |
|
|
|
107 |
// Re-calculate the cost when gateway is changed.
|
|
|
108 |
rootElement.addEventListener('change', e => {
|
|
|
109 |
if (e.target.matches(Selectors.elements.gateways)) {
|
|
|
110 |
updateCostRegion(rootElement, rootNode.dataset.cost);
|
|
|
111 |
}
|
|
|
112 |
});
|
|
|
113 |
|
|
|
114 |
const gateways = await getAvailableGateways(rootNode.dataset.component, rootNode.dataset.paymentarea, rootNode.dataset.itemid);
|
|
|
115 |
const context = {
|
|
|
116 |
gateways
|
|
|
117 |
};
|
|
|
118 |
|
|
|
119 |
const {html, js} = await Templates.renderForPromise('core_payment/gateways', context);
|
|
|
120 |
Templates.replaceNodeContents(rootElement.querySelector(Selectors.regions.gatewaysContainer), html, js);
|
|
|
121 |
selectSingleGateway(rootElement);
|
|
|
122 |
await updateCostRegion(rootElement, rootNode.dataset.cost);
|
|
|
123 |
};
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Auto-select the gateway if there is only one gateway.
|
|
|
127 |
*
|
|
|
128 |
* @param {HTMLElement} root An HTMLElement that contains the cost region
|
|
|
129 |
*/
|
|
|
130 |
const selectSingleGateway = root => {
|
|
|
131 |
const gateways = root.querySelectorAll(Selectors.elements.gateways);
|
|
|
132 |
|
|
|
133 |
if (gateways.length == 1) {
|
|
|
134 |
gateways[0].checked = true;
|
|
|
135 |
}
|
|
|
136 |
};
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Shows the cost of the item the user is purchasing in the cost region.
|
|
|
140 |
*
|
|
|
141 |
* @param {HTMLElement} root An HTMLElement that contains the cost region
|
|
|
142 |
* @param {string} defaultCost The default cost that is going to be displayed if no gateway is selected
|
|
|
143 |
* @returns {Promise<void>}
|
|
|
144 |
*/
|
|
|
145 |
const updateCostRegion = async(root, defaultCost = '') => {
|
|
|
146 |
const gatewayElement = root.querySelector(Selectors.values.gateway);
|
|
|
147 |
const surcharge = parseInt((gatewayElement || {dataset: {surcharge: 0}}).dataset.surcharge);
|
|
|
148 |
const cost = (gatewayElement || {dataset: {cost: defaultCost}}).dataset.cost;
|
|
|
149 |
const valueStr = surcharge ? await getString('feeincludesurcharge', 'core_payment', {fee: cost, surcharge: surcharge}) : cost;
|
|
|
150 |
|
|
|
151 |
const surchargeStr = await getString('labelvalue', 'core',
|
|
|
152 |
{
|
|
|
153 |
label: await getString('cost', 'core'),
|
|
|
154 |
value: valueStr
|
|
|
155 |
}
|
|
|
156 |
);
|
|
|
157 |
|
|
|
158 |
const {html, js} = await Templates.renderForPromise('core_payment/fee_breakdown', {surchargestr: surchargeStr});
|
|
|
159 |
Templates.replaceNodeContents(root.querySelector(Selectors.regions.costContainer), html, js);
|
|
|
160 |
};
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Process payment using the selected gateway.
|
|
|
164 |
*
|
|
|
165 |
* @param {string} gateway The gateway to be used for payment
|
|
|
166 |
* @param {string} component Name of the component that the itemId belongs to
|
|
|
167 |
* @param {string} paymentArea Name of the area in the component that the itemId belongs to
|
|
|
168 |
* @param {number} itemId An internal identifier that is used by the component
|
|
|
169 |
* @param {string} description Description of the payment
|
|
|
170 |
* @returns {Promise<string>}
|
|
|
171 |
*/
|
|
|
172 |
const processPayment = async(gateway, component, paymentArea, itemId, description) => {
|
|
|
173 |
const paymentMethod = await import(`paygw_${gateway}/gateways_modal`);
|
|
|
174 |
return paymentMethod.process(component, paymentArea, itemId, description);
|
|
|
175 |
};
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Set up the payment actions.
|
|
|
179 |
*/
|
|
|
180 |
export const init = () => {
|
|
|
181 |
if (!init.initialised) {
|
|
|
182 |
// Event listeners should only be registered once.
|
|
|
183 |
init.initialised = true;
|
|
|
184 |
registerEventListeners();
|
|
|
185 |
}
|
|
|
186 |
};
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* Whether the init function was called before.
|
|
|
190 |
*
|
|
|
191 |
* @static
|
|
|
192 |
* @type {boolean}
|
|
|
193 |
*/
|
|
|
194 |
init.initialised = false;
|