-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
10,990 additions
and
3,875 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
import stripePackage from "stripe"; | ||
import handler from "./libs/handler-lib"; | ||
import { calculateCost } from "./libs/billing-lib"; | ||
import { success, failure } from "./libs/response-lib"; | ||
|
||
export async function main(event, context) { | ||
export const main = handler(async (event, context) => { | ||
const { storage, source } = JSON.parse(event.body); | ||
const amount = calculateCost(storage); | ||
const description = "Scratch charge"; | ||
|
||
// Load our secret key from the environment variables | ||
const stripe = stripePackage(process.env.stripeSecretKey); | ||
|
||
try { | ||
await stripe.charges.create({ | ||
source, | ||
amount, | ||
description, | ||
currency: "usd" | ||
}); | ||
return success({ status: true }); | ||
} catch (e) { | ||
return failure({ message: e.message }); | ||
} | ||
} | ||
await stripe.charges.create({ | ||
source, | ||
amount, | ||
description, | ||
currency: "usd" | ||
}); | ||
return { status: true }; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import AWS from "aws-sdk"; | ||
import util from "util"; | ||
|
||
// Log AWS SDK calls | ||
AWS.config.logger = { log: debug }; | ||
|
||
let logs; | ||
let timeoutTimer; | ||
|
||
export function init(event, context) { | ||
logs = []; | ||
|
||
// Log API event | ||
debug("API event", { | ||
body: event.body, | ||
pathParameters: event.pathParameters, | ||
queryStringParameters: event.queryStringParameters, | ||
}); | ||
|
||
// Start timeout timer | ||
timeoutTimer = setTimeout(() => { | ||
timeoutTimer && flush(new Error("Lambda will timeout in 100 ms")); | ||
}, context.getRemainingTimeInMillis() - 100); | ||
} | ||
|
||
export function end() { | ||
// Clear timeout timer | ||
clearTimeout(timeoutTimer); | ||
timeoutTimer = null; | ||
} | ||
|
||
export function flush(e) { | ||
logs.forEach(({ date, string }) => console.debug(date, string)); | ||
console.error(e); | ||
} | ||
|
||
export default function debug() { | ||
logs.push({ | ||
date: new Date(), | ||
string: util.format.apply(null, arguments), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
export function call(action, params) { | ||
const dynamoDb = new AWS.DynamoDB.DocumentClient(); | ||
const client = new AWS.DynamoDB.DocumentClient(); | ||
|
||
return dynamoDb[action](params).promise(); | ||
} | ||
export default { | ||
get : (params) => client.get(params).promise(), | ||
put : (params) => client.put(params).promise(), | ||
query : (params) => client.query(params).promise(), | ||
update: (params) => client.update(params).promise(), | ||
delete: (params) => client.delete(params).promise(), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as debug from "./debug-lib"; | ||
|
||
export default function handler(lambda) { | ||
return function (event, context) { | ||
return Promise.resolve() | ||
// Start debugger | ||
.then(() => debug.init(event, context)) | ||
// Run the Lambda | ||
.then(() => lambda(event, context)) | ||
// On success | ||
.then((responseBody) => [200, responseBody]) | ||
// On failure | ||
.catch((e) => { | ||
// Print debug messages | ||
debug.flush(e); | ||
return [500, { error: e.message }]; | ||
}) | ||
// Return HTTP response | ||
.then(([statusCode, body]) => ({ | ||
statusCode, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Credentials": true, | ||
}, | ||
body: JSON.stringify(body), | ||
})) | ||
// Cleanup debugger | ||
.finally(debug.end); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.