├── .gitignore ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var http = require("http"); 2 | 3 | const fetch = require("node-fetch"); 4 | 5 | const moment = require("moment"); 6 | 7 | require("dotenv").config(); 8 | 9 | const start_of_yesterday = moment().subtract(1, "days").startOf("day").unix(); 10 | 11 | const end_of_yesterday = moment().subtract(1, "days").endOf("day").unix(); 12 | 13 | let params = { 14 | limit: 100, 15 | start: start_of_yesterday, 16 | end: end_of_yesterday, 17 | }; 18 | 19 | const stripe_auth = { 20 | Authorization: `Bearer ${process.env.REACT_APP_STRIPE_KEY}`, 21 | }; 22 | const stripe_url = `https://api.stripe.com/v1/charges?limit=${params.limit}&created[gte]=${params.start}&created[lte]=${end_of_yesterday}`; 23 | 24 | console.log(stripe_url); 25 | 26 | const loadCustomers = async () => 27 | await fetch(stripe_url, { 28 | method: "GET", 29 | headers: stripe_auth, 30 | }) 31 | .then((res) => (res.ok ? res : Promise.reject(res))) 32 | .then((res) => res.json()) 33 | .then(function (charges) { 34 | const paid_charges = charges.data.filter(function (charge) { 35 | return charge.paid === true; 36 | }); 37 | const sum_charge_amount = function (charges, amount) { 38 | return charges.reduce(function (previous, current) { 39 | return previous + current[amount] / 100; 40 | }, 0); 41 | }; 42 | 43 | console.log(sum_charge_amount(paid_charges, "amount")); 44 | 45 | // console.log("🔫", paid_charges); 46 | 47 | return paid_charges; 48 | }); 49 | 50 | const hostname = "127.0.0.1"; 51 | const port = 3000; 52 | 53 | const server = http.createServer((req, res) => { 54 | res.statusCode = 200; 55 | res.setHeader("Content-Type", "text/plain"); 56 | loadCustomers(); 57 | res.end("Hello World"); 58 | }); 59 | 60 | server.listen(port, hostname, () => { 61 | console.log(`Server running at http://${hostname}:${port}/`); 62 | }); 63 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node__aws_lambda", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "data-uri-to-buffer": { 8 | "version": "4.0.0", 9 | "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz", 10 | "integrity": "sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==" 11 | }, 12 | "dotenv": { 13 | "version": "8.2.0", 14 | "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", 15 | "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" 16 | }, 17 | "fetch-blob": { 18 | "version": "3.1.4", 19 | "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.4.tgz", 20 | "integrity": "sha512-Eq5Xv5+VlSrYWEqKrusxY1C3Hm/hjeAsCGVG3ft7pZahlUAChpGZT/Ms1WmSLnEAisEXszjzu/s+ce6HZB2VHA==", 21 | "requires": { 22 | "node-domexception": "^1.0.0", 23 | "web-streams-polyfill": "^3.0.3" 24 | } 25 | }, 26 | "formdata-polyfill": { 27 | "version": "4.0.10", 28 | "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", 29 | "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", 30 | "requires": { 31 | "fetch-blob": "^3.1.2" 32 | } 33 | }, 34 | "moment": { 35 | "version": "2.26.0", 36 | "resolved": "https://registry.npmjs.org/moment/-/moment-2.26.0.tgz", 37 | "integrity": "sha512-oIixUO+OamkUkwjhAVE18rAMfRJNsNe/Stid/gwHSOfHrOtw9EhAY2AHvdKZ/k/MggcYELFCJz/Sn2pL8b8JMw==" 38 | }, 39 | "node-domexception": { 40 | "version": "1.0.0", 41 | "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", 42 | "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==" 43 | }, 44 | "node-fetch": { 45 | "version": "3.1.1", 46 | "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.1.1.tgz", 47 | "integrity": "sha512-SMk+vKgU77PYotRdWzqZGTZeuFKlsJ0hu4KPviQKkfY+N3vn2MIzr0rvpnYpR8MtB3IEuhlEcuOLbGvLRlA+yg==", 48 | "requires": { 49 | "data-uri-to-buffer": "^4.0.0", 50 | "fetch-blob": "^3.1.3", 51 | "formdata-polyfill": "^4.0.10" 52 | } 53 | }, 54 | "web-streams-polyfill": { 55 | "version": "3.2.0", 56 | "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.0.tgz", 57 | "integrity": "sha512-EqPmREeOzttaLRm5HS7io98goBgZ7IVz79aDvqjD0kYXLtFZTc0T/U6wHTPKyIjb+MdN7DFIIX6hgdBEpWmfPA==" 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node__aws_lambda", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "dotenv": "^8.2.0", 14 | "moment": "^2.26.0", 15 | "node-fetch": "^3.1.1" 16 | } 17 | } 18 | --------------------------------------------------------------------------------