├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── README.md ├── files └── firestore │ └── product.json ├── package.json ├── tsconfig.json └── uploader.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | files/service_key.json -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "pwa-node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "skipFiles": ["/**"], 12 | "program": "${workspaceFolder}\\app\\server.js", 13 | "preLaunchTask": "tsc: build - tsconfig.json", 14 | "outFiles": ["${workspaceFolder}/app/**/*.js"] 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "typescript", 6 | "tsconfig": "tsconfig.json", 7 | "option": "watch", 8 | "problemMatcher": ["$tsc-watch"], 9 | "group": "build", 10 | "label": "tsc: watch - tsconfig.json" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Backend for ecommerce app 2 | 3 | This is just a starter repo for stripe backend integration. 4 | Check out the video for the code [Youtube](https://youtu.be/ElgfjrWn7Mg) 5 | 6 | ## Firestore service key for admin access 7 | 8 | You need to create a private key from firebase. Goto project settings --> service accounts --> click on `Generate new private key`. 9 | Place the downloaded file under `files/` folder with name `service_key.json` 10 | 11 | ## Running 12 | 13 | For compiling ts to js use. 14 | 15 | ```bash 16 | tsc -w 17 | ``` 18 | 19 | For staring the node sever. 20 | 21 | ```bash 22 | nodemon ./app/server.js 23 | ``` 24 | 25 | ## Stripe testing 26 | 27 | For local testing run the following command. This will forward the stripe events to your localhost node sever. (Only required for local testing) 28 | 29 | ```bash 30 | stripe listen --forward-to localhost:4242/webhook 31 | ``` 32 | 33 | ## Firestore products 34 | 35 | Use the `uploader.js` to quickly populate the products for the e-commerce app. You need to have the `service_key.json` file. 36 | 37 | ```bash 38 | node uploader.js 39 | ``` 40 | -------------------------------------------------------------------------------- /files/firestore/product.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "category": "jewelery", 4 | "description": "Classic Created Wedding Engagement Solitaire Diamond Promise Ring for Her. Gifts to spoil your love more for Engagement, Wedding, Anniversary, Valentine's Day...", 5 | "id": "7", 6 | "image": "https://images.unsplash.com/photo-1611200047983-8df87804e582?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80", 7 | "price": 9, 8 | "title": "Black shoe" 9 | }, 10 | { 11 | "category": "women's clothing", 12 | "description": "95% RAYON 5% SPANDEX, Made in USA or Imported, Do Not Bleach, Lightweight fabric with great stretch for comfort, Ribbed on sleeves and neckline / Double stitching on bottom hem", 13 | "id": "18", 14 | "image": "https://images.unsplash.com/photo-1597248881519-db089d3744a5?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1050&q=80", 15 | "price": 9, 16 | "title": "Yellow shoe" 17 | }, 18 | { 19 | "category": "Coffee", 20 | "description": "100% POLYURETHANE(shell) 100% POLYESTER(lining) 75% POLYESTER 25% COTTON (SWEATER), Faux leather material for style and comfort / 2 pockets of front, 2-For-One Hooded denim style faux leather jacket, Button detail on waist / Detail stitching at sides, HAND WASH ONLY / DO NOT BLEACH / LINE DRY / DO NOT IRON", 21 | "id": "16", 22 | "image": "https://images.unsplash.com/photo-1559056199-641a0ac8b55e?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1050&q=80", 23 | "price": 29, 24 | "title": "Coffee" 25 | }, 26 | { 27 | "category": "Shoes", 28 | "description": "From our Legends Collection, the Naga was inspired by the mythical water dragon that protects the ocean's pearl. Wear facing inward to be bestowed with love and abundance, or outward for protection.", 29 | "id": "5", 30 | "image": "https://images.unsplash.com/photo-1618329340733-ab4554d28b15?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=903&q=80", 31 | "price": 695, 32 | "title": "Yellow shoes" 33 | }, 34 | { 35 | "category": "Shoes", 36 | "description": "Expand your PS4 gaming experience, Play anywhere Fast and easy, setup Sleek design with high capacity, 3-year manufacturer's limited warranty", 37 | "id": "12", 38 | "image": "https://images.unsplash.com/photo-1618860202521-71e9f4bdcfaa?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=967&q=80", 39 | "price": 114, 40 | "title": "Red shoe" 41 | }, 42 | { 43 | "category": "electronics", 44 | "description": "3D NAND flash are applied to deliver high transfer speeds Remarkable transfer speeds that enable faster bootup and improved overall system performance. The advanced SLC Cache Technology allows performance boost and longer lifespan 7mm slim design suitable for Ultrabooks and Ultra-slim notebooks. Supports TRIM command, Garbage Collection technology, RAID, and ECC (Error Checking & Correction) to provide the optimized performance and enhanced reliability.", 45 | "id": "11", 46 | "image": "https://images.unsplash.com/photo-1583394838336-acd977736f90?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=631&q=80", 47 | "price": 109, 48 | "title": "Headset" 49 | }, 50 | { 51 | "category": "Stationery", 52 | "description": "Note:The Jackets is US standard size, Please choose size as your usual wear Material: 100% Polyester; Detachable Liner Fabric: Warm Fleece. Detachable Functional Liner: Skin Friendly, Lightweigt and Warm.Stand Collar Liner jacket, keep you warm in cold weather. Zippered Pockets: 2 Zippered Hand Pockets, 2 Zippered Pockets on Chest (enough to keep cards or keys)and 1 Hidden Pocket Inside.Zippered Hand Pockets and Hidden Pocket keep your things secure. Humanized Design: Adjustable and Detachable Hood and Adjustable cuff to prevent the wind and water,for a comfortable fit. 3 in 1 Detachable Design provide more convenience, you can separate the coat and inner as needed, or wear it together. It is suitable for different season and help you adapt to different climates", 53 | "id": "15", 54 | "image": "https://images.unsplash.com/photo-1583485088034-697b5bc54ccd?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=623&q=80", 55 | "price": 56, 56 | "title": "Pen" 57 | }, 58 | { 59 | "category": "electronics", 60 | "description": "100% Polyester, Machine wash, 100% cationic polyester interlock, Machine Wash & Pre Shrunk for a Great Fit, Lightweight, roomy and highly breathable with moisture wicking fabric which helps to keep moisture away, Soft Lightweight Fabric with comfortable V-neck collar and a slimmer fit, delivers a sleek, more feminine silhouette and Added Comfort", 61 | "id": "19", 62 | "image": "https://images.unsplash.com/photo-1546868871-7041f2a55e12?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=700&q=80", 63 | "price": 7, 64 | "title": "watch" 65 | }, 66 | { 67 | "category": "bycycle", 68 | "description": "Satisfaction Guaranteed. Return or exchange any order within 30 days.Designed and sold by Hafeez Center in the United States. Satisfaction Guaranteed. Return or exchange any order within 30 days.", 69 | "id": "6", 70 | "image": "https://images.unsplash.com/photo-1532298229144-0ec0c57515c7?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MzJ8fHByb2R1Y3R8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=60", 71 | "price": 168, 72 | "title": "Cycle" 73 | }, 74 | { 75 | "category": "men's clothing", 76 | "description": "Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.", 77 | "id": "2", 78 | "image": "https://images.unsplash.com/photo-1616740540792-3daec604777d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=700&q=80", 79 | "price": 22, 80 | "title": "Water bottle" 81 | }, 82 | { 83 | "category": "Headphones", 84 | "description": "95%Cotton,5%Spandex, Features: Casual, Short Sleeve, Letter Print,V-Neck,Fashion Tees, The fabric is soft and has some stretch., Occasion: Casual/Office/Beach/School/Home/Street. Season: Spring,Summer,Autumn,Winter.", 85 | "id": "20", 86 | "image": "https://images.unsplash.com/photo-1505751104546-4b63c93054b1?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTcyfHxwcm9kdWN0fGVufDB8fDB8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=60", 87 | "price": 12, 88 | "title": "Sony headset" 89 | }, 90 | { 91 | "category": "Hair care", 92 | "description": "USB 3.0 and USB 2.0 Compatibility Fast data transfers Improve PC Performance High Capacity; Compatibility Formatted NTFS for Windows 10, Windows 8.1, Windows 7; Reformatting may be required for other operating systems; Compatibility may vary depending on user’s hardware configuration and operating system", 93 | "id": "9", 94 | "image": "https://images.unsplash.com/photo-1620915789336-129994d5efc8?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80", 95 | "price": 64, 96 | "title": "Shampoo" 97 | } 98 | ] 99 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "firestore-server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "scripts": { 6 | "start": "nodemon --watch app app/server.js", 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "stripe forward": "stripe listen --forward-to localhost:4242/webhook" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "body-parser": "^1.19.0", 15 | "express": "^4.17.1", 16 | "firebase": "^8.6.3", 17 | "firebase-admin": "^10.0.2", 18 | "stripe": "^8.216.0" 19 | }, 20 | "devDependencies": { 21 | "concurrently": "^6.2.0", 22 | "nodemon": "^2.0.7", 23 | "ts-node": "^10.0.0", 24 | "ts-node-dev": "^1.1.6" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNext", 4 | "module": "commonjs", 5 | "sourceMap": true, 6 | "skipLibCheck": true, 7 | "outDir": "./app", 8 | "rootDir": "./src", 9 | "resolveJsonModule": true 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /uploader.js: -------------------------------------------------------------------------------- 1 | var admin = require('firebase-admin'); 2 | 3 | var serviceAccount = require('./files/service_key.json'); 4 | 5 | admin.initializeApp({ 6 | credential: admin.credential.cert(serviceAccount), 7 | }); 8 | 9 | const firestore = admin.firestore(); 10 | const path = require('path'); 11 | const fs = require('fs'); 12 | const directoryPath = path.join(__dirname, '/files/firestore'); 13 | 14 | fs.readdir(directoryPath, function (err, files) { 15 | if (err) { 16 | return console.log('Unable to scan directory: ' + err); 17 | } 18 | 19 | files.forEach(function (file) { 20 | var lastDotIndex = file.lastIndexOf('.'); 21 | 22 | var menu = require('./files/firestore/' + file); 23 | 24 | menu.forEach(function (obj) { 25 | firestore 26 | .collection(file.substring(0, lastDotIndex)) 27 | .add(obj) 28 | .then(function (docRef) { 29 | console.log('Document written'); 30 | }) 31 | .catch(function (error) { 32 | console.error('Error adding document: ', error); 33 | }); 34 | }); 35 | }); 36 | }); 37 | --------------------------------------------------------------------------------