Overview
The Stripe Invoice Sync Flow enables Flexprice to synchronize invoices with Stripe, allowing for seamless payment processing and reconciliation across both platforms. This system handles different collection methods, webhook processing, and ensures accurate payment tracking.Key Features
- Invoice Synchronization: Automatic sync of Flexprice invoices to Stripe
- Collection Methods: Support for
charge_automaticallyandsend_invoicemethods - Payment Reconciliation: Real-time payment tracking across platforms
- Webhook Processing: Comprehensive webhook handling for all payment events
- Credit/Offline Restrictions: Disabled when invoice sync is enabled
- Metadata Tracking: Uses
flexprice_payment_idandflexprice_invoice_idfor reconciliation
Invoice Sync Lifecycle
Collection Methods
Charge Automatically
Whencollection_method is set to charge_automatically:
- Invoice Creation: Creates a draft invoice in Stripe with
charge_automaticallycollection method - Auto-advance: When enabled, Stripe advances the invoice and attempts to charge the customer’s default payment method. Flexprice still finalizes the Stripe invoice during sync when this setting is disabled.
- Payment Processing: Stripe attempts collection when auto-advance is enabled and collection can proceed
- Webhook Events: Listen for
invoice_payment.paid,invoice.paid,invoice.payment_failedandpayment_intent.succeeded
auto_advancedefaults totrue, except forcharge_automaticallywhen the customer has no default payment method; then it defaults tofalse. Setting it tofalsedisables Stripe’s automatic advancement and collection, but Flexprice still finalizes the invoice during sync.
- Uses customer’s default payment method
- Automatic retry on failure (based on Stripe settings)
- Subscription status depends of payment behavior until payment succeeds
Send Invoice
Whencollection_method is set to send_invoice:
- Invoice Creation: Creates a invoice in Stripe with
send_invoicecollection method - Due Date: Sets the invoice due date from Flexprice invoice
- Send Invoice: Sends Stripe’s invoice email by default; set
send_invoice_emailtofalseto suppress it - Customer Action: Customer must manually pay using Stripe’s hosted invoice page
- Webhook Events: Listen for
invoice_payment.paid,invoice.paid,invoice.payment_failedandpayment_intent.succeeded
send_invoice_emaildefaults totrueforsend_invoice; set it tofalseto prevent Stripe’s invoice email
- Manual payment required by customer
- Subscription status depends on payment behavior setting
- More control over payment timing
Outbound invoice settings
Set these optional fields insync_config.invoice_sync_settings:
auto_advance follows the collection method and saved default payment method; a failed lookup keeps it enabled. send_invoice_email defaults to sending Stripe’s email for send_invoice invoices. An invoice already paid or overpaid in Flexprice is synced as paid, without another Stripe collection attempt or invoice email. Sync also adds Stripe invoice-item adjustments for Flexprice discounts, prepaid credits, and exclusive tax.
Payment Flows
Stripe Payment Methods
1. Invoice Link (Stripe Hosted)
Flow: Webhook Events:invoice.paid when there is no PaymentIntent; invoice_payment.paid when the payment includes a PaymentIntent
- Trigger: Customer pays via Stripe invoice link
- Action:
invoice.paidreconciles the invoice without a PaymentIntent.invoice_payment.paidsettles the matching pending hosted-invoice payment, or creates an external payment record if none exists. - Reconciliation: Uses
flexprice_invoice_idfrom invoice metadata
- Failure:
invoice.payment_failedis handled for outbound-synced invoices; see Failed Payments
2. Stripe Invoice Auto-collection
Flow: Webhook Events:invoice_payment.paid includes a PaymentIntent; invoice.paid is reconciled without one.
- Trigger: Stripe attempts automatic collection for a
charge_automaticallyinvoice - Action: Reconciles a payment when the event includes a PaymentIntent, or reconciles the paid invoice without one
- Failure:
invoice.payment_failedis handled separately frompayment_intent.payment_failed; see Failed Payments
Flexprice Payment Methods
1. Checkout Link (Flexprice Hosted)
Flow: Webhook Handler:checkout.session.completed
- Trigger: Customer completes payment via Stripe Checkout
- Action: Updates payment status to
SUCCEEDED - Reconciliation: Uses
flexprice_payment_idfrom PaymentIntent metadata
- Failure: A
payment_intent.payment_failedevent with matchingflexprice_payment_idmetadata records a failed attempt and leaves the Checkout payment open for retry on the same live link.
2. Charge Card (Flexprice Direct)
Flow: The direct card API handles successful charges synchronously.payment_intent.succeeded does not drive this success update; payment_intent.payment_failed can update the payment to FAILED. Neither event itself reconciles invoice payment status.
Webhook Processing
Webhook Event Types
Metadata Requirements
Critical: Never deleteflexprice_payment_id or flexprice_invoice_id from Stripe metadata. These are essential for payment reconciliation.
Required Metadata Fields:
flexprice_payment_id: Links Stripe payment to Flexprice payment recordflexprice_invoice_id: Links Stripe invoice to Flexprice invoice recordflexprice_customer_id: Links Stripe customer to Flexprice customer record
Payment Reconciliation
Reconciliation Process
- Webhook Received: Stripe sends webhook with payment event
- Metadata Extraction: Extract
flexprice_payment_idorflexprice_invoice_id - Record Lookup: Find corresponding Flexprice record
- Status Update: Update payment/invoice status
- Amount Verification: Ensure payment amounts match
- Event Triggering: Send internal events for downstream processing
Restrictions and Edge Cases
When Invoice Sync is Enabled
Disabled Payment Methods:- ❌ Credit Payments: Cannot use wallet credits for payment
- ❌ Offline Payments: Cannot mark payments as offline
- ❌ Manual Payment Recording: Must use Stripe payment methods
- Invoice sync requires payment reconciliation with Stripe
- Credit and offline payments bypass Stripe’s payment tracking
- This ensures accurate financial reporting and reconciliation
Edge Cases
1. Partial Payments
- Stripe: Handles partial payments automatically
- Flexprice: Syncs partial payment amounts; the hosted-invoice payment-link path below requires the full remaining balance
- Reconciliation: Tracks cumulative payment amounts
2. Refunds
- Stripe: Process refunds through Stripe dashboard
- Flexprice: Manual reconciliation required
3. Failed Payments
- Non-link PaymentIntent failure: Updates that Flexprice payment to
FAILED; this update does not itself reconcile the invoice. - Checkout payment-link decline: When
payment_intent.payment_failedhas matchingflexprice_payment_idmetadata, it recordspayment.attempt.failedonly. The parent payment remains open and the customer can retry the same live Checkout link. invoice.payment_failed: Handled only when invoice outbound sync is enabled, a Flexprice mapping exists, and the mapped invoice is not already paid. It can mark a subscription incomplete when the invoice is finalized, has a positive balance, is a subscription-cycle invoice, and belongs to an active subscription with incomplete-payment behavior and either automatic collection or an overdue invoice. If an open PaymentIntent is found, Flexprice records a separate failed payment; without one, it creates no payment record. The handler does not directly set the invoice’spayment_statustoFAILED.
Synced invoice payment links
For an outbound-synced, finalized invoice, a/payments link uses the Stripe-hosted invoice page only after the hosted URL is available, no amount has already been paid, and the requested amount equals the full amount_remaining. This hosted-page PaymentIntent does not carry flexprice_payment_id. On failure, invoice.payment_failed can create a separate FAILED card payment when an open PaymentIntent is found; the original pending payment-link record stays open. Without an open PaymentIntent, no failed payment record is created. When payment succeeds through invoice_payment.paid with a PaymentIntent, Flexprice settles the existing pending hosted-invoice payment; if none exists, it creates an external payment record. invoice.paid without a PaymentIntent reconciles the mapped invoice. Partial payments are not supported on this hosted-page path. Voiding the Flexprice invoice attempts to void its synced Stripe invoice after the local commit; this is best-effort, and an already-paid Stripe invoice is left alone.
Configuration
Enabling Invoice Sync
- Stripe Connection: Configure Stripe integration
- Enable Sync: Set
"invoice": {"inbound": false, "outbound": true} - Customer Sync: Ensure customers are synced to Stripe
- Webhook Setup: Configure webhook endpoints
Webhook Endpoints
checkout.session.completedpayment_intent.succeededpayment_intent.payment_failedinvoice_payment.paid
invoice.paid and invoice.payment_failed.
Error Handling
Common Errors
Error Recovery
- Webhook Failures: Stripe retries failed webhooks
- Sync Failures: Manual retry via admin interface
Testing
Test Scenarios
- Invoice Sync: Create invoice and verify Stripe sync
- Payment Link: Generate link and complete payment
- Direct Charge: Charge customer directly
- Webhook Processing: Simulate webhook events
- Reconciliation: Verify payment amounts match
Test Data
Test Cards:4242424242424242: Successful payment4000000000000002: Card declined4000000000009995: Insufficient funds

