Okra-billing package
This is an Okra billing TypeScript Package with methods for handling billings.
Usage
import {} from 'okra-billing'
// Retrieve income status, products, plan, and company information
const usedIncome = usedProduct?.income;
const companySelect =
'free_calls micro_lending name billing_email sandbox_callback_token';
const [products, plan, incomeStatus, company] = await Promise.all([
getManyDocument({ type: { $in: billable_products } }, 'product', {
select: 'prices type okra_lite_discount',
}),
getOneDocument({ owner }, 'plan', {
populate: [
{
path: 'type',
select: 'default_discount name type',
model: 'PlanType',
},
],
}),
usedIncome && getOneDocument({ record: recordId }, 'income', {}),
getDocumentById(owner, 'company', { select: companySelect }),
]);
// call getCostAndBreadown to get the cost and breakdown of the products
const { charge, productBreakDown } = getCostAndBreadown({
plan,
record,
amountOfTrans,
usedIncome: incomeStatus,
products,
company,
});
// update the company and wallet information with the charge
const walletUpdate = await sendToAPI(
{
model: 'wallet',
query: { currency, owner },
update: { $inc: { amount: -charge } },
options: { new: true },
},
'db/update-id',
);
// Create the receipt
const receiptData = createReceiptObject(
charge,
productBreakDown,
record,
plan,
amountOfTrans,
walletUpdate,
);
return sendToAPI(
{ model: 'receipt', doc: receiptData, from: 'billing-service' },
'db/save',
);