How to redirect using TRPC?

I am generating stripe checkout url from trpc endpoint and want to redirect user to that page. Here is what I am doing right now
export const purchaseRouter = createTRPCRouter({
getUrl: publicProcedure
.input(z.object({ text: z.string() }))
.query(async ({ input, ctx }) => {
const host = ctx.req.headers.host;
if (!host) {
throw new Error("No host");
}
const session = await stripe.checkout.sessions.create({
line_items: [
{
price: "price_1N7vQ8CxIhQOHpG7clowFgAm",
quantity: 1,
},
],
mode: "payment",
success_url: `https://${host}/?success=true`,
cancel_url: `https://${host}/?canceled=true`,
});
ctx.res.redirect(303, session.url);
}),
});
export const purchaseRouter = createTRPCRouter({
getUrl: publicProcedure
.input(z.object({ text: z.string() }))
.query(async ({ input, ctx }) => {
const host = ctx.req.headers.host;
if (!host) {
throw new Error("No host");
}
const session = await stripe.checkout.sessions.create({
line_items: [
{
price: "price_1N7vQ8CxIhQOHpG7clowFgAm",
quantity: 1,
},
],
mode: "payment",
success_url: `https://${host}/?success=true`,
cancel_url: `https://${host}/?canceled=true`,
});
ctx.res.redirect(303, session.url);
}),
});
3 Replies
.snkt
.snktOP2y ago
but it's not working
.snkt
.snktOP2y ago
here is the error
Matvey
Matvey2y ago
You don't need to redirect the tRPC call, just return the url and redirect on the client with window.location.href = sessionUrl You're now redirecting the fetch() call, not the browser tab
Want results from more Discord servers?
Add your server