MDL-69166 core_payment: post to process payment
authorShamim Rezaie <shamim@moodle.com>
Fri, 27 Dec 2019 13:19:21 +0000 (00:19 +1100)
committerShamim Rezaie <shamim@moodle.com>
Mon, 26 Oct 2020 21:37:08 +0000 (08:37 +1100)
enrol/fee/classes/plugin.php
lang/en/payment.php
payment/amd/build/gateways_modal.min.js
payment/amd/build/gateways_modal.min.js.map
payment/amd/build/selectors.min.js
payment/amd/build/selectors.min.js.map
payment/amd/src/gateways_modal.js
payment/amd/src/selectors.js
payment/classes/helper.php

index f2d4200..b1c5220 100644 (file)
@@ -207,7 +207,8 @@ class enrol_fee_plugin extends enrol_plugin {
                 echo '</div>';
             } else {
                 \core_payment\helper::gateways_modal_requirejs();
-                $attributes = core_payment\helper::gateways_modal_link_params($cost, $instance->currency);
+                $attributes = core_payment\helper::gateways_modal_link_params($cost, $instance->currency, 'enrol_fee',
+                        $instance->id);
 
                 echo '<div align="center">' .
                       html_writer::tag('button', get_string("sendpaymentbutton", "enrol_paypal"), $attributes) .
index b9f6322..9d0e615 100644 (file)
@@ -23,5 +23,6 @@
  */
 
 $string['nogateway'] = 'There is no payment gateway that can be used.';
+$string['nogatewayselected'] = 'You first need to select a payment gateway.';
 $string['selectpaymenttype'] = 'Select payment type';
 $string['supportedcurrencies'] = 'Supported currencies';
index f6c364c..3660ca7 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js and b/payment/amd/build/gateways_modal.min.js differ
index 83ffa6f..034b0fd 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js.map and b/payment/amd/build/gateways_modal.min.js.map differ
index de2f08e..480f147 100644 (file)
Binary files a/payment/amd/build/selectors.min.js and b/payment/amd/build/selectors.min.js differ
index f68775a..fc5d505 100644 (file)
Binary files a/payment/amd/build/selectors.min.js.map and b/payment/amd/build/selectors.min.js.map differ
index 8579158..a95c797 100644 (file)
@@ -28,6 +28,7 @@ import {get_string as getString} from 'core/str';
 import {getGatewaysSupportingCurrency} from 'core_payment/repository';
 import Selectors from './selectors';
 import * as ModalEvents from 'core/modal_events';
+import {add as addToast, addToastRegion} from 'core/toast';
 
 /**
  * Register event listeners for the module.
@@ -54,41 +55,82 @@ const show = (rootNode, {
     focusOnClose = null,
 } = {}) => {
     Templates.render('core_payment/gateways_modal', {})
-        .done(content => {
-            ModalFactory.create({
-                title: getString('selectpaymenttype', 'core_payment'),
-                body: content,
-            })
-            .done(function(modal) {
-                const currency = rootNode.dataset.currency;
-                getGatewaysSupportingCurrency(currency)
-                    .done(gateways => {
-                        const context = {
-                            gateways: []
-                        };
+    .done(content => {
+        ModalFactory.create({
+            type: ModalFactory.types.SAVE_CANCEL,
+            title: getString('selectpaymenttype', 'core_payment'),
+            body: content,
+        })
+        .done(function(modal) {
+            addToastRegion(modal.getRoot()[0]);
+            const currency = rootNode.dataset.currency;
+            getGatewaysSupportingCurrency(currency)
+            .done(gateways => {
+                const context = {
+                    gateways: []
+                };
 
-                        for (let gateway of gateways) {
-                            context.gateways.push(gateway);
-                        }
+                for (let gateway of gateways) {
+                    context.gateways.push(gateway);
+                }
 
-                        Templates.render('core_payment/gateways', context)
-                            .done((html, js) => {
-                                Templates.replaceNodeContents(modal.getRoot().find(Selectors.regions.gatewaysContainer),
-                                    html, js);
-                            });
+                Templates.render('core_payment/gateways', context)
+                    .done((html, js) => {
+                        Templates.replaceNodeContents(modal.getRoot().find(Selectors.regions.gatewaysContainer),
+                            html, js);
                     });
+            });
+
+            modal.getRoot().on(ModalEvents.hidden, function() {
+                // Destroy when hidden.
+                modal.destroy();
+                try {
+                    focusOnClose.focus();
+                } catch (e) {
+                    // eslint-disable-line
+                }
+            });
 
-                modal.getRoot().on(ModalEvents.hidden, function() {
-                    // Destroy when hidden.
-                    modal.destroy();
-                    try {
-                        focusOnClose.focus();
-                    } catch (e) {
-                        // eslint-disable-line
-                    }
-                });
+            modal.getRoot().on(ModalEvents.save, function(e) {
+                const root = modal.getRoot()[0];
+                const gateway = (root.querySelector(Selectors.values.gateway) || {value: ''}).value;
 
-                modal.show();
+                if (gateway) {
+                    processPayment(
+                        root,
+                        gateway,
+                        rootNode.dataset.amount,
+                        rootNode.dataset.currency,
+                        rootNode.dataset.component,
+                        rootNode.dataset.componentid
+                    );
+                } else {
+                    getString('nogatewayselected', 'core_payment').then(message => {
+                        return addToast(message);
+                    });
+                }
+
+                e.preventDefault();
             });
+
+            modal.show();
         });
+    });
+};
+
+/**
+ * Process payment using the selected gateway.
+ *
+ * @param {HTMLElement} rootElement The root element of the main modal
+ * @param {string} gateway The gateway to be used for payment
+ * @param {number} amount Amount of payment
+ * @param {string} currency The currency in the three-character ISO-4217 format
+ * @param {string} component Name of the component that the componentid belongs to
+ * @param {number} componentid An internal identifier that is used by the component
+ * @returns {Promise<void>}
+ */
+const processPayment = async(rootElement, gateway, amount, currency, component, componentid) => {
+    const paymentMethod = await import(`pg_${gateway}/gateways_modal`);
+
+    paymentMethod.process(rootElement, amount, currency, component, componentid);
 };
index 35be2db..5bfc1af 100644 (file)
@@ -26,4 +26,7 @@ export default {
     regions: {
         gatewaysContainer: '[data-region="gateways-container"]',
     },
+    values: {
+        gateway: '[data-region="gateways-container"] input[type="radio"]:checked',
+    },
 };
index 1fb9261..ef9df49 100644 (file)
@@ -98,12 +98,14 @@ class helper {
      * @param string $currency Currency of payment
      * @return array
      */
-    public static function gateways_modal_link_params(float $amount, string $currency: array {
+    public static function gateways_modal_link_params(float $amount, string $currency, string $component, int $componentid): array {
         return [
             'id' => 'gateways-modal-trigger',
             'role' => 'button',
             'data-amount' => $amount,
             'data-currency' => $currency,
+            'data-component' => $component,
+            'data-componentid' => $componentid,
         ];
     }
 }