Skip to content

Commit 424f6a6

Browse files
committed
Fix Stripe donation page
1 parent 3980650 commit 424f6a6

5 files changed

Lines changed: 57 additions & 19 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"serve-favicon": "~2.3.0",
3131
"sha.js": "^2.4.5",
3232
"socket.io": "^1.4.8",
33+
"stripe": "^4.11.0",
3334
"yamljs": "^0.2.7"
3435
},
3536
"devDependencies": {

public/js/zepto.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ var express = require('express');
33
var router = express.Router();
44
var settings = require('../settings');
55

6+
var stripe = require("stripe")(
7+
settings.stripePrivateKey
8+
);
9+
610
var views;
711
fs.readdir('.viewsMin/pages', function(err, data) {;
812
views = data;
@@ -30,12 +34,27 @@ router.get('/:page?', function(req, res, next) {
3034
if (page === 'index') {
3135
res.redirect(301, '/');
3236
} else if (views.indexOf(page + '.ejs') !== -1) {
33-
res.render('pages/' + page, {socket: ':' + settings.socket, title: titles[page]});
37+
res.render('pages/' + page, {socket: ':' + settings.socket, title: titles[page], stripePublishKey: settings.stripePublishKey});
3438
} else {
3539
next();
3640
}
3741
});
3842

43+
router.post('/donate', function(req, res, next) {
44+
let data = JSON.parse(req.body.data);
45+
46+
stripe.charges.create({
47+
amount: data.price,
48+
currency: "usd",
49+
source: data.id, // obtained with Stripe.js
50+
description: "Donation from " + data.email
51+
}, function(err, charge) {
52+
if (err) return res.sendStatus(400);
53+
else res.sendStatus(200);
54+
});
55+
56+
});
57+
3958
router.all('/download/version.php', function(req, res, next) {
4059
res.json({"version": "3.0.1"});
4160
});

settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"db": "randomuser",
55
"maxResults": 5000,
66
"limit": 20000,
7-
"resetInterval": 60000
7+
"resetInterval": 60000,
8+
"stripePublishKey": "",
9+
"stripePrivateKey": ""
810
}

views/pages/donate.ejs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,50 @@
2525
<div class="dropdown_frame">
2626
<select id="donation_amount" class="dropdown">
2727
<option value="100">$1.00</option>
28-
<option value="200">$2.00</option>
29-
<option value="404">$4.04</option>
30-
<option value="502">$5.02</option>
28+
<option value="500">$5.00</option>
3129
<option value="1000">$10.00</option>
32-
<option value="1337">$13.37</option>
33-
<option value="2500">$25.00</option>
34-
<option value="9001">$90.01</option>
30+
<option value="1500">$15.00</option>
31+
<option value="2000">$20.00</option>
32+
<option value="5000">$50.00</option>
33+
<option value="10000">$100.00</option>
3534
</select>
3635
</div>
3736
<button id="donate_button" class="button">Donate Now</button>
3837
</div>
3938

4039
<script>
4140
var handler = StripeCheckout.configure({
42-
key: 'pk_migDTfk5iGQSrQE0ONijdnKXB0P5Z',
43-
image: 'img/donate_small.png',
44-
token: function(token, args) {
45-
}
41+
key: '<%=stripePublishKey%>',
42+
image: 'img/donate_small.png',
43+
token: function(token, args) {
44+
token.price = Number(document.getElementById('donation_amount').value);
45+
46+
$.ajax({
47+
type: 'POST',
48+
url: '/donate',
49+
data: { data: JSON.stringify(token) },
50+
success: function(data) {
51+
window.alert("Thanks for your donation!");
52+
},
53+
error: function(data) {
54+
window.alert("There was a problem processing your donation. Please make sure your card information is valid and try again.");
55+
}
56+
});
57+
}
4658
});
59+
4760
document.getElementById('donate_button').addEventListener('click', function(e) {
48-
handler.open({
49-
name: 'Random User API',
50-
description: 'Thanks for donating!',
51-
amount: document.getElementById('donation_amount').value
52-
});
53-
e.preventDefault();
61+
handler.open({
62+
name: 'Random User API',
63+
description: 'Thanks for donating!',
64+
amount: Number(document.getElementById('donation_amount').value)
65+
});
66+
e.preventDefault();
5467
});
5568
</script>
5669

57-
<p style="text-align: center;">Don't have your card details handy? <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QBRGZ79MNX9W6" target="_blank">Click here to donate via PayPal</a></p>
70+
<%//<p style="text-align: center;">Don't have your card details handy? <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QBRGZ79MNX9W6" target="_blank">Click here to donate via PayPal</a></p>%>
71+
5872
5973
</section>
6074

0 commit comments

Comments
 (0)