Last active
March 8, 2019 20:12
-
-
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.
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
// 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¤cy=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