modem-pay
Version:
A TypeScript SDK for integrating with the Modem Pay payment gateway, enabling seamless payment processing and financial services in your applications.
154 lines (153 loc) • 5.92 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const base_1 = __importDefault(require("./base"));
class InvoicesResource extends base_1.default {
constructor(apiKey, maxRetries, timeout) {
super(apiKey, maxRetries, timeout);
this.getMetadata = (params, draft = false) => {
let newSubtotal = 0;
let total = params.amount;
let type = "simple";
let amount = params.amount;
if (params.line_items &&
Array.isArray(params.line_items) &&
params.line_items.length > 0) {
params.line_items.forEach((item) => {
newSubtotal += item.quantity * item.unit_price;
});
type = "professional";
}
else {
if (params.amount === undefined || params.amount === null) {
throw new Error("Amount is required when no line items are provided.");
}
if (params.amount <= 9) {
throw new Error("Amount must be greater than 9 when no line items are provided.");
}
newSubtotal = params.amount;
}
let discountAmount = 0;
if (params.discount) {
if (params.discount.type === "percentage") {
discountAmount = (newSubtotal * params.discount.amount) / 100;
}
else {
discountAmount = params.discount.amount;
}
}
total = newSubtotal - discountAmount;
amount = total;
return {
sub_total: newSubtotal,
total,
type,
currency: "GMD",
amount,
sent_date: draft ? null : new Date().toISOString(),
};
};
}
/** Creates an invoice */
create(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.request({
method: "post",
url: `${this.apiURL}/v1/invoices`,
headers: this.getHeaders(),
data: {
data: Object.assign(Object.assign(Object.assign({}, params), this.getMetadata(params)), { state: "published" }),
},
});
});
}
/** Creates an invoice in draft mode */
draft(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.request({
method: "post",
url: `${this.apiURL}/v1/invoices/draft`,
headers: this.getHeaders(),
data: { data: Object.assign(Object.assign({}, params), this.getMetadata(params, true)) },
});
});
}
/** Retrieves an invoice by its id. */
retrieve(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.request({
method: "get",
url: `${this.apiURL}/v1/invoices/${id}`,
headers: this.getHeaders(),
});
});
}
update(id, params) {
return __awaiter(this, void 0, void 0, function* () {
return this.request({
method: "put",
url: `${this.apiURL}/v1/invoices/${id}`,
data: {
data: Object.assign(Object.assign({}, params), this.getMetadata(params)),
},
headers: this.getHeaders(),
});
});
}
/** Manually pay an invoice. */
pay(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.request({
method: "post",
url: `${this.apiURL}/v1/invoices/${id}/pay`,
headers: this.getHeaders(),
});
});
}
/** Sends a draft invoice to the customer. */
sendDraftInvoice(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.request({
method: "post",
url: `${this.apiURL}/v1/invoices/${id}/send`,
headers: this.getHeaders(),
});
});
}
/**
* Sends a payment reminder for an invoice to the customer.
*/
sendReminder(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.request({
method: "post",
url: `${this.apiURL}/v1/invoices/${id}/send-reminder`,
headers: this.getHeaders(),
});
});
}
/** Returns a list of invoices. */
list(options = { limit: 10 }) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.request({
method: "get",
url: `${this.apiURL}/v1/invoices`,
headers: this.getHeaders(),
params: Object.assign({ offset: 0 }, options),
});
return data;
});
}
}
exports.default = InvoicesResource;