GenerateInvoicesJob. This ensures that recurring charges are billed on time without manual intervention.
How it works
Scheduled execution
The
GenerateInvoicesJob runs on a regular schedule, typically daily. It evaluates all subscriptions to determine which ones are due for billing.Subscription processing
The job iterates through all subscriptions and checks billing eligibility based on the subscription’s next billing date and the current time.
Active phase check
Only subscriptions with an active phase containing recurring items are processed. If a subscription has no active phase or the active phase contains only one-time items, it is skipped.
Due date calculation
The job compares each subscription’s
next_billing_date with the current time. If the billing date has arrived or passed, the subscription is eligible for invoice generation.Invoice structure
When an invoice is generated, it contains:- Invoice level — customer, account, and subscription references
- Line items — one line item per subscription phase item
- Pricing — uses overridden prices when set, otherwise falls back to the base product price
- Quantities — reflects the quantity specified in each phase item
Example
Duplicate prevention
The invoice generation system includes safeguards to prevent duplicate invoices. If an invoice has already been generated for a billing period, the job will not create another one for the same period.Billing cycle anchors
Invoice generation respects billing cycle anchors. When a subscription has an anchor set, thenext_billing_date calculation aligns with the anchor date rather than the subscription creation date.
Initial invoices
When a subscription is first created, the system may generate an initial invoice depending on the proration behavior:| Proration behavior | Initial invoice |
|---|---|
create_prorations | Prorated charge for the partial period between creation and the billing anchor |
always_invoice | Immediate full-price invoice |
none | No initial invoice if an anchor is set; immediate full-price invoice if no anchor |