@vulog/aima-billing
Version:
Invoice management, credits, wallets, and refunds.
164 lines (163 loc) • 7.67 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
let zod = require("zod");
//#region src/chargeProduct.ts
const schema$6 = zod.z.object({
productId: zod.z.string().trim().min(1).uuid(),
serviceId: zod.z.string().trim().min(1).uuid().nullable().optional(),
amount: zod.z.number().min(0),
userId: zod.z.string().trim().min(1).uuid(),
entityId: zod.z.string().trim().min(1).uuid(),
subscriptionId: zod.z.string().trim().min(1).uuid().nullable().optional(),
productNotes: zod.z.string().trim().nullable().optional(),
originId: zod.z.string().trim().nullable().optional(),
chargeProductRequestId: zod.z.string().trim().min(1).uuid().nullable().optional(),
additionalInfo: zod.z.object({ manualPayment: zod.z.boolean().optional() }).optional()
});
const chargeProduct = async (client, info) => {
const result = schema$6.safeParse(info);
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
return client.post(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/product`, info).then(({ data }) => data);
};
//#endregion
//#region src/getUserCreditsByEntityId.ts
const getUserCreditsByEntityId = async (client, id) => {
const result = zod.z.string().trim().min(1).uuid().safeParse(id);
if (!result.success) throw new TypeError("Invalid id", { cause: result.error.issues });
return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallet/entity/${id}`).then(({ data }) => data);
};
//#endregion
//#region src/addCredits.ts
const schema$5 = zod.z.object({
initialAmount: zod.z.number().min(0),
validityStartDate: zod.z.string().datetime(),
validityEndDate: zod.z.string().datetime(),
notes: zod.z.string().trim().nullable().optional(),
discountCategory: zod.z.enum(["CREDITS", "PERCENTAGE"]),
oneTimeUsage: zod.z.boolean().optional(),
entityId: zod.z.string().trim().min(1).uuid(),
type: zod.z.enum([
"LOCAL",
"GLOBAL",
"PERIODIC"
]),
usage: zod.z.enum([
"TRIP",
"REGISTRATION",
"PRODUCTS",
"ALL"
])
});
const addCredits = async (client, payload) => {
const result = schema$5.safeParse(payload);
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
return client.post(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/entities/${payload.entityId}/wallets`, result.data).then(({ data }) => data);
};
//#endregion
//#region src/getInvoiceById.ts
const getInvoiceById = async (client, id) => {
if (!zod.z.string().trim().min(1).uuid().safeParse(id).success) throw new Error("Invalid invoice ID");
return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${id}`).then(({ data }) => data);
};
//#endregion
//#region src/getInvoicePdf.ts
const getInvoicePdf = async (client, invoiceId) => {
const result = zod.z.string().trim().min(1).uuid().safeParse(invoiceId);
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
const { data } = await client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${invoiceId}/pdf`, { responseType: "arraybuffer" });
return data && data.byteLength > 0 ? data : null;
};
//#endregion
//#region src/getInvoicesByTripId.ts
const getInvoicesByTripId = async (client, tripId) => {
const result = zod.z.string().trim().min(1).safeParse(tripId);
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/trips/${result.data}/invoices`).then(({ data }) => data);
};
//#endregion
//#region src/getRefundableAmount.ts
const schema$4 = zod.z.string().trim().min(1);
const getRefundableAmount = async (client, invoiceId) => {
const result = schema$4.safeParse(invoiceId);
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${invoiceId}/refundableAmount`).then(({ data }) => data);
};
//#endregion
//#region src/refund.ts
const schema$3 = zod.z.object({
amount: zod.z.number().min(0).optional(),
note: zod.z.string().trim().nullable().optional(),
paymentIntentPspReference: zod.z.string().trim().min(1).optional()
});
const refund = async (client, payload) => {
const result = schema$3.safeParse(payload);
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
const filteredData = Object.fromEntries(Object.entries(result.data).filter(([_, value]) => value !== void 0 && value !== null));
return client.post(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${payload.invoiceId}/refund`, filteredData).then(({ data }) => data);
};
//#endregion
//#region src/getWalletsByEntity.ts
const getWalletsByEntity = async (client, entityId) => {
const result = zod.z.string().trim().min(1).uuid().safeParse(entityId);
if (!result.success) throw new TypeError("Invalid entityId", { cause: result.error.issues });
return client.get(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallet/entity/${entityId}`).then(({ data }) => data);
};
//#endregion
//#region src/updateWallet.ts
const schema$2 = zod.z.object({
walletId: zod.z.string().trim().min(1).uuid(),
actions: zod.z.array(zod.z.object({
op: zod.z.enum(["replace"]),
path: zod.z.enum(["/availableAmount", "/percentage"]),
value: zod.z.string().min(1)
})).min(1).max(1)
});
const updateWallet = async (client, walletId, actions) => {
const result = schema$2.safeParse({
walletId,
actions
});
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
await client.patch(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallets/${walletId}`, actions, { headers: { "Content-Type": "application/json-patch+json" } });
};
//#endregion
//#region src/payInvoice.ts
const schema$1 = zod.z.object({
invoiceId: zod.z.string().trim().min(1).uuid(),
manualPayment: zod.z.object({
requiresActionReturnUrl: zod.z.string().default(""),
online: zod.z.boolean().default(true),
scope: zod.z.enum(["RENTAL", "DEPOSIT"])
})
});
const payInvoice = async (client, invoiceId, manualPayment) => {
const result = schema$1.safeParse({
invoiceId,
manualPayment
});
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
return client.post(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/invoices/${result.data.invoiceId}/payment`, result.data.manualPayment).then(({ data }) => data);
};
//#endregion
//#region src/retry-payment.ts
const schema = zod.z.object({ invoiceId: zod.z.string().trim().min(1).uuid() });
/**
* Retry an invoices payment stuck in REFUSED and ERROR status, move the status to PENDING or PENDING_MANUAL_PAYMENT
*/
const retryPayment = async (client, invoiceId) => {
const result = schema.safeParse({ invoiceId });
if (!result.success) throw new TypeError("Invalid args", { cause: result.error.issues });
return client.post(`/boapi/proxy/user/billing/fleets/${client.clientOptions.fleetId}/invoices/${result.data.invoiceId}/retry-payment`, {}).then(({ data }) => data);
};
//#endregion
exports.addCredits = addCredits;
exports.chargeProduct = chargeProduct;
exports.getInvoiceById = getInvoiceById;
exports.getInvoicePdf = getInvoicePdf;
exports.getInvoicesByTripId = getInvoicesByTripId;
exports.getRefundableAmount = getRefundableAmount;
exports.getUserCreditsByEntityId = getUserCreditsByEntityId;
exports.getWalletsByEntity = getWalletsByEntity;
exports.payInvoice = payInvoice;
exports.refund = refund;
exports.retryPayment = retryPayment;
exports.updateWallet = updateWallet;