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 - Customers 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 customers to the checkout page
  3. When payment completes, customers 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 customers 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 customer.

Handling the success redirect

When a payment is successful, the customer 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

Customers 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 through 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 customers

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 customer leaves your website
  • 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, reach out to our support team.