Skip to main content

Using Redirects Instead of Webhooks

When creating checkout sessions with Flex, you have two options for handling successful payments:
  1. Webhooks - Flex sends webhook events to your server when payments complete
  2. Redirects - Users are redirected to your success URL, and you fetch the checkout session details
This guide covers how to use the redirect approach, which can be simpler to implement and test.

Overview

Instead of setting up webhook endpoints, you can:
  1. Create a checkout session with success_url and cancel_url parameters
  2. Redirect users to the checkout page
  3. When payment completes, users are redirected to your success_url with the checkout session ID as a query parameter
  4. 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_KEY
  • Content-Type: application/json
Response:
Key Fields:
  • status: Payment status (open, complete, expired)
  • amount_total: Total amount in cents
  • customer_details: Customer information
  • line_items: Items purchased

Handling Cancellations

Users who cancel the checkout process are redirected to your cancel_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 is complete 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
This provides the best user experience while ensuring robust payment processing.

Need help? If you have questions about implementing redirects or need assistance with your integration, please reach out to our support team.