This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sw-yx
committed
May 9, 2019
1 parent
67886c3
commit 20fa1bb
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/functions-templates/js/stripe-subscription/.netlify-function-template.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
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.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/functions-templates/js/stripe-subscription/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/functions-templates/js/stripe-subscription/stripe-subscription.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |