1/** 2 * Copyright (c) 2020 Nexi Payments S.p.A. 3 * 4 * @author iPlusService S.r.l. 5 * @copyright Copyright (c) 2020 Nexi Payments S.p.A. (https://ecommerce.nexi.it) 6 * @license GNU General Public License v3.0 7 * 8 * @category Payment Module 9 * 10 * @version 8.0.0 11 */ 12let paymentsClient = null; 13 14const baseRequest = { 15 apiVersion: 2, 16 apiVersionMinor: 0, 17}; 18 19function getXPayGooglePaymentDataRequest(configuration) { 20 const paymentDataRequest = Object.assign({}, baseRequest); 21 if (configuration.config.NEXIXPAY_GATEWAY === "xpay") { 22 paymentDataRequest.allowedPaymentMethods = [ 23 Object.assign({}, getXPayGooglePayBaseCardPaymentMethod(configuration), { 24 tokenizationSpecification: { 25 type: "PAYMENT_GATEWAY", 26 parameters: { 27 gateway: "nexi", 28 gatewayMerchantId: 29 configuration.config.NEXIXPAY_GOOGLE_PAY_GATEWAY_MERCHANT_ID, 30 }, 31 }, 32 }), 33 ]; 34 } else if (configuration.config.NEXIXPAY_GATEWAY === "npg") { 35 paymentDataRequest.allowedPaymentMethods = [ 36 Object.assign({}, getXPayGooglePayBaseCardPaymentMethod(configuration), { 37 tokenizationSpecification: { 38 type: "PAYMENT_GATEWAY", 39 parameters: { 40 gateway: "nexigtw", 41 gatewayMerchantId: 42 configuration.config.NEXIXPAY_GOOGLE_PAY_GATEWAY_MERCHANT_ID, 43 }, 44 }, 45 }), 46 ]; 47 } 48 49 paymentDataRequest.transactionInfo = { 50 countryCode: configuration.transactionInfo.countryCode, 51 currencyCode: configuration.transactionInfo.currencyCode, 52 totalPriceStatus: "FINAL", 53 totalPrice: configuration.transactionInfo.totalPrice, 54 }; 55 56 paymentDataRequest.merchantInfo = { 57 merchantId: configuration.config.NEXIXPAY_GOOGLE_PAY_MERCHANT_ID, 58 merchantName: configuration.config.NEXIXPAY_GOOGLE_PAY_MERCHANT_NAME, 59 }; 60 61 return paymentDataRequest; 62} 63 64function onXPayGooglePayLoaded() { 65 getXPayGooglePayConfiguration().then(function (configuration) { 66 if ( 67 configuration.config.NEXIXPAY_TEST_MODE == true && 68 configuration.config.NEXIXPAY_GATEWAY === "xpay" 69 ) { 70 paymentsClient = new google.payments.api.PaymentsClient({ 71 environment: "TEST", 72 }); 73 } else { 74 paymentsClient = new google.payments.api.PaymentsClient({ 75 environment: "PRODUCTION", 76 }); 77 } 78 79 const isReadyToPayRequest = Object.assign({}, baseRequest, { 80 allowedPaymentMethods: [ 81 getXPayGooglePayBaseCardPaymentMethod(configuration), 82 ], 83 }); 84 85 paymentsClient 86 .isReadyToPay(isReadyToPayRequest) 87 .then(function (response) { 88 if (response.result) { 89 addXPayGooglePayButton(configuration); 90 prefetchXPayGooglePaymentData(configuration); 91 } 92 }) 93 .catch(function (err) { 94 console.error(err); 95 }); 96 }); 97} 98 99function addXPayGooglePayButton(configuration) { 100 const button = paymentsClient.createButton({ 101 onClick: function () { 102 onXPayGooglePaymentButtonClicked(configuration); 103 }, 104 buttonSizeMode: "fill", 105 buttonColor: configuration.config.NEXIXPAY_GOOGLE_PAY_BUTTON_COLOR, 106 buttonType: configuration.config.NEXIXPAY_GOOGLE_PAY_BUTTON_TYPE, 107 buttonLocale: configuration.lang, 108 }); 109 110 document.getElementById("google-pay-button-container").appendChild(button); 111} 112 113function prefetchXPayGooglePaymentData(configuration) { 114 const paymentDataRequest = getXPayGooglePaymentDataRequest(configuration); 115 paymentDataRequest.transactionInfo = { 116 countryCode: configuration.transactionInfo.countryCode, 117 currencyCode: configuration.transactionInfo.currencyCode, 118 totalPriceStatus: "FINAL", 119 totalPrice: configuration.transactionInfo.totalPrice, 120 }; 121 122 paymentsClient.prefetchPaymentData(paymentDataRequest); 123} 124 125function onXPayGooglePaymentButtonClicked(configuration) { 126 const paymentDataRequest = getXPayGooglePaymentDataRequest(configuration); 127 128 paymentsClient 129 .loadPaymentData(paymentDataRequest) 130 .then(function (paymentData) { 131 processXPayGooglePayPayment(paymentData); 132 }) 133 .catch(function (err) { 134 console.error(err); 135 }); 136} 137 138function processXPayGooglePayPayment(paymentData) { 139 paymentToken = paymentData.paymentMethodData.tokenizationData.token; 140 finalizeXPayGooglePayPayment(paymentData); 141} 142 143function getXPayGooglePayBaseCardPaymentMethod(configuration) { 144 const allowedCardNetworks = []; 145 146 configuration.cards.forEach(function (card) { 147 if (card === "MC") { 148 allowedCardNetworks.push("MASTERCARD"); 149 } else if (card === "UPI") { 150 } else if (card === "Maestro") { 151 } else { 152 allowedCardNetworks.push(card); 153 } 154 }); 155 156 return { 157 type: "CARD", 158 parameters: { 159 allowedAuthMethods: ["PAN_ONLY", "CRYPTOGRAM_3DS"], 160 allowedCardNetworks: allowedCardNetworks, 161 }, 162 }; 163} 164 165function getXPayGooglePayConfiguration() { 166 return new Promise(function (resolve, reject) { 167 jQuery.ajax({ 168 type: "GET", 169 data: { 170 action: "getXPayGooglePayConfiguration", 171 }, 172 url: $("#nexi_googlepay_url").val(), 173 success: function (response) { 174 resolve(JSON.parse(response, true)); 175 }, 176 error: function (xhr, ajaxOptions, thrownError) { 177 console.error( 178 thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText 179 ); 180 }, 181 }); 182 }); 183} 184 185function loadGooglePaySdk(sdkLink, onGooglePaySdkLoad) { 186 if (window.Build === undefined) { 187 const imported = document.createElement("script"); 188 imported.onload = onGooglePaySdkLoad; 189 imported.src = sdkLink; 190 imported.type = "text/javascript"; 191 192 document.head.appendChild(imported); 193 } else { 194 onGooglePaySdkLoad(); 195 } 196} 197 198function finalizeXPayGooglePayPayment(paymentData) { 199 return new Promise(function (resolve, reject) { 200 jQuery.ajax({ 201 type: "POST", 202 data: { 203 action: "finalizeXPayGooglePayPayment", 204 body: paymentData, 205 }, 206 url: $("#nexi_googlepay_url").val(), 207 success: function (response) { 208 responseObj = JSON.parse(response, true); 209 210 if (responseObj.url) { 211 window.location.href = responseObj.url; 212 } else { 213 loadGooglePaySdk(responseObj.sdkLink, function () { 214 new Build({ 215 onBuildFlowStateChange: function (evtData) { 216 if (evtData.event === "BUILD_FLOW_STATE_CHANGE") { 217 if ( 218 evtData.data.state === "REDIRECTED_TO_EXTERNAL_DOMAIN" || 219 evtData.data.state === "PAYMENT_COMPLETE" 220 ) { 221 window.location.replace(responseObj.controllerUrlGooglePay); 222 } 223 } 224 }, 225 }); 226 }); 227 228 document.getElementById("google-pay-button-container").innerHTML = ""; 229 230 document.getElementById( 231 "google-pay-button-container" 232 ).style.visibility = "hidden"; 233 234 for (var i = 0; i < responseObj.iframe.fields.length; i++) { 235 var iframe = document.createElement("iframe"); 236 237 iframe.src = responseObj.iframe.fields[i].src; 238 239 document 240 .getElementById("google-pay-button-container") 241 .appendChild(iframe); 242 } 243 244 document.getElementById("google-pay-loader").style.visibility = 245 "visible"; 246 } 247 }, 248 249 error: function (xhr, ajaxOptions, thrownError) { 250 console.error( 251 thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText 252 ); 253 }, 254 }); 255 }); 256}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.