File tree 6 files changed +39
-17
lines changed
examples/ecommerce-netlify-functions
6 files changed +39
-17
lines changed Original file line number Diff line number Diff line change 2
2
3
3
module . exports = Object . freeze ( {
4
4
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'
6
8
} ) ;
Original file line number Diff line number Diff line change 2
2
3
3
module . exports = Object . freeze ( {
4
4
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
+
6
9
} ) ;
Original file line number Diff line number Diff line change 2
2
const mongoose = require ( 'mongoose' ) ;
3
3
4
4
const productSchema = new mongoose . Schema ( {
5
- productName : String ,
6
- productPrice : Number
5
+ name : String ,
6
+ price : Number ,
7
+ image : String
7
8
} ) ;
8
9
9
10
const Product = mongoose . model ( 'Product' , productSchema ) ;
Original file line number Diff line number Diff line change @@ -15,10 +15,12 @@ const handler = async(event) => {
15
15
if ( cart == null ) {
16
16
return { statusCode : 404 , body : JSON . stringify ( { message : 'Cart not found' } ) } ;
17
17
}
18
-
18
+ if ( ! Array . isArray ( event . body . items ) ) {
19
+ return { statusCode : 500 , body : JSON . stringify ( { error : 'items is not an array' } ) } ;
20
+ }
19
21
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 ( ) ) ) {
22
24
cart . items . push ( product ) ;
23
25
await cart . save ( ) ;
24
26
} else {
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const stripe = require ( '../../integrations/stripe' )
4
-
4
+ const config = require ( '../../.config' ) ;
5
5
const { Cart, Order, Product } = require ( '../../models' ) ;
6
6
const connect = require ( '../../connect' ) ;
7
7
@@ -19,19 +19,19 @@ const handler = async(event) => {
19
19
price_data : {
20
20
currency : 'usd' ,
21
21
product_data : {
22
- name : product . productName
22
+ name : product . name
23
23
} ,
24
- unit_amount : product . productPrice
24
+ unit_amount : product . price
25
25
} ,
26
26
quantity : cart . items [ i ] . quantity
27
27
} ) ;
28
- total = total + ( product . productPrice * cart . items [ i ] . quantity ) ;
28
+ total = total + ( product . price * cart . items [ i ] . quantity ) ;
29
29
}
30
30
const session = await stripe . checkout . sessions . create ( {
31
31
line_items : stripeProducts . line_items ,
32
32
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
35
35
} ) ;
36
36
const intent = await stripe . paymentIntents . retrieve ( session . payment_intent ) ;
37
37
if ( intent . status !== 'succeeded' ) {
Original file line number Diff line number Diff line change @@ -8,13 +8,27 @@ async function createProducts() {
8
8
await mongoose . connect ( config . mongodbUri ) ;
9
9
10
10
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'
13
14
} ) ;
14
15
15
16
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'
18
32
} ) ;
19
33
20
34
console . log ( 'done' ) ;
You can’t perform that action at this time.
0 commit comments