nakapay-sdk
Version:
Node.js SDK for the NakaPay API to integrate Bitcoin Lightning payments.
142 lines • 4.44 kB
JavaScript
;
/**
* NakaPay Node.js SDK
* A lightweight wrapper around the NakaPay API for Node.js applications
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NakaPay = void 0;
const axios_1 = __importDefault(require("axios"));
class NakaPay {
/**
* Initialize the NakaPay client
* @param apiKey Your NakaPay API key
* @param options Configuration options
*/
constructor(apiKey, options = {}) {
if (!apiKey || apiKey.trim() === '') {
throw new Error('API key is required');
}
this.apiKey = apiKey;
this.baseUrl = options.baseUrl || 'https://api.nakapay.app';
this.client = axios_1.default.create({
baseURL: this.baseUrl,
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
}
/**
* Create a new payment request
* @param options Payment request options
* @returns Payment request details
*/
async createPaymentRequest(options) {
try {
const response = await this.client.post('/api/v1/payment-requests', options);
return response.data;
}
catch (error) {
this.handleError(error);
}
}
/**
* Get payment request details
* @param id Payment request ID
* @returns Payment request details
*/
async getPaymentRequest(id) {
try {
const response = await this.client.get(`/api/v1/payment-requests/${id}`);
return response.data;
}
catch (error) {
this.handleError(error);
}
}
/**
* Get payment request status
* @param id Payment request ID
* @returns Payment status
*/
async getPaymentStatus(id) {
try {
const response = await this.client.get(`/api/v1/payment-requests/${id}/status`);
return response.data;
}
catch (error) {
this.handleError(error);
}
}
/**
* Get current business profile
* @returns Business profile information
*/
async getBusinessProfile() {
try {
const response = await this.client.get('/api/v1/business/profile');
return response.data;
}
catch (error) {
this.handleError(error);
}
}
/**
* Get payment history for your business
* @param options Pagination options
* @returns List of payment requests
*/
async getPayments(businessId, options = {}) {
try {
const response = await this.client.get(`/api/v1/businesses/${businessId}/payments`, {
params: options,
});
return response.data;
}
catch (error) {
this.handleError(error);
}
}
/**
* Register a webhook for payment notifications
* @param businessId Business ID
* @param options Webhook options
* @returns Success message
*/
async registerWebhook(businessId, options) {
try {
const response = await this.client.post(`/api/v1/businesses/${businessId}/webhooks`, options);
return response.data;
}
catch (error) {
this.handleError(error);
}
}
/**
* Handle API errors
* @param error Error object
*/
handleError(error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const { status, data } = error.response;
throw new Error(`NakaPay API Error (${status}): ${data.message || JSON.stringify(data)}`);
}
else if (error.request) {
// The request was made but no response was received
throw new Error('No response received from NakaPay API');
}
else {
// Something happened in setting up the request that triggered an Error
throw new Error(`Error making request to NakaPay API: ${error.message}`);
}
}
}
exports.NakaPay = NakaPay;
exports.default = NakaPay;
//# sourceMappingURL=index.js.map