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

Getting Started


This guide provides the essential steps to install, configure, and make your first API call using the PaymentKit Node.js SDK. Following these instructions will help you set up a basic integration quickly.

Installation#

To begin, add the PaymentKit Node.js SDK to your project using npm or your preferred package manager.

npm install @blocklet/payment-js

Configuration#

After installation, you need to import and configure the SDK. The primary setting is the environment, which determines whether API calls are made to the test or live infrastructure. For development, always use the test environment to avoid processing real transactions.

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

// Set environment to test mode for development.
// This uses mock data and does not process real payments.
payment.environments.setTestMode(true);

// For production applications, uncomment the following line to use live mode:
// payment.environments.setLiveMode(true);

Making Your First API Call#

A common starting point is creating a checkout session for a one-time payment. The following example demonstrates how to create a session that will redirect a customer to a secure, hosted payment page.

This code creates a session for a single item. Upon successful payment, the user is sent to the success_url; if they cancel, they are redirected to the cancel_url.

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

// Ensure you have configured the environment as shown above.
payment.environments.setTestMode(true);

async function createCheckoutSession() {
try {
const session = await payment.checkout.sessions.create({
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
mode: 'payment',
line_items: [
{
// IMPORTANT: Replace with a valid Price ID from your PaymentKit dashboard.
price_id: 'price_xxx',
quantity: 1
}
]
});

console.log('Checkout Session Created:', session);
// In a real application, you would redirect your user to session.url.
console.log('Redirect URL:', session.url);

} catch (error) {
console.error('An error occurred:', error.message);
}
}

createCheckoutSession();

To run this example, save it as a JavaScript file (e.g., test-payment.js) and execute it with Node.js. Remember to replace 'price_xxx' with an actual Price ID from your PaymentKit account.

Next Steps#

Now that you have successfully set up the SDK and made an API call, you are ready to explore more advanced features. Move on to the Core Concepts section to learn about implementing different payment models like subscriptions and credit-based billing.