Accept Google Pay™
This guide will walk you through the process of accepting Google Pay™ payments on your website, while leveraging the Basis Theory platform to perform all necessary operations on PCI-sensitive data. First, let's take a look at how the Google Pay™ payment flow works.
First, you will render the Google Pay™ button on your page using Google's SDKs. When the user clicks the button, Google Pay™ will present the payment sheet to the user, which will contain the payment information (e.g., shipping details, transaction amount, card number, expiration date, etc.).
When the user clicks the Google "Pay" button, a sequence of three high-level steps is initiated:
- Creation of Google Pay™ payment data - The client application (Android or Web) creates a payment data request object that contains the payment information and sends that request to the Google Pay™ API. The Google Pay™ API returns a payment data response object, which contains the encrypted payment information.
- Decryption of the payment data - The encrypted payment data is then sent to Basis Theory for decryption and storage of the sensitive cardholder information. Then Basis Theory returns either a Google Pay™ resource or a Token Intent based on the provided encrypted token that can be used to process the payment.
- Processing of the payment data - The Basis Theory Token Intents and Google Pay™ resources are forwarded to the payment processor via Basis Theory Proxy, which translates Token Intents and Tokens back to raw data before sending the request. When the successful payment response returns, the client application can complete the payment and inform the user that the payment was successful.
Getting Started
To get started, you will need to create a Basis Theory Account and a TEST Tenant.
Google Pay™ Setup
To start, we want to ensure that you're able to connect to Google's APIs. Make sure you've met all the pre-requisites in Google's setup guide.
Rendering the Google Pay™ Button
The integration between your client application and Google can be done directly, without any involvement from Basis Theory.
We'll be building our integration using plain HTML and JS, but there are quickstarts and API documentation provided for React and other frameworks from Google. We'll be following Google's official guides for Web all the way until step 9.
<!DOCTYPE html>
<html>
<head>
<title>Google Pay™ Integration</title>
<script async src="https://pay.google.com/gp/p/js/pay.js" onload="onGooglePayLoaded()"></script>
<script>
let paymentsClient;
let baseRequest = {
apiVersion: 2,
apiVersionMinor: 0,
};
let tokenizationSpecification = {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'basistheory',
gatewayMerchantId: '<TENANT_ID>'
}
}
const allowedCardNetworks = ["AMEX", "DISCOVER", "INTERAC", "JCB", "MASTERCARD", "VISA"];
const allowedCardAuthMethods = ["PAN_ONLY", "CRYPTOGRAM_3DS"];
const baseCardPaymentMethod = {
type: 'CARD',
parameters: {
allowedAuthMethods: allowedCardAuthMethods,
allowedCardNetworks: allowedCardNetworks
}
};
// Initialize Google Pay™ API when the library is loaded
async function onGooglePayLoaded() {
paymentsClient = new google.payments.api.PaymentsClient({environment: 'TEST'});
const isReadyToPayRequest = Object.assign({}, baseRequest, {
allowedPaymentMethods: [baseCardPaymentMethod]
});
try {
// Check if the user is ready to pay
const response = await paymentsClient.isReadyToPay(isReadyToPayRequest);
if (response.result) {
createAndAddButton();
} else {
console.error('Google Pay™ is not available.');
}
} catch (error) {
console.error('Error checking readiness:', error);
}
}
// Create and add the Google Pay™ button
function createAndAddButton() {
const button = paymentsClient.createButton({
onClick: onGooglePaymentButtonClicked
});
document.getElementById('container').appendChild(button);
}
// Handle button click and load payment data
async function onGooglePaymentButtonClicked() {
const paymentDataRequest = Object.assign({}, baseRequest, {
allowedPaymentMethods: [
Object.assign({}, baseCardPaymentMethod, {
tokenizationSpecification: tokenizationSpecification
})
],
transactionInfo: {
totalPriceStatus: 'FINAL',
totalPrice: '10.00',
currencyCode: 'USD'
},
merchantInfo: {
merchantName: 'Example Merchant'
}
});
try {
const paymentData = await paymentsClient.loadPaymentData(paymentDataRequest);
console.log('Payment data:', paymentData);
} catch (error) {
console.error('Error loading payment data:', error);
}
}
</script>
</head>
<body>
<h1>Google Pay™ Integration</h1>
<div id="container"></div>
</body>
</html>
Note that for the tokenizationSpecification, we're setting gateway to basistheory and gatewayMerchantId to your tenantId created from the Storing the Google Pay™ Payment Data with Basis Theory section.
Now when we run this code, we should see a Google Pay™ button on our page. Clicking the "GPay" button will present a payment sheet. Click on the "Continue" button and take a look at the console to see the encrypted payment data.