Skip to content

Instantly share code, notes, and snippets.

@chandler767
Last active March 8, 2019 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chandler767/3e99607fabcfe9af62978b34634cecac to your computer and use it in GitHub Desktop.
Save chandler767/3e99607fabcfe9af62978b34634cecac to your computer and use it in GitHub Desktop.
Charge a customer token from a PubNub Function in a serverless environment using Stripe.
// DO NOT USE THIS CODE CLIENT SIDE. FOR A PUBNUB SERVERLESS ENV ONLY.
// Set the module event type to "On Request".
// Create a token with Stripe.JS: https://stripe.com/docs/stripe-js
// Or use a test token: token=tok_visa
export default (request, response) => {
const vault = require("vault");
const xhr = require("xhr");
const token = request.params.token;
return vault.get("stripe_secret_key").then((apiKey) => {
const http_options = {
"method": "POST",
"headers": {
"Authorization": "Bearer "+apiKey,
'Content-Type': 'application/x-www-form-urlencoded',
},
"body": "amount=100&currency=usd&source="+token,
};
return xhr.fetch("https://api.stripe.com/v1/charges", http_options).then((resp) => {
if (resp.status == 200) {
// Record that the user has paid and do something.
}
response.status = resp.status;
return response.send(resp);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment