Error Handling
Learn how to handle errors returned by the ZSI Pay SDK and build reliable error handling into your Node.js integration.
Handle SDK errors
SDK operations are asynchronous and can fail for several reasons, including invalid configuration, invalid requests, authentication failures, network problems, or errors returned by the ZSI Pay API.
try {
const payment = await zsiPay.pay.init({
customer_email: "customer@example.com",
currency: "USD",
items: [
{
id: "item_123",
name: "Premium Subscription",
quantity: 1,
price: 5000,
},
],
});
console.log(payment);
} catch (error) {
console.error("Payment creation failed:", error);
}Common error categories
Errors can generally be grouped by where the failure occurred.
Configuration errors
The SDK is initialized with missing or invalid configuration.
Validation errors
The payment request contains invalid or incomplete data.
Authentication errors
The supplied API credentials are invalid or cannot authenticate the request.
API errors
The ZSI Pay API rejects a request or returns an application-level error.
Network errors
The request cannot reach the ZSI Pay API or the connection fails.
Timeout errors
A request takes longer than the configured timeout.
Webhook verification errors
An incoming webhook cannot be verified using the configured webhook secret.
Safe error handling
Avoid returning raw SDK or API errors directly to customers. Log useful diagnostic information on the server and return a safe, application-specific response to the client.
try {
const payment = await zsiPay.pay.init({
customer_email: "customer@example.com",
currency: "USD",
items: [
{
id: "item_123",
name: "Premium Subscription",
quantity: 1,
price: 5000,
},
],
});
return payment;
} catch (error) {
console.error("ZSI Pay error:", error);
throw new Error(
"Unable to create payment. Please try again."
);
}Inspecting an error
When debugging an integration, inspect the error object on the server. Avoid exposing sensitive information in production responses or logs.
try {
await zsiPay.sessions.findByTrackId(
"7107319375257"
);
} catch (error) {
if (error instanceof Error) {
console.error("Error:", error.message);
console.error("Stack:", error.stack);
} else {
console.error("Unknown error:", error);
}
}Validation errors
Invalid payment input can cause the request to fail before a payment session is created. Validate your application data before calling the SDK when possible.
const email = "customer@example.com";
if (!email) {
throw new Error("Customer email is required");
}
try {
const payment = await zsiPay.pay.init({
customer_email: email,
currency: "USD",
items: [
{
id: "item_123",
name: "Premium Subscription",
quantity: 1,
price: 5000,
},
],
});
console.log(payment);
} catch (error) {
console.error("Unable to create payment:", error);
}Authentication errors
Authentication failures usually indicate that the API credentials are missing, incorrect, expired, or configured for the wrong environment.
const zsiPay = new ZSIPay({
apiKey: process.env.ZSI_PAY_API_KEY!,
apiSecret: process.env.ZSI_PAY_API_SECRET!,
});
try {
const payment = await zsiPay.pay.init({
customer_email: "customer@example.com",
currency: "USD",
items: [
{
id: "item_123",
name: "Premium Subscription",
quantity: 1,
price: 5000,
},
],
});
console.log(payment);
} catch (error) {
console.error(
"Check your ZSI Pay API credentials:",
error
);
}Never log the API secret itself when diagnosing authentication failures.
Network and timeout errors
Network failures and request timeouts are different from payment failures. A network error does not necessarily mean that the payment itself failed.
try {
const payment = await zsiPay.pay.init({
customer_email: "customer@example.com",
currency: "USD",
items: [
{
id: "item_123",
name: "Premium Subscription",
quantity: 1,
price: 5000,
},
],
});
console.log(payment);
} catch (error) {
console.error("Request failed:", error);
// Do not automatically assume that the payment failed.
// Check the payment session when appropriate.
}Important
If a network failure happens after a request reaches the payment system, blindly retrying may create an unintended duplicate payment unless your integration provides an idempotency mechanism.
Webhook verification errors
Webhook verification failures should be treated as security failures. Do not process the event if signature verification fails.
try {
const event = zsiPay.webhooks.constructEvent({
rawBody,
headers: {
"zsi-pay-webhook-signature": signature,
"zsi-pay-webhook-timestamp": timestamp,
},
});
// Process only after successful verification
console.log(event);
} catch (error) {
console.error(
"Webhook verification failed:",
error
);
return res.status(400).json({
message: "Invalid webhook",
});
}See the Webhooks guide for the complete verification flow.
Retries
If retry configuration is enabled in your SDK client, retry behavior should be limited to failures that are safe to retry.
const zsiPay = new ZSIPay({
apiKey: process.env.ZSI_PAY_API_KEY!,
apiSecret: process.env.ZSI_PAY_API_SECRET!,
timeout: 10000,
maxRetries: 3,
});Configure retries according to your application's requirements. For payment-creation requests, make sure your integration can safely handle a retry before enabling aggressive retry behavior.
Logging and monitoring
Server-side logging can help you diagnose failed requests and investigate payment issues.
try {
const payment = await zsiPay.pay.init({
customer_email: "customer@example.com",
currency: "USD",
items: [
{
id: "item_123",
name: "Premium Subscription",
quantity: 1,
price: 5000,
},
],
});
console.log("Payment created:", payment.trackId);
} catch (error) {
console.error("ZSI Pay request failed:", error);
}Avoid logging API secrets, webhook secrets, authentication headers, or other sensitive customer information.
Recommended production pattern
const createPayment = async () => {
try {
const payment = await zsiPay.pay.init({
customer_email: "customer@example.com",
currency: "USD",
items: [
{
id: "item_123",
name: "Premium Subscription",
quantity: 1,
price: 5000,
},
],
});
return {
success: true,
payment,
};
} catch (error) {
console.error("ZSI Pay payment error:", error);
return {
success: false,
message: "Unable to create payment",
};
}
};Keep payment processing on the server and expose only the minimum information your frontend needs, such as the checkout URL.
Error handling checklist
- • Wrap asynchronous SDK calls in try/catch.
- • Validate application input before creating payments.
- • Never expose API or webhook secrets to customers.
- • Do not assume a network error means a payment failed.
- • Verify webhook signatures before processing events.
- • Make webhook processing idempotent.
- • Avoid blindly retrying payment-creation requests.
- • Log useful diagnostic information on the server.
- • Return safe, application-specific error messages.
