UNPKG

nodejs-cryptomus

Version:

A comprehensive Node.js client for the Cryptomus API

206 lines (179 loc) 4.94 kB
import { CryptomusClient } from '../client'; import { CreatePaymentRequest, PaymentInfo, PaymentStatusRequest, PaymentHistoryRequest, CreateStaticWalletRequest, GenerateQrCodeRequest, RefundRequest, BlockWalletRequest, TestWebhookRequest, ResendWebhookRequest } from '../types'; import { filterUndefined, ensureString } from '../utils/helpers'; /** * Payment service for managing invoices and payments */ export class PaymentService { private readonly client: CryptomusClient; /** * Create a new payment service * * @param client - Cryptomus API client */ constructor(client: CryptomusClient) { this.client = client; } /** * Create a new payment invoice * * @param params - Payment creation parameters * @returns Created payment information */ async createInvoice(params: CreatePaymentRequest): Promise<PaymentInfo> { const payload = { ...params, amount: ensureString(params.amount) }; const response = await this.client.requestPayment<PaymentInfo>( '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: PaymentStatusRequest): Promise<PaymentInfo> { if (!params.uuid && !params.order_id) { throw new Error('Either uuid or order_id must be provided'); } const response = await this.client.requestPayment<PaymentInfo>( 'POST', '/payment/info', filterUndefined(params) ); return response.result; } /** * Get payment history * * @param params - History request parameters * @returns List of payments */ async getPaymentHistory(params: PaymentHistoryRequest = {}): Promise<PaymentInfo[]> { const response = await this.client.requestPayment<PaymentInfo[]>( '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: CreateStaticWalletRequest): Promise<PaymentInfo> { const response = await this.client.requestPayment<PaymentInfo>( 'POST', '/payment/static', params ); return response.result; } /** * Block a static wallet * * @param params - Block wallet parameters * @returns Success message */ async blockWallet(params: BlockWalletRequest): Promise<{ message: string }> { const response = await this.client.requestPayment<{ message: string }>( '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: GenerateQrCodeRequest): Promise<{ qr_code: string }> { const response = await this.client.requestPayment<{ qr_code: string }>( 'POST', '/payment/qr', params ); return response.result; } /** * Refund a payment * * @param params - Refund parameters * @returns Refund information */ async refund(params: RefundRequest): Promise<{ message: string }> { const response = await this.client.requestPayment<{ message: string }>( 'POST', '/payment/refund', params ); return response.result; } /** * Test a payment webhook * * @param params - Test webhook parameters * @returns Success message */ async testWebhook(params: TestWebhookRequest): Promise<{ message: string }> { if (!params.uuid && !params.order_id) { throw new Error('Either uuid or order_id must be provided'); } const response = await this.client.requestPayment<{ message: string }>( 'POST', '/payment/test-webhook', filterUndefined(params) ); return response.result; } /** * Resend a payment webhook * * @param params - Resend webhook parameters * @returns Success message */ async resendWebhook(params: ResendWebhookRequest): Promise<{ message: string }> { if (!params.uuid && !params.order_id) { throw new Error('Either uuid or order_id must be provided'); } const response = await this.client.requestPayment<{ message: string }>( 'POST', '/payment/resend-webhook', filterUndefined(params) ); return response.result; } /** * Get list of available payment services * * @returns List of available payment services */ async getServices(): Promise<any[]> { const response = await this.client.requestPayment<any[]>( 'GET', '/payment/services' ); return response.result; } }