Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 20fa1bb

Browse files
author
sw-yx
committed
add stripe subcription template
1 parent 67886c3 commit 20fa1bb

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const chalk = require("chalk");
2+
3+
module.exports = {
4+
name: "stripe-charge",
5+
description: "Stripe Charge: Charge a user with Stripe",
6+
async onComplete() {
7+
console.log(
8+
`${chalk.yellow("stripe-charge")} function created from template!`
9+
);
10+
if (!process.env.STRIPE_SECRET_KEY) {
11+
console.log(
12+
`note this function requires ${chalk.yellow(
13+
"STRIPE_SECRET_KEY"
14+
)} build environment variable set in your Netlify Site.`
15+
);
16+
let siteData = { name: "YOURSITENAMEHERE" };
17+
try {
18+
siteData = await this.netlify.api.getSite({
19+
siteId: this.netlify.site.id
20+
});
21+
} catch (e) {
22+
// silent error, not important
23+
}
24+
console.log(
25+
`Set it at: https://app.netlify.com/sites/${
26+
siteData.name
27+
}/settings/deploys#environment-variables (must have CD setup)`
28+
);
29+
}
30+
}
31+
};

src/functions-templates/js/stripe-subscription/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.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "stripe-subscription",
3+
"version": "1.0.0",
4+
"description": "netlify functions:create - Create a subscription with Stripe",
5+
"main": "stripe-subscription.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"netlify",
11+
"serverless",
12+
"apis",
13+
"stripe",
14+
"js"
15+
],
16+
"author": "Netlify",
17+
"license": "MIT",
18+
"dependencies": {
19+
"stripe": "^6.28.0"
20+
}
21+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// with thanks https://github.com/LukeMwila/stripe-subscriptions-backend/blob/master/stripe-api/index.ts
2+
3+
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
4+
5+
const respond = (fulfillmentText: any): any => {
6+
return {
7+
statusCode: 200,
8+
body: JSON.stringify(fulfillmentText),
9+
headers: {
10+
"Access-Control-Allow-Credentials": true,
11+
"Access-Control-Allow-Origin": "*",
12+
"Content-Type": "application/json"
13+
}
14+
};
15+
};
16+
17+
exports.handler = async function(event, context) {
18+
try {
19+
const incoming = JSON.parse(event.body);
20+
const { stripeToken, email, productPlan } = incoming;
21+
} catch (err) {
22+
console.error(`error with parsing function parameters: `, err);
23+
return {
24+
statusCode: 400,
25+
body: JSON.stringify(err)
26+
};
27+
}
28+
try {
29+
const data = await createCustomerAndSubscribeToPlan(
30+
stripeToken,
31+
email,
32+
productPlan
33+
);
34+
return respond(data);
35+
} catch (err) {
36+
return respond(err);
37+
}
38+
};
39+
40+
async function createCustomerAndSubscribeToPlan(
41+
stripeToken: string,
42+
email: string,
43+
productPlan: string
44+
) {
45+
// create a customer
46+
const customer = await stripe.customers.create({
47+
email: email,
48+
source: stripeToken
49+
});
50+
// retrieve created customer id to add customer to subscription plan
51+
const customerId = customer.id;
52+
// create a subscription for the newly created customer
53+
const subscription = await stripe.subscriptions.create({
54+
customer: customerId,
55+
items: [{ plan: productPlan }]
56+
});
57+
return subscription;
58+
}

0 commit comments

Comments
 (0)