Skip to content

Commit 242dac3

Browse files
committed
Updating to serverless-bundle
1 parent 0c99287 commit 242dac3

File tree

8 files changed

+103
-16
lines changed

8 files changed

+103
-16
lines changed

billing.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import stripePackage from "stripe";
2+
import { calculateCost } from "./libs/billing-lib";
3+
import { success, failure } from "./libs/response-lib";
4+
5+
export async function main(event, context) {
6+
const { storage, source } = JSON.parse(event.body);
7+
const amount = calculateCost(storage);
8+
const description = "Scratch charge";
9+
10+
// Load our secret key from the environment variables
11+
const stripe = stripePackage(process.env.stripeSecretKey);
12+
13+
try {
14+
await stripe.charges.create({
15+
source,
16+
amount,
17+
description,
18+
currency: "usd"
19+
});
20+
return success({ status: true });
21+
} catch (e) {
22+
return failure({ message: e.message });
23+
}
24+
}

env.example

Lines changed: 0 additions & 12 deletions
This file was deleted.

libs/billing-lib.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function calculateCost(storage) {
2+
const rate = storage <= 10
3+
? 4
4+
: storage <= 100
5+
? 2
6+
: 1;
7+
8+
return rate * storage * 100;
9+
}

mocks/billing-event.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"body": "{\"source\":\"tok_visa\",\"storage\":21}",
3+
"requestContext": {
4+
"identity": {
5+
"cognitoIdentityId": "USER-SUB-1234"
6+
}
7+
}
8+
}

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"serverless-offline": "^5.3.3"
1919
},
2020
"dependencies": {
21+
"stripe": "^7.4.0",
2122
"uuid": "^3.3.2"
2223
}
2324
}

serverless.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ custom:
1414
stage: ${opt:stage, self:provider.stage}
1515
# Set the table name here so we can use it while testing locally
1616
tableName: ${self:custom.stage}-notes
17+
# Load our secret environment variables based on the current stage.
18+
# Fallback to default if it is not in prod.
19+
environment: ${file(env.yml):${self:custom.stage}, file(env.yml):default}
1720

1821
provider:
1922
name: aws
@@ -25,6 +28,7 @@ provider:
2528
# under process.env.
2629
environment:
2730
tableName: ${self:custom.tableName}
31+
stripeSecretKey: ${self:custom.environment.stripeSecretKey}
2832

2933
iamRoleStatements:
3034
- Effect: Allow
@@ -105,6 +109,15 @@ functions:
105109
cors: true
106110
authorizer: aws_iam
107111

112+
billing:
113+
handler: billing.main
114+
events:
115+
- http:
116+
path: billing
117+
method: post
118+
cors: true
119+
authorizer: aws_iam
120+
108121
# Create our resources with separate CloudFormation templates
109122
resources:
110123
# API Gateway Errors

tests/billing.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { calculateCost } from "../libs/billing-lib";
2+
3+
test("Lowest tier", () => {
4+
const storage = 10;
5+
6+
const cost = 4000;
7+
const expectedCost = calculateCost(storage);
8+
9+
expect(cost).toEqual(expectedCost);
10+
});
11+
12+
test("Middle tier", () => {
13+
const storage = 100;
14+
15+
const cost = 20000;
16+
const expectedCost = calculateCost(storage);
17+
18+
expect(cost).toEqual(expectedCost);
19+
});
20+
21+
test("Highest tier", () => {
22+
const storage = 101;
23+
24+
const cost = 10100;
25+
const expectedCost = calculateCost(storage);
26+
27+
expect(cost).toEqual(expectedCost);
28+
});

0 commit comments

Comments
 (0)