1/* 2 * Define the version of the Google Pay API referenced when creating your 3 * configuration 4 */ 5const baseRequest = { 6 apiVersion: 2, 7 apiVersionMinor: 0, 8}; 9let paymentsClient = null, 10 allowedPaymentMethods = null, 11 merchantInfo = null; 12/* Configure your site's support for payment methods supported by the Google Pay */ 13function getGoogleIsReadyToPayRequest(allowedPaymentMethods) { 14 return Object.assign({}, baseRequest, { 15 allowedPaymentMethods: allowedPaymentMethods, 16 }); 17} 18/* Fetch Default Config from PayPal via PayPal SDK */ 19async function getGooglePayConfig() { 20 if (allowedPaymentMethods == null || merchantInfo == null) { 21 const googlePayConfig = await paypal.Googlepay().config(); 22 allowedPaymentMethods = googlePayConfig.allowedPaymentMethods; 23 merchantInfo = googlePayConfig.merchantInfo; 24 } 25 return { 26 allowedPaymentMethods, 27 merchantInfo, 28 }; 29} 30/* Configure support for the Google Pay API */ 31async function getGooglePaymentDataRequest() { 32 const paymentDataRequest = Object.assign({}, baseRequest); 33 const { 34 allowedPaymentMethods, 35 merchantInfo 36 } = await getGooglePayConfig(); 37 paymentDataRequest.allowedPaymentMethods = allowedPaymentMethods; 38 paymentDataRequest.transactionInfo = getGoogleTransactionInfo(); 39 paymentDataRequest.merchantInfo = merchantInfo; 40 paymentDataRequest.callbackIntents = ["PAYMENT_AUTHORIZATION"]; 41 return paymentDataRequest; 42} 43 44function onPaymentAuthorized(paymentData) { 45 return new Promise(function(resolve, reject) { 46 processPayment(paymentData) 47 .then(function(data) { 48 resolve({ 49 transactionState: "SUCCESS" 50 }); 51 }) 52 .catch(function(errDetails) { 53 resolve({ 54 transactionState: "ERROR" 55 }); 56 }); 57 }); 58 59} 60 61function getGooglePaymentsClient() { 62 if (paymentsClient === null) { 63 paymentsClient = new google.payments.api.PaymentsClient({ 64 environment: "PRODUCTION", 65 paymentDataCallbacks: { 66 onPaymentAuthorized: onPaymentAuthorized, 67 }, 68 }); 69 } 70 return paymentsClient; 71} 72async function onGooglePayLoaded() { 73 const paymentsClient = getGooglePaymentsClient(); 74 const { 75 allowedPaymentMethods 76 } = await getGooglePayConfig(); 77 paymentsClient 78 .isReadyToPay(getGoogleIsReadyToPayRequest(allowedPaymentMethods)) 79 .then(function(response) { 80 if (response.result) { 81 addGooglePayButton(); 82 } 83 }) 84 .catch(function(err) { 85 console.error(err); 86 }); 87} 88 89function addGooglePayButton() { 90 if($('#googlepay-button-container').length == 0){ return; } 91 const paymentsClient = getGooglePaymentsClient(); 92 const button = paymentsClient.createButton({ 93 onClick: onGooglePaymentButtonClicked, 94 buttonSizeMode: 'fill', 95 }); 96 document.getElementById("googlepay-button-container").appendChild(button); 97} 98 99function getGoogleTransactionInfo() { 100 101/* 102 example_transaction = { 103 displayItems: [{ 104 label: "Subtotal", 105 type: "SUBTOTAL", 106 price: "100.00", 107 }, 108 { 109 label: "Tax", 110 type: "TAX", 111 price: "10.00", 112 }, 113 ], 114 countryCode: "US", 115 currencyCode: "USD", 116 totalPriceStatus: "FINAL", 117 totalPrice: "110.00", 118 totalPriceLabel: "Total", 119 }; 120 121 console.log(google_pay_transaction_info); 122 console.log(example_transaction); 123*/ 124 125 return google_pay_transaction_info; 126} 127async function onGooglePaymentButtonClicked() { 128 const paymentDataRequest = await getGooglePaymentDataRequest(); 129 paymentDataRequest.transactionInfo = getGoogleTransactionInfo(); 130 const paymentsClient = getGooglePaymentsClient(); 131 paymentsClient.loadPaymentData(paymentDataRequest); 132} 133 134/* 135async function processPayment(paymentData) { 136 try { 137 const { 138 currencyCode, 139 totalPrice 140 } = getGoogleTransactionInfo(); 141 const order = { 142 intent: "CAPTURE", 143 purchase_units: [{ 144 amount: { 145 currency_code: currencyCode, 146 value: totalPrice, 147 }, 148 }, ], 149 }; 150 // Create Order 151 const { 152 id 153 } = await fetch('/paypal_checkout.php', { 154 method: "POST", 155 // Use the "body" parameter to optionally pass additional order information 156 // such as product ID or amount 157 body: JSON.stringify({ 158 rt: 'create', 159 paymentSource: 'googlepay', 160 }), 161 }).then((res) => res.json()); 162 163 const { 164 status 165 } = await paypal.Googlepay().confirmOrder({ 166 orderId: id, 167 paymentMethodData: paymentData.paymentMethodData, 168 }); 169 170 if (status === "APPROVED") { 171 // Capture the Order 172 const captureResponse = await fetch('/paypal_checkout.php', { 173 method: "POST", 174 body: JSON.stringify({ 175 rt: 'capture', 176 paypal_order_id: id, 177 }), 178 }).then((res) => res.json()); 179 return { 180 transactionState: "SUCCESS" 181 }; 182 } else { 183 return { 184 transactionState: "ERROR" 185 }; 186 } 187 } catch (err) { 188 return { 189 transactionState: "ERROR", 190 error: { 191 message: err.message, 192 }, 193 }; 194 } 195} 196//*/ 197 198//* 199async function processPayment(paymentData) { 200 try { 201 const { 202 currencyCode, 203 totalPrice 204 } = getGoogleTransactionInfo(); 205 const order = { 206 intent: "CAPTURE", 207 purchase_units: [{ 208 amount: { 209 currency_code: currencyCode, 210 value: totalPrice, 211 }, 212 }, ], 213 }; 214 215 const { 216 id 217 } = await fetch('/paypal_checkout.php', { 218 method: "POST", 219 // Use the "body" parameter to optionally pass additional order information 220 // such as product ID or amount 221 body: JSON.stringify({ 222 rt: 'create', 223 paymentSource: 'googlepay', 224 }), 225 }).then((res) => res.json()); 226 const { 227 status 228 } = await paypal.Googlepay().confirmOrder({ 229 orderId: id, 230 paymentMethodData: paymentData.paymentMethodData, 231 }); 232 if (status === "APPROVED") { 233 const captureResponse = await fetch('/paypal_checkout.php', { 234 method: "POST", 235 body: JSON.stringify({ 236 rt: 'capture', 237 paypal_order_id: id, 238 }), 239 }).then((res) => res.json()); 240 window.location = '/cart/thank-you?cart_action=paypal_confirm'; 241 return { 242 transactionState: "SUCCESS" 243 }; 244 } else { 245 msg = encodeURI('An error occurred while trying to process your transaction (code: gp: c).'); 246 window.location = '/cart/checkout?cart_action=checkout&msg=' + msg; 247 return { 248 transactionState: "ERROR" 249 }; 250 } 251 } catch (err) { 252 console.log(err); 253 msg = encodeURI(err.message); 254 console.log(err); 255 console.log(msg); 256 window.location = '/cart/checkout?cart_action=checkout&msg=' + msg; 257 return { 258 transactionState: "ERROR", 259 error: { 260 message: err.message, 261 }, 262 }; 263 } 264} 265// */
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.