#dogge_pau
1 messages · Page 1 of 1 (latest)
hi! do you have more details?
This functions runs when someone clicks on checkout button:
const handlePayment = async()=>{
try
{
const stripe = stripePromise;
const res = await makePaymentRequest.post("/api/orders",
{
products: cartItems,
})
console.log(res.data.redirect(303, res.data.stripeSession.url))
// await stripe.redirectToCheckout(
// {
// sessionId: res.data.stripeSession.id,
// })
}catch(error)
{
console.log(error)
}
}
i am getting type error undefined redirect
res.data.redirect(303, res.data.stripeSession.url)
I don't understand what the code is trying to do
it looks like that is backend code (for Node.js) but you put it in the frontend instead?
yes something liek that i myself dont know much as i am beginner and followed along a tutorial on yt but he used redirectToCheckout which isnt working anymore
yeah that is an older way of doing things
`("use strict");
const stripe = require("stripe")(process.env.STRIPE_KEY);
/**
- order controller
*/
const { createCoreController } = require("@strapi/strapi").factories;
module.exports = createCoreController("api::order.order", ({ strapi }) => ({
async create(ctx) {
const { products } = ctx.request.body;
try {
const lineItems = await Promise.all(
products.map(async (product) => {
const item = await strapi
.service("api::product.product")
.findOne(product.id);
return {
price_data: {
currency: "inr",
product_data: {
name: item.productname,
},
unit_amount: Math.round(item.price * 100),
},
quantity: product.attributes.quantity,
};
})
);
const session = await stripe.checkout.sessions.create({
shipping_address_collection: { allowed_countries: ["IN"] },
payment_method_types: ["card"],
mode: "payment",
success_url: process.env.CLIENT_URL + "/success",
cancel_url: process.env.CLIENT_URL + "?success=false",
line_items: lineItems,
});
await strapi
.service("api::order.order")
.create({ data: { products, stripeId: session.id } });
return { stripeSession: session };
// session.redirect(303, session.url);
} catch (error) {
ctx.response.status = 500;
return { error };
}
},
}));`
if you're a beginner maybe try using https://stripe.com/docs/no-code/payment-links
what parts specifically don't work?
the code is working fine but i am not able to setup the redirect function and gettings hte url from session
ok. So back on the frontend, if you do console.log(res.data.stripeSession.url) what value is that logging?
also what is the code for makePaymentRequest(...) .
its showing type error of undefined redirect instead of showing log
export const makePaymentRequest = axios.create({ baseURL: process.env.REACT_APP_DEV_URL, headers: { Authorization: "bearer " + process.env.REACT_APP_STRIPE_APP_KEY, }, });
remove the line that is throwing that error then
i.e. console.log(res.data.redirect(303, res.data.stripeSession.url)) (like I said that line doesn't make sense on the frontend).
so i need to use it in backend ,if yes how do i do it?
your backend code already seems to be mostly correct
so please just do what I asked : add console.log(res.data.stripeSession.url) (on the frontend) and see what value is that logging?
okk
i am getting the checkout link in log
great
now what should i do to redirect it to actual checkout page
so then replace that log line with window.location.href = res.data.stripeSession.url
we do not support UPI at the moment on Stripe, no.
oh