UNPKG

node-nowpayments-api

Version:

Node NowPayments API client

250 lines (246 loc) 7.63 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/index.ts var src_exports = {}; __export(src_exports, { NowPaymentsClient: () => NowPaymentsClient, verifyWebhook: () => verifyWebhook }); module.exports = __toCommonJS(src_exports); // src/client.ts var import_axios = __toESM(require("axios")); var NowPaymentsClient = class { constructor(apiKey, requestOptions = {}) { this.globalRequestOptions = __spreadValues({ baseURL: "https://api.nowpayments.io", headers: { accept: "application/json", "x-api-key": apiKey, "content-type": "application/json" } }, requestOptions); this.axiosInstance = import_axios.default.create(this.globalRequestOptions); } getApiStatus() { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.get("/v1/status"); return { result: data }; } catch (error) { return { error }; } }); } getAvailableCurrencies(params) { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.get("/v1/currencies", { params: { fixed_rate: params.fixed_rate } }); return { result: data }; } catch (error) { return { error }; } }); } getAvailableFullCurrencies() { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.get("/v1/full-currencies"); return { result: data }; } catch (error) { return { error }; } }); } getAvailableCheckedCoins(params) { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.get("/v1/merchant/coins", { params: { fixed_rate: params.fixed_rate } }); return { result: data }; } catch (error) { return { error }; } }); } getEstimatedPrice(params) { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.get("/v1/estimate", { params: { amount: params.amount, currency_from: params.currency_from, currency_to: params.currency_to } }); return { result: data }; } catch (error) { return { error }; } }); } createPayment(params) { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.post("/v1/payment", params); return { result: data }; } catch (error) { return { error }; } }); } payoutAuthentication(params) { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.post("/v1/auth", params); return { result: data }; } catch (error) { return { error }; } }); } getBalance() { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.get("/v1/balance"); return { result: data }; } catch (error) { return { error }; } }); } getPayoutStatus(params) { return __async(this, null, function* () { try { const { data } = yield this.axiosInstance.get( `/v1/payout/${params.payout_id}` ); return { result: data }; } catch (error) { return { error }; } }); } createPayout(params, authorization) { return __async(this, null, function* () { const jwtToken = "Bearer " + authorization.replace("Bearer ", ""); try { const { data } = yield this.axiosInstance.post("/v1/payout", params, { headers: { Authorization: jwtToken } }); return { result: data }; } catch (error) { return { error }; } }); } verifyPayout(params, authorization) { return __async(this, null, function* () { const jwtToken = "Bearer " + authorization.replace("Bearer ", ""); try { const { data } = yield this.axiosInstance.post( `/v1/payout/${params.payout_id}/verify`, { verification_code: params.verification_code }, { headers: { Authorization: jwtToken } } ); return { result: data }; } catch (error) { return { error }; } }); } }; // src/webhook.ts var import_crypto = __toESM(require("crypto")); var verifyWebhook = (rawBody, signature, ipnSecret) => { if (!signature) { return { isVerified: false, error: "NO_SIGNATURE" }; } const npSignature = import_crypto.default.createHmac("sha512", ipnSecret).update(JSON.stringify(rawBody, Object.keys(rawBody).sort())).digest("hex"); if (signature !== npSignature) { return { isVerified: false, error: "INVALID_SIGNATURE" }; } return { isVerified: true, typedBody: rawBody }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { NowPaymentsClient, verifyWebhook });