#dogge_pau

1 messages · Page 1 of 1 (latest)

glacial burrowBOT
silver hamlet
#

hi! do you have more details?

earnest turret
#

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

silver hamlet
#

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?

earnest turret
#

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

silver hamlet
#

yeah that is an older way of doing things

earnest turret
#

`("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 };
        }
      },
    }));`
silver hamlet
earnest turret
#

i tried but it didnt workoed in my case

#

that is my backend code

silver hamlet
#

what parts specifically don't work?

earnest turret
#

the code is working fine but i am not able to setup the redirect function and gettings hte url from session

silver hamlet
#

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(...) .

earnest turret
#

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, }, });

silver hamlet
#

i.e. console.log(res.data.redirect(303, res.data.stripeSession.url)) (like I said that line doesn't make sense on the frontend).

earnest turret
#

so i need to use it in backend ,if yes how do i do it?

silver hamlet
#

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?

earnest turret
#

okk

earnest turret
#

i am getting the checkout link in log

silver hamlet
#

great

earnest turret
#

now what should i do to redirect it to actual checkout page

silver hamlet
#

so then replace that log line with window.location.href = res.data.stripeSession.url

earnest turret
#

okk

#

and is ther UPI option in payment method available?

glacial burrowBOT
silver hamlet
#

we do not support UPI at the moment on Stripe, no.

earnest turret
#

oh