MDL-69166 pg_paypal: Support multiple currencies on the same page
authorShamim Rezaie <shamim@moodle.com>
Wed, 15 Jan 2020 04:58:32 +0000 (15:58 +1100)
committerShamim Rezaie <shamim@moodle.com>
Tue, 27 Oct 2020 03:37:28 +0000 (14:37 +1100)
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 ddb3850..6a780da 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 9c5b3a6..99d5636 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 0266b8b..ba4416e 100644 (file)
@@ -132,11 +132,21 @@ export const process = async(amount, currency, component, componentid, descripti
  */
 const callExternalFunction = (jsFile, func) => {
     // Check to see if this file has already been loaded. If so just go straight to the func.
-    if (callExternalFunction.currentlyloaded.includes(jsFile)) {
+    if (callExternalFunction.currentlyloaded == jsFile) {
         func();
         return;
     }
 
+    // PayPal can only work with one currency at the same time. We have to unload the previously loaded script
+    // if it was loaded for a different currency. Weird way indeed, but the only way.
+    // See: https://github.com/paypal/paypal-checkout-components/issues/1180
+    if (callExternalFunction.currentlyloaded) {
+        const suspectedScript = document.querySelector(`script[src="${callExternalFunction.currentlyloaded}"]`);
+        if (suspectedScript) {
+            suspectedScript.parentNode.removeChild(suspectedScript);
+        }
+    }
+
     const script = document.createElement('script');
 
     if (script.readyState) {
@@ -155,13 +165,13 @@ const callExternalFunction = (jsFile, func) => {
     script.setAttribute('src', jsFile);
     document.head.appendChild(script);
 
-    callExternalFunction.currentlyloaded.push(jsFile);
+    callExternalFunction.currentlyloaded = jsFile;
 };
 
 /**
- * Holds the list of external JavaScript files.
+ * Holds the full url of loaded external JavaScript file.
  *
  * @static
- * @type {Array}
+ * @type {string}
  */
-callExternalFunction.currentlyloaded = [];
+callExternalFunction.currentlyloaded = '';