Skip to content

Commit 57b6e8d

Browse files
committed
Update packages and lambda.
1 parent 58ca6c3 commit 57b6e8d

6 files changed

Lines changed: 634 additions & 818 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
src/bundle.js
2-
lambda/*
32
.netlify
43
node_modules
54
dist

lambda/package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lambda/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "lambda",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "purchase.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"stripe": "^6.30.0"
13+
}
14+
}

lambda/purchase.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require("dotenv").config();
2+
3+
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
4+
5+
const statusCode = 200;
6+
const headers = {
7+
"Access-Control-Allow-Origin": "*",
8+
"Access-Control-Allow-Headers": "Content-Type"
9+
};
10+
11+
exports.handler = async function(event, context, callback) {
12+
// We only care to do anything if this is our POST request.
13+
if (event.httpMethod !== "POST") {
14+
return {
15+
statusCode,
16+
headers,
17+
body: "This was not a POST request!"
18+
};
19+
}
20+
21+
// Parse the body contents into an object.
22+
const data = JSON.parse(event.body);
23+
24+
// Make sure we have all required data. Otherwise, get outta here.
25+
if (!data.token || !data.amount || !data.idempotency_key) {
26+
const message = "Required information is missing!";
27+
28+
console.error(message);
29+
30+
return {
31+
statusCode,
32+
headers,
33+
body: JSON.stringify({
34+
status: "failed",
35+
message
36+
})
37+
};
38+
}
39+
40+
try {
41+
const charge = await stripe.charges.create(
42+
{
43+
currency: "usd",
44+
amount: data.amount,
45+
source: data.token.id,
46+
receipt_email: data.token.email,
47+
description: `charge for a widget`
48+
},
49+
{
50+
idempotency_key: data.idempotency_key
51+
}
52+
);
53+
} catch (e) {
54+
console.error(e.message);
55+
56+
return {
57+
statusCode: 424,
58+
headers,
59+
body: JSON.stringify({
60+
status: "failed",
61+
message: e.message
62+
})
63+
};
64+
}
65+
66+
const status =
67+
charge === null || charge.status !== "succeeded" ? "failed" : charge.status;
68+
69+
return {
70+
statusCode,
71+
headers,
72+
body: JSON.stringify({
73+
status,
74+
message: "Charge successfully created!"
75+
})
76+
};
77+
};

0 commit comments

Comments
 (0)