Skip to content
Browse files

Add the ability to create a test charge.

  • Loading branch information...
1 parent 1b90261 commit 7357df20843a44a40a7690dac8c2c2894910e1ea @boucher committed
Showing with 80 additions and 11 deletions.
  1. +3 −0 README
  2. +58 −9 index.html
  3. +2 −1 package.json
  4. +17 −1 server.js
View
3 README
@@ -15,6 +15,9 @@ node web.js
Open in browser with:
http://localhost:8080
+Add your stripe api key to your env:
+heroku config:add STRIPE_SECRET_KEY=<YOUR KEY>
+
Configure your stripe webhooks here:
https://manage.stripe.com/#account/webhooks
View
67 index.html
@@ -1,30 +1,79 @@
<html>
<head>
+<style>
+body {
+ font-family: 'Lucida Grand', Helvetica, Arial, sans-serif;
+}
+
+h1 {
+ padding-bottom: 6px;
+ margin-right: 350px;
+ border-bottom: 1px solid #aaa;
+}
+
+#test-form {
+ font-size: 80%;
+ background-color: #e6e6e6;
+ border-radius: 8px;
+ position: absolute;
+ padding: 12px;
+ top: 10px;
+ right: 10px;
+ width: 300px;
+}
+
+label {
+ display: inline-block;
+ width: 100px;
+ text-align: right;
+}
+</style>
<script src="/dnode.js" type="text/javascript"></script>
+<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function () {
-
- var cashRegisterAudio = document.getElementById('cash_register');
-
DNode(function(){
this.chargeSucceeded = function(charge) {
- var chargeLi = document.createElement('li');
- chargeLi.innerHTML = "Charge for $"+(charge.amount/100).toFixed(2) +
- " with description '"+charge.description+"' succeeded!";
+ var chargeLi = $('<li></li>');
+ $(chargeLi).text("Charge for $"+(charge.amount/100).toFixed(2) +
+ " with description '"+charge.description+"' succeeded!");
- document.getElementById('charges').appendChild(chargeLi);
- cashRegisterAudio.play();
+ $('#charges').prepend(chargeLi);
+ document.getElementById('cash_register').play();
}
}).connect();
-
};
</script>
</head>
<body>
+<div>
+ <h1>Payments!</h1>
+</div>
+
<ul id="charges">
</ul>
+<div id="test-form">
+ <h2>Create a test payment</h2>
+ <p>This will make an actual test payment on the configured test account,
+ and use our actual webhook system to send the webhook, which will then show up here.</p>
+ <form action="/test-payment" method="POST" target="hidden">
+ <label for="amount">Amount: </label>
+ <input id="amount" name="amount" type="text" />
+ <br />
+
+ <label for="amount">Description: </label>
+ <input id="description" name="description" type="text" />
+ <br />
+
+ <label for="submit">&nbsp;</label>
+ <input type="submit" value="Send" />
+ </form>
+
+ <iframe name="hidden" id="hidden" width="0" height="0" style="width:0; height:0;"></iframe>
+</div>
+
<audio id="cash_register" src="/cash_register.wav" style="display:none;" preload></audio>
</body>
</html>
View
3 package.json
@@ -3,6 +3,7 @@
"version": "0.0.1",
"dependencies": {
"express": "2.5.6",
- "dnode": "0.9.6"
+ "dnode": "0.9.6",
+ "stripe": "1.1.0"
}
}
View
18 server.js
@@ -3,10 +3,26 @@ var emitter = new EventEmitter;
var express = require('express');
var app = express.createServer();
+var stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
app.use(express.static(__dirname));
app.use(express.bodyParser())
+app.post('/test-payment', function(request, response){
+ stripe.charges.create({
+ currency: 'usd',
+ amount: Math.floor(request.body.amount*100) || 100,
+ description: request.body.description || "",
+ card: {
+ number: '4242424242424242',
+ exp_month: '12',
+ exp_year: '2018'
+ }
+ }, function(err, charge){
+ response.send(JSON.stringify(charge));
+ })
+});
+
app.post('/stripe-webhook', function(request, response){
console.log(request.body)
if (request.body.type === 'charge.succeeded') {
@@ -37,4 +53,4 @@ app.listen(process.env.PORT || 8080);
var dnode = require('dnode');
var server = dnode(chargeServer);
-server.listen(app, {"io":{"transports":['xhr-polling'], "polling duration":10}});
+server.listen(app, {"io":{"transports":['xhr-polling'], "polling duration":10}});

0 comments on commit 7357df2

Please sign in to comment.
Something went wrong with that request. Please try again.