Skip to content

Instantly share code, notes, and snippets.

@ShariqAnsari88
Created December 18, 2022 21:14
Show Gist options
  • Save ShariqAnsari88/4a1a11deb1d45367404baf37dcced36b to your computer and use it in GitHub Desktop.
Save ShariqAnsari88/4a1a11deb1d45367404baf37dcced36b to your computer and use it in GitHub Desktop.
E-Commerce by JS Dev
("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.title,
},
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 };
} catch (error) {
ctx.response.status = 500;
return { error };
}
},
}));
Copy link

ghost commented Sep 7, 2023

bro i get error on line :const stripe = require("stripe")(process.env.STRIPE_KEY);

can u plzz help me

@ShubhamBohra0009
Copy link

bro i get error on line :const stripe = require("stripe")(process.env.STRIPE_KEY);

can u plzz help me

buddy you need to have an env file with the required key for this to work

@Sujayz22
Copy link

my env file is not being read

@Vivek-Pandit
Copy link

bro i get error on line :const stripe = require("stripe")(process.env.STRIPE_KEY);

can u plzz help me

@DreamyMedhi
Copy link

i got an error in the line:const stripe = require("stripe")(process.env.STRIPE_KEY);

@RahulKirtoniya
Copy link

bro i get error on line :const stripe = require("stripe")(process.env.STRIPE_KEY);
can u plzz help me

buddy you need to have an env file with the required key for this to work

("use strict");
const stripe = require("stripe")(process.env.STRIPE_KEY);

@yashdargude
Copy link

bro i get error on line :const stripe = require("stripe")(process.env.STRIPE_KEY);
can u plzz help me

buddy you need to have an env file with the required key for this to work

("use strict"); const stripe = require("stripe")(process.env.STRIPE_KEY);

still the same error
do help?

@ShubhamBohra0009
Copy link

bro i get error on line :const stripe = require("stripe")(process.env.STRIPE_KEY);
can u plzz help me

buddy you need to have an env file with the required key for this to work

("use strict"); const stripe = require("stripe")(process.env.STRIPE_KEY);

still the same error do help?

I don't know what exact error are you getting so I'll explain both.

Issue 1 : related to process.env.STRIPE_KEY
if you are getting this error then first try replacing it with the actual stripe key that is being pointed to the .env file.
If it solves the issue than it means the issue is with env file. Check if you have used right file name extension for env file. Check if the variable name in the env file exactly matches with what you're using.
And last but not least what most people does is that they don't restart the project/server after updating the env file. So restart it and check if it works or not.

Issue 2 : stripe
use command npm install stripe in case you've forgot to install it.

If the issue persists then do show the exact error you're getting for further assistance. 👍

@AyanBairagya
Copy link

i get the same error

@avinashmaurya86
Copy link

("use strict"); const stripe = require("stripe")(process.env.STRIPE_KEY);

getting error in this line
kindly help ASAP

@avinashmaurya86
Copy link

please help sir

@avinashmaurya86
Copy link

process.env.STRIPE_KEY

still the error not resolved

@RahulD05
Copy link

("use strict"); const stripe = require("stripe")(process.env.STRIPE_KEY);

getting error in this line kindly help ASAP

use // @ts-ignore above your require statement.

@RahulD05
Copy link

im getting this error, what should i do??
error

@professorSergio12
Copy link

i am getting the same error
Screenshot (158)

@himubey
Copy link

himubey commented Apr 2, 2024

If you get error try this
fixorder

@Ayanokoji-Kiyotaka-20
Copy link

Screenshot 2024-05-14 at 3 01 33 PM Screenshot 2024-05-14 at 3 03 04 PM Screenshot 2024-05-14 at 3 03 54 PM Screenshot 2024-05-14 at 3 05 22 PM Screenshot 2024-05-14 at 3 06 00 PM

Can anyone please help... I'm getting
AxiosError {message: 'Request failed with status code 401', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}

@RahulD05
Copy link

Screenshot 2024-05-14 at 3 01 33 PM Screenshot 2024-05-14 at 3 03 04 PM Screenshot 2024-05-14 at 3 03 54 PM Screenshot 2024-05-14 at 3 05 22 PM Screenshot 2024-05-14 at 3 06 00 PM
Can anyone please help... I'm getting AxiosError {message: 'Request failed with status code 401', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}

Plz provide the product.jsx page ss

@Ayanokoji-Kiyotaka-20
Copy link

Screenshot 2024-05-14 at 4 27 35 PM

@RahulD05
Copy link

Screenshot 2024-05-14 at 4 27 35 PM

Okay, seems like you're not getting the desired product images and data. Hopefully, The problem here lies in map function seens you are using map function in javascript you have to make sure to add optional chaining in order to fetch details from strapi. I'm attaching the screenshot of my code snippet to have better understanding. Feel free to ask if the problem still exists.
IMG-20240514-WA0007

@RahulD05
Copy link

Don't forget to cross check this component > Category.jsx page.
IMG-20240514-WA0008

@DarkHarsh
Copy link

for those getting error before and after stripe integration in order.js file
use // @ts-ignore above your require statement.
it works.

@professorSergio12
Copy link

If anyone who successfully hosts this website. please message me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment