Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netlify functions example #11892

Merged
merged 26 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fa7f196
without stripe
IslandRhythms Jun 3, 2022
200668b
not entirely sure how to redirect
IslandRhythms Jun 3, 2022
92787e8
Update checkout.js
IslandRhythms Jun 3, 2022
c9baa82
All thats left is checkout
IslandRhythms Jun 6, 2022
e865463
not done yet
IslandRhythms Jun 7, 2022
062ca30
added checkout test
IslandRhythms Jun 13, 2022
d983d33
created fixtures
IslandRhythms Jul 1, 2022
ad97863
fix addToCart test
IslandRhythms Jul 1, 2022
e852249
fix getCart test
IslandRhythms Jul 1, 2022
a85e5a5
fix getProducts test and remove test data on test completion
IslandRhythms Jul 1, 2022
9a8fad2
fix removeFromCart and removeFromCart test
IslandRhythms Jul 1, 2022
e89b2f9
almost done
IslandRhythms Jul 5, 2022
0d2d299
sinon not doing sinon things
IslandRhythms Jul 5, 2022
3c87f27
not getting sinon error messages
IslandRhythms Jul 5, 2022
6dec0c5
thanks val
IslandRhythms Jul 5, 2022
b693176
Update index.js
IslandRhythms Jul 5, 2022
dfb75b4
add
IslandRhythms Jul 6, 2022
115f9fa
need to fix tests
IslandRhythms Jul 6, 2022
41712f1
tests fixed
IslandRhythms Jul 6, 2022
28b5557
added dummy product script
IslandRhythms Jul 6, 2022
42f40d5
made tests work with live build
IslandRhythms Jul 7, 2022
118c97a
Merge branch 'master' into netlify-functions-example
vkarpov15 Jul 16, 2022
5301deb
fix: cleanup and various updates
vkarpov15 Jul 17, 2022
2e6b064
made requested changes
IslandRhythms Jul 19, 2022
eced2c7
Merge branch 'master' into netlify-functions-example
vkarpov15 Jul 20, 2022
2751883
fix tests
vkarpov15 Jul 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/ecommerce-netlify-functions/.config/development.js
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',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 camelCase, no snake_case

cancel_url: 'localhost:3000/cancel'
});
5 changes: 4 additions & 1 deletion examples/ecommerce-netlify-functions/.config/test.js
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
Expand Up @@ -2,8 +2,9 @@
const mongoose = require('mongoose');

const productSchema = new mongoose.Schema({
productName: String,
productPrice: Number
name: String,
price: Number,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't update tests to reflect this change

image: String
});

const Product = mongoose.model('Product', productSchema);
Expand Down
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) {
IslandRhythms marked this conversation as resolved.
Show resolved Hide resolved
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
@@ -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
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