Making CartContext available in trpc?

Hi! Simple trpc/t3 noob question. I made a CartContext provider wrapped around my app, and now I would like trpc to create the Stripe checkout url based on the items in the cart. How can I access cart.items? I guess I have to do something in trpc.ts?
<SessionProvider session={session}>
<CartContext.Provider value={cartContext}>
<ErrorBoundary>
<Component {...pageProps} />
</ErrorBoundary>
</CartContext.Provider>
</SessionProvider>
);
};

export default api.withTRPC(MyApp);
<SessionProvider session={session}>
<CartContext.Provider value={cartContext}>
<ErrorBoundary>
<Component {...pageProps} />
</ErrorBoundary>
</CartContext.Provider>
</SessionProvider>
);
};

export default api.withTRPC(MyApp);
stripe router:
createShoppingCheckoutSession: publicProcedure.mutation(async ({ ctx }) => {
const { stripe, req, cart } = ctx;

console.log(cart); // Undefined for now

const line_items = cart.map((item) => ({
price: item.id,
quantity: item.quantity,
}));

const baseUrl =
env.NODE_ENV === "development"
? `http://${req?.headers.host ?? "localhost:3000"}`
: `https://${req?.headers.host ?? env.NEXTAUTH_URL}`;

const checkoutSession = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
mode: "payment",
line_items,
success_url: `${baseUrl}/thank-you?checkoutSuccess=true`,
cancel_url: `${baseUrl}/thank-you?checkoutCanceled=true`,
});

if (!checkoutSession) {
throw new Error("Could not create checkout session");
}

return { checkoutUrl: checkoutSession.url };
}),
createShoppingCheckoutSession: publicProcedure.mutation(async ({ ctx }) => {
const { stripe, req, cart } = ctx;

console.log(cart); // Undefined for now

const line_items = cart.map((item) => ({
price: item.id,
quantity: item.quantity,
}));

const baseUrl =
env.NODE_ENV === "development"
? `http://${req?.headers.host ?? "localhost:3000"}`
: `https://${req?.headers.host ?? env.NEXTAUTH_URL}`;

const checkoutSession = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
mode: "payment",
line_items,
success_url: `${baseUrl}/thank-you?checkoutSuccess=true`,
cancel_url: `${baseUrl}/thank-you?checkoutCanceled=true`,
});

if (!checkoutSession) {
throw new Error("Could not create checkout session");
}

return { checkoutUrl: checkoutSession.url };
}),
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server