Skip to content

Commit

Permalink
updated charge
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Apr 16, 2015
1 parent 82ede5e commit 782d64f
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 64 deletions.
3 changes: 2 additions & 1 deletion server/models/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var Customer = new Schema({
username: String,
password: String,
token: String,
time: {type: Date, default: Date.now}
time: { type: Date, default: Date.now },
admin: { type: Boolean, default: false }
});

Customer.plugin(passportLocalMongoose);
Expand Down
4 changes: 2 additions & 2 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');

var User = new Schema({
username: String,
password: String,
username: { type: String, required: true },
password: { type: String, required: true },
admin: { type: Boolean, default: false },
});

Expand Down
45 changes: 29 additions & 16 deletions server/routes/charge.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
var express = require('express');
var router = express.Router();
var Customer = require('../models/customer.js');
var Product = require('../models/product.js');
var config = require('../_config.js');
var stripe = require('stripe')(config.StripeKeys.secretKey);
var passport = require('passport');


router.get('/charge', function(req, res, next) {
res.render('charge');
router.get('/products', function(req, res){
return Product.find({}, function(err, data) {
if (err) {
if (err) { return next(err); }
} else {
return res.render('products', {products: data});
}
});
});

router.get('/charge/:id', function(req, res, next) {
var productID = req.params.id;
return Product.findById(productID, function(err, data) {
if (err) {
if (err) { return next(err); }
} else {
return res.render('charge', {product: data});
}
});
});

router.get('/stripe', function(req, res, next) {
Expand All @@ -17,23 +35,18 @@ router.get('/stripe', function(req, res, next) {
router.post('/stripe', function(req, res, next) {
// Obtain StripeToken
var stripeToken = req.body.stripeToken;
Customer.register(new Customer(
{ username : req.body.username, token: stripeToken }),
req.body.password,
function(err, user) {
if (err) {
if (err) { return next(err); }
} else {
passport.authenticate('local')(req, res, function () {
console.log("Success!");
});
}
var newCustomer = new Customer({token: stripeToken });
newCustomer.save(function(err) {
if (err) {
if (err) { return next(err); }
} else {
console.log("Success!");
}
);
});
// Create Charge
var charge =
{
amount: 10*100,
amount: parseInt(req.body.amount)*100,
currency: 'USD',
card: stripeToken
};
Expand All @@ -43,7 +56,7 @@ router.post('/stripe', function(req, res, next) {
if (err) { return next(err); }
} else {
console.log('Successful charge sent to Stripe!');
res.render('congrats', { charge: charge.amount/100.00});
res.render('congrats', { charge: charge.amount/100.00, product: req.body.name});
}
}
);
Expand Down
69 changes: 40 additions & 29 deletions server/views/charge.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,49 @@
<div class="container">
<h1>Charge</h1>
<br>
<a href="/">Back home</a>
<a href="/products">Back to product list</a>
<br><br>
<h4>Use this data for testing: </h4>
<ul>
<li>Card Number - 4242-4242-4242-4242</li>
<li>CVC Code - any three digits</li>
<li>Expiration - any date in the future</li>
</ul>
<h2>One time payment</h2>
<br>
<form id="payment-form" role="form" autocomplete="on" action="/stripe" method="POST" style="max-width: 300px;">
<div id="charge-error" class="alert alert-danger payment-errors"></div>
<div class="form-group">
<label>Credit Card Info</label>
<input type="tel" class="form-control card-number cc-number" data-stripe="number" autocomplete="cc-number" placeholder="•••• •••• •••• ••••" required>
</div>
<div class="form-group">
<input type="tel" class="form-control cc-cvc card-cvc" data-stripe="cvc" autocomplete="off" placeholder="CVC" required>
</div>
<div class="form-group">
<label>Card Expiration Date</label>
<input type="text" data-stripe="exp-month" placeholder="MM" class="card-expiry-month form-control" required>
<input type="text" data-stripe="exp-year" placeholder="YYYY" class="card-expiry-year form-control" required>
<div class="row">
<div class="col-md-4">
<div>
<h2>You are buying:</h2>
<ul>
<li>Product Name: <em>{{product.name}}</em></li>
<li>Amount: <em>${{product.amount}}</em></li>
</ul>
</div>
<div>
<h2>Use this data for testing:</h2>
<ul>
<li>Card Number - 4242-4242-4242-4242</li>
<li>CVC Code - any three digits</li>
<li>Expiration - any date in the future</li>
</ul>
</div>
</div>
<div class="form-group">
<label>User Information</label>
<input type="text" name='username' placeholder="username" class="form-control" required>
<input type="password" name='password' placeholder="password" class="form-control" required>
<div class="col-md-4">
<h2>One time payment</h2>
<br>
<form id="payment-form" role="form" autocomplete="on" action="/stripe" method="POST" style="max-width: 300px;">
<div id="charge-error" class="alert alert-danger payment-errors"></div>
<div class="form-group">
<label>Credit Card Info</label>
<input type="tel" class="form-control card-number cc-number" data-stripe="number" autocomplete="cc-number" placeholder="•••• •••• •••• ••••" required>
</div>
<div class="form-group">
<input type="tel" class="form-control cc-cvc card-cvc" data-stripe="cvc" autocomplete="off" placeholder="CVC" required>
</div>
<div class="form-group">
<label>Card Expiration Date</label>
<input type="text" data-stripe="exp-month" placeholder="MM" class="card-expiry-month form-control" required>
<input type="text" data-stripe="exp-year" placeholder="YYYY" class="card-expiry-year form-control" required>
</div>
<input style="display:none" name="amount" value="{{product.amount}}">
<input style="display:none" name="name" value="{{product.name}}">
<button type="submit" class="btn btn-default submit-button btn-block">Submit</button>
</form>
</div>
<button type="submit" class="btn btn-default submit-button btn-block">Submit</button>
</form>
</div>
</div>

<br>

Expand Down
2 changes: 1 addition & 1 deletion server/views/congrats.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<a href="/">Back home</a>
<br>
<h1>Congrats!</h1>
<p class="lead">You just purchased a shirt for $<span class="charge">{{charge}}</span></p>
<p class="lead">You just purchased a {{product}} for $<span class="charge">{{charge}}</span></p>
<br>
<img src="{{ baseurl }}/img/shirt.png"/>
</div>
Expand Down
4 changes: 2 additions & 2 deletions server/views/partials/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/charge">Purchase</a></li>
<li><a href="/products">Products</a></li>
{% if user %}
<li><a href="/auth/admin">Admin</a></li>
<li><a href="/auth/logout">Logout</a></li>
{% else %}
<li><a href="/auth/logout">Admin Login</a></li>
<li><a href="/auth/login">Admin Login</a></li>
{% endif %}
</ul>
</div>
Expand Down
31 changes: 31 additions & 0 deletions server/views/products.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends '_base.html' %}

{% block title %}{% endblock %}


{% block content %}

<div class="container">
<h1>Product List</h1>
<p class="lead">Which would you like to purchase?</p>
<a href="/">Go home</a>
<br><br>
<table class="table table-hover" style="max-width:400px;">
<thead>
<tr>
<th>Product Name</th>
<th>Product Price</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td><a href="/charge/{{product._id.toString()}}">{{ product.name }}</a></td>
<td>${{ product.amount }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

{% endblock %}
26 changes: 13 additions & 13 deletions test/04_charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ describe('charge.js Routes', function(){
});
});

describe('GET /charge', function(){
it('should return a view', function(done){
request(app)
.get('/charge')
.end(function(err, res){
assert.equal(res.statusCode, 200);
assert.equal(res.status, 200);
assert.equal(res.type, 'text/html');
res.text.should.containEql('<h1>Charge</h1>');
done();
});
});
});
// describe('GET /charge', function(){
// it('should return a view', function(done){
// request(app)
// .get('/charge')
// .end(function(err, res){
// assert.equal(res.statusCode, 200);
// assert.equal(res.status, 200);
// assert.equal(res.type, 'text/html');
// res.text.should.containEql('<h1>Charge</h1>');
// done();
// });
// });
// });

});

Expand Down

0 comments on commit 782d64f

Please sign in to comment.