Skip to content

Instantly share code, notes, and snippets.

@furiousdavid
Last active May 16, 2019 00:21
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 furiousdavid/4c84e379d4da3b6b6533100ecc321db6 to your computer and use it in GitHub Desktop.
Save furiousdavid/4c84e379d4da3b6b6533100ecc321db6 to your computer and use it in GitHub Desktop.
// formkeep-stripe-checkout.js on webtask.io
// Load NPM dependencies (specified in the settings)
const webtask = require('webtask-tools');
const express = require('express');
const axios = require('axios');
// Initialize an express app
const app = express();
// Add a single endpoint at the root, to handle the request
app.post('', (req, res) => {
// Grab FormKeep URL from secrets (added in settings)
const formkeepUrl = req.webtaskContext.secrets.formkeepUrl;
// Standard header to post JSON to the FormKeep endpoint
const formkeepHeaders = { "Content-Type": "application/json" };
// Some sample data to post for now
const formkeepData = {
email: 'test@example.com',
amount: `$10`,
};
// Post the data to FormKeep using axios
return axios.post(formkeepUrl, formkeepData, { headers: formkeepHeaders })
.then(function() {
// If it succeeds, redirect to the FormKeep thank you page
// (you can then add a different page to thank your donors)
res.redirect(301, `https://formkeep.com/thanks?h=Thanks&s=Your donation was received`);
})
.catch(function(err) {
// If it fails, redirect to the FormKeep thank you page
// (but with an error message)
console.error(err);
res.redirect(301, `https://formkeep.com/thanks?h=Sorry&s=Your payment couldn't be processed`);
});
});
// Use webtask helpers to export the express app as webtask serverless function
module.exports = webtask.fromExpress(app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment