npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@basis-theory/google-pay-js

v1.0.0

Published

Basis Theory utility for decrypting Google Pay Tokens

Downloads

64

Readme

Basis Theory Google Pay JS

Version GitHub Workflow Status (with event) License

Utility library for decrypting Google Payment Tokens in Node.js environments.

Features

  • Google Pay PaymentMethodToken Decryption: Securely decrypt user-authorized Google Pay transaction tokens using easy-to-use interfaces.
  • Key Rotation: Never worry about missing payments because of key rotation unpredictable behavior. Just add multiple keys to the decryption context and rest assured that both new and old tokens will be decrypted during rotation window.

This package only supports Google Pay ECv2 protocol.

Google Pay Setup

Follow Google Pay API guides for your platform of choosing (Web or Android). In order to use this library, you need to be PCI Level 1 certified and chose tokenization type DIRECT.

⚠️ If you are not PCI Level 1 certified, reach out to us to learn how to use PAYMENT_GATEWAY tokenization type.

Installation

Install the package using NPM:

npm install @basis-theory/google-pay-js --save

Or Yarn:

yarn add @basis-theory/google-pay-js

Node.js

The examples below show how to load private keys from the File System into Buffers, using samples from this repository. But you can load them from your KMS, secret manager, configuration, etc.

import { GooglePaymentTokenContext } from '@basis-theory/google-pay-js';
import fs from 'fs';
import token from './test/fixtures/ec/token.new.json';

// create the decryption context
const context = new GooglePaymentTokenContext({
  // add as many merchant certificates you need
  merchants: [
    {
      // optional certificate identifier
      identifier: 'my-merchant-identifier',
      // the private key is in PEM format
      privateKeyPem: fs.readFileSync('./test/fixtures/key.new.pem'),
    },
    {
      identifier: 'my-merchant-identifier',
      // the private key is in PEM format
      privateKeyPem: fs.readFileSync('./test/fixtures/key.old.pem'),
    },
  ],
});

try {
  // the paymentData you get back from loadPaymentData Promise/event
  const token = JSON.parse(
    paymentData.paymentMethodData.tokenizationData.token
  );
  // decrypts Google's PaymentMethodToken
  console.log(context.decrypt(token));
} catch (error) {
  // couldn't decrypt the token with given merchant keys
}

Reactors

This package is available to use in Reactors context. The example below shows how to decrypt Google Pay tokens and vault the PAN compliantly.

const { Buffer } = require('buffer');
const { GooglePaymentTokenContext } = require('@basis-theory/google-pay-js');
const {
  CustomHttpResponseError,
} = require('@basis-theory/basis-theory-reactor-formulas-sdk-js');

module.exports = async function (req) {
  const {
    bt,
    args: {
      // invoke the reactor passing the paymentData you get back from loadPaymentData Promise/event
      paymentData: {
        paymentMethodData: {
          tokenizationData: { token: tokenString, ...tokenizationData },
          ...paymentMethodData
        },
        ...paymentData
      },
    },
    configuration: { PRIMARY_PRIVATE_KEY_PEM, SECONDARY_PRIVATE_KEY_PEM },
  } = req;

  // creates token context from keys configured in Reactor
  const context = new GooglePaymentTokenContext({
    merchants: [
      {
        privateKeyPem: Buffer.from(PRIMARY_PRIVATE_KEY_PEM),
      },
      {
        privateKeyPem: Buffer.from(SECONDARY_PRIVATE_KEY_PEM),
      },
    ],
  });

  try {
    const token = JSON.parse(tokenString);
    // decrypts Google's PaymentMethodToken
    const {
      paymentMethodDetails: { pan, expirationMonth, expirationYear },
    } = context.decrypt(token);

    // vaults PAN
    const btToken = await bt.tokens.create({
      type: 'card',
      data: {
        number: pan,
        expiration_month: String(expirationMonth).padStart(2, '0'),
        expiration_year: String(expirationYear),
      },
    });

    // returns original details and vaulted token without sensitive PAN
    return {
      raw: {
        btToken,
        paymentData: {
          paymentMethodData: {
            tokenizationData,
            ...paymentMethodData,
          },
          ...paymentData,
        },
      },
    };
  } catch (error) {
    throw new CustomHttpResponseError({
      status: 500,
      body: {
        message: error.message,
      },
    });
  }
};