Skip to content

Instantly share code, notes, and snippets.

@glennblock
Created April 11, 2018 17:25
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 glennblock/39714c590929fc962219f8bff28d3c3a to your computer and use it in GitHub Desktop.
Save glennblock/39714c590929fc962219f8bff28d3c3a to your computer and use it in GitHub Desktop.
'use latest';
import express from 'express';
import { fromExpress } from 'webtask-tools';
import bodyParser from 'body-parser';
import stripe from 'stripe';
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/payment', (req,res) => {
var ctx = req.webtaskContext;
var STRIPE_SECRET_KEY = ctx.secrets.STRIPE_SECRET_KEY;
console.log(req.body);
stripe(STRIPE_SECRET_KEY).charges.create({
amount: req.query.amount,
currency: req.query.currency,
source: req.body.stripeToken,
description: req.query.description
}, (err, charge) => {
const status = err ? 400 : 200;
const message = err ? err.message : 'Payment done!';
res.writeHead(status, { 'Content-Type': 'text/html' });
return res.end('<h1>' + message + '</h1>');
});
});
// comment this to disable the test form
app.get('/', (req, res) => {
var ctx = req.webtaskContext;
res.send(renderView(ctx));
});
function renderView(ctx) {
//change /stripe-payment below to your task name
return `
<form action="/stripe-payment/payment?currency=USD&amount=2000&description=Test%20item" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="${ctx.secrets.STRIPE_PUBLISHABLE_KEY}"
data-amount="2000"
data-name="Stripe.com"
data-description="Test item"
data-locale="auto">
</script>
</form>
`;
}
module.exports = fromExpress(app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment