@aios-inc/cashier-nestjs
v1.8.2
Published
NestJS module for Cashier: unified subscription billing with forRoot, forRootAsync, an injectable CashierService and an exception filter.
Readme
@aios-inc/cashier-nestjs
NestJS module for Cashier, unified subscription billing for Node.js and TypeScript. Register it with forRoot or forRootAsync, inject CashierService anywhere, and turn billing errors into proper HTTP responses. Supports NestJS 9, 10, 11 and 12.
Install
npm install @aios-inc/cashier @aios-inc/cashier-nestjs stripe recurlyUsage
import { Injectable, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { APP_FILTER } from '@nestjs/core';
import {
CashierExceptionFilter,
CashierModule,
CashierService,
} from '@aios-inc/cashier-nestjs';
import { CashierProvider } from '@aios-inc/cashier';
@Module({
imports: [
CashierModule.forRootAsync({
isGlobal: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
default: CashierProvider.Stripe,
providers: {
stripe: { apiKey: config.getOrThrow('STRIPE_SECRET_KEY') },
recurly: { apiKey: config.getOrThrow('RECURLY_API_KEY') },
},
}),
}),
],
providers: [{ provide: APP_FILTER, useClass: CashierExceptionFilter }],
})
export class AppModule {}
@Injectable()
export class BillingService {
constructor(private readonly cashier: CashierService) {}
getInvoice(invoiceId: string) {
return this.cashier.use().invoices.get(invoiceId);
}
}| Call | Driver |
| ------------------------------------------------- | ---------------------------------------------------------------- |
| cashier.use() | The default provider. Switch providers by changing the config. |
| cashier.use(CashierProvider.Recurly) | A provider from providers. |
| cashier.use(CashierProvider.Stripe, { apiKey }) | Other credentials, given at runtime. |
Documentation
- NestJS guide: every option, the exception filter and testing.
- API reference: customers, invoices, payments, products, prices and subscriptions.
- Errors: every error class and the HTTP responses.
License
MIT © AIOS
