How to use hosted checkout
View as MarkdownCreate a checkout session on your server, then redirect the shopper to the GoDaddy-hosted checkout URL.
Overview
Hosted checkout sends the shopper to a checkout page that GoDaddy hosts. Your server creates a checkout session with createCheckoutSession, returns the session url, and your page redirects the shopper to that URL.
The hosted page is the /c/:sessionId entrypoint. The session id is in the path and the session token is in the URL fragment (#token). That route doesn't create a session. It loads a session your server already created. Use the url the session returns and don't construct it yourself. The fragment carries the session token so it doesn't reach intermediate servers as a query parameter.
This page covers the redirect flow. Go to How to render embedded checkout if the shopper must stay on your site. Go to About online payments for how you confirm the payment after the shopper returns.
Prerequisites
The following prerequisites are required before you can redirect a shopper to hosted checkout:
- a server route that creates a checkout session and returns
{ url: session.url }
Go to How to create a checkout session to build the server route.
Redirect the shopper
The following procedure fetches the session and sends the shopper to hosted checkout.
-
Fetch the session from your endpoint:
const response = await fetch('/api/checkout-session', { method: 'POST' }); const data = await response.json(); if (!response.ok) { throw new Error(data.error ?? 'Failed to create checkout session'); } if (!data.url) { throw new Error('No checkout URL returned from API'); } -
Go to the session URL:
window.location.href = data.url;
Redirect from the server instead
Return Response.redirect(session.url, 302) (or res.redirect(302, session.url) in Express) immediately after createCheckoutSession and skip the fetch-and-redirect steps.
Handle success and cancel
After a successful payment, the shopper goes to your successUrl. After a cancel, the shopper goes to your returnUrl.
- Serve public HTTPS pages at both URLs. Checkout rejects loopback URLs at session create time, so local development needs a tunnel.
- Checkout stores
successUrlexactly as you sent it and expands nothing in it. The URL has to be final before you create the session. - To carry an order ID in
successUrl, create the draft order first and passdraftOrderId. UsinglineItemscreates the order during session create, so no ID exists before you call. - A declined or failed payment stays on the hosted checkout page, where the shopper might retry.
Go to How to create a checkout session for session create error messages and what to change.
Last updated on
How is this guide?