Skip to content

Instantly share code, notes, and snippets.

@royashbrook
Created November 9, 2020 02:01
Show Gist options
  • Save royashbrook/32fa19501aa018fd802d3f7a5d15a476 to your computer and use it in GitHub Desktop.
Save royashbrook/32fa19501aa018fd802d3f7a5d15a476 to your computer and use it in GitHub Desktop.
Google App Script for Stripe Client Keys
// this is a google script method of getting a client key from stripe
// this means you don't have to get a server to take stripe payments because
// this can be your server. at the time of this script creation, you are
// limited to 20k per month. in order to 'deploy' this you'll have to
// go to https://script.google.com/ and create a new script, replace the
// body of the initial script file with this and then deploy it as a web app
// you'll have to give it the required permissions it asks for the first time
// and ensure you set it up for anonymous access.
// this function will just return the secret key for the client from stripe
// so someone can pay you without needing a server.
function doGet(e) {
const opt = {
method: 'POST',
headers: {'Authorization': 'Bearer ReplaceThisTextWithTheStripeAPISecretKey'},
redirect: 'follow'
};
//ensure you update hte following line with the price and currency you want. you can change the post method if you want
//i just have the sample data from the stripe setup page in here
const uri = 'https://api.stripe.com/v1/payment_intents?amount=1099&currency=usd&metadata[integration_check]=accept_a_payment';
//this is just like a very basic key to protect the google app script.
// it's not required, but i wanted to just add a little protection so people couldn't randomly
// call the script. obviously if someone crawls your code, they can get this key
// but it's just to prevent spamming initially i suppose. i used a GUID here and just added ?myguid to the get request
const key = 'replace this with whatever you want, i used a GUID';
let rsp = ''
if(e && e.queryString && e.queryString === key){
let res = UrlFetchApp.fetch(uri, opt)
rsp = JSON.parse(res.getContentText()).client_secret
}
return ContentService
.createTextOutput(rsp)
.setMimeType(ContentService.MimeType.JSON)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment