takefy-cryptomus
Version:
TypeScript SDK for the Cryptomus payment system API
197 lines (196 loc) • 7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymentsService = void 0;
const base_1 = require("./base");
/**
* Service class for handling payment-related operations in the Cryptomus API.
* Provides methods for creating payments, managing wallets, and handling discounts.
*/
class PaymentsService extends base_1.BaseService {
/**
* Creates a new payment in the Cryptomus system.
*
* @param {CreatePaymentRequest} data - Payment creation parameters including amount, currency, and order details.
* @returns {Promise<CreatePaymentResponse>} A promise that resolves with the created payment details.
* @throws {Error} If the payment creation fails or returns an error response.
*
* @example
* ```typescript
* const payment = await payments.create({
* amount: "100",
* currency: "USD",
* order_id: "order123"
* });
* ```
*/
async create(params) {
// Validate and convert amount if it's a number
if (typeof params.amount === 'number') {
params.amount = params.amount.toFixed(2);
}
else if (typeof params.amount === 'string') {
// Validate string format
if (!/^\d+(\.\d{2})?$/.test(params.amount)) {
throw new Error('Amount must be in format "XX.XX" with exactly 2 decimal places');
}
}
return this.request("/payment", "POST", params);
}
/**
* Creates a new wallet for receiving payments.
*
* @param {CreateWalletRequest} data - Parameters for creating the wallet.
* @returns {Promise<CreateWalletResponse>} A promise that resolves with the created wallet details.
* @throws {Error} If the request fails or returns an error response.
*
* @example
* ```typescript
* const wallet = await payments.createWallet({
* currency: "USDT",
* network: "TRX",
* order_id: "wallet123"
* });
* ```
*/
async createWallet(params) {
return this.request("/wallet", "POST", params);
}
/**
* Generates a QR code for a specific wallet address.
*
* @param {CreateQrCodeForWalletRequest} data - Parameters for generating the QR code.
* @returns {Promise<CreateQrCodeForWalletResponse>} A promise that resolves with the QR code details.
* @throws {Error} If the request fails or returns an error response.
*
* @example
* ```typescript
* const qr = await payments.createQrCodeForWallet({
* wallet_address_uuid: "wallet-uuid"
* });
* ```
*/
async createQrCodeForWallet(params) {
return this.request("/wallet/qr", "POST", params);
}
async createQrCodeForInvoice(params) {
return this.request("/payment/qr", "POST", params);
}
async blockStaticWallet(params) {
return this.request("/wallet/block-address", "POST", params);
}
async refundPaymentOnBlockedWallet(params) {
return this.request("/wallet/blocked-address-refund", "POST", params);
}
/**
* Retrieves information about a specific payment.
*
* @param {GetPaymentInfoRequest} data - Parameters to identify the payment (uuid or order_id).
* @returns {Promise<GetPaymentInfoResponse>} A promise that resolves with the payment information.
* @throws {Error} If the request fails or returns an error response.
*
* @example
* ```typescript
* const info = await payments.getInfo({
* uuid: "payment-uuid"
* });
* ```
*/
async getInfo(params) {
return this.request("/payment/info", "POST", params);
}
async refund(params) {
return this.request("/payment/refund", "POST", params);
}
async resendWebhook(params) {
return this.request("/payment/resend", "POST", params);
}
async testPaymentWebhook(params) {
return this.request("/test-webhook/payment", "POST", params);
}
async testPayoutWebhook(params) {
return this.request("/test-webhook/payout", "POST", params);
}
async testWalletWebhook(params) {
return this.request("/test-webhook/wallet", "POST", params);
}
/**
* Retrieves a list of available payment services and their details.
*
* @returns {Promise<ListServicesResponse>} A promise that resolves with the list of payment services.
* @throws {Error} If the request fails or returns an error response.
*
* @example
* ```typescript
* const services = await payments.listServices();
* ```
*/
async listServices() {
return this.request("/payment/services", "POST");
}
/**
* Retrieves payment history based on specified date range and filters.
*
* @param {GetPaymentsHistoryRequest} data - Parameters for filtering payment history.
* @returns {Promise<GetPaymentsHistoryResponse>} A promise that resolves with the payment history.
* @throws {Error} If the request fails or returns an error response.
*
* @example
* ```typescript
* const history = await payments.getHistory({
* date_from: "2025-01-01",
* date_to: "2025-02-01"
* });
* ```
*/
async getHistory(params) {
return this.request("/payment/list", "POST", params);
}
async verifyWebhookSignature(params) {
const { ipAddress, request } = params;
if (ipAddress &&
typeof ipAddress === "string" &&
ipAddress !== "91.227.144.54") {
return false;
}
if (request &&
request.sign &&
typeof request.sign === "string" &&
this.generateSign(request, true, true) !== request.sign) {
return false;
}
return true;
}
/**
* Retrieves a list of available discounts for payments.
*
* @returns {Promise<ListDiscountsResponse>} A promise that resolves with the list of discounts.
* @throws {Error} If the request fails or returns an error response.
*
* @example
* ```typescript
* const discounts = await payments.listDiscounts();
* ```
*/
async listDiscounts() {
return this.request("/payment/discount/list", "POST", undefined, true);
}
/**
* Sets or updates a discount for payments.
*
* @param {SetDiscountForPaymentMethodRequest} data - Parameters for setting the discount.
* @returns {Promise<SetDiscountForPaymentMethodResponse>} A promise that resolves with the updated discount details.
* @throws {Error} If the request fails or returns an error response.
*
* @example
* ```typescript
* const discount = await payments.setDiscount({
* currency: "USDT",
* percent: 5
* });
* ```
*/
async setDiscount(params) {
return this.request("/payment/discount/set", "POST", params, true);
}
}
exports.PaymentsService = PaymentsService;