Created
May 1, 2017 23:53
-
-
Save melaniecarr23/119222df4a509628a704c4b3b2a72918 to your computer and use it in GitHub Desktop.
Stripe Controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nodemailer = require('nodemailer'); | |
// setup transporter to email from localhost noreply address | |
var transporter = nodemailer.createTransport({ | |
host: 'localhost', | |
port: 25, | |
tls:{ | |
rejectUnauthorized: false | |
} | |
}); | |
var path = require('path'); | |
var stripe = require("stripe")("sk_test_mykey"); | |
var mongoose = require("mongoose"); | |
var User = mongoose.model('User'); | |
var Order = mongoose.model('Order'); | |
var Registration = mongoose.model('Registration'); | |
module.exports.stripePayment = function(req, res) { | |
var cart = JSON.parse(req.body.cart); | |
cart.chargeamt = Math.floor(parseFloat(cart.totalPrice * 100)); | |
console.log("Cart",cart); | |
var customer = { | |
stripeEmail: req.body.stripeEmail, | |
stripeToken: req.body.stripeToken, | |
username: cart.username, | |
stripeCustomer: cart.customerID | |
}; | |
console.log("Customer",customer); | |
// If new customer: | |
if(!customer.stripeCustomer) { | |
console.log("NEW stripe customer."); | |
//create stripe customer and update user profile | |
userStripeCustomer(customer, cart); | |
} else { | |
console.log("Existing stripe user found."); | |
//charge the customer | |
chargeCustomer(customer, cart); | |
} | |
}; | |
var userStripeCustomer = function(customer, cart) { | |
//create stripe customer | |
console.log("Create new stripe customer for user ", customer.username); | |
stripe.customers.create({ | |
email: customer.stripeEmail, | |
source: customer.stripeToken | |
}).then(function(stripecustomer){ | |
console.log("Created customer in stripeAPI", stripecustomer.id); | |
User.findOneAndUpdate({username: customer.username}, {$set:{stripeCustomer: stripecustomer.id }}, {new: true}, function(err, user){ | |
if(err) { | |
return console.log("Error updating user with stripe account ID."); | |
} else { | |
console.log("User's stripe customer number saved."); | |
customer.stripeCustomer = user.stripeCustomer; | |
console.log("Updated customer info ",customer); | |
//charge the customer | |
chargeCustomer(customer, cart); | |
} | |
}); | |
}).catch(function(error) { | |
console.log("There was an error creating the stripe customer",error); | |
return; | |
}); | |
}; | |
var chargeCustomer = function(customer, cart) { | |
console.log("Charging customer for order", cart); | |
console.log("Customer data", customer); | |
stripe.charges.create({ | |
amount: cart.chargeamt, | |
currency: "usd", | |
description: "Pottstown Rumble Registration", | |
metadata: {username: customer.username}, | |
customer: customer.stripeCustomer, | |
}).then(function(charge) { | |
console.log("The stripe payment completed successfully!", charge.id, charge.amount); | |
// Use and save the charge info. | |
userNewOrder(charge, cart); | |
}).catch(function(err, res) { | |
// Deal with an error | |
console.log("Error processing card. ",err.message); | |
res.status(500).json(err); | |
}); | |
}; | |
var userNewOrder = function(charge, cart, req, res) { | |
console.log("Saving customer ORDER for charge",charge.id); | |
Order | |
.create({ | |
cart: { | |
username: cart.username, | |
items: cart.items, | |
totalQty: cart.totalQty, | |
totalPrice: cart.totalPrice | |
}, | |
customer: cart.customerID, | |
paymentID: charge.id, | |
paymentStatus: "PAID", | |
user: cart.username | |
}, function(err, order) { | |
if (err) { | |
console.log("Error creating order",err); | |
res | |
.status(400) | |
.json(err); | |
} else { | |
console.log("Order created!", order._id); | |
saveUserRegistrations(order); | |
} | |
}); | |
}; | |
var saveUserRegistrations = function(order, req, res) { | |
console.log("Saving registrations from order",order.id); | |
//put cart items into array of objects | |
var registrations = generateRegistrationsArray(order); | |
// console.log("Registrations ",registrations); | |
var paymentId = order.paymentID; | |
var userobject = order.user; | |
// add paymentid to arrays | |
for (var i in registrations) { | |
registrations[i].paymentId = paymentId; | |
registrations[i].users = userobject; | |
} | |
console.log("Registrations with payment ID and username added",registrations); | |
//save the registrations to the db | |
saveRegistrations(registrations); | |
// emailPlayers(registrations); | |
//nodemailerConfirmation(registrations); | |
}; | |
var generateRegistrationsArray = function(order) { | |
console.log("Generating array from cart items"); | |
var arr = []; | |
for(var i=0; i < order.cart.items.length; i++) { | |
arr.push({ | |
event: order.cart.items[i].event, | |
field: order.cart.items[i].field, | |
division: order.cart.items[i].division, | |
level: order.cart.items[i].level, | |
group: order.cart.items[i].group, | |
numplayers: order.cart.items[i].numplayers, | |
price: order.cart.items[i].price, | |
players: order.cart.items[i].players, | |
net: null, | |
team: null, | |
notes: null, | |
paymentNote: null, | |
active: true | |
}); | |
} | |
console.log("Registrations array",arr); | |
return arr; | |
}; | |
//save registrations and redirect to success page | |
var saveRegistrations = function(registrations, req, res) { | |
var reginfo = registrations; | |
Registration | |
.collection.insert(registrations) | |
.then(function(r) { | |
console.log("Successfully saved registrations!",r.insertedCount); | |
return res.redirect('/'); | |
// nodemailerConfirmation(reginfo); | |
}).catch(function(error){ | |
console.log(error); | |
}); | |
}; | |
var nodemailerConfirmation = function(registrations, req, res, $window, $location) { | |
transporter.sendMail({ | |
from: '', | |
to: '', | |
subject: 'Test nodemailer email', | |
html: '<b>Rocking the email server for FREE!</b>' + registrations, | |
text: 'Nodemailer email confirmation of registration!' | |
}, function(err){ | |
if(!err){ | |
console.log("Sent successfully!"); | |
res.redirect('/'); | |
} else { | |
console.log("There was an error sending the email", err.message); | |
return; | |
} | |
}); | |
}; | |
function display_create(url, req, res) { | |
var fs = require('fs'); | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
fs.readFile('./public/angular-app/register/checkout-success.html', function(e, c) { | |
var data = { | |
title: "Registration Success!", | |
message: "You have successfully registered", | |
url: url | |
}; | |
var html = html.render(c.toString(), {locals: data}); | |
console.log(html); | |
res.end(html); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment