Skip to content

Commit 0063807

Browse files
committed
lambda function
1 parent bfa5bc8 commit 0063807

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
# dependencies
44
/node_modules
5-
/.pnp
6-
.pnp.js
75

86
# testing
97
/coverage
108

119
# production
1210
/build
11+
/lambda
1312

1413
# misc
1514
.DS_Store
15+
.env
1616
.env.local
1717
.env.development.local
1818
.env.test.local

src/lambda/charge.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const stripe = require('stripe')('sk_test_yaKbjP7rkSGdMeQtQvTMx4cG');
2+
3+
exports.handler = (event, context, callback) => {
4+
// This will allow us to freeze open connections to a database
5+
// context.callbackWaitsForEmptyEventLoop = false;
6+
7+
// Only allow POST
8+
if (event.httpMethod !== 'POST') {
9+
return callback(null, { statusCode: 405, body: 'Method Not Allowed' });
10+
}
11+
12+
const data = JSON.parse(event.body);
13+
14+
if (!data.token || parseInt(data.amount) < 1) {
15+
return callback(null, {
16+
statusCode: 400,
17+
body: JSON.stringify({
18+
message: 'Some required fields were not supplied.',
19+
}),
20+
});
21+
}
22+
23+
stripe.charges
24+
.create({
25+
amount: parseInt(data.amount),
26+
currency: 'usd',
27+
description: 'Dreamcast game shop',
28+
source: data.token,
29+
})
30+
.then(({ status }) => {
31+
return callback(null, {
32+
statusCode: 200,
33+
body: JSON.stringify({ status }),
34+
});
35+
})
36+
.catch(err => {
37+
return callback(null, {
38+
statusCode: 400,
39+
body: JSON.stringify({
40+
message: `Error: ${err.message}`,
41+
}),
42+
});
43+
});
44+
};

0 commit comments

Comments
 (0)