Google Pay Request Objects

For complete sample code please refer here

Available dictionary objects

iStatus

const iStatus = {
    success: 100,
    unsupported: -100,
    error: -200
}

Use: iStatus.success

GPEnvironment

const GPEnvironment = {
    test: "TEST",
    production: "PRODUCTION"
}

Use: GPEnvironment.test

GPButtonColor

const GPButtonColor = {
    default: "default",
    black: "black",
    white: "white"
}

Use: GPButtonColor.white

GPButtonType

const GPButtonType = {
    buy: "buy",
    donate: "donate",
    plain: "plain"
}

Use: GPButtonType.buy

GPButtonSizeMode

const GPButtonSizeMode = {
    static: "static",
    fill: "fill"
}

Use: GPButtonSizeMode.fill

GPBillingAddressFormat

const GPBillingAddressFormat = {
    min: "MIN",
    full: "FULL"
}

Use: GPBillingAddressFormat.min

Request objects

GooglePayRequest

The Main object that contains all the information necessary to communicate with Google Pay API.

Payment Request example

initGP: function() {
    return {
        merchantInfo: this.merchantInfo,
        buttonOptions: this.buttonOptions,
        environment: this.getGPEnvironment(),
        billingParameters: this.billingParams,
        shippingParameters: {
            emailRequired: this.shippingParams.emailRequired,
            onGetShippingCosts: "gpRequest.shippingParams.onGetShippingCosts",
            onGetShippingOptions: "gpRequest.shippingParams.onGetShippingOptions"
        },
        onGetTransactionInfo: "gpRequest.onGetTransactionInfo",
        onBeforeProcessPayment: "gpRequest.onBeforeProcessPayment",
        onProcessPayment: "gpRequest.onProcessPayment",
        onPaymentCanceled: "gpRequest.onPaymentCanceled",
        onGPButtonLoaded: "gpRequest.gpButtonLoaded"
    };
}

onGPButtonLoaded example

gpButtonLoaded: function(resp) {
    if (!resp) return;
    if (resp.status === iStatus.success) {
        showHide("divGpay", true);
        showHide("lbGPPayload", true);
    } else if (resp.reason) {
        alert(resp.reason);
    }
}

onGetTransactionInfo example

onGetTransactionInfo: function () {
    let amt = this.getAmount();
    return {
        displayItems: [
            {
                label: "Subtotal",
                type: "SUBTOTAL",
                price: amt.toString(),
            },
            {
                label: "Tax",
                type: "TAX",
                price: (0.1 * amt).toString(),
            }
        ],
        countryCode: 'US',
        currencyCode: "USD",
        totalPriceStatus: "FINAL",
        totalPrice: (1.1 * amt).toString(),
        totalPriceLabel: "Total"
    }
}

onBeforeProcessPayment callback example

onBeforeProcessPayment: function () {
    return new Promise(function (resolve, reject) {
        try {
            //Do some validation here
            resolve(iStatus.success);
        } catch (err) {
            reject(err);
        }
    });
}

onProcessPayment callback example

onProcessPayment: function (paymentRequest) {
    let self = this;
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            try {
                console.log("paymentRequest", JSON.stringify(paymentRequest));
                paymentToken = paymentRequest.paymentData.paymentMethodData.tokenizationData.token;
                console.log("paymentToken", paymentToken);
                const amt = (chained([paymentRequest, "transactionInfo", "totalPrice"]) && paymentRequest.transactionInfo.totalPrice) || 0;
                try {
                    if (amt <= 0) 
                        throw "Payment is not authorized. Invalid amount. Amount must be greater than 0";
                    authorizeGPay({ token: paymentToken, amount: amt})
                    .then((resp) => {
                        gpRequest.handleResponse(resp);
                        resolve(resp);
                    })
                    .catch((rej) => {
                        console.error("Payment is not authorized", JSON.stringify(rej));
                        setTimeout(function () { alert("Payment is not authorized. Please check the logs") }, 500);
                        reject(rej);
                    });
                } catch (err) {
                    const emsg = JSON.stringify(err);
                    console.error(emsg);
                    setTimeout(function () { alert(emsg) }, 500);
                    reject({error: err});
                }
            } catch (e) {
                reject(e);
            }
        }, 100); //3000);
    });
}

onPaymentCanceled callback example

onPaymentCanceled: function(respCanceled) {
    setTimeout(function () { alert("Payment was canceled") }, 500);
}

MerchantInfo Object

Merchant Info example

merchantInfo: {
    merchantName: "Example Merchant"
}

GPButtonLoadedResult Object

BillingParameters Object

Billing Parameters example

billingParameters: {
    transactionId: "b65c7435-b57a-407e-a6e0-b166518d5d97",
    allowedAuthMethods: ["PAN_ONLY"],
    allowedCardNetworks: ["VISA", "MASTERCARD"],
    emailRequired: true
    billingAddressRequired: true,
    billingAddressFormat: GPBillingAddressFormat.min,
    phoneNumberRequired: true
}

ShippingParameters Object

Shipping Parameters example

shippingParams: {
    allowedCountryCodes: ['US'],
    onGetShippingCosts: function (shippingData) {
        logDebug({
            label: "onGetShippingCosts",
            data: shippingData
        });
        return {
            "shipping-001": "0.00",
            "shipping-002": "1.99",
            "shipping-003": "10.00"
        }
    },
    onGetShippingOptions: function (shippingData) {
        logDebug({
            label: "onGetShippingOptions",
            data: shippingData
        });
        let selectedOptionid = "shipping-001";
        if (shippingData && shippingData.shippingOptionData && shippingData.shippingOptionData.id !== "shipping_option_unselected") {
            selectedOptionid = shippingData.shippingOptionData.id;
        }
        return {
            defaultSelectedOptionId: selectedOptionid,
            shippingOptions: [
                {
                    "id": "shipping-001",
                    "label": "Free: Standard shipping",
                    "description": "Free Shipping delivered in 5 business days."
                },
                {
                    "id": "shipping-002",
                    "label": "$1.99: Standard shipping",
                    "description": "Standard shipping delivered in 3 business days."
                },
                {
                    "id": "shipping-003",
                    "label": "$10: Express shipping",
                    "description": "Express shipping delivered in 1 business day."
                },
            ]
        };
    }
}

ShippingData Object

ShippingData object example

{
  "shippingAddress": {
    "countryCode": "US",
    "postalCode": "10601",
    "locality": "White Plains",
    "administrativeArea": "NY"
  },
  "shippingOptionData": {
    "id": "shipping-001"
  }
}

ShippingAddress Object

ShippingOptionData Object

ButtonOptions Object

Button Options example

buttonOptions: {
    buttonColor: GPButtonColor.white,
    buttonType: GPButtonType.buy,
    buttonSizeMode: GPButtonSizeMode.full
}

Last updated