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

Commit

Permalink
add stripe subcription template
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed May 9, 2019
1 parent 67886c3 commit 20fa1bb
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const chalk = require("chalk");

module.exports = {
name: "stripe-charge",
description: "Stripe Charge: Charge a user with Stripe",
async onComplete() {
console.log(
`${chalk.yellow("stripe-charge")} function created from template!`
);
if (!process.env.STRIPE_SECRET_KEY) {
console.log(
`note this function requires ${chalk.yellow(
"STRIPE_SECRET_KEY"
)} build environment variable set in your Netlify Site.`
);
let siteData = { name: "YOURSITENAMEHERE" };
try {
siteData = await this.netlify.api.getSite({
siteId: this.netlify.site.id
});
} catch (e) {
// silent error, not important
}
console.log(
`Set it at: https://app.netlify.com/sites/${
siteData.name
}/settings/deploys#environment-variables (must have CD setup)`
);
}
}
};
39 changes: 39 additions & 0 deletions src/functions-templates/js/stripe-subscription/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/functions-templates/js/stripe-subscription/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "stripe-subscription",
"version": "1.0.0",
"description": "netlify functions:create - Create a subscription with Stripe",
"main": "stripe-subscription.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"netlify",
"serverless",
"apis",
"stripe",
"js"
],
"author": "Netlify",
"license": "MIT",
"dependencies": {
"stripe": "^6.28.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// with thanks https://github.com/LukeMwila/stripe-subscriptions-backend/blob/master/stripe-api/index.ts

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

const respond = (fulfillmentText: any): any => {
return {
statusCode: 200,
body: JSON.stringify(fulfillmentText),
headers: {
"Access-Control-Allow-Credentials": true,
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
}
};
};

exports.handler = async function(event, context) {
try {
const incoming = JSON.parse(event.body);
const { stripeToken, email, productPlan } = incoming;
} catch (err) {
console.error(`error with parsing function parameters: `, err);
return {
statusCode: 400,
body: JSON.stringify(err)
};
}
try {
const data = await createCustomerAndSubscribeToPlan(
stripeToken,
email,
productPlan
);
return respond(data);
} catch (err) {
return respond(err);
}
};

async function createCustomerAndSubscribeToPlan(
stripeToken: string,
email: string,
productPlan: string
) {
// create a customer
const customer = await stripe.customers.create({
email: email,
source: stripeToken
});
// retrieve created customer id to add customer to subscription plan
const customerId = customer.id;
// create a subscription for the newly created customer
const subscription = await stripe.subscriptions.create({
customer: customerId,
items: [{ plan: productPlan }]
});
return subscription;
}

0 comments on commit 20fa1bb

Please sign in to comment.