Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
94 lines (82 sloc) 3.04 KB
name: Stripe payment
author:
name: nachoesmite
link: https://github.com/nachoesmite
type: sample
order: 11
description: |
A webtask that can be used for submitting payments to Stripe. Includes a test form with a pay button.
note:
title: instructions
content: |
This template creates a Webtask with 2 routes.
`/` - Returns a test form with a pay button
`/payment` - Payment endpoint which invokes the Stripe API. Clients must send the payload in application/x-www-form-urlencoded format
* You must set a valid `STRIPE_SECRET_KEY` for the `payment` API to work.
* Set a valid `STRIPE_PUBLISHABLE_KEY` for the payment page to work.
HTML code snippet example to embed into your web-page / application.
```html
<form action="{url_to_your_webtask}&currency=USD&amount=2000&description=Test%20item" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{STRIPE_PUBLISHABLE_KEY}"
data-amount="2000"
data-name="Stripe.com"
data-description="Test Item"
data-image="/img/documentation/checkout/marketplace.png"
data-locale="auto">
</script>
</form>
```
More info about stripe checkout: https://stripe.com/docs/checkout
code:
es6: |
'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.use(bodyParser.json());
app.post('/payment', (req,res) => {
var ctx = req.webtaskContext;
var STRIPE_SECRET_KEY = ctx.secrets.STRIPE_SECRET_KEY;
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);
secrets:
STRIPE_PUBLISHABLE_KEY: 'REPLACE_WITH_STRIPE_PUBLISHABLE_KEY'
STRIPE_SECRET_KEY: 'REPLACE_WITH_STRIPE_SECRET_KEY'