/* * Define the version of the Google Pay API referenced when creating your * configuration */ const baseRequest = { apiVersion: 2, apiVersionMinor: 0, }; let paymentsClient = null, allowedPaymentMethods = null, merchantInfo = null; /* Configure your site's support for payment methods supported by the Google Pay */ function getGoogleIsReadyToPayRequest(allowedPaymentMethods) { return Object.assign({}, baseRequest, { allowedPaymentMethods: allowedPaymentMethods, }); } /* Fetch Default Config from PayPal via PayPal SDK */ async function getGooglePayConfig() { if (allowedPaymentMethods == null || merchantInfo == null) { const googlePayConfig = await paypal.Googlepay().config(); allowedPaymentMethods = googlePayConfig.allowedPaymentMethods; merchantInfo = googlePayConfig.merchantInfo; } return { allowedPaymentMethods, merchantInfo, }; } /* Configure support for the Google Pay API */ async function getGooglePaymentDataRequest() { const paymentDataRequest = Object.assign({}, baseRequest); const { allowedPaymentMethods, merchantInfo } = await getGooglePayConfig(); paymentDataRequest.allowedPaymentMethods = allowedPaymentMethods; paymentDataRequest.transactionInfo = getGoogleTransactionInfo(); paymentDataRequest.merchantInfo = merchantInfo; paymentDataRequest.callbackIntents = ["PAYMENT_AUTHORIZATION"]; return paymentDataRequest; } function onPaymentAuthorized(paymentData) { return new Promise(function(resolve, reject) { processPayment(paymentData) .then(function(data) { resolve({ transactionState: "SUCCESS" }); }) .catch(function(errDetails) { resolve({ transactionState: "ERROR" }); }); }); } function getGooglePaymentsClient() { if (paymentsClient === null) { paymentsClient = new google.payments.api.PaymentsClient({ environment: "TEST", paymentDataCallbacks: { onPaymentAuthorized: onPaymentAuthorized, }, }); } return paymentsClient; } async function onGooglePayLoaded() { const paymentsClient = getGooglePaymentsClient(); const { allowedPaymentMethods } = await getGooglePayConfig(); paymentsClient .isReadyToPay(getGoogleIsReadyToPayRequest(allowedPaymentMethods)) .then(function(response) { if (response.result) { addGooglePayButton(); } }) .catch(function(err) { console.error(err); }); } function addGooglePayButton() { if($('#googlepay-button-container').length == 0){ return; } const paymentsClient = getGooglePaymentsClient(); const button = paymentsClient.createButton({ onClick: onGooglePaymentButtonClicked, buttonSizeMode: 'fill', }); document.getElementById("googlepay-button-container").appendChild(button); } function getGoogleTransactionInfo() { /* example_transaction = { displayItems: [{ label: "Subtotal", type: "SUBTOTAL", price: "100.00", }, { label: "Tax", type: "TAX", price: "10.00", }, ], countryCode: "US", currencyCode: "USD", totalPriceStatus: "FINAL", totalPrice: "110.00", totalPriceLabel: "Total", }; console.log(google_pay_transaction_info); console.log(example_transaction); */ return google_pay_transaction_info; } async function onGooglePaymentButtonClicked() { const paymentDataRequest = await getGooglePaymentDataRequest(); paymentDataRequest.transactionInfo = getGoogleTransactionInfo(); const paymentsClient = getGooglePaymentsClient(); paymentsClient.loadPaymentData(paymentDataRequest); } /* async function processPayment(paymentData) { try { const { currencyCode, totalPrice } = getGoogleTransactionInfo(); const order = { intent: "CAPTURE", purchase_units: [{ amount: { currency_code: currencyCode, value: totalPrice, }, }, ], }; // Create Order const { id } = await fetch('/paypal_checkout.php', { method: "POST", // Use the "body" parameter to optionally pass additional order information // such as product ID or amount body: JSON.stringify({ rt: 'create', paymentSource: 'googlepay', }), }).then((res) => res.json()); const { status } = await paypal.Googlepay().confirmOrder({ orderId: id, paymentMethodData: paymentData.paymentMethodData, }); if (status === "APPROVED") { // Capture the Order const captureResponse = await fetch('/paypal_checkout.php', { method: "POST", body: JSON.stringify({ rt: 'capture', paypal_order_id: id, }), }).then((res) => res.json()); return { transactionState: "SUCCESS" }; } else { return { transactionState: "ERROR" }; } } catch (err) { return { transactionState: "ERROR", error: { message: err.message, }, }; } } //*/ //* async function processPayment(paymentData) { try { const { currencyCode, totalPrice } = getGoogleTransactionInfo(); const order = { intent: "CAPTURE", purchase_units: [{ amount: { currency_code: currencyCode, value: totalPrice, }, }, ], }; const { id } = await fetch('/paypal_checkout.php', { method: "POST", // Use the "body" parameter to optionally pass additional order information // such as product ID or amount body: JSON.stringify({ rt: 'create', paymentSource: 'googlepay', }), }).then((res) => res.json()); const { status } = await paypal.Googlepay().confirmOrder({ orderId: id, paymentMethodData: paymentData.paymentMethodData, }); if (status === "APPROVED") { const captureResponse = await fetch('/paypal_checkout.php', { method: "POST", body: JSON.stringify({ rt: 'capture', paypal_order_id: id, }), }).then((res) => res.json()); window.location = '/cart/thank-you?cart_action=paypal_confirm'; return { transactionState: "SUCCESS" }; } else { msg = encodeURI('An error occurred while trying to process your transaction (code: gp: c).'); window.location = '/cart/checkout?cart_action=checkout&msg=' + msg; return { transactionState: "ERROR" }; } } catch (err) { console.log(err); msg = encodeURI(err.message); console.log(err); console.log(msg); window.location = '/cart/checkout?cart_action=checkout&msg=' + msg; return { transactionState: "ERROR", error: { message: err.message, }, }; } } // */