Skip to content

Commit

Permalink
made requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Jul 19, 2022
1 parent 5301deb commit 2e6b064
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
4 changes: 3 additions & 1 deletion examples/ecommerce-netlify-functions/.config/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

module.exports = Object.freeze({
mongodbUri: 'mongodb://localhost:27017/ecommerce',
stripeSecretKey: 'YOUR STRIPE KEY HERE'
stripeSecretKey: 'YOUR STRIPE KEY HERE',
success_url: 'localhost:3000/success',
cancel_url: 'localhost:3000/cancel'
});
5 changes: 4 additions & 1 deletion examples/ecommerce-netlify-functions/.config/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

module.exports = Object.freeze({
mongodbUri: 'mongodb://localhost:27017/ecommerce_test',
stripeSecretKey: 'test'
stripeSecretKey: 'test',
success_url: 'localhost:3000/success',
cancel_url: 'localhost:3000/cancel'

});
5 changes: 3 additions & 2 deletions examples/ecommerce-netlify-functions/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
const mongoose = require('mongoose');

const productSchema = new mongoose.Schema({
productName: String,
productPrice: Number
name: String,
price: Number,
image: String
});

const Product = mongoose.model('Product', productSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ const handler = async(event) => {
if (cart == null) {
return { statusCode: 404, body: JSON.stringify({ message: 'Cart not found' }) };
}

if(!Array.isArray(event.body.items)) {
return { statusCode: 500, body: JSON.stringify({ error: 'items is not an array' }) };
}
for (const product of event.body.items) {
const exists = cart.items.find(item => item.productId.toString() === product.productId.toString());
if (!exists && products.find(p => product.productId.toString() === p._id.toString())) {
const exists = cart.items.find(item => item?.productId?.toString() === product?.productId?.toString());
if (!exists && products.find(p => product?.productId?.toString() === p?._id?.toString())) {
cart.items.push(product);
await cart.save();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const stripe = require('../../integrations/stripe')

const config = require('../../.config');
const { Cart, Order, Product } = require('../../models');
const connect = require('../../connect');

Expand All @@ -19,19 +19,19 @@ const handler = async(event) => {
price_data: {
currency: 'usd',
product_data: {
name: product.productName
name: product.name
},
unit_amount: product.productPrice
unit_amount: product.price
},
quantity: cart.items[i].quantity
});
total = total + (product.productPrice * cart.items[i].quantity);
total = total + (product.price * cart.items[i].quantity);
}
const session = await stripe.checkout.sessions.create({
line_items: stripeProducts.line_items,
mode: 'payment',
success_url: 'insert a url here',
cancel_url: 'insert a url here'
success_url: config.success_url,
cancel_url: config.cancel_url
});
const intent = await stripe.paymentIntents.retrieve(session.payment_intent);
if (intent.status !== 'succeeded') {
Expand Down
22 changes: 18 additions & 4 deletions examples/ecommerce-netlify-functions/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ async function createProducts() {
await mongoose.connect(config.mongodbUri);

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

await Product.create({
productName: 'Another Dummy Product',
productPrice: 600
name: 'iPhone SE',
price: 600,
image: 'https://images.unsplash.com/photo-1529618160092-2f8ccc8e087b'
});

await Product.create({
name: 'iPhone 12 Pro',
price: 700,
image: 'https://images.unsplash.com/photo-1603921326210-6edd2d60ca68'
});

await Product.create({
name: 'iPhone 11',
price: 800,
image: 'https://images.unsplash.com/photo-1574755393849-623942496936'
});

console.log('done');
Expand Down

0 comments on commit 2e6b064

Please sign in to comment.