nodejs-cryptomus
Version:
A comprehensive Node.js client for the Cryptomus API
127 lines (126 loc) • 3.95 kB
JavaScript
import { filterUndefined, ensureString } from '../utils/helpers';
/**
* Payment service for managing invoices and payments
*/
export class PaymentService {
/**
* Create a new payment service
*
* @param client - Cryptomus API client
*/
constructor(client) {
this.client = client;
}
/**
* Create a new payment invoice
*
* @param params - Payment creation parameters
* @returns Created payment information
*/
async createInvoice(params) {
const payload = {
...params,
amount: ensureString(params.amount)
};
const response = await this.client.requestPayment('POST', '/payment', payload);
return response.result;
}
/**
* Get payment information by UUID or order ID
*
* @param params - Parameters containing UUID or order ID
* @returns Payment information
*/
async getPaymentInfo(params) {
if (!params.uuid && !params.order_id) {
throw new Error('Either uuid or order_id must be provided');
}
const response = await this.client.requestPayment('POST', '/payment/info', filterUndefined(params));
return response.result;
}
/**
* Get payment history
*
* @param params - History request parameters
* @returns List of payments
*/
async getPaymentHistory(params = {}) {
const response = await this.client.requestPayment('POST', '/payment/list', filterUndefined(params));
return response.result;
}
/**
* Create a static wallet
*
* @param params - Static wallet creation parameters
* @returns Created wallet information
*/
async createStaticWallet(params) {
const response = await this.client.requestPayment('POST', '/payment/static', params);
return response.result;
}
/**
* Block a static wallet
*
* @param params - Block wallet parameters
* @returns Success message
*/
async blockWallet(params) {
const response = await this.client.requestPayment('POST', '/payment/block-address', params);
return response.result;
}
/**
* Generate QR code for a payment
*
* @param params - QR code generation parameters
* @returns QR code data URL
*/
async generateQrCode(params) {
const response = await this.client.requestPayment('POST', '/payment/qr', params);
return response.result;
}
/**
* Refund a payment
*
* @param params - Refund parameters
* @returns Refund information
*/
async refund(params) {
const response = await this.client.requestPayment('POST', '/payment/refund', params);
return response.result;
}
/**
* Test a payment webhook
*
* @param params - Test webhook parameters
* @returns Success message
*/
async testWebhook(params) {
if (!params.uuid && !params.order_id) {
throw new Error('Either uuid or order_id must be provided');
}
const response = await this.client.requestPayment('POST', '/payment/test-webhook', filterUndefined(params));
return response.result;
}
/**
* Resend a payment webhook
*
* @param params - Resend webhook parameters
* @returns Success message
*/
async resendWebhook(params) {
if (!params.uuid && !params.order_id) {
throw new Error('Either uuid or order_id must be provided');
}
const response = await this.client.requestPayment('POST', '/payment/resend-webhook', filterUndefined(params));
return response.result;
}
/**
* Get list of available payment services
*
* @returns List of available payment services
*/
async getServices() {
const response = await this.client.requestPayment('GET', '/payment/services');
return response.result;
}
}