Created
July 31, 2018 20:17
-
-
Save glennblock/bc4f730e6cec23c0d45925c109c99fec to your computer and use it in GitHub Desktop.
Stripe task
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
'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