@mozaic-io/mozaic-sdk-node
Version:
The Mozaic Node SDK enables you to pay your creators easily via the Mozaic API.
153 lines (152 loc) • 8.09 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymentCycle = void 0;
const BaseResource_1 = require("../BaseResource");
const PaymentCycleEntry_1 = require("./PaymentCycleEntry");
const PaymentCycleEntryList_1 = require("./PaymentCycleEntryList");
/**
* A payment cycle
*/
class PaymentCycle extends BaseResource_1.BaseResource {
/**
* @internal
* Internal use only. Please use the Mozaic object to utilize this object.
* @param paymentCycleApi
* @param paymentCycle
*/
constructor(mozaic, paymentCycleApi, paymentCycle) {
var _a, _b;
super();
this._mozaic = mozaic;
this._paymentCycleApi = paymentCycleApi;
this.rawObject = paymentCycle;
this.paymentCycleId = this.throwIfNullOrUndefined("paymentCycle.id", paymentCycle.id);
this.status = this.throwIfNullOrUndefined("paymentCycle.status", paymentCycle.status);
this.amount = paymentCycle.amount;
this.fees = paymentCycle.fees;
this.shortId = this.throwIfNullOrUndefined("paymentCycle.short_id", paymentCycle.short_id);
this.paymentCycleEntryCount = paymentCycle.total_entries;
this.feeDirection = paymentCycle.fee_direction;
this.memo = paymentCycle.memo;
if (paymentCycle.accounting_from !== null)
this.accountingFrom = new Date((_a = paymentCycle.accounting_from) !== null && _a !== void 0 ? _a : "");
if (paymentCycle.accounting_to !== null)
this.accountingTo = new Date((_b = paymentCycle.accounting_to) !== null && _b !== void 0 ? _b : "");
this.name = this.getValueOrDefault(paymentCycle.name, "");
this.invoiceId = paymentCycle.invoice_id;
}
/**
* Adds a payment cycle entry to the current payment cycle. The payment cycle entry pays the given user the specified amount.
* @param name The first and last name of the user to pay.
* @param email The email address of the user to pay. If the user has not already joined Mozaic, they will be sent an email invite to join.
* @param amount The amount of money to be paid to the user.
* @param currency The currency that the amount will be paid in. Foreign exchange rates may apply to cross-boarder payments.
* @param externalId An optional external ID that can be used to identify the payment recipient in an external system. This ID will be returned in the PaymentCycleEntry object.
* @param memo An optional free text field that can be used to describe the payment. This field has a maximum length of 140 characters.
*/
addPaymentCycleEntry(name, email, amount, currency, externalId, memo) {
return __awaiter(this, void 0, void 0, function* () {
const deets = {
to: {
name: name,
email: email
},
original_amount: {
currency: currency,
quantity: amount
},
external_id: externalId,
memo: memo
};
var result = yield this.execute(() => this._paymentCycleApi.createPaymentCycleEntry(this.paymentCycleId, deets));
return new PaymentCycleEntry_1.PaymentCycleEntry(result);
});
}
/**
* Gets payment cycle entries for the payment cycle. Use limit and page to page through
* the results for large data sets.
* @param limit The number of records in a page from 1 - 100
* @param page The number of the page, starts at 1
* @param sortBy The field to sort the results by.
* @returns
*/
getPaymentCycleEntries(limit, page, sortBy, sortByAscending) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
this.throwIfLimitOrPageAreInvalid(limit, page);
const result = yield this.execute(() => this._paymentCycleApi.listPaymentCycleEntries(this.paymentCycleId, undefined, undefined, undefined, sortBy, sortByAscending, limit, page, undefined, undefined));
const data = (_a = result.data) === null || _a === void 0 ? void 0 : _a.map((value) => new PaymentCycleEntry_1.PaymentCycleEntry(value));
return new PaymentCycleEntryList_1.PaymentCycleEntryList(result, data);
});
}
/**
* Completes the payment cycle by withdrawing money from your specified wallet item and then
* distributing it to recipients on the payment cycle.
* @param walletItem The source of funding for the payment cycle. This must be a Payment wallet item and
* not a Payout wallet item.
* @returns A new PaymentCycle object representing the finalized payment cycle.
*/
finalize(walletItem) {
return __awaiter(this, void 0, void 0, function* () {
var wallets = yield this._mozaic.Wallets.getWallets();
var existingPaymentMethod = wallets.find((value) => {
return value.paymentMethods.find((pm) => {
if (pm.paymentMethodId == walletItem.paymentMethodId)
return walletItem;
});
});
if (existingPaymentMethod === undefined)
throw new Error("An invalid payment method id was selected. You must use a valid payment method. (Did you select a payout method by mistake?)");
const result = yield this.execute(() => this._paymentCycleApi.finalizePaymentCycleEntry(this.paymentCycleId, {
auto_advance: true,
payment_method_id: walletItem.paymentMethodId
}));
return new PaymentCycle(this._mozaic, this._paymentCycleApi, result);
});
}
/**
* Completes the payment cycle using "Pay by Invoice". You will need to download the invoice
* through the Invoices.getInvoice method, and then send a payment to the bank account listed
* on the invoice.
* @returns A new PaymentCycle object representing the finalized payment cycle.
*/
finalizeByInvoice() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.execute(() => this._paymentCycleApi.finalizePaymentCycleEntry(this.paymentCycleId, {
auto_advance: true,
payment_method_id: null,
collection_method: "send_invoice",
ach_auto_reconciliation: false,
auto_finalize_invoice: true
}));
return new PaymentCycle(this._mozaic, this._paymentCycleApi, result);
});
}
/**
* Retrieves an invoice for this payment cycle. Note that an invoice is not generated for a payment
* cycle until it has been finalized.
*
* @returns an ArrayBuffer containing the bytes of an Invoice PDF. It can be saved using:
*
* ```fs.writeFileSync(fileName, Buffer.from(arrayBuffer));```
*/
getInvoice() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const invoiceId = (_a = this.invoiceId) !== null && _a !== void 0 ? _a : "";
if (invoiceId === "")
throw new Error("There isn't an invoice assigned to this payment cycle yet. Please finalize the payment cycle to receive an invoice.");
return this._mozaic.Invoices.getInvoice(invoiceId);
});
}
}
exports.PaymentCycle = PaymentCycle;