Using Redirects Instead of Webhooks
When creating checkout sessions with Flex, you have two options for handling successful payments:- Webhooks - Flex sends webhook events to your server when payments complete
- Redirects - Users are redirected to your success URL, and you fetch the checkout session details
Overview
Instead of setting up webhook endpoints, you can:- Create a checkout session with
success_urlandcancel_urlparameters - Redirect users to the checkout page
- When payment completes, users are redirected to your
success_urlwith the checkout session ID as a query parameter - Fetch the checkout session details from your success page using the Flex API
Creating a Checkout Session with Redirect URLs
When creating a checkout session, specify the URLs where users should be redirected:Important: Include{CHECKOUT_SESSION_ID}in your success URL.
Flex will replace this with the actual checkout session ID when redirecting the user.
Handling the Success Redirect
When a payment is successful, the user will be redirected to your success URL with the checkout session ID as a query parameter. On your success page, extract this ID and fetch the full checkout session details.Example: Node.js/Express
Example: Python/Django
Example: PHP
API Endpoint Details
GET /v1/checkout/sessions/
Retrieves a checkout session by ID. URL:https://api.withflex.com/v1/checkout/sessions/{CHECKOUT_SESSION_ID}
Method: GET
Headers:
Authorization: Bearer YOUR_API_KEYContent-Type: application/json
status: Payment status (open,complete,expired)amount_total: Total amount in centscustomer_details: Customer informationline_items: Items purchased
Handling Cancellations
Users who cancel the checkout process are redirected to yourcancel_url. No checkout session ID is provided in this case.
Security Considerations
1. Verify the Checkout Session
Always fetch the checkout session from the Flex API to verify its authenticity. Never trust data passed via URL parameters alone.2. Check Payment Status
Ensure the checkout session status iscomplete before fulfilling orders:
3. Prevent Duplicate Processing
Implement idempotency to prevent processing the same order multiple times:4. Handle Expired Sessions
Checkout sessions expire after 24 hours. Handle expired sessions gracefully:Testing
Test Mode
Use test API keys to create test checkout sessions. Test payments will use Stripe’s test card numbers.Local Development
For local testing, use tools like ngrok to create public URLs:When to Use Redirects vs Webhooks
Use Redirects When
- You want simple, quick implementation
- Your fulfillment process can happen synchronously
- You primarily need to handle successful payments
- You want to show immediate confirmation to users
Use Webhooks When
- You need to handle all payment events (failed, disputed, etc.)
- You have complex background processing requirements
- You need to handle payments that complete after the user leaves your site
- You want to decouple payment processing from user sessions
Combining Both Approaches
You can use both redirects and webhooks together:- Use redirects for immediate user feedback and basic order fulfillment
- Use webhooks for comprehensive event handling and background processing
Need help? If you have questions about implementing redirects or need assistance with your integration, please reach out to our support team.