With direct integration, the Google Pay button appears in your checkout page, where customers complete payment without being redirected to a payment page.
Customers can use the following supported web browsers:
Add the Google Pay JavaScript library to the bottom of the <body>
of your checkout page:
<script
async
src="https://pay.google.com/gp/p/js/pay.js"
onload="onGooglePayLoaded()">
</script>
Note: The onload
event handler is used to display the Google Pay button. For more information, see Display the Google Pay button.
Create an element in the <body>
of your checkout page where you want to display the Google Pay button:
<div id="button-container" style="width: 160px; height: 40px;"></div>
Note: This element is populated in a later step. For more information, see Display the Google Pay button.
1. Define which version of the Google Pay API you are using.
On the client-side, create an object containing the major and minor versions of the Google Pay API that your integration supports:
const baseRequest = {
apiVersion: 2,
apiVersionMinor: 0
};
2. Configure the Google Pay payment token request.
Note: Google Pay uses payment tokens to encrypt the customer’s payment details for secure processing.
Create a tokenizationSpecification
object:
const tokenizationSpecification = {
type: 'PAYMENT_GATEWAY',
parameters: {
'gateway': 'multisafepay',
'gatewayMerchantId': 'yourMultiSafepayAccountId'
}
};
gateway
, specify multisafepay
.gatewayMerchantId
, specify your MultiSafepay account ID.3. Define supported payment card networks.
Create an allowedCardNetworks
array containing card networks accepted by your website:
const allowedCardNetworks = ["MAESTRO", "MASTERCARD", "VISA"];
Options: MAESTRO
, MASTERCARD
, VISA
.
For more information about supported payment card networks, see Google Pay – Request objects.
4. Define supported authentication methods.
Create an allowedCardAuthMethods
array containing authentication methods accepted by your website:
const allowedCardAuthMethods = ["CRYPTOGRAM_3DS", "PAN_ONLY"];
Options: CRYPTOGRAM_3DS
, PAN_ONLY
.
For more information about authentication methods, see Google Pay – Request objects.
5. Describe your supported payment methods.
Combine the supported payment card networks and authentication methods to describe what your site supports for the CARD
payment method:
const baseCardPaymentMethod = {
type: 'CARD',
parameters: {
allowedAuthMethods: allowedCardAuthMethods,
allowedCardNetworks: allowedCardNetworks
}
};
6. Extend the baseCardPaymentMethod
object to include the tokenizationSpecification
:
const cardPaymentMethod = Object.assign(
{tokenizationSpecification: tokenizationSpecification},
baseCardPaymentMethod
);
Create a Google Pay paymentsClient
object and specify the relevant environment:
const paymentsClient =
new google.payments.api.PaymentsClient({environment: 'TEST'});
When you have finished testing, change the environment to PRODUCTION
.
1. Add your supported payment methods to your baseRequest
object:
const isReadyToPayRequest = Object.assign({}, baseRequest);
isReadyToPayRequest.allowedPaymentMethods = [baseCardPaymentMethod];
2. Create an event handler for when the Google Pay JavaScript library is loaded.
With the isReadyToPay()
method, check if the Google Pay API is supported by the customer’s device and browser:
function onGooglePayLoaded() {
paymentsClient.isReadyToPay(IsReadyToPayRequest)
.then(function(response) {
if (response.result) {
addGooglePayButton();
}
})
.catch(function(err) {
// Show error in developer console for debugging
console.error(err);
});
}
To create a Google Pay button, populate the button-container
element:
function addGooglePayButton() {
const buttonContainer = document.getElementById('button-container');
const button = paymentsClient.createButton({
buttonType: 'plain',
onClick: onGooglePaymentButtonClicked
});
buttonContainer.appendChild(button);
}
For infomation about styling your Google Pay button, see Google Pay:
Create a function that returns a PaymentDataRequest
object:
function getGooglePaymentDataRequest() {
const paymentDataRequest = Object.assign({}, baseRequest);
paymentDataRequest.allowedPaymentMethods = [cardPaymentMethod];
paymentDataRequest.transactionInfo = {
totalPriceStatus: 'FINAL',
totalPrice: '123.45',
currencyCode: 'EUR',
countryCode: 'NL'
};
paymentDataRequest.merchantInfo = {
merchantName: 'Example Merchant'
merchantId: '12345678901234567890'
};
return paymentDataRequest;
}
You will call this function from the Google Pay button event handler in the next step. This way, attributes like the totalPrice
can be updated up until the customer chooses to pay.
For more information about the transactionInfo
object, see Google Pay – TransactionInfo.
In the TEST
environment:
merchantName
= Example Merchant
merchantId
= 12345678901234567890
In the PRODUCTION
environment:
merchantName
= Your merchant namemerchantId
= Your Google Pay merchant IDTo see your Google Pay merchant ID, sign in to your Google Pay Business Console.
For more information about the merchantInfo
object, see Google Pay – Request object.
1. Create an event handler for the Google Pay button:
loadPaymentData()
method.function onGooglePaymentButtonClicked() {
const paymentDataRequest = getGooglePaymentDataRequest();
paymentsClient.loadPaymentData(paymentDataRequest)
.then(function(paymentData) {
processPayment(paymentData);
})
.catch(function(err){
console.error(err);
});
}
2. Pass the paymentData
from the response to your server:
function processPayment(paymentData) {
// send paymentData to your server
}
For more information about the paymentData
object, see Google Pay – Response objects.
From your server, create an order > Wallet order. See also Examples > Google Pay direct.
For the gateway_info.payment_token
, use PaymentData.PaymentMethodData.PaymentMethodTokenizationData.token
.
In response to the API request you made in the previous step, you receive a payment_url
(see API Reference – create order > Wallet order. See also Examples > Google Pay direct).
Pass the payment_url
from your server to the customer’s device and redirect the customer to the URL:
document.location = payment_url
Depending on how the customer’s card is stored in their Google Pay account, the URL references your success page, or a 3D Secure authentication page.
If the customer’s credit card was stored as:
CRYPTOGRAM_3DS
), the payment_url
redirects to your success page.PAN_ONLY
), the payment_url
may redirect to a 3D Secure authentication page.If 3D Secure authentication is required, the customer is automatically redirected to your success page after authentication.
After you’ve implemented the steps above, to test your integration:
Then, when you’re ready to go live:
paymentsClient
object, set the environment to PRODUCTION
.merchantInfo
to your business name and Google Pay merchant ID.
Feedback
Propose a change on GitHub
or
send an email to [email protected]
Other languages
For an explanation in another language, contact your account manager.