node-nowpayments-api
Version:
Node NowPayments API client
215 lines (212 loc) • 5.98 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
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 __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/client.ts
import axios from "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 = axios.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
import crypto from "crypto";
var verifyWebhook = (rawBody, signature, ipnSecret) => {
if (!signature) {
return { isVerified: false, error: "NO_SIGNATURE" };
}
const npSignature = crypto.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
};
};
export {
NowPaymentsClient,
verifyWebhook
};