Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
核心概念

环境


PaymentKit SDK 在两种不同的环境中运行:Live ModeTest Mode。这种分离对于开发和测试你的集成至关重要,可以避免影响你的生产数据或处理真实支付。本指南将说明如何配置和在这些环境之间切换。

  • Live Mode:用于生产环境,处理包含真实客户数据的实际交易。SDK 默认使用此模式。
  • Test Mode:用于开发和测试,使用测试数据。此模式下不会发生真实的资金转移。

工作原理#

SDK 通过一个全局设置来管理活动环境。默认情况下,它在 Live Mode 下初始化。每个 API 请求都会自动包含当前模式,以确保 PaymentKit 后端在正确的上下文中处理该请求。你可以在应用程序生命周期的任何时刻通过编程方式切换此设置。

"PaymentKit API""PaymentKit SDK""Your Application""PaymentKit API""PaymentKit SDK""Your Application"Internal state set tolivemode = falseInternal state set tolivemode = truepayment.environments.setTestMode(true)payment.subscriptions.list()GET /api/subscriptions?livemode=falseTest Data ResponseReturns list of test subscriptionspayment.environments.setLiveMode(true)payment.subscriptions.list()GET /api/subscriptions?livemode=trueProduction Data ResponseReturns list of live subscriptions

编程方式配置#

你可以在应用程序运行时的任何时刻,使用 environments 对象在不同环境之间切换。

设置模式#

使用 setTestMode()setLiveMode() 方法来明确设置活动环境。

import payment from '@blocklet/payment-js';

async function manageEnvironments() {
// 切换到测试环境进行开发
payment.environments.setTestMode(true);

// 此后的所有 API 调用都将在测试模式下进行。
// 例如,创建结账会话将使用测试后端。
// const testSession = await payment.checkout.sessions.create(...);

console.log('已切换到测试模式。');

// 要切换回生产环境以供生产使用
payment.environments.setLiveMode(true);

console.log('已切换回 Live Mode。');
}

manageEnvironments();

最佳实践是在应用程序开始执行时就明确设置模式,以避免对生产数据进行意外操作。

检查当前模式#

要验证当前激活的是哪种模式,你可以使用 getTestMode()getLivemode() 这两个 getter 方法。

import payment from '@blocklet/payment-js';

// 将模式设置为测试
payment.environments.setTestMode(true);

// 验证当前状态
const isTest = payment.environments.getTestMode();
console.log(`测试模式是否已激活? ${isTest}`);
// 预期输出:测试模式是否已激活? true

const isLive = payment.environments.getLivemode();
console.log(`Live Mode 是否已激活? ${isLive}`);
// 预期输出:Live Mode 是否已激活? false


理解如何管理环境是确保开发工作流程顺畅的关键。既然你已经可以在测试模式和 Live Mode 之间切换,接下来请在错误处理指南中学习如何处理潜在问题。