Accept Google Pay
Add the Google Pay button to your web checkout with the Preczn client, test it, and process a Google Pay transaction.
Add a Google Pay button to your checkout page and charge the customer's Google Pay card. You add one <div> to your page, and Preczn renders the Google Pay button inside a Preczn-hosted frame in that <div>. Google's payment sheet opens from the frame. When the customer authorizes the payment, Preczn decrypts the Google Pay data and sends a single-use token to your callback. Your server charges the token with Process Transaction.
You don't register domains, and neither your platform nor your merchants need a Google Pay merchant ID. The token works for one transaction and only for the merchant that loaded the button. See Wallets.
How it works
sequenceDiagram
actor Customer
participant Page as Your checkout page
participant Frame as Preczn-hosted frame
participant Google as Google Pay
participant Preczn as Preczn API
participant Server as Your server
Page->>Frame: Mount the frame in your div (amount, currency)
Frame->>Google: Show the Google Pay button
Customer->>Google: Select Google Pay and authorize the payment
Google-->>Frame: Encrypted payment data
Frame->>Preczn: Send the encrypted payment data
Preczn-->>Frame: Single-use token
Frame-->>Page: Token, in your callback
Page->>Server: Send the token and billing details
Server->>Preczn: POST /v1/transactions
Preczn-->>Server: Transaction result
Google Pay requirements
Preczn handles most of the Google Pay Integration Checklist for you. Your remaining responsibilities are mainly about branding and presentation.
Your responsibilities
- Adhere to the Google Pay APIs Acceptable Use Policy and the Google Pay API Terms of Service.
- Use only approved Google Pay assets and follow the Google Pay Brand guidelines.
- Display Google Pay on parity with your other third-party payment methods.
- Show the user a final price before the transaction is processed. If the amount can vary based on data returned from the Google Pay API, present a confirmation page with the final price.
Handled by Preczn
- Keeping your existing risk checks and controls in place across all payment method types.
- Confirming the card networks and authentication methods your payment processor accepts in your country.
- Rotating encryption keys annually and providing Preczn's PCI Attestation of Compliance to Google.
You can skip testing the following Google Pay features, which Preczn doesn't support:
- Google Pay for Android (in-app payments)
- Dynamic price updates
- Requesting the cardholder's telephone number
- Requesting a shipping address separate from billing, or presenting shipping options in Google Pay
Prerequisites
- A public API key (
pub_) for your platform. Never put a private key in browser code. See API keys. - A merchant with Google Pay turned on and a connection that processes Google Pay. See Wallets and the Google Pay column in Supported connections.
- A checkout page served over HTTPS.
Add Google Pay to your page
To add the Google Pay button, complete the following steps:
-
Add one
<div>with thedata-preczn="googlepay"attribute where you want the button:<div data-preczn="googlepay"></div>The Preczn client mounts the frame in the first element that matches
[data-preczn="googlepay"]. The button, the Google Pay SDK, and the payment sheet run inside the frame, apart from your page.Your website must comply with the Google Pay acceptable use policy. For more information, see Google's Acceptable Use Policy for Google Pay API.
-
Add the Preczn client to the
<head>of your page, withresources=googlepay:<script fetchpriority="high" src="https://api.preczn.com/v1/clients/preczn.min.js?resources=googlepay&merchantId={merchantId}&publicApiKey={publicApiKey}"></script>For every loader option, such as loading several features with one script tag, see Browser client reference.
Query parameter Description merchantIdThe Preczn merchant ID that the button charges publicApiKeyYour platform's public API key resourcesA comma-separated list of client resources. Include googlepay -
Define the callback that receives the result or the errors:
function googlePayCallback(result, errors) { if (errors) { console.error(`Google Pay errors:\n - ${errors.join("\n - ")}`); return; } // Send result.token and result.billingContact to your server. }On success,
errorsisnullandresultholds the token. See the Callback result section. On failure,resultisnullanderrorsis an array of strings. See the Callback errors section. -
Call
initGooglePayButtonwith the amount and your callback:Preczn.GooglePay.initGooglePayButton(1250, googlePayCallback, "US", "USD");Parameter Type Required Description Amount numberYes An integer in the smallest currency unit, such as 1250for $12.50Callback functionYes Receives the result or the errors Country code stringNo ISO 3166-1 alpha-2. Defaults to USCurrency code stringNo ISO 4217. Defaults to USDOptions objectNo Button styling. See the Customize the button's appearance section If you call
initGooglePayButtonbefore the client finishes loading, the client runs the call once it's ready. To run your own code when the client is ready, add an event listener for thePreczn.GooglePay.readyevent ondocument, before the client's<script>tag. You can also checkPreczn?.GooglePay?.ready, which istrueonce the client is ready. -
Send the token to your server and charge it. See the Charge the Google Pay token section.
Customize the button's appearance
By default, the button shows Google's standard "Buy with G Pay" label. To change its appearance, pass an options object as the fifth argument to initGooglePayButton:
Preczn.GooglePay.initGooglePayButton(1250, googlePayCallback, "US", "USD", {
buttonType: "pay",
buttonColor: "black",
buttonSizeMode: "fill",
buttonLocale: "en",
buttonRadius: 4,
buttonBorderType: "no_border"
});Pass any subset of these keys. Any key you leave out uses Google's default.
| Option | Accepted values | Google's default |
|---|---|---|
buttonType | book, buy, checkout, donate, order, pay, plain, subscribe | buy ("Buy with G Pay") |
buttonColor | default, black, white | default |
buttonSizeMode | static, fill | static |
buttonLocale | An ISO 639-1 language code, such as en, es, or fr | The browser's language |
buttonRadius | The corner radius in pixels. 0 gives square corners | Google's radius |
buttonBorderType | no_border, default_border | default_border |
Preczn passes these values to Google's <google-pay-button> component without checking them. The component handles a value it doesn't recognize. These six keys are the only appearance settings, because the button runs inside the frame, where your page's CSS doesn't reach.
Size the button with fill
With buttonSizeMode: "static", the button renders at Google's standard size, and your container needs no size. With buttonSizeMode: "fill", the button fills your [data-preczn="googlepay"] container in both directions, so you size it with CSS. Give the container an explicit width and height. A container with no height collapses, and the button doesn't render.
<div data-preczn="googlepay" style="width: 320px; height: 48px;"></div>Callback result
| Property | Description |
|---|---|
token | The single-use token for this Google Pay payment |
walletType | Always GOOGLEPAY |
type | Always CREDIT |
brand | VISA, MASTERCARD, AMEX, DISCOVER, or OTHER |
bin | The card's bank identification number |
last4 | The card number's last four digits |
expiration | The card's expiration date, as MMYY |
merchantId | The merchant that loaded the button |
billingContact | The customer's name, billing address, and email, when Google Pay provides them |
The frame asks Google Pay for the customer's email and full billing address, with format set to FULL in BillingAddressParameters. billingContact.country is an ISO 3166-1 alpha-3 code.
{
"walletType": "GOOGLEPAY",
"type": "CREDIT",
"brand": "VISA",
"bin": "432100",
"last4": "0012",
"expiration": "1228",
"merchantId": "mid_test_{merchantId}",
"token": "tkn_test_{tokenId}",
"billingContact": {
"firstName": "{firstName}",
"lastName": "{lastName}",
"address": "123 Main Street",
"address2": "Suite 100",
"city": "Springfield",
"region": "IL",
"postal": "62701",
"country": "USA",
"email": "[email protected]"
}
}Callback errors
The client sends these errors to your callback:
| Error | Cause and fix |
|---|---|
Invalid API Key | publicApiKey isn't an active public key. Use your platform's public key |
Invalid Merchant ID | merchantId isn't a merchant on your platform. Check the ID |
Google Pay not enabled for merchant | Google Pay is off for the merchant. See Wallets |
Google Pay can only be initialized on a secure (HTTPS) connection | Serve the page over HTTPS |
No Google Pay container element found on page | Add a <div data-preczn="googlepay"> element |
Unauthorized Request | Preczn rejected the key or merchant while creating the token. Check both |
Failed to decrypt and tokenize Google Pay payment data | Preczn couldn't create the token. Try again |
If an error persists, contact Preczn support.
Use a Content Security Policy
If your page uses a Content Security Policy, add these directives so the Preczn client can load and connect to the Preczn API. The frame comes from api.preczn.com, and the Google Pay SDK and button load inside it.
| Directive | Value |
|---|---|
script-src | api.preczn.com pay.google.com |
connect-src | api.preczn.com pay.google.com www.google.com google.com |
frame-src | api.preczn.com pay.google.com |
img-src | www.gstatic.com |
Charge the Google Pay token
To charge the token, complete the following steps on your server:
-
Send
POST /v1/transactionswithtypeset tosale, themerchantIdthat loaded the button, the token inpayment.token, and theamount. Copy the customer's details frombillingContactintofirstName,lastName,email, andbillingAddress.Send the same
currencythat you passed toinitGooglePayButton, or leave it out if you usedUSD. Preczn rejects the transaction when the currency doesn't match the token.{ "merchantId": "mid_test_{merchantId}", "type": "sale", "amount": 1250, "currency": "USD", "payment": { "token": "tkn_test_{tokenId}" }, "firstName": "{firstName}", "lastName": "{lastName}", "email": "[email protected]", "billingAddress": { "address": "123 Main Street", "address2": "Suite 100", "city": "Springfield", "region": "IL", "postal": "62701", "country": "USA" } }The token works once, so send an idempotency key to retry safely. See Idempotency keys.
-
Check
authorization.statusin the response.Ameans approved.payment.walletTypeisGOOGLEPAY. For the other statuses, see Charge a payment.{ "id": "txn_test_{transactionId}", "type": "sale", "amount": 1250, "currency": "USD", "merchantId": "mid_test_{merchantId}", "authorization": { "status": "A", "approvedAmount": 1250, "processorCode": "{processorCode}", "processorMessage": "{processorMessage}" }, "payment": { "bin": "432100", "brand": "VISA", "last4": "0012", "type": "CREDIT", "walletType": "GOOGLEPAY" } }This response is abbreviated. For every field, see Process Transaction.
Test Google Pay
In test mode, Preczn runs Google Pay in Google's test environment. For test card data, see Google's Google Pay mock test cards page.
Frequently asked questions
Do I need to obtain a Google Pay merchant ID?
No. Neither your platform nor your merchants need to register with Google or get a Google Pay merchant ID. Preczn manages the Google Pay merchant configuration for you. You need only the Preczn merchant ID and your public API key to load the button.
Next
- To accept Apple Pay too, see Accept Apple Pay.
- To handle a transaction that returns
P(pending), see Handle synchronous and asynchronous results.
Updated 4 days ago
