Handle Stripe Payment via WebTask
var stripe = require('stripe');
var url = require('url');
var querystring = require('querystring');
module.exports = function (ctx, req, res) {
var amount = ctx.body.amount;
var currency = ctx.body.currency;
var description = ctx.body.description;
var productID = ctx.body.productID;
var token = ctx.body.stripeToken;
var email = ctx.body.stripeEmail;
var origin = req.headers.origin;
var meta = {
"amount": amount,
"currency": currency,
"description": description,
"productID": productID,
"stripeEmail": email,
"origin": origin,
// "host": req.headers.host,
// "headers": req.headers,
// "method": req.method,
// "param":req.param,
"url" : req.url,
};
function renderView(params) {
//change /stripe-payment below to your task name
return `
<html>
<head>
<meta http-equiv="refresh" content="10;url=${params.origin}" />
</head>
<body>
<h1>
${params.title}
</h1>
<h2>${params.desc}</h2>
<p>redirecting back ${params.origin} in 10 seconds... <a href="${params.origin}">Go back now</a></p>
</body>
`;
}
stripe(ctx.secrets.stripeSecretKey).charges.create({
amount: amount,
currency: currency,
source: token,
description: description,
metadata: meta,
receipt_email: email,
}, function (error, charge) {
var status;
var title;
var desc;
if (error) {
status = 400;
title = "Error occurred, payment was cancelled";
desc = error.message;
} else {
status = 200;
title = `Thanks for Purchasing ${description} (${productID})`;
desc = `We will send the info for your purchase to ${email} shortly.`;
}
res.writeHead(status, { 'Content-Type': 'text/html' });
return res.end(renderView({
title: title,
desc:desc,
origin: origin,
}));
});
//return res.end(JSON.stringify(dict));
};