Declare the version of the Google Pay API that your site uses. The major and minor versions are required in the fields of each passed object, and are included in the response.
The following code sample shows declared API versions:
Replace example and exampleGatewayMerchantId with the appropriate values for your payment provider. Use the following table to find the specific gateway and gatewayMerchantId values for your payment provider:
The PAYMENT_GATEWAY tokenization type is the most common merchant implementation of the card payment method in the Google Pay API. If your payment provider isn't supported, you might be able to accept Google Pay by a DIRECT integration. For more information, see the Direct tokenization documentation.
Step 3: Define supported payment card networks
Define the card networks accepted by your site. See the following code sample:
The Google Pay API might return cards on file with Google (PAN_ONLY), and/or a device token on an Android device that's authenticated with a 3-D Secure cryptogram (CRYPTOGRAM_3DS). See the following code sample:
For more information, see CardParameters in our Object reference documentation. Also, check with your gateway or processor for the card networks that are supported, and for support for Android device tokens.
Step 4: Describe your allowed payment methods
To describe your allowed payment methods, complete the following steps:
Combine your supported authentication methods and supported card networks in order to describe your site's support for the CARD payment method. See the following code sample:
Extend the base card payment method object to describe information you expect to be returned to your application. Include a description of the tokenized payment data. See the following code sample:
After the Google Pay API JavaScript library loads, initialize a PaymentsClient object. Initial development uses a TEST environment, which returns dummy payment methods that are suitable to reference the structure of a payment response. In this environment, a selected payment method isn't capable of a transaction. See the following code sample.
const paymentsClient = new google.payments.api.PaymentsClient({environment:'TEST'});
For more information about the requirements for a PRODUCTION environment that returns chargeable payment methods, see the Integration checklist.
Step 6: Determine readiness to pay with the Google Pay API
To determine readiness to pay with the Google Pay API, complete the following steps:
Add your allowed payment methods to your base request object. See the following code sample:
Call isReadyToPay() to determine if the Google Pay API is supported by the current device and browser for your specified payment methods. See the following code sample:
paymentsClient.isReadyToPay(isReadyToPayRequest) .then(function(response){ if(response.result){ // add a Google Pay payment button } }) .catch(function(err){ // show error in developer console for debugging console.error(err); });
Step 7: Add a Google Pay payment button
Add a Google Pay payment button to your page to encourage shoppers to check out with payment methods that are supported by the Google Pay API and your site. For more information about available button types, colors, and display requirements, see the Brand guidelines.
See the following payment button code sample:
const button = paymentsClient.createButton({onClick:()=> console.log('TODO: click handler'), allowedPaymentMethods:[]});// make sure to provide an allowed payment method document.getElementById('container').appendChild(button);
Step 8: Create a PaymentDataRequest object
To create a PaymentDataRequest object, complete the following steps:
Build a JavaScript object that describes your site's support for the Google Pay API. For a full list of supported properties, see PaymentDataRequest. See the following code sample:
Add the payment methods supported by your app, such as any configuration of additional data that's expected in the response. See the following code sample:
Provide a user-visible merchant name, and use our TESTmerchantId value when in TEST. For more details, and for information on when to replace the TESTmerchantId value, see MerchantInfo. See the following code sample of a user-visible merchant name:
Step 9: Register an event handler for user gestures
To register an event handler for user gestures, complete the following steps:
Register a click event handler for the purchase button. The event handler calls loadPaymentData() immediately after it interacts with the Google Pay, payment button.
After a Google user grants permission for your site to receive information about the user's selected form of payment and optional contact data, handle the response from the Google Pay API.
Extract the payment token from the paymentData response. If you implement a gateway integration, pass this token to your gateway without any modifications.
paymentsClient.loadPaymentData(paymentDataRequest).then(function(paymentData){ // if using gateway tokenization, pass this token without modification paymentToken = paymentData.paymentMethodData.tokenizationData.token; }).catch(function(err){ // show error in developer console for debugging console.error(err); });
Step 10 (optional): Prefetch payment data before user interaction
To improve execution time, prefetch your website's payment configuration from the Google Pay API before you call loadPaymentData(). See the following example:
Authorize Payments is used to start the payment process and acknowledge a payment's authorization status. To set up Authorize Payments, take the following steps:
The onPaymentAuthorized() callback is invoked with a PaymentData object by Google after a payer approves payment through a user gesture, such as if they click Pay.
The callback returns a Promise<PaymentAuthorizationResult> value. The PaymentAuthorizationResult object has a SUCCESS or ERROR transaction state status. Upon success, the payment sheet is closed successfully. If you encounter an error, the payment sheet renders the error details returned after the payment is processed. The user can change the payment sheet’s payment data and authorize the payment again. See the following code sample:
Step 12 (optional for shipping-enabled): Set up Dynamic Price Updates
Dynamic Price Updates allows a merchant to dynamically update shipping options and transaction information based on a chosen shipping address. Additionally, you can dynamically update transaction information based on a chosen shipping option.
To set up Dynamic Price Updates, take the following steps:
The callback returns a Promise<PaymentDataRequestUpdate>. The PaymentDataRequestUpdate object has new transaction information, shipping options, and a payment data error. This data updates the payment sheet.
Handle any exception cases, such as an unserviceable shipping address or invalid shipping option, directly in the payment sheet. Set up a PaymentDataError object to highlight the reason for the error with an error message to the user. Be sure to include the related intent in the message. For details on how to set up the object and message, see the following code sample:
function onPaymentDataChanged(intermediatePaymentData){ returnnewPromise(function(resolve, reject){
let shippingAddress = intermediatePaymentData.shippingAddress; let shippingOptionData = intermediatePaymentData.shippingOptionData; let paymentDataRequestUpdate ={};
The callback must return a Promise<PaymentDataRequestUpdate>. The PaymentDataRequestUpdate object has new transaction information, shipping options, offer data, and a payment data error. This data updates the payment sheet.
Handle any exception cases, such as an invalid promo code, directly in the payment sheet. Set up a PaymentDataError object to highlight the reason for the error with an error message to the user. Be sure to include the related intent in the message. For details on how to set up the object and message, see the following code sample, which references an object (validPromoCodes) containing the promo code values:
function onPaymentDataChanged(intermediatePaymentData){ returnnewPromise(function(resolve, reject){
let redemptionCodes =[]; let shippingOptionData = intermediatePaymentData.shippingOptionData; let paymentDataRequestUpdate ={}; paymentDataRequestUpdate.newTransactionInfo = getGoogleTransactionInfo();
// ensure that promo codes set is unique if(typeof intermediatePaymentData.offerData !='undefined'){ // convert to set then back to array redemptionCodes =Array.from( newSet(intermediatePaymentData.offerData.redemptionCodes) ); }
// OPTIONAL: ensure that the newest promo code is the only one applied // redemptionCodes = new Array(redemptionCodes[redemptionCodes.length -1]);
The example code blocks in this section show a complete end-to-end example of the Google Pay API JavaScript library tutorial, Authorize Payments, Dynamic Price Updates, and Promo Codes.