# @basis-theory/google-pay-js 中文文档

规范地址：https://npmcn.com/package/%40basis-theory/google-pay-js/

版本：1\.0\.0
npm 元数据更新时间：2026-11-02T18:33:32.438Z
中文译文更新时间：2026-11-07T18:03:50.560Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40basis-theory/google-pay-js
原文对照：https://npmcn.com/package/%40basis-theory/google-pay-js/?lang=original
项目主页：https://github.com/Basis-Theory/google-pay-js#readme
源码仓库：https://github.com/Basis-Theory/google-pay-js

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：23.9 kB，6 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=16
- 运行依赖：1 个
- 首次发布：2024-02-21T13:10:56.453Z
- 最新版本发布：2024-02-21T13:10:56.708Z
- 近 30 天下载量：6448（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

依据 解密 Google Pay Tokens 的理论工具

## 安装

```sh
npm install @basis-theory/google-pay-js
```

也可以使用：`pnpm add @basis-theory/google-pay-js`、`yarn add @basis-theory/google-pay-js`、`bun add @basis-theory/google-pay-js`

## README

# 基础理论 Google Pay JS

![Version](https://img.shields.io/npm/v/%40basis-theory/google-pay-js) ![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/Basis-Theory/google-pay-js/release.yml) ![License](https://img.shields.io/npm/l/%40basis-theory%2Fgoogle-pay-js)

用于在Node.js环境中解密Google Pay Tokens的工具库.

## 特征

-   **谷歌付费 [支付方法](https://developers.google.com/pay/api/web/guides/resources/payment-data-cryptography#payment-method-token-structure) 解密**:使用易用接口安全地解密用户授权的Google Pay交易符.
-   密钥旋转:从不担心由于密钥旋转不可预测的行为导致的付款缺失. 只要在解密上下文中添加多键,并放心在旋转窗口时会同时解密出新和旧的令牌.

> 此软件包只支持 Google Pay `ECv2` 协议.

## 谷歌付费设置

[遵循 Google Pay API 指南](https://developers.google.com/pay/api) 用于您选择的平台(Web或Android)。 要使用此库, 您需要经过 PCI 一级认证并选择了符号化类型 `DIRECT`。 。 。 .

> 如果你没有PCI一级认证, [联系我们](https://basistheory.com/contact) 学习如何使用 `PAYMENT_GATEWAY` 符号化类型 .

## 安装

使用国家防范机制安装软件包:

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

或者亚恩:

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

## 节点.js

下面的例子显示如何使用来自此寄存器的样本从文件系统将私钥加载到缓冲器中. 但你可以从你的KMS,秘密管理器,配置等设备中装入.

```javascript
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
}
```

## 反应堆

此软件包可用于 [反应堆](https://developers.basistheory.com/docs/concepts/what-are-reactors) 上下文。 以下例子说明如何解密Google Pay的代币并按规定为PAN提供保险.

```javascript
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,
      },
    });
  }
};
```

## 常见问题

### @basis-theory/google-pay-js 是什么？

依据 解密 Google Pay Tokens 的理论工具

### 如何安装 @basis-theory/google-pay-js？

在项目目录运行 npm install @basis-theory/google-pay-js，或使用 pnpm add / yarn add / bun add。

### @basis-theory/google-pay-js 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @basis-theory/google-pay-js 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @basis-theory/google-pay-js 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=16。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @basis-theory/google-pay-js 可以商用吗？

@basis-theory/google-pay-js 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @basis-theory/google-pay-js 的下载量有多少？

根据 npm 官方下载统计，@basis-theory/google-pay-js 在 2026/10/08 – 2026/11/06 这 30 天内被下载 6,448 次，日均约 215 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @basis-theory/google-pay-js 还在维护吗？

最新版本 1\.0\.0 发布于 2024/02/21（2 年前）；至今共发布 1 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/02/21。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

