@basis-theory/apple-pay-js

中文文档
2.0.4发布于 2026/07/08Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 2.1万

Basis Theory utility for decrypting Apple Pay Tokens

原始使用文档
文档目录6 节
  1. Features
  2. Apple Pay Setup
  3. Installation
  4. Node.js
  5. Reactors
  6. 💡 Inspiration

Basis Theory Apple Pay JS

Version GitHub Workflow Status (with event) License

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

Features

  • Apple Pay PKPaymentToken Decryption: Securely decrypt user-authorized Apple Pay transaction tokens using easy-to-interact interfaces.

    EncryptionRegionSupport
    RSA_v1China✅
    EC_v1All Others✅
  • Payment Processing Certificate Rotation: Never worry about missing payments because of certificate rotation unpredictable behavior. Just add both certificates to the decryption context and rest assured that both new and old tokens will be decrypted during rotation window.

  • Automatic Decryption Strategy Detection: Transparent integration for decrypting Apple's Payment Token regardless of the employed encryption standard.

Apple Pay Setup

A pre-requisite to use this package is that you must have completed your Merchant Apple Pay Setup. This can be a time-consuming process, so the guides below will help you with step-by-step instructions to obtain the necessary files to issue and decrypt Apple Pay Tokens:

  1. Apple Developer Program Enrollment
  2. Create a Merchant ID
  3. Create a Payment Processing Certificate
  4. Create a Merchant Identity Certificate

To collect payments with Apple Pay in your frontend, Apple has specific guides for:

Installation

Install the package using NPM:

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

Or Yarn:

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

Node.js

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

If you need help understanding the risks associated with decrypting and manipulating the various forms of cardholder data in your own systems, reach out to us.

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

// create the decryption context
const context = new ApplePaymentTokenContext({
  // add as many merchant certificates you need
  merchants: [
    {
      // optional certificate identifier
      identifier: 'merchant.basistheory.com-old',
      // the certificate and the private key are Buffers in PEM format
      certificatePem: fs.readFileSync(
        './test/fixtures/ec/apple/apple_pay.new.pem'
      ),
      privateKeyPem: fs.readFileSync(
        './test/fixtures/ec/apple/private.new.key'
      ),
    },
    {
      identifier: 'merchant.basistheory.com-new',
      certificatePem: fs.readFileSync(
        './test/fixtures/ec/apple/apple_pay.new.pem'
      ),
      privateKeyPem: fs.readFileSync(
        './test/fixtures/ec/apple/private.new.key'
      ),
    },
    {
      identifier: 'merchant.basistheory.china',
      certificatePem: fs.readFileSync(
        './test/fixtures/rsa/apple/apple_pay.pem'
      ),
      privateKeyPem: fs.readFileSync('./test/fixtures/rsa/apple/private.key'),
    },
  ],
});

try {
  // decrypts Apple's PKPaymentToken paymentData
  console.log(context.decrypt(token.paymentData));
} catch (error) {
  // couldn't decrypt the token with given merchant certificates
}

Reactors

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

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

module.exports = async function (req) {
  const {
    bt,
    args: {
      applePayToken: { paymentData, ...applePayToken },
    },
    configuration: {
      PRIMARY_CERTIFICATE_PEM,
      PRIMARY_PRIVATE_KEY_PEM,
      SECONDARY_CERTIFICATE_PEM,
      SECONDARY_PRIVATE_KEY_PEM,
    },
  } = req;

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

  try {
    // decrypts Apple's PKPaymentToken paymentData
    const {
      applicationPrimaryAccountNumber,
      applicationExpirationDate,
      ...restPaymentData
    } = context.decrypt(paymentData);

    // vaults Apple Device PAN (DPAN)
    const btToken = await bt.tokens.create({
      type: 'card',
      data: {
        number: applicationPrimaryAccountNumber,
        expiration_month: applicationExpirationDate.slice(2, 4),
        expiration_year: `20${applicationExpirationDate.slice(-2)}`,
      },
    });

    // returns transaction details and vaulted token without sensitive DPAN
    return {
      raw: {
        btToken,
        applePayToken: {
          paymentData: restPaymentData,
          ...applePayToken,
        },
      },
    };
  } catch (error) {
    throw new CustomHttpResponseError({
      status: 500,
      body: {
        message: error.message,
      },
    });
  }
};

💡 Inspiration

This package was inspired by Spreedly Gala, particularly this fork.

版本与发布记录

@basis-theory/apple-pay-js 共发布 8 个版本,首次发布于 2023/09/12;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.4latest2026/07/084 个月前
2.0.32025/09/191.1 年前
2.0.22024/01/292 年前
2.0.12024/01/262 年前
2.0.02024/01/182 年前
1.1.02023/09/143 年前

关于 @basis-theory/apple-pay-js 的常见问题

@basis-theory/apple-pay-js 是什么?

Basis Theory utility for decrypting Apple Pay Tokens

如何安装 @basis-theory/apple-pay-js?

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

@basis-theory/apple-pay-js 支持 TypeScript 吗?

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

@basis-theory/apple-pay-js 支持 ESM 还是 CommonJS?

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

@basis-theory/apple-pay-js 需要什么版本的 Node.js?

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

@basis-theory/apple-pay-js 可以商用吗?

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

@basis-theory/apple-pay-js 的下载量有多少?

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

@basis-theory/apple-pay-js 还在维护吗?

最新版本 2.0.4 发布于 2026/07/08(4 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 1 个;首次发布于 2023/09/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。