Google Pay
With merchant-hosted Google Pay the customer never leaves for SimplePay's payment page: the Google Pay button is on your page, and the payment goes through SimplePay's dogooglepay endpoint.
Two parts:
- browser:
renderGooglePayButtonfromsimplepay-js-sdk/google-pay - server:
startGooglePayfromsimplepay-js-sdk
Fulfil the order from the IPN here too.
Setup
- Create a merchant profile in the Google Pay & Wallet Console and get your checkout domain approved.
- Send the Google merchant ID to SimplePay.
- Use your SimplePay merchant ID as
gatewayMerchantId. - In the sandbox use Google Pay's
TESTenvironment.
Google Pay does not support the HUF_SZEP currency.
Browser: the button
import { renderGooglePayButton } from 'simplepay-js-sdk/google-pay'
await renderGooglePayButton({
container: '#google-pay', // CSS selector or HTMLElement
merchantId: 'GOOGLE_MERCHANT_ID',
merchantName: 'Shop Ltd.',
environment: 'TEST', // TEST | PRODUCTION
gatewayMerchantId: 'SIMPLEPAY_MERCHANT_ID',
authorizationEndpoint: '/api/google-pay',
currency: 'HUF',
total: '1212', // shown in the Google Pay sheet
orderRef: 'order-12',
countryCode: 'HU', // optional, default HU
allowedCardNetworks: ['VISA', 'MASTERCARD'], // optional, default VISA, MASTERCARD, AMEX
onResult: (result) => { /* successful response from your server */ },
onError: (error) => { /* error */ },
})What it does:
- Loads
https://pay.google.com/gp/p/js/pay.js(unless already loaded). - Asks Google Pay (
isReadyToPay) whether the browser can pay. If not, it renders no button and returns without an error. - On click it opens the Google Pay sheet, then
POSTs{ googlePayToken, orderRef }toauthorizationEndpoint.googlePayTokenis already Base64. onResultreceives your server's response. If it contains error code3004and aredirectUrl, the button sends the customer to the 3DS page (httpsonly).
Without onError, errors are thrown.
Server: the charge
import { startGooglePay } from 'simplepay-js-sdk'
// POST /api/google-pay body: { googlePayToken, orderRef }
const order = await loadOrder(orderRef) // read the amount on the server!
const response = await startGooglePay({
orderRef: order.orderRef,
total: order.total,
currency: 'HUF',
customerEmail: order.email,
language: 'EN',
googlePayToken, // unchanged
invoice: order.invoice, // optional
}, {
redirectUrl: 'https://example.com/payment/back', // the customer returns here after 3DS
})
return response // return it to the browserThe server decides the amount
SimplePay charges the total your server passes to startGooglePay. The button's total is only what the Google Pay sheet shows. They should match, but always compute from the order on the server.
3DS
When the card issuer asks for strong customer authentication, SimplePay returns error code 3004 with a redirectUrl. startGooglePay then does not throw; it returns the response and the button redirects. After 3DS the customer comes back to redirectUrl with r and s, which getPaymentResponse verifies. Without 3DS the customer stays on the page.
Errors
startGooglePay throws when:
- the token is missing:
Missing Google Pay token - the currency is
HUF_SZEP:Google Pay does not support HUF_SZEP - in production the return URL is not
https - the response signature is wrong or missing
- SimplePay returns any error code other than
3004withredirectUrl, or3004withoutredirectUrl:SimplePay API error: …
