Skip to content

iquirino/google-wallet

Repository files navigation

Google Wallet Library

The Google Wallet library provides a convenient way to manage Google Wallet functionality in your TypeScript or JavaScript projects. It offers a simple and intuitive interface for integrating with the Google Wallet REST API.

References/Links

Google Wallet Tutorial

Google Wallet Documentation


Installation

You can install the library via npm:

npm install google-wallet

Usage

Importing the Library

  • Loyalty Card

    import {
      LoyaltyClient,
      LoyaltyClass,
      LoyaltyObject
    } from "google-wallet/lib/esm/loyalty"; //works with esm and ecs
  • Generic Card

    import {
      GenericClient,
      GenericClass,
      GenericObject
    } from "google-wallet/lib/cjs/generic";
  • Gift Card

    import {
      GiftCardClient,
      GiftCardClass,
      GiftCardObject
    } from "google-wallet/lib/esm/gift-card";
  • Offer

    import {
      OfferClient,
      OfferClass,
      OfferObject
    } from "google-wallet/lib/esm/offer";
  • Boarding Passes

    import { ... } from "google-wallet/lib/esm/boarding-pass";
  • Events

    import { ... } from "google-wallet/lib/esm/event";
  • This library includes typings for Boarding Passes and Events, enabling you to work with these Google Wallet features. However, it currently does not provide a built-in client implementation for interacting with the Google REST API endpoints. If you require functionality specific to these features, you are encouraged to implement the client yourself or refer to any available documentation on their usage. Contributions regarding the client implementation or relevant documentation are highly appreciated and can benefit the community.

Configuration

Before using the library, you need to configure your Google application credentials. Make sure you have a valid Google Cloud project and the required credentials in JSON format.

const GOOGLE_APPLICATION_CREDENTIALS = JSON.parse(
  process.env.GOOGLE_APPLICATION_CREDENTIALS
);

Example: Creating Loyalty Cards

To create or manage loyalty cards using the library, you can follow this example:

const loyalty = new LoyaltyClient(GOOGLE_APPLICATION_CREDENTIALS);

// Create a loyalty class
const classPrefix = "fixeloyalty_class";
const classData: LoyaltyClass = {
  // Define the class data
};

let loyaltyClass = await loyalty.getClass(issuerId, classPrefix);
if (!loyaltyClass) {
  loyaltyClass = await loyalty.createClass(classData);
} else {
  loyaltyClass = await loyalty.patchClass(classData);
}

// Create or update a loyalty object
const objectSuffix = "loyalty_object";
const objectData: LoyaltyObject = {
  // Define the object data
};

let loyaltyObject = await loyalty.getObject(issuerId, objectSuffix);
if (!loyaltyObject) {
  loyaltyObject = await loyalty.createObject(objectData);
} else {
  // Update the object data
  loyaltyObject = await loyalty.patchObject(objectData);
}

Generate JWT token and save URL

import jwt from "jsonwebtoken";
const token = jwt.sign({
  iss: GOOGLE_APPLICATION_CREDENTIALS.client_email,
  aud: "google",
  origins: ["www.example.com"],
  typ: "savetowallet",
  payload: {
    // The listed classes and objects will be created
    loyaltyClasses: [loyaltyClass],
    loyaltyObjects: [loyaltyObject],
  },
}, GOOGLE_APPLICATION_CREDENTIALS.private_key, {
  algorithm: "RS256",
});
const saveUrl = `https://pay.google.com/gp/v/save/${token}`;

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License.

Contact

For any inquiries or questions, you can reach out to the project maintainer creating a new issue.