MDL-69166 core_payment: Add paymentarea to the payment subsystem
[moodle.git] / payment / gateway / paypal / amd / build / gateways_modal.min.js.map
1 {"version":3,"sources":["../src/gateways_modal.js"],"names":["showModalWithPlaceholder","ModalFactory","Templates","render","body","create","modal","show","process","component","paymentArea","componentId","description","callback","Promise","all","Repository","getConfigForJs","paypalConfig","currency","amount","cost","getRoot","on","ModalEvents","hidden","destroy","paypalScript","clientid","callExternalFunction","setBody","paypal","Buttons","createOrder","data","actions","order","purchase_units","currency_code","value","Truncate","truncate","length","stripTags","application_context","shipping_preference","brand_name","brandname","onApprove","outsideClick","e","preventDefault","Ajax","call","methodname","args","paymentarea","componentid","orderid","orderID","then","res","hide","getBody","jsFile","func","currentlyloaded","suspectedScript","document","querySelector","parentNode","removeChild","script","createElement","readyState","onreadystatechange","onload","setAttribute","head","appendChild"],"mappings":"2iBAuBA,OACA,OACA,OACA,OACA,OACA,O,u3DAQMA,CAAAA,CAAwB,4CAAG,yGACTC,SADS,gBAEbC,WAAUC,MAAV,CAAiB,qCAAjB,CAAwD,EAAxD,CAFa,0BAEzBC,IAFyB,4BACIC,MADJ,wBACvBC,CADuB,QAI7BA,CAAK,CAACC,IAAN,GAJ6B,yBAKtBD,CALsB,2CAAH,uD,CAkBjBE,CAAO,4CAAG,WAAMC,CAAN,CAAiBC,CAAjB,CAA8BC,CAA9B,CAA2CC,CAA3C,CAAwDC,CAAxD,oHAKTC,CAAAA,OAAO,CAACC,GAAR,CAAY,CAClBf,CAAwB,EADN,CAElBgB,CAAU,CAACC,cAAX,CAA0BR,CAA1B,CAAqCC,CAArC,CAAkDC,CAAlD,CAFkB,CAAZ,CALS,0BAGfL,CAHe,MAIfY,CAJe,MASbC,CATa,CASFD,CAAY,CAACC,QATX,CAUbC,CAVa,CAUJF,CAAY,CAACG,IAVT,CAYnBf,CAAK,CAACgB,OAAN,GAAgBC,EAAhB,CAAmBC,UAAYC,MAA/B,CAAuC,UAAM,CAEzCnB,CAAK,CAACoB,OAAN,EACH,C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AT,CAAc,CAChBvD,CAAK,CAACwD,IAAN,GACA,MAAOjD,CAAAA,CAAQ,CAACgD,CAAD,CAClB,CAZM,CAaV,CAxCU,CAAf,EAyCG1D,MAzCH,CAyCUG,CAAK,CAACyD,OAAN,GAAgB,CAAhB,CAzCV,CA0CH,CA7CmB,CAApB,CAnBmB,yCAAH,uD,aAiFpB,GAAMlC,CAAAA,CAAoB,CAAG,SAACmC,CAAD,CAASC,CAAT,CAAkB,CAE3C,GAAIpC,CAAoB,CAACqC,eAArB,EAAwCF,CAA5C,CAAoD,CAChDC,CAAI,GACJ,MACH,CAKD,GAAIpC,CAAoB,CAACqC,eAAzB,CAA0C,CACtC,GAAMC,CAAAA,CAAe,CAAGC,QAAQ,CAACC,aAAT,wBAAsCxC,CAAoB,CAACqC,eAA3D,QAAxB,CACA,GAAIC,CAAJ,CAAqB,CACjBA,CAAe,CAACG,UAAhB,CAA2BC,WAA3B,CAAuCJ,CAAvC,CACH,CACJ,CAED,GAAMK,CAAAA,CAAM,CAAGJ,QAAQ,CAACK,aAAT,CAAuB,QAAvB,CAAf,CAEA,GAAID,CAAM,CAACE,UAAX,CAAuB,CACnBF,CAAM,CAACG,kBAAP,CAA4B,UAAW,CACnC,GAAuB,UAAnB,OAAKD,UAAL,EAAoD,QAAnB,OAAKA,UAA1C,CAAkE,CAC9D,KAAKC,kBAAL,CAA0B,IAA1B,CACAV,CAAI,EACP,CACJ,CACJ,CAPD,IAOO,CACHO,CAAM,CAACI,MAAP,CAAgB,UAAW,CACvBX,CAAI,EACP,CACJ,CAEDO,CAAM,CAACK,YAAP,CAAoB,KAApB,CAA2Bb,CAA3B,EACAI,QAAQ,CAACU,IAAT,CAAcC,WAAd,CAA0BP,CAA1B,EAEA3C,CAAoB,CAACqC,eAArB,CAAuCF,CAC1C,CApCD,CA4CAnC,CAAoB,CAACqC,eAArB,CAAuC,E","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * This module is responsible for PayPal content in the gateways modal.\n *\n * @module     pg_paypal/gateway_modal\n * @copyright  2020 Shamim Rezaie <shamim@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport * as Repository from './repository';\nimport Templates from 'core/templates';\nimport Truncate from 'core/truncate';\nimport Ajax from 'core/ajax';\nimport ModalFactory from 'core/modal_factory';\nimport ModalEvents from 'core/modal_events';\nimport {get_string as getString} from 'core/str';\n\n/**\n * Creates and shows a modal that contains a placeholder.\n *\n * @returns {Promise<Modal>}\n */\nconst showModalWithPlaceholder = async() => {\n    const modal = await ModalFactory.create({\n        body: await Templates.render('pg_paypal/paypal_button_placeholder', {})\n    });\n    modal.show();\n    return modal;\n};\n\n/**\n * Process the payment.\n *\n * @param {string} component Name of the component that the componentId belongs to\n * @param {string} paymentArea The area of the component that the componentId belongs to\n * @param {number} componentId An internal identifier that is used by the component\n * @param {string} description Description of the payment\n * @param {processCallback} callback The callback function to call when processing is finished\n * @returns {Promise<void>}\n */\nexport const process = async(component, paymentArea, componentId, description, callback) => {\n\n    const [\n        modal,\n        paypalConfig,\n    ] = await Promise.all([\n        showModalWithPlaceholder(),\n        Repository.getConfigForJs(component, paymentArea, componentId),\n    ]);\n    const currency = paypalConfig.currency;\n    const amount = paypalConfig.cost; // Cost with surcharge.\n\n    modal.getRoot().on(ModalEvents.hidden, () => {\n        // Destroy when hidden.\n        modal.destroy();\n    });\n\n    const paypalScript = `https://www.paypal.com/sdk/js?client-id=${paypalConfig.clientid}&currency=${currency}`;\n\n    callExternalFunction(paypalScript, () => {\n        modal.setBody(''); // We have to clear the body. The render method in paypal.Buttons will render everything.\n\n        paypal.Buttons({ // eslint-disable-line\n            // Set up the transaction.\n            createOrder: function(data, actions) {\n                return actions.order.create({\n                    purchase_units: [{ // eslint-disable-line\n                        amount: {\n                            currency_code: currency, // eslint-disable-line\n                            value: amount\n                        },\n                        description: Truncate.truncate(description, {length: 127, stripTags: true}),\n                    }],\n                    application_context: { // eslint-disable-line\n                        shipping_preference: 'NO_SHIPPING', // eslint-disable-line\n                        brand_name: Truncate.truncate(paypalConfig.brandname, {length: 127, stripTags: true}), // eslint-disable-line\n                    },\n                });\n            },\n            // Finalise the transaction.\n            onApprove: function(data) {\n                modal.getRoot().on(ModalEvents.outsideClick, (e) => {\n                    // Prevent closing the modal when clicking outside of it.\n                    e.preventDefault();\n                });\n\n                modal.setBody(getString('authorising', 'pg_paypal'));\n\n                // Call server to validate and capture payment for order.\n                return Ajax.call([{\n                    methodname: 'pg_paypal_create_transaction_complete',\n                    args: {\n                        component,\n                        paymentarea: paymentArea,\n                        componentid: componentId,\n                        orderid: data.orderID,\n                    },\n                }])[0]\n                .then(function(res) {\n                    modal.hide();\n                    return callback(res);\n                });\n            }\n        }).render(modal.getBody()[0]);\n    });\n};\n\n/**\n * The callback definition for process.\n *\n * @callback processCallback\n * @param {bool} success\n * @param {string} message\n */\n\n/**\n * Calls a function from an external javascript file.\n *\n * @param {string} jsFile URL of the external JavaScript file\n * @param {function} func The function to call\n */\nconst callExternalFunction = (jsFile, func) => {\n    // Check to see if this file has already been loaded. If so just go straight to the func.\n    if (callExternalFunction.currentlyloaded == jsFile) {\n        func();\n        return;\n    }\n\n    // PayPal can only work with one currency at the same time. We have to unload the previously loaded script\n    // if it was loaded for a different currency. Weird way indeed, but the only way.\n    // See: https://github.com/paypal/paypal-checkout-components/issues/1180\n    if (callExternalFunction.currentlyloaded) {\n        const suspectedScript = document.querySelector(`script[src=\"${callExternalFunction.currentlyloaded}\"]`);\n        if (suspectedScript) {\n            suspectedScript.parentNode.removeChild(suspectedScript);\n        }\n    }\n\n    const script = document.createElement('script');\n\n    if (script.readyState) {\n        script.onreadystatechange = function() {\n            if (this.readyState == 'complete' || this.readyState == 'loaded') {\n                this.onreadystatechange = null;\n                func();\n            }\n        };\n    } else {\n        script.onload = function() {\n            func();\n        };\n    }\n\n    script.setAttribute('src', jsFile);\n    document.head.appendChild(script);\n\n    callExternalFunction.currentlyloaded = jsFile;\n};\n\n/**\n * Holds the full url of loaded external JavaScript file.\n *\n * @static\n * @type {string}\n */\ncallExternalFunction.currentlyloaded = '';\n"],"file":"gateways_modal.min.js"}