diff --git a/payment/gateway/paypal/amd/build/gateways_modal.min.js b/payment/gateway/paypal/amd/build/gateways_modal.min.js index 1207d27dd79..8511ad8488c 100644 --- a/payment/gateway/paypal/amd/build/gateways_modal.min.js +++ b/payment/gateway/paypal/amd/build/gateways_modal.min.js @@ -1,2 +1,2 @@ -function _typeof(a){"@babel/helpers - typeof";if("function"==typeof Symbol&&"symbol"==typeof Symbol.iterator){_typeof=function(a){return typeof a}}else{_typeof=function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a}}return _typeof(a)}define ("pg_paypal/gateways_modal",["exports","./repository","core/templates","./selectors","core/truncate"],function(a,b,c,d,e){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.process=void 0;b=h(b);c=f(c);d=f(d);e=f(e);function f(a){return a&&a.__esModule?a:{default:a}}function g(){if("function"!=typeof WeakMap)return null;var a=new WeakMap;g=function(){return a};return a}function h(a){if(a&&a.__esModule){return a}if(null===a||"object"!==_typeof(a)&&"function"!=typeof a){return{default:a}}var b=g();if(b&&b.has(a)){return b.get(a)}var c={},d=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var e in a){if(Object.prototype.hasOwnProperty.call(a,e)){var f=d?Object.getOwnPropertyDescriptor(a,e):null;if(f&&(f.get||f.set)){Object.defineProperty(c,e,f)}else{c[e]=a[e]}}}c.default=a;if(b){b.set(a,c)}return c}function i(a,b){return n(a)||m(a,b)||k(a,b)||j()}function j(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function k(a,b){if(!a)return;if("string"==typeof a)return l(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);if("Object"===c&&a.constructor)c=a.constructor.name;if("Map"===c||"Set"===c)return Array.from(c);if("Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c))return l(a,b)}function l(a,b){if(null==b||b>a.length)b=a.length;for(var c=0,d=Array(b);ca.length)b=a.length;for(var c=0,d=Array(b);c.\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 \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 Selectors from './selectors';\nimport Truncate from 'core/truncate';\n\n/**\n * Renders a placeholder in the modal.\n *\n * @param rootElement\n * @returns {Promise}\n */\nconst showPlaceholder = async(rootElement) => {\n const {html, js} = await Templates.renderForPromise('pg_paypal/paypal_button_placeholder', {});\n Templates.replaceNodeContents(rootElement.querySelector(Selectors.regions.gatewaysContainer), html, js);\n};\n\nexport const process = async(rootElement, amount, currency, component, componentid, description) => {\n\n const [\n ,\n paypalConfig,\n ] = await Promise.all([\n showPlaceholder(rootElement),\n Repository.getConfigForJs(),\n ]);\n\n const paypalScript = `https://www.paypal.com/sdk/js?client-id=${paypalConfig.clientid}¤cy=${currency}`;\n\n callExternalFunction(paypalScript, () => {\n rootElement.querySelector(Selectors.buttons.save).style.display = 'none';\n rootElement.querySelector(Selectors.regions.gatewaysContainer).innerHTML = '';\n paypal.Buttons({ // eslint-disable-line\n createOrder: function(data, actions) {\n // This function sets up the details of the transaction, including the amount and line item details.\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 }).render(Selectors.regions.gatewaysContainer);\n });\n};\n\nconst callExternalFunction = (jsFile, callback) => {\n // Check to see if this file has already been loaded. If so just go straight to the callback.\n if (callExternalFunction.currentlyloaded.includes(jsFile)) {\n callback();\n return;\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 callback();\n }\n };\n } else {\n script.onload = function() {\n callback();\n };\n }\n\n script.setAttribute('src', jsFile);\n document.head.appendChild(script);\n\n callExternalFunction.currentlyloaded.push(jsFile);\n};\n\ncallExternalFunction.currentlyloaded = [];\n"],"file":"gateways_modal.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/gateways_modal.js"],"names":["showPlaceholder","rootElement","Templates","renderForPromise","html","js","replaceNodeContents","querySelector","Selectors","regions","gatewaysContainer","process","amount","currency","component","componentid","description","Promise","all","Repository","getConfigForJs","paypalConfig","paypalScript","clientid","callExternalFunction","buttons","save","style","display","innerHTML","paypal","Buttons","createOrder","data","actions","order","create","purchase_units","currency_code","value","Truncate","truncate","length","stripTags","application_context","shipping_preference","brand_name","brandname","onApprove","authorize","then","authorization","authorizationID","payments","authorizations","id","Ajax","call","methodname","args","orderid","orderID","authorizationid","res","Notification","addNotification","message","type","success","render","jsFile","callback","currentlyloaded","includes","script","document","createElement","readyState","onreadystatechange","onload","setAttribute","head","appendChild","push"],"mappings":"uhBAuBA,OACA,OACA,OACA,OACA,OACA,O,u3DAQMA,CAAAA,CAAe,4CAAG,WAAMC,CAAN,4GACKC,WAAUC,gBAAV,CAA2B,qCAA3B,CAAkE,EAAlE,CADL,iBACbC,CADa,GACbA,IADa,CACPC,CADO,GACPA,EADO,CAEpBH,UAAUI,mBAAV,CAA8BL,CAAW,CAACM,aAAZ,CAA0BC,UAAUC,OAAV,CAAkBC,iBAA5C,CAA9B,CAA8FN,CAA9F,CAAoGC,CAApG,EAFoB,wCAAH,uD,CAKRM,CAAO,4CAAG,WAAMV,CAAN,CAAmBW,CAAnB,CAA2BC,CAA3B,CAAqCC,CAArC,CAAgDC,CAAhD,CAA6DC,CAA7D,8GAKTC,CAAAA,OAAO,CAACC,GAAR,CAAY,CAClBlB,CAAe,CAACC,CAAD,CADG,CAElBkB,CAAU,CAACC,cAAX,EAFkB,CAAZ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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 .\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 \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 Selectors from './selectors';\nimport Truncate from 'core/truncate';\nimport Ajax from 'core/ajax';\nimport Notification from 'core/notification';\n\n/**\n * Renders a placeholder in the modal.\n *\n * @param rootElement\n * @returns {Promise}\n */\nconst showPlaceholder = async(rootElement) => {\n const {html, js} = await Templates.renderForPromise('pg_paypal/paypal_button_placeholder', {});\n Templates.replaceNodeContents(rootElement.querySelector(Selectors.regions.gatewaysContainer), html, js);\n};\n\nexport const process = async(rootElement, amount, currency, component, componentid, description) => {\n\n const [\n ,\n paypalConfig,\n ] = await Promise.all([\n showPlaceholder(rootElement),\n Repository.getConfigForJs(),\n ]);\n\n const paypalScript = `https://www.paypal.com/sdk/js?client-id=${paypalConfig.clientid}¤cy=${currency}&intent=authorize`;\n\n callExternalFunction(paypalScript, () => {\n rootElement.querySelector(Selectors.buttons.save).style.display = 'none';\n rootElement.querySelector(Selectors.regions.gatewaysContainer).innerHTML = '';\n paypal.Buttons({ // eslint-disable-line\n createOrder: function(data, actions) {\n // This function sets up the details of the transaction, including the amount and line item details.\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 onApprove: function(data, actions) {\n // Authorize the transaction.\n actions.order.authorize().then(function(authorization) {\n // Get the authorization id.\n const authorizationID = authorization.purchase_units[0].payments.authorizations[0].id;\n\n // Call your server to validate and capture the transaction.\n return Ajax.call([{\n methodname: 'pg_paypal_transaction_complete',\n args: {\n component,\n componentid,\n orderid: data.orderID,\n authorizationid: authorizationID,\n },\n }])[0]\n .then(function(res) {\n Notification.addNotification({\n message: res.message,\n type: res.success ? 'success' : 'error'\n });\n });\n });\n }\n }).render(Selectors.regions.gatewaysContainer);\n });\n};\n\nconst callExternalFunction = (jsFile, callback) => {\n // Check to see if this file has already been loaded. If so just go straight to the callback.\n if (callExternalFunction.currentlyloaded.includes(jsFile)) {\n callback();\n return;\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 callback();\n }\n };\n } else {\n script.onload = function() {\n callback();\n };\n }\n\n script.setAttribute('src', jsFile);\n document.head.appendChild(script);\n\n callExternalFunction.currentlyloaded.push(jsFile);\n};\n\ncallExternalFunction.currentlyloaded = [];\n"],"file":"gateways_modal.min.js"} \ No newline at end of file diff --git a/payment/gateway/paypal/amd/src/gateways_modal.js b/payment/gateway/paypal/amd/src/gateways_modal.js index b84c985af42..4878736d5cc 100644 --- a/payment/gateway/paypal/amd/src/gateways_modal.js +++ b/payment/gateway/paypal/amd/src/gateways_modal.js @@ -25,6 +25,8 @@ import * as Repository from './repository'; import Templates from 'core/templates'; import Selectors from './selectors'; import Truncate from 'core/truncate'; +import Ajax from 'core/ajax'; +import Notification from 'core/notification'; /** * Renders a placeholder in the modal. @@ -47,7 +49,7 @@ export const process = async(rootElement, amount, currency, component, component Repository.getConfigForJs(), ]); - const paypalScript = `https://www.paypal.com/sdk/js?client-id=${paypalConfig.clientid}¤cy=${currency}`; + const paypalScript = `https://www.paypal.com/sdk/js?client-id=${paypalConfig.clientid}¤cy=${currency}&intent=authorize`; callExternalFunction(paypalScript, () => { rootElement.querySelector(Selectors.buttons.save).style.display = 'none'; @@ -69,6 +71,30 @@ export const process = async(rootElement, amount, currency, component, component }, }); }, + onApprove: function(data, actions) { + // Authorize the transaction. + actions.order.authorize().then(function(authorization) { + // Get the authorization id. + const authorizationID = authorization.purchase_units[0].payments.authorizations[0].id; + + // Call your server to validate and capture the transaction. + return Ajax.call([{ + methodname: 'pg_paypal_transaction_complete', + args: { + component, + componentid, + orderid: data.orderID, + authorizationid: authorizationID, + }, + }])[0] + .then(function(res) { + Notification.addNotification({ + message: res.message, + type: res.success ? 'success' : 'error' + }); + }); + }); + } }).render(Selectors.regions.gatewaysContainer); }); };