How can I add a go back arrow to Stripe checkout?
This post might have stale content, as dev is 29 commits ahead.
How can I add a go back arrow to the Stripe checkout? I'm using the checkout function from useCustomer()
To add a "go back" arrow to the Stripe checkout when using useCustomer(), you need to pass a cancel_url to Stripe. This is done via the checkoutSessionParams field in the checkout function.
When you provide a cancel_url, Stripe automatically includes a back button (usually in the header or as a "Return to [Business]" link) that redirects the user back to your application if they decide not to complete the purchase.
###Example
import { useCustomer } from "autumn-js/react";
export default function PurchaseButton() {
const { checkout } = useCustomer();
const handleCheckout = async () => {
await checkout({
productId: "your_product_id",
successUrl: `${window.location.origin}/success`,
// Add this to enable the back arrow in Stripe Checkout
checkoutSessionParams: {
cancel_url: window.location.href,
},
});
};
return (
<button onClick={handleCheckout}>
Upgrade Plan
</button>
);
}
###Why this works:
- The
checkoutfunction inautumn-jsallows you to passcheckoutSessionParams, which are passed directly to the Stripe API when creating the Checkout Session. - Stripe's Checkout API uses the
cancel_urlparameter to determine whether to show a back button and where it should point. - In the Autumn server implementation (
server/src/internal/customers/add-product/handleCreateCheckout.ts), thecheckoutSessionParamsare spread into the Stripe session creation call:let sessionParams: Stripe.Checkout.SessionCreateParams = { // ... other params ...(attachParams.checkoutSessionParams || {}), };
Note: Ensure you use cancel_url (snake_case) inside the checkoutSessionParams object to match what the Stripe API expects.
END OF POST
Log in to add a comment