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

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:

  1. Add one <div> with the data-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.

  2. Add the Preczn client to the <head> of your page, with resources=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 parameterDescription
    merchantIdThe Preczn merchant ID that the button charges
    publicApiKeyYour platform's public API key
    resourcesA comma-separated list of client resources. Include googlepay
  3. 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, errors is null and result holds the token. See the Callback result section. On failure, result is null and errors is an array of strings. See the Callback errors section.

  4. Call initGooglePayButton with the amount and your callback:

    Preczn.GooglePay.initGooglePayButton(1250, googlePayCallback, "US", "USD");
    ParameterTypeRequiredDescription
    AmountnumberYesAn integer in the smallest currency unit, such as 1250 for $12.50
    CallbackfunctionYesReceives the result or the errors
    Country codestringNoISO 3166-1 alpha-2. Defaults to US
    Currency codestringNoISO 4217. Defaults to USD
    OptionsobjectNoButton styling. See the Customize the button's appearance section

    If you call initGooglePayButton before 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 the Preczn.GooglePay.ready event on document, before the client's <script> tag. You can also check Preczn?.GooglePay?.ready, which is true once the client is ready.

  5. 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.

OptionAccepted valuesGoogle's default
buttonTypebook, buy, checkout, donate, order, pay, plain, subscribebuy ("Buy with G Pay")
buttonColordefault, black, whitedefault
buttonSizeModestatic, fillstatic
buttonLocaleAn ISO 639-1 language code, such as en, es, or frThe browser's language
buttonRadiusThe corner radius in pixels. 0 gives square cornersGoogle's radius
buttonBorderTypeno_border, default_borderdefault_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

PropertyDescription
tokenThe single-use token for this Google Pay payment
walletTypeAlways GOOGLEPAY
typeAlways CREDIT
brandVISA, MASTERCARD, AMEX, DISCOVER, or OTHER
binThe card's bank identification number
last4The card number's last four digits
expirationThe card's expiration date, as MMYY
merchantIdThe merchant that loaded the button
billingContactThe 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:

ErrorCause and fix
Invalid API KeypublicApiKey isn't an active public key. Use your platform's public key
Invalid Merchant IDmerchantId isn't a merchant on your platform. Check the ID
Google Pay not enabled for merchantGoogle Pay is off for the merchant. See Wallets
Google Pay can only be initialized on a secure (HTTPS) connectionServe the page over HTTPS
No Google Pay container element found on pageAdd a <div data-preczn="googlepay"> element
Unauthorized RequestPreczn rejected the key or merchant while creating the token. Check both
Failed to decrypt and tokenize Google Pay payment dataPreczn 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.

DirectiveValue
script-srcapi.preczn.com pay.google.com
connect-srcapi.preczn.com pay.google.com www.google.com google.com
frame-srcapi.preczn.com pay.google.com
img-srcwww.gstatic.com

Charge the Google Pay token

To charge the token, complete the following steps on your server:

  1. Send POST /v1/transactions with type set to sale, the merchantId that loaded the button, the token in payment.token, and the amount. Copy the customer's details from billingContact into firstName, lastName, email, and billingAddress.

    Send the same currency that you passed to initGooglePayButton, or leave it out if you used USD. 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.

  2. Check authorization.status in the response. A means approved. payment.walletType is GOOGLEPAY. 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


Did this page help you?
Sponsor
SponsoredKunjungi sekarang
Promo