MDL-69166 core_payment: add a callback for when payment is done
authorShamim Rezaie <shamim@moodle.com>
Sat, 11 Jan 2020 19:28:34 +0000 (06:28 +1100)
committerShamim Rezaie <shamim@moodle.com>
Tue, 27 Oct 2020 04:44:59 +0000 (15:44 +1100)
payment/amd/build/gateways_modal.min.js
payment/amd/build/gateways_modal.min.js.map
payment/amd/src/gateways_modal.js
payment/gateway/paypal/amd/build/gateways_modal.min.js
payment/gateway/paypal/amd/build/gateways_modal.min.js.map
payment/gateway/paypal/amd/src/gateways_modal.js

index 0c03f71..84578b7 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js and b/payment/amd/build/gateways_modal.min.js differ
index d457464..55f793b 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 39251ca..b87a8bf 100644 (file)
@@ -29,6 +29,7 @@ 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';
+import Notification from 'core/notification';
 
 /**
  * Register event listeners for the module.
@@ -103,6 +104,18 @@ const show = (rootNode, {
                                 rootNode.dataset.component,
                                 rootNode.dataset.componentid,
                                 rootNode.dataset.description,
+                                ({success, message = ''}) => {
+                                    modal.hide();
+                                    if (success) {
+                                        Notification.addNotification({
+                                            message: message,
+                                            type: 'success',
+                                        });
+                                        location.reload();
+                                    } else {
+                                        Notification.alert('', message);
+                                    }
+                                },
                             );
                         } else {
                             getString('nogatewayselected', 'core_payment').then(message => {
@@ -127,10 +140,19 @@ const show = (rootNode, {
  * @param {string} component Name of the component that the componentid belongs to
  * @param {number} componentid An internal identifier that is used by the component
  * @param {string} description Description of the payment
+ * @param {processPaymentCallback} callback The callback function to call when processing is finished
  * @returns {Promise<void>}
  */
-const processPayment = async(gateway, amount, currency, component, componentid, description) => {
+const processPayment = async(gateway, amount, currency, component, componentid, description, callback) => {
     const paymentMethod = await import(`pg_${gateway}/gateways_modal`);
 
-    paymentMethod.process(amount, currency, component, componentid, description);
+    paymentMethod.process(amount, currency, component, componentid, description, callback);
 };
+
+/**
+ * The callback definition for processPayment.
+ *
+ * @callback processPaymentCallback
+ * @param {bool} success
+ * @param {string} message
+ */
index b60eba1..ddb3850 100644 (file)
Binary files a/payment/gateway/paypal/amd/build/gateways_modal.min.js and b/payment/gateway/paypal/amd/build/gateways_modal.min.js differ
index 3d793bc..9c5b3a6 100644 (file)
Binary files a/payment/gateway/paypal/amd/build/gateways_modal.min.js.map and b/payment/gateway/paypal/amd/build/gateways_modal.min.js.map differ
index a86daf8..0266b8b 100644 (file)
@@ -25,8 +25,8 @@ import * as Repository from './repository';
 import Templates from 'core/templates';
 import Truncate from 'core/truncate';
 import Ajax from 'core/ajax';
-import Notification from 'core/notification';
 import ModalFactory from 'core/modal_factory';
+import ModalEvents from 'core/modal_events';
 
 /**
  * Creates and shows a modal that contains a placeholder.
@@ -50,9 +50,10 @@ const showPlaceholder = async() => {
  * @param {string} component Name of the component that the componentid belongs to
  * @param {number} componentid An internal identifier that is used by the component
  * @param {string} description Description of the payment
+ * @param {processCallback} callback The callback function to call when processing is finished
  * @returns {Promise<void>}
  */
-export const process = async(amount, currency, component, componentid, description) => {
+export const process = async(amount, currency, component, componentid, description, callback) => {
 
     const [
         modal,
@@ -62,6 +63,11 @@ export const process = async(amount, currency, component, componentid, descripti
         Repository.getConfigForJs(),
     ]);
 
+    modal.getRoot().on(ModalEvents.hidden, () => {
+        // Destroy when hidden.
+        modal.destroy();
+    });
+
     const paypalScript = `https://www.paypal.com/sdk/js?client-id=${paypalConfig.clientid}&currency=${currency}&intent=authorize`;
 
     callExternalFunction(paypalScript, () => {
@@ -101,10 +107,8 @@ export const process = async(amount, currency, component, componentid, descripti
                         },
                     }])[0]
                     .then(function(res) {
-                        Notification.addNotification({
-                            message: res.message,
-                            type: res.success ? 'success' : 'error'
-                        });
+                        modal.hide();
+                        return callback(res);
                     });
                 });
             }
@@ -112,6 +116,14 @@ export const process = async(amount, currency, component, componentid, descripti
     });
 };
 
+/**
+ * The callback definition for process.
+ *
+ * @callback processCallback
+ * @param {bool} success
+ * @param {string} message
+ */
+
 /**
  * Calls a function from an external javascript file.
  *