loopback4-billing¶
Overview¶
The loopback4-billing package is designed to integrate billing functionality into LoopBack 4 applications. It provides an abstraction layer to work with billing services such as Chargebee, Stripe etc, offering common billing operations like creating and managing customers, invoices, payment sources, and transactions.
The package uses a provider pattern to abstract different billing implementations, making it easy to switch between billing services like REST API or SDK-based providers.
Key Features¶
Customer Management: Create, retrieve, update, and delete customers.
Invoice Management: Create, retrieve, update, and delete invoices.
Payment Source Management: Add, apply, retrieve, and delete payment sources for invoices.
Payment Status Tracking: Check payment status of invoices.
Installation¶
To install loopback4-billing, use npm:
Usage¶
Integration in Your LoopBack 4 Application¶
- Configure and load BillingComponent in the application constructor as shown below.
- Inject the BillingProvider into your controller. Inject the BillingProvider into your controller or service where you need to perform billing operations. ```ts export class BillingController { constructor( @inject(BillingComponentBindings.BillingProvider) private readonly billingProvider: IService, ) {} }
```
- Use BillingProvider Methods for Billing Operations Use the methods provided by the BillingProvider to manage billing entities like customers, invoices, and payment sources like we have displayed an instance of using BillingProvider for creating a invoice.Similarly you can use all the other methods provided by billing provider.
IService Interface and Available Methods¶
The IService interface defines a comprehensive list of methods to manage billing entities. Below is a summary of each method.
Customer Management¶
TCustomer is defined as
- getCustomers(customerId: string): Promise<TCustomer> - Retrieves details of a specific customer by ID.
- updateCustomerById(customerId: string, customerDto: Partial<TCustomer>): Promise<void> - Updates details of a specific customer by ID.
- deleteCustomer(customerId: string): Promise<void> - Deletes a customer by ID.
Payment Source Management¶
TpaymentSource is defined as
- applyPaymentSourceForInvoice(invoiceId: string, transaction: Transaction): Promise<TInvoice> - Applies an existing payment source to an invoice. Transaction defines as
Example of Transaction: if invoice is not being paid by payment_source.if invoice is being paid by payment_source
- retrievePaymentSource(paymentSourceId: string): Promise<TPaymentSource> - Retrieves details of a specific payment source.
- deletePaymentSource(paymentSourceId: string): Promise<void> - Deletes a payment source by ID.
- getPaymentStatus(invoiceId: string): Promise<boolean> - Checks the payment status of a specific invoice. It returns whether the invoice is paid or not.
Invoice Management¶
TInvoice is defined as :
- retrieveInvoice(invoiceId: string): Promise<TInvoice> - Retrieves details of a specific invoice.
- updateInvoice(invoiceId: string, invoice: Partia<TInvoice>): Promise
-Updates an existing invoice by ID.
- deleteInvoice(invoiceId: string): Promise<void> - Deletes an invoice by ID.
Configuration¶
The loopback4-billing package relies on the configuration of the chosen billing provider (e.g., Chargebee). To configure the package for your application, follow the steps below.
Step 1: Set Up Billing Provider¶
For ChargeBee -¶
To use Chargebee as the billing provider, you need to configure the Chargebee API keys and site URL in your application. You can set these values in the environment variables of your LoopBack 4 project.
after that, bind these values with the ChargeBeeBindings.Config as shown below.
For Stripe -¶
To use Stripe as the billing provider, you need to configure the Stripe secret Key in your application. You can set these values in the environment variables of your LoopBack 4 project.
Step 2: Register Billing Component¶
To use the billing component in your LoopBack 4 application. you need to register it in your application.ts file. And bind the Choosen billing Provider with their respective key. If provider is REST API based then bind it with BillingComponentBindings.RestProvider, else if the provider is SDK based bind it with BillingComponentBindings.SDKProvider.
For ChargeBee -¶
as Chargebee is a SDK based provider, so bind it with sdk provider binding.
For Stripe -¶
as Stripe is a SDK based provider, so bind it with sdk provider binding.
Step 3: Use the Billing Service in Controllers or Services¶
You can inject the BillingProvider or IService interface into your controller or service and use the billing operations as described below.
The method of using providers with in controllers and services is going to be same for all type of billing provider integrations provided by Loopback4-billing.
Feedback¶
If you've noticed a bug or have a question or have a feature request, search the issue tracker to see if someone else in the community has already created a ticket. If not, go ahead and make one! All feature requests are welcome. Implementation time may vary. Feel free to contribute the same, if you can. If you think this extension is useful, please star it. Appreciation really helps in keeping this project alive.
Contributing¶
Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.
Code of conduct¶
Code of conduct guidelines here.
