Environments
The PaymentKit SDK operates in two distinct environments: Live Mode and Test Mode. This separation is essential for developing and testing your integration without affecting your production data or processing real payments. This guide explains how to configure and switch between these environments.
- Live Mode: For production use, processing real transactions with actual customer data. The SDK defaults to this mode.
- Test Mode: For development and testing purposes, using test data. No real money is moved.
How It Works#
The SDK manages the active environment through a global setting. By default, it is initialized in Live Mode. Every API request automatically includes the current mode, ensuring the PaymentKit backend processes it in the correct context. You can programmatically switch this setting at any point in your application's lifecycle.
Programmatic Configuration#
You can switch between environments at any point in your application's runtime using the environments object.
Setting the Mode#
Use the setTestMode() and setLiveMode() methods to explicitly set the active environment.
import payment from '@blocklet/payment-js';
async function manageEnvironments() {
// Switch to the test environment for development
payment.environments.setTestMode(true);
// All subsequent API calls will now be made in test mode.
// For example, creating a checkout session will use the test backend.
// const testSession = await payment.checkout.sessions.create(...);
console.log('Switched to Test Mode.');
// To switch back to the live environment for production use
payment.environments.setLiveMode(true);
console.log('Switched back to Live Mode.');
}
manageEnvironments();It is best practice to set the mode explicitly at the beginning of your application's execution to avoid unintended operations on your production data.
Checking the Current Mode#
To verify which mode is currently active, you can use the getTestMode() and getLivemode() getter methods.
import payment from '@blocklet/payment-js';
// Set the mode to test
payment.environments.setTestMode(true);
// Verify the current state
const isTest = payment.environments.getTestMode();
console.log(`Is test mode active? ${isTest}`);
// Expected output: Is test mode active? true
const isLive = payment.environments.getLivemode();
console.log(`Is live mode active? ${isLive}`);
// Expected output: Is live mode active? falseUnderstanding how to manage environments is key to a smooth development workflow. Now that you can switch between test and live modes, learn how to handle potential issues in the Error Handling guide.