Skip to content

Commit 2e6b064

Browse files
committed
made requested changes
1 parent 5301deb commit 2e6b064

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

examples/ecommerce-netlify-functions/.config/development.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
module.exports = Object.freeze({
44
mongodbUri: 'mongodb://localhost:27017/ecommerce',
5-
stripeSecretKey: 'YOUR STRIPE KEY HERE'
5+
stripeSecretKey: 'YOUR STRIPE KEY HERE',
6+
success_url: 'localhost:3000/success',
7+
cancel_url: 'localhost:3000/cancel'
68
});

examples/ecommerce-netlify-functions/.config/test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22

33
module.exports = Object.freeze({
44
mongodbUri: 'mongodb://localhost:27017/ecommerce_test',
5-
stripeSecretKey: 'test'
5+
stripeSecretKey: 'test',
6+
success_url: 'localhost:3000/success',
7+
cancel_url: 'localhost:3000/cancel'
8+
69
});

examples/ecommerce-netlify-functions/models.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
const mongoose = require('mongoose');
33

44
const productSchema = new mongoose.Schema({
5-
productName: String,
6-
productPrice: Number
5+
name: String,
6+
price: Number,
7+
image: String
78
});
89

910
const Product = mongoose.model('Product', productSchema);

examples/ecommerce-netlify-functions/netlify/functions/addToCart.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ const handler = async(event) => {
1515
if (cart == null) {
1616
return { statusCode: 404, body: JSON.stringify({ message: 'Cart not found' }) };
1717
}
18-
18+
if(!Array.isArray(event.body.items)) {
19+
return { statusCode: 500, body: JSON.stringify({ error: 'items is not an array' }) };
20+
}
1921
for (const product of event.body.items) {
20-
const exists = cart.items.find(item => item.productId.toString() === product.productId.toString());
21-
if (!exists && products.find(p => product.productId.toString() === p._id.toString())) {
22+
const exists = cart.items.find(item => item?.productId?.toString() === product?.productId?.toString());
23+
if (!exists && products.find(p => product?.productId?.toString() === p?._id?.toString())) {
2224
cart.items.push(product);
2325
await cart.save();
2426
} else {

examples/ecommerce-netlify-functions/netlify/functions/checkout.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const stripe = require('../../integrations/stripe')
4-
4+
const config = require('../../.config');
55
const { Cart, Order, Product } = require('../../models');
66
const connect = require('../../connect');
77

@@ -19,19 +19,19 @@ const handler = async(event) => {
1919
price_data: {
2020
currency: 'usd',
2121
product_data: {
22-
name: product.productName
22+
name: product.name
2323
},
24-
unit_amount: product.productPrice
24+
unit_amount: product.price
2525
},
2626
quantity: cart.items[i].quantity
2727
});
28-
total = total + (product.productPrice * cart.items[i].quantity);
28+
total = total + (product.price * cart.items[i].quantity);
2929
}
3030
const session = await stripe.checkout.sessions.create({
3131
line_items: stripeProducts.line_items,
3232
mode: 'payment',
33-
success_url: 'insert a url here',
34-
cancel_url: 'insert a url here'
33+
success_url: config.success_url,
34+
cancel_url: config.cancel_url
3535
});
3636
const intent = await stripe.paymentIntents.retrieve(session.payment_intent);
3737
if (intent.status !== 'succeeded') {

examples/ecommerce-netlify-functions/seed.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,27 @@ async function createProducts() {
88
await mongoose.connect(config.mongodbUri);
99

1010
await Product.create({
11-
productName: 'Dummy Product',
12-
productPrice: 500
11+
name: 'iPhone 12',
12+
price: 500,
13+
image: 'https://images.unsplash.com/photo-1611472173362-3f53dbd65d80'
1314
});
1415

1516
await Product.create({
16-
productName: 'Another Dummy Product',
17-
productPrice: 600
17+
name: 'iPhone SE',
18+
price: 600,
19+
image: 'https://images.unsplash.com/photo-1529618160092-2f8ccc8e087b'
20+
});
21+
22+
await Product.create({
23+
name: 'iPhone 12 Pro',
24+
price: 700,
25+
image: 'https://images.unsplash.com/photo-1603921326210-6edd2d60ca68'
26+
});
27+
28+
await Product.create({
29+
name: 'iPhone 11',
30+
price: 800,
31+
image: 'https://images.unsplash.com/photo-1574755393849-623942496936'
1832
});
1933

2034
console.log('done');

0 commit comments

Comments
 (0)