-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe-payment.js
77 lines (64 loc) · 2.09 KB
/
stripe-payment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use latest';
import express from 'express';
import { fromExpress } from 'webtask-tools';
import bodyParser from 'body-parser';
import stripe from 'stripe';
var firebase = require('firebase');
var config = {
apiKey: "AIzaSyAI0hjtt6DpgtboWpyOcWsh_Xn6BxVYjMY",
authDomain: "bobago-c7da5.firebaseapp.com",
databaseURL: "https://bobago-c7da5.firebaseio.com",
projectId: "bobago-c7da5",
Storagebucket: "bobago-c7da5.appspot.com",
messagingSenderId: "246035056974"
};
firebase.initializeApp(config);
var database = firebase.database();
bodyParser.urlencoded();
var app = express();
app.use(bodyParser.urlencoded());
app.post('/payment', (req,res) => {
var ctx = req.webtaskContext;
var STRIPE_SECRET_KEY = ctx.secrets.STRIPE_SECRET_KEY;
var token = req.body.id;
stripe(STRIPE_SECRET_KEY).customers.create({
email: "some email",
source: token,
}).then(function(customer) {
//save customer
database.ref("Customer ID").child(customer.email).set(customer.id);
database.ref("Users").child("hi").set("sucks");
stripe(STRIPE_SECRET_KEY).charges.create({
amount: req.query.amount,
currency: req.query.currency,
description: req.query.description,
customer: customer.id,
}, (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);