Environments
The PaymentKit SDK operates in two distinct modes: live and test. This separation is crucial for developing and testing your integration without affecting real financial data. All API requests are processed in one of these environments, ensuring that your test data remains isolated from your production transactions.
By default, the SDK is initialized in live mode. It is essential to explicitly set the mode to test during development to prevent accidental real charges.
Switching Modes#
You can control the active environment using the payment.environments
object. This setting is global and will apply to all subsequent API calls made through the SDK instance.
Using Test Mode
To enable test mode, call setTestMode(true)
. This routes all API requests to the sandbox environment, where you can simulate transactions without any real financial impact. This is the recommended approach for all development and testing phases.
import payment from '@blocklet/payment-js';
// Switch to the test environment
payment.environments.setTestMode(true);
console.log('Is test mode active?', payment.environments.getTestMode()); // true
console.log('Is live mode active?', payment.environments.getLivemode()); // false
// Now, any API call will be a test call.
// const testSubscriptions = await payment.subscriptions.list();
Using Live Mode
For production use, you can switch to live mode by calling setLivemode(true)
. All subsequent requests will be sent to the production environment and will involve real transactions.
import payment from '@blocklet/payment-js';
// Switch to the live environment for production
payment.environments.setLivemode(true);
console.log('Is test mode active?', payment.environments.getTestMode()); // false
console.log('Is live mode active?', payment.environments.getLivemode()); // true
// Subsequent API calls will affect real data.
// const liveSubscriptions = await payment.subscriptions.list();
Checking the Current Mode#
You can programmatically check the current mode at any point in your application's lifecycle to add conditional logic for different environments.
Method | Return Type | Description |
---|---|---|
|
| Returns |
|
| Returns |
This example shows how to check the default mode, switch it, and then use a conditional check.
import payment from '@blocklet/payment-js';
// The SDK is in live mode by default
console.log(`Initial mode: ${payment.environments.getLivemode() ? 'Live' : 'Test'}`);
// Switch to test mode for development
payment.environments.setTestMode(true);
console.log(`Current mode: ${payment.environments.getLivemode() ? 'Live' : 'Test'}`);
if (payment.environments.getTestMode()) {
console.log('Running in a safe test environment.');
// Add test-specific logic here
} else {
console.warn('WARNING: Running in live production mode.');
// Add production-specific logic here
}
With environment management configured, you are ready to explore the main functionalities. Proceed to Core Concepts to learn about implementing payment workflows.