const fetch = require("node-fetch");
const API_KEY = process.env.API_KEY;
const credentials = Buffer.from(`${API_KEY}:`).toString("base64");
const response = await fetch("https://api.leanrails.com/v1/payment_intents", {
method: "POST",
headers: {
Authorization: `Basic ${credentials}`,
"Content-Type": "application/json",
"Idempotency-Key": crypto.randomUUID(),
},
body: JSON.stringify({ amount: 2000, currency: "usd" }),
});
const body = await response.json();
if (!response.ok) {
const { error } = body;
switch (error.type) {
case "authentication_error":
// Check your API key configuration
console.error("Auth failed:", error.message);
break;
case "invalid_request_error":
// Fix the request parameters
console.error(`Invalid param '${error.param}': ${error.message}`);
break;
case "rate_limit_error":
// Back off and retry
console.warn("Rate limited. Retrying after delay...");
break;
case "payment_error":
// Show a message to the customer
console.error("Payment failed:", error.message);
break;
case "api_error":
// Retry with the same idempotency key
console.error("Server error. Retrying...");
break;
}
}