Skip to content

Commit a6746e0

Browse files
Replace checkout.finalize({ redirectUrl }) with checkout.finalize({ navigate }) (#2729)
Co-authored-by: Alexis Aguilar <[email protected]>
1 parent 3b64640 commit a6746e0

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

docs/guides/development/custom-flows/billing/checkout-existing-payment-method.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This guide will walk you through how to build a custom user interface for a chec
4141
'use client'
4242
import * as React from 'react'
4343
import { SignedIn, ClerkLoaded } from '@clerk/nextjs'
44+
import { useRouter } from 'next/navigation'
4445
import { CheckoutProvider, useCheckout, usePaymentMethods } from '@clerk/nextjs/experimental'
4546
import { useMemo, useState } from 'react'
4647

@@ -99,6 +100,8 @@ This guide will walk you through how to build a custom user interface for a chec
99100
const [isProcessing, setIsProcessing] = useState(false)
100101
const [paymentMethodId, setPaymentMethodId] = useState<string | null>(null)
101102

103+
const router = useRouter()
104+
102105
const defaultMethod = useMemo(() => data?.find((method) => method.isDefault), [data])
103106

104107
const submitSelectedMethod = async () => {
@@ -110,7 +113,9 @@ This guide will walk you through how to build a custom user interface for a chec
110113
// Confirm checkout with payment method
111114
await confirm({ paymentSourceId })
112115
// Complete checkout and redirect
113-
finalize({ redirectUrl: '/dashboard' })
116+
await finalize({
117+
navigate: () => router.push('/dashboard'),
118+
})
114119
} catch (error) {
115120
console.error('Payment failed:', error)
116121
} finally {

docs/guides/development/custom-flows/billing/checkout-new-payment-method.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ For the custom flow that allows users to add a new payment method to their accou
5252
PaymentElement,
5353
usePaymentElement,
5454
} from '@clerk/nextjs/experimental'
55+
import { useRouter } from 'next/navigation'
5556

5657
export default function CheckoutPage() {
5758
return (
@@ -106,6 +107,8 @@ For the custom flow that allows users to add a new payment method to their accou
106107
const { isFormReady, submit } = usePaymentElement()
107108
const [isProcessing, setIsProcessing] = React.useState(false)
108109

110+
const router = useRouter()
111+
109112
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
110113
e.preventDefault()
111114
if (!isFormReady || isProcessing) return
@@ -121,7 +124,9 @@ For the custom flow that allows users to add a new payment method to their accou
121124
// Confirm checkout with payment method
122125
await confirm(data)
123126
// Complete checkout and redirect
124-
finalize({ redirectUrl: '/dashboard' })
127+
await finalize({
128+
navigate: () => router.push('/dashboard'),
129+
})
125130
} catch (error) {
126131
console.error('Payment failed:', error)
127132
} finally {

docs/reference/hooks/use-checkout.mdx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ There are two ways to use `useCheckout()`:
152152
---
153153

154154
- `finalize()`
155-
- `(params?: { redirectUrl: string }) => void`
155+
- `(params?: { navigate?: SetActiveNavigate }) => void`
156156

157-
A function that finalizes the checkout process. Can optionally accept a `redirectUrl` to navigate the user to upon completion.
157+
A function that finalizes the checkout process. Can optionally accept a `navigate()` function to redirect the user after completion.
158158

159159
---
160160

@@ -226,6 +226,7 @@ The `useCheckout()` hook can be used with a context provider for managing state
226226
'use client'
227227

228228
import { useCheckout } from '@clerk/nextjs/experimental'
229+
import { useRouter } from 'next/navigation'
229230

230231
export function CheckoutFlow() {
231232
const { checkout } = useCheckout()
@@ -262,6 +263,8 @@ The `useCheckout()` hook can be used with a context provider for managing state
262263
const [isProcessing, setIsProcessing] = useState(false)
263264
const [paymentMethodId, setPaymentMethodId] = useState<string | null>(null)
264265

266+
const router = useRouter()
267+
265268
const submitSelectedMethod = async () => {
266269
if (isProcessing || !paymentMethodId) return
267270
setIsProcessing(true)
@@ -273,7 +276,9 @@ The `useCheckout()` hook can be used with a context provider for managing state
273276
})
274277
// Calling `.finalize` enables you to sync the client-side state with the server-side state of your users.
275278
// It revalidates all authorization checks computed within server components.
276-
finalize({ redirectUrl: '/dashboard' })
279+
await finalize({
280+
navigate: () => router.push('/dashboard'),
281+
})
277282
} catch (error) {
278283
console.error('Payment failed:', error)
279284
} finally {

docs/reference/hooks/use-payment-methods.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,23 @@ The following example demonstrates how to use `usePaymentMethods()` in a checkou
198198

199199
```tsx
200200
import { usePaymentMethods, useCheckout } from '@clerk/nextjs/experimental'
201+
import { useRouter } from 'next/navigation'
201202

202203
function CheckoutPaymentSelection() {
203204
const { data, isLoading } = usePaymentMethods({ for: 'user' })
204205
const { checkout } = useCheckout()
205206
const { confirm, finalize } = checkout
206207

208+
const router = useRouter()
209+
207210
const handlePaymentSubmit = async (paymentMethodId: string) => {
208211
try {
209212
// Confirm checkout with selected payment method
210213
await confirm({ paymentSourceId: paymentMethodId })
211214
// Complete checkout and redirect
212-
finalize({ redirectUrl: '/dashboard' })
215+
await finalize({
216+
navigate: () => router.push('/dashboard'),
217+
})
213218
} catch (error) {
214219
console.error('Payment failed:', error)
215220
}

0 commit comments

Comments
 (0)