Google Pay

Take a Google Pay payment

Usage

index.html
HTML

<!-- Provide a target element for the Google Pay button -->

<form>

  <div id="google-pay-button"></div>

</form>

<!-- Configure the Web Payments SDK and Google Pay -->

<script type="text/javascript">

  async function main() {

    const payments = Square.payments(APPLICATION_ID, LOCATION_ID);



    const paymentRequest = payments.paymentRequest({

      countryCode: 'US',

      currencyCode: 'USD',

      total: {

        amount: '1.00',

        label: 'Total',

      },

    });



    const googlePay = await payments.googlePay(paymentRequest);

    await googlePay.attach('#google-pay-button');



    async function eventHandler(event) {

      event.preventDefault();



      try {

        const result = await googlePay.tokenize();

        if (result.status === 'OK') {

          console.log(`Payment token is ${result.token}`);

        }

      } catch (e) {

        console.error(e);

      }

    };



    const googlePayButtonTarget = document.getElementById('google-pay-button');

    googlePayButtonTarget.addEventListener('click', eventHandler);

  }



  main();

</script>

Payments

Returned by Square.payments(appId, locationId).

Use this object to instantiate Payment methods. Learn more on the Payments page.

Methods

JS
JavaScript

const payments = Square.payments(appId, locationId);

GooglePay

The Google Pay Payment Method.

Google Pay supports taking payments via credit cards. Utilizing the PaymentRequest object, you can handle Shipping Option selection, as well as collect Shipping and Billing addresses.

Methods

JS
JavaScript

const paymentRequest = payments.paymentRequest({

  countryCode: 'US',

  currencyCode: 'USD',

  total: {

    amount: '5.79',

    label: 'Total'

  },

});



const googlePay = await payments.googlePay(paymentRequest);

await googlePay.attach('#googlePay');



const googlePayButtonTarget = document.getElementById('googlePay');

googlePayButtonTarget.onclick = async () => {

  const tokenResult = await googlePay.tokenize();



  // Pass `tokenResult.token` to your server, and then call the /v2/payments API

  // to complete the payment

}