refundEventTickets
Processes refunds for all valid tickets associated with an event through Stripe, updates ticket statuses, and cancels the event. This action handles batch refund processing with error handling for individual ticket failures.Function Signature
Parameters
The unique identifier of the event for which to refund all tickets
Return Value
Returns
true if all refunds were processed successfullyRefund Flow
The action follows a comprehensive workflow to ensure all tickets are properly refunded:-
Event Validation
- Fetches event details from Convex database
- Throws error if event not found
-
Stripe Connect ID Retrieval
- Gets the event owner’s Stripe Connect account ID
- Required for processing refunds through the connected account
- Throws error if Stripe Connect ID not found
-
Ticket Retrieval
- Queries all valid tickets for the event
- Only processes tickets with valid payment information
-
Batch Refund Processing
- Processes all ticket refunds concurrently using
Promise.allSettled - Each ticket refund includes:
- Stripe refund API call
- Ticket status update in database
- Handles individual ticket failures gracefully
- Processes all ticket refunds concurrently using
-
Validation
- Checks if all refunds succeeded
- Throws error if any refunds failed
-
Event Cancellation
- Cancels the event (does not delete it)
- Only executed if all refunds succeed
Stripe Refund API Usage
Each ticket refund uses the Stripe Refunds API with the connected account context:Refund Parameters
- payment_intent: The Stripe Payment Intent ID associated with the ticket purchase
- reason: Set to
"requested_by_customer"for all refunds - stripeAccount: The event owner’s Connect account ID (ensures refund is processed from their account)
Error Handling
The action implements robust error handling at multiple levels:Event-Level Errors
- Event not found
- Stripe Connect ID not found
- Some or all refunds failed
Ticket-Level Errors
Individual ticket refunds may fail for various reasons:- Payment information not found
- Stripe API errors (e.g., payment intent not refundable)
- Network issues
Database Updates
For each successful refund, the ticket status is updated in the Convex database:Event Cancellation
After all refunds are processed successfully, the event is cancelled (not deleted):Example Usage
Concurrent Processing
The action usesPromise.allSettled to process all refunds concurrently, improving performance for events with many tickets:
- Processes all refunds in parallel
- Doesn’t stop processing if one refund fails
- Collects all results for validation
Result Validation
Important Considerations
- Authentication: While this action doesn’t explicitly check authentication, it should only be called by authorized users (event organizers)
- Stripe Connect Context: All refunds are processed through the event owner’s Stripe Connect account, ensuring funds are returned from the correct account
- Idempotency: If called multiple times for the same event, Stripe will return an error for already-refunded payment intents
- Refund Timing: Refunds typically appear in customer accounts within 5-10 business days, depending on their bank
- Application Fees: When a refund is issued, Stripe automatically handles the return of application fees
Related Actions
- Stripe Checkout Session - Creating payment sessions for ticket purchases
- Stripe Connect Actions - Managing Stripe Connect accounts