Merge branch 'thedavemarshall-update_billing_info' into develop

This commit is contained in:
Tyler Renelle 2014-07-21 14:48:17 -06:00
commit dda7f86433
4 changed files with 48 additions and 0 deletions

View file

@ -122,6 +122,26 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
});
}
$rootScope.showStripeEdit = function(){
StripeCheckout.open({
key: window.env.STRIPE_PUB_KEY,
address: false,
name: 'Update',
description: 'Update the card to be charged for your subscription',
panelLabel: 'Update Card',
token: function(data) {
var url = '/stripe/subscribe/edit?plan=basic_earned';
$scope.$apply(function(){
$http.post(url, data).success(function() {
window.location.reload(true);
}).error(function(err) {
alert(err);
});
})
}
});
}
$rootScope.cancelSubscription = function(){
if (!confirm(window.env.t('sureCancelSub'))) return;
window.location.href = '/' + user.purchased.plan.paymentMethod.toLowerCase() + '/subscribe/cancel?_id=' + user._id + '&apiToken=' + user.apiToken;

View file

@ -142,6 +142,32 @@ api.stripeSubscribeCancel = function(req, res, next) {
});
}
api.stripeSubscribeEdit = function(req, res, next) {
var stripe = require("stripe")(nconf.get('STRIPE_API_KEY'));
var token = req.body.id;
var user = res.locals.user;
var user_id = user.purchased.plan.customerId;
var sub_id;
async.waterfall([
function(cb){
stripe.customers.listSubscriptions(user_id, cb);
},
function(response, cb) {
sub_id = response.data[0].id;
console.warn(sub_id);
console.warn([user_id, sub_id, { card: token }]);
stripe.customers.updateSubscription(user_id, sub_id, { card: token }, cb);
},
function(response, cb) {
user.save(cb);
}
], function(err, saved){
if (err) return res.send(500, err.toString()); // don't json this, let toString() handle errors
res.send(200);
});
}
api.paypalSubscribe = function(req,res,next) {
var uuid = res.locals.user._id;
// Authenticate a future subscription of ~5 USD

View file

@ -13,6 +13,7 @@ router.get('/paypal/subscribe/cancel', auth.authWithUrl, i18n.getUserLanguage, p
router.post('/paypal/ipn', i18n.getUserLanguage, payments.paypalIPN); // misc ipn handling
router.post("/stripe/checkout", auth.auth, i18n.getUserLanguage, payments.stripeCheckout);
router.post("/stripe/subscribe/edit", auth.auth, i18n.getUserLanguage, payments.stripeSubscribeEdit)
//router.get("/stripe/subscribe", auth.authWithUrl, i18n.getUserLanguage, payments.stripeSubscribe); // checkout route is used (above) with ?plan= instead
router.get("/stripe/subscribe/cancel", auth.authWithUrl, i18n.getUserLanguage, payments.stripeSubscribeCancel);

View file

@ -219,4 +219,5 @@ script(id='partials/options.settings.subscription.html',type='text/ng-template')
|  
span.glyphicon.glyphicon-ok
div(ng-include="'partials/options.settings.subscription.perks.html'")
.btn.btn-primary(ng-if='!user.purchased.plan.dateTerminated', ng-click='showStripeEdit()') Update Card
.btn.btn-sm.btn-danger(ng-if='!user.purchased.plan.dateTerminated', ng-click='cancelSubscription()')=env.t('cancelSub')