Created
May 16, 2014 15:20
-
-
Save namlet/25b1885592978c637447 to your computer and use it in GitHub Desktop.
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
exports.charge = function(req, res){ | |
var pkg = req.body; | |
var token = pkg.token; | |
console.log("POST BODY"); | |
console.log(req.body); | |
var charge = rk.stripe.charges.create({ | |
amount: 3999, // amount in cents, again | |
currency: "usd", | |
card: token.id, | |
description: "Payment to My Teen Driver from " + token.email | |
}, function(err, charge_response) { | |
console.log("Response from Stripe:"); | |
if (err) { | |
if (err.type === 'StripeCardError') { | |
// The card has been declined | |
} | |
console.log(err); | |
res.send(err); | |
} else { | |
console.log("CHARGE RESPONSE"); | |
console.log(charge_response); | |
//res.send(charge_response); | |
//TODO: refactor this assingment out if not necessary | |
var stripe_charge = charge_response; | |
var user = rk.db.User.create({ | |
name: stripe_charge.card.name, | |
email: token.email, | |
address_line1: stripe_charge.card.address_line1, | |
address_line2: stripe_charge.card.address_line2, | |
address_city: stripe_charge.card.address_city, | |
address_state: stripe_charge.card.address_state, | |
address_zip: stripe_charge.card.address_zip, | |
address_country: stripe_charge.card.address_country | |
}).success(function(user){ | |
//res.send(user); | |
//TODO: assign code | |
var charge = rk.db.Charge.create({ | |
user_id: user.id, | |
charge_id: stripe_charge.id, | |
token_id: token.id, | |
name: stripe_charge.card.name, | |
card_id: stripe_charge.card.id, | |
card_type: stripe_charge.card.type, | |
card_last4: stripe_charge.card.last4, | |
card_fingerprint: stripe_charge.card.fingerprint, | |
card_exp_month: stripe_charge.card.exp_month, | |
card_exp_year: stripe_charge.card.exp_year, | |
created: stripe_charge.created, | |
currency: stripe_charge.currency, | |
description: stripe_charge.description, | |
paid: stripe_charge.paid, | |
amount: stripe_charge.amount | |
}).success(function(charge_model){ | |
//TODO: update something? | |
//STRIPE POSTS TWICE CAUSING ERROR | |
res.send(user, charge_model); | |
}); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment