File tree Expand file tree Collapse file tree 6 files changed +39
-17
lines changed
examples/ecommerce-netlify-functions Expand file tree Collapse file tree 6 files changed +39
-17
lines changed Original file line number Diff line number Diff line change 22
33module . 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} ) ;
Original file line number Diff line number Diff line change 22
33module . 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} ) ;
Original file line number Diff line number Diff line change 22const mongoose = require ( 'mongoose' ) ;
33
44const productSchema = new mongoose . Schema ( {
5- productName : String ,
6- productPrice : Number
5+ name : String ,
6+ price : Number ,
7+ image : String
78} ) ;
89
910const Product = mongoose . model ( 'Product' , productSchema ) ;
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change 11'use strict' ;
22
33const stripe = require ( '../../integrations/stripe' )
4-
4+ const config = require ( '../../.config' ) ;
55const { Cart, Order, Product } = require ( '../../models' ) ;
66const 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' ) {
Original file line number Diff line number Diff 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' ) ;
You can’t perform that action at this time.
0 commit comments