Scheduled invoices enable merchants to automate recurring billing for their customers using the Stax Pay API. By creating a schedule record, you can define when and how often invoices are generated, with options to automatically charge a saved payment method (autopay) or allow the customer to manually pay each invoice.
This page provides an in-depth overview of the scheduled invoice system, its functionality, and the lifecycle of scheduled invoices.
How Scheduled Invoices Work
A schedule record acts as an "invoice generator." It stores information such as:
- Customer Details: Who the invoice is for.
- Invoice Details: The total amount, taxes, line items, and other metadata.
- Frequency Rules: How often the invoice should be generated (daily, weekly, monthly, etc.).
- Autopay Setup: Optional saved payment method to automatically process payments.
At each scheduled interval:
- An invoice is generated based on the schedule's details.
- If a payment method is attached (autopay), a payment is attempted immediately.
- The system updates the schedule's
next_run_at
field to calculate the next occurrence.
Note: If no payment method is attached, the customer can manually pay the invoice, and optionally save a payment method for future use.
Schedule Lifecycle and Statuses
Scheduled invoices can have the following statuses:
Status | Description |
---|---|
NOT STARTED | The schedule's DTSTART is in the future, and the schedule has not yet generated any invoices. |
WAITING | The schedule is active and awaiting the next run date. |
PENDING | The DTSTART has passed, but the system has not yet processed the schedule. |
PAUSED | The schedule has been paused manually, and the next_run_at is set to null . |
COMPLETED | The schedule has reached its defined end (COUNT or UNTIL parameters). |
DELETED | The schedule has been soft-deleted, making it inaccessible but still stored in the database. |
Key Status Transitions
- Pause and Resume: Pausing a schedule clears the
next_run_at
field. To resume, setactive=true
and update theDTSTART
with a future date. - Delete: Deleting a schedule sets the
deleted_at
field, halts future occurrences, and makes the schedule inaccessible.
Common Use Cases
Recurring Payments
Scheduled invoices simplify recurring payments by automating invoice generation and processing.
- Example: A monthly subscription service can create a schedule with
FREQ=MONTHLY;COUNT=12
.
Deferred Billing
Schedules allow merchants to defer billing to a future date without requiring immediate action from the customer.
- Example: Generate a one-time invoice for a specific date in the future with
FREQ=MONTHLY;COUNT=1
.
Installment Payments
Merchants can split a larger payment into multiple installments with specific amounts and due dates.
- Example: A three-part payment plan can use
FREQ=WEEKLY;COUNT=3
.
RRule Parameter Guide
The rule
field in a schedule specifies the recurrence pattern using the iCalendar RRule . Here are some examples:
Recurrence | RRule Example |
---|---|
Repeat monthly indefinitely | DTSTART=20230101T120000Z;FREQ=MONTHLY |
Repeat monthly for 12 months | DTSTART=20230101T120000Z;FREQ=MONTHLY;COUNT=12 |
Every other week until December 31, 2025 | DTSTART=20230101T120000Z;FREQ=WEEKLY;UNTIL=20251231T000000Z;INTERVAL=2 |
One-time invoice on a future date | DTSTART=20230101T120000Z;FREQ=DAILY;COUNT=1 |
Important:
DTSTART
must be in the future and formatted in ISO 8601 UTC (YYYYMMDDTHHMMSSZ
).- For testing, avoid using
DTSTART
values close to the current time to account for API latency. - Try a JavaScript utility to see how the RRule spec works, called Jakubroztocil rrule.
Best Practices
- Metadata: Populate
meta
fields (subtotal, tax, line items) to ensure invoices contain complete and accurate details for customers. - Autopay: Attach a
payment_method_id
for automatic payments, reducing manual intervention. - Email Notifications: Set
email_notification=false
if you want to disable automatic email receipts for generated invoices. - To delete a schedule, call DELETE invoice/schedule/id. A deleted_at variable will be set for the time of deletion. There is no way to undelete a schedule without a support request.
Troubleshooting and Error Handling
Issue | Cause | Solution |
---|---|---|
Schedule does not generate invoices | DTSTART is in the past or incorrectly formatted. | Use a future DTSTART in the correct ISO 8601 format. |
Invoice not processed on time | The scheduler is delayed due to system processing. | Wait a few minutes and check the PENDING status. |
Schedule paused unintentionally | active=false was set during an update. | Update the rule with a valid DTSTART and set active=true . |
Unable to delete a schedule | Invalid or missing schedule ID. | Verify the schedule ID and ensure the schedule is not already deleted. |
Advanced Features
Future Occurrences
Use this to estimate revenue or visualize the schedule's execution.
Thefuture_occurrences
is an array of timestamps and is truncated to 50 upcoming occurrences for the schedule. Estimating total revenue for a schedule works best if you have a specific end date or if your calculation only relies on 50 or fewer occurrences.
Note on Pausing Scheduled InvoicesIf you use PUT invoice/schedule/id to pause the schedule. It will clear the next_run_at date also. This means no new invoices will be generated. To resume the schedule, you must mark active to 1 or true again, and you will need to update your rule’s DTSTART to have a date in the future. A date in the past is never allowed during a PUT or POST operation.