billing - stripe - exclude pending items
MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -284,8 +284,7 @@ export default class StripeBillingIntegration extends BillingIntegration {
}
private async createStripeInvoice(customerID: string, userID: string, idempotencyKey: string): Promise<Stripe.Invoice> {
- // Let's create the STRIPE invoice
- const stripeInvoice: Stripe.Invoice = await this.stripe.invoices.create({
+ const creationParameters: Stripe.InvoiceCreateParams = {
customer: customerID,
// collection_method: 'send_invoice', //Default option is 'charge_automatically'
// days_until_due: 30, // Optional when using default settings
@@ -294,8 +293,13 @@ export default class StripeBillingIntegration extends BillingIntegration {
tenantID: this.tenant.id,
userID
},
- }, {
- // idempotency_key: idempotencyKey?.toString(),
+ };
+ if (FeatureToggles.isFeatureActive(Feature.BILLING_INVOICES_EXCLUDE_PENDING_ITEMS)) {
+ // New STRIPE API to exclude PENDING ITEMS from the new Invoice
+ creationParameters.pending_invoice_items_behavior = 'exclude';
+ }
+ // Let's create the STRIPE invoice
+ const stripeInvoice: Stripe.Invoice = await this.stripe.invoices.create(creationParameters, {
idempotencyKey: idempotencyKey?.toString(), // STRIPE version 8.137.0 - property has been renamed!!!
});
return stripeInvoice;
@@ -1177,18 +1181,22 @@ export default class StripeBillingIntegration extends BillingIntegration {
let refreshDataRequired = false;
const userID: string = user.id;
const customerID: string = user.billingData?.customerID;
- // Check whether a DRAFT invoice can be used
- let stripeInvoice: Stripe.Invoice;
- if (this.settings.billing?.immediateBillingAllowed) {
- // immediateBillingAllowed is ON - we want an invoice per transaction
- // Because of some STRIPE constraints the invoice creation must be postpone!
- stripeInvoice = null;
- } else {
- // immediateBillingAllowed is OFF - let's add to the latest DRAFT invoice (if any)
+ // Check whether a DRAFT invoice can be used or not
+ let stripeInvoice: Stripe.Invoice = null;
+ if (!this.settings.billing?.immediateBillingAllowed) {
+ // immediateBillingAllowed is OFF - let's retrieve to the latest DRAFT invoice (if any)
stripeInvoice = await this.getLatestDraftInvoiceOfTheMonth(this.tenant.id, userID, customerID);
}
+ if (FeatureToggles.isFeatureActive(Feature.BILLING_INVOICES_EXCLUDE_PENDING_ITEMS)) {
+ if (!stripeInvoice) {
+ // NEW STRIPE API - Invoice can mow be created before its items
+ stripeInvoice = await this.createStripeInvoice(customerID, userID, this.buildIdemPotencyKey(billingInvoiceItem.transactionID, 'invoice'));
+ }
// Let's create an invoice item per dimension
- // When the stripeInvoice is null a pending item is created
+ await this.createStripeInvoiceItems(customerID, billingInvoiceItem, stripeInvoice.id);
+ refreshDataRequired = true;
+ } else {
+ // FORMER STRIPE API - Items must be created before the invoice (as PENDING items)
await this.createStripeInvoiceItems(customerID, billingInvoiceItem, stripeInvoice?.id);
if (!stripeInvoice) {
// Let's create a new DRAFT invoice (if none has been found)
@@ -1197,6 +1205,7 @@ export default class StripeBillingIntegration extends BillingIntegration {
// Here an existing invoice is being reused
refreshDataRequired = true;
}
+ }
let operationResult: StripeChargeOperationResult;
if (this.settings.billing?.immediateBillingAllowed) {
// Let's try to bill the stripe invoice using the default payment method of the customer

MODIFY
src/utils/FeatureToggles.ts
src/utils/FeatureToggles.ts
// Declare here the list of features
export enum Feature {
+ BILLING_INVOICES_EXCLUDE_PENDING_ITEMS,
BILLING_PREVENT_CUSTOMER_DELETION,
BILLING_SHOW_PRICING_DETAIL,
BILLING_PLATFORM_USE_EXPRESS_ACCOUNT,
@@ -9,6 +10,7 @@ export enum Feature {
export default class FeatureToggles {
// Comment out the features that you want to switch OFF
private static activeFeatures: Feature[] = [
+ Feature.BILLING_INVOICES_EXCLUDE_PENDING_ITEMS,
Feature.BILLING_PREVENT_CUSTOMER_DELETION,
Feature.BILLING_PLATFORM_USE_EXPRESS_ACCOUNT,
];

billing - stripe - exclude pending items