This tutorial shows how to create a Stripe charge using Functions. Use cases for this Function include selling products or services, restricting access to a web page, selling a file, and accepting donations.
Functions is a serverless environment for executing functions. Functions are JavaScript event handlers that can be executed on in-transit PubNub messages or in the request/response style of a RESTful API over HTTPS. If you are familiar with Node.js and Express, the On Request event handler development will be second nature.
Here are a few things you could use Functions to do:
Advantages of Functions:
Check out the Stripe Charge Block for a faster way to get started.
Go to your PubNub Admin Dashboard and select your app. Then click on the keyset that was created for that app.
Next, click “Functions” in the sidebar and then create a new module named “Stripe”.
Create a function with the name “Stripe”, set the event type to “On Request”, and set the path to “stripe”.
Delete any code in the editor and replace it with a function to create a Stripe charge:
// 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); }); }); };
You’ll need to add your Stripe secret key to the PubNub vault. Click on “My Secrets” in the function editor and add a key named “stripe_secret_key” with your Stripe secret key.
Edit the query string to change the currency and charge amount.
Use a Stripe test token to test the function.
Use Curl from your Terminal to send a test request to the function:
YOUR_SUB_KEY_HERE
” with your Subscribe Key from your PubNub Admin Dashboard.curl -X POST https://pubsub.pubnub.com/v1/blocks/sub-key/YOUR_SUB_KEY_HERE/stripe?token=tok_visa
The easiest way to create card input form you can use with this function is by using Stripe Checkout.
Here are a few ideas:
Have suggestions or questions about the content of this post? Reach out at devrel@pubnub.com.
There are common underlying technologies for a dating app, and in this post, we’ll talk about the major technologies and designs...
Michael Carroll
How to use geohashing, JavaScript, Google Maps API, and BART API to build a real-time public transit schedule app.
Michael Carroll
How to track and stream real-time vehicle location on a live-updating map using EON, JavaScript, and the Mapbox API.
Michael Carroll