mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-07-19 20:34:44 +00:00
Edit stripe cc subscriptions
This commit is contained in:
parent
f83f2acc1b
commit
5cc4c60329
4 changed files with 48 additions and 0 deletions
|
|
@ -121,6 +121,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(){
|
$rootScope.cancelSubscription = function(){
|
||||||
if (!confirm(window.env.t('sureCancelSub'))) return;
|
if (!confirm(window.env.t('sureCancelSub'))) return;
|
||||||
window.location.href = '/' + user.purchased.plan.paymentMethod.toLowerCase() + '/subscribe/cancel?_id=' + user._id + '&apiToken=' + user.apiToken;
|
window.location.href = '/' + user.purchased.plan.paymentMethod.toLowerCase() + '/subscribe/cancel?_id=' + user._id + '&apiToken=' + user.apiToken;
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
api.paypalSubscribe = function(req,res,next) {
|
||||||
var uuid = res.locals.user._id;
|
var uuid = res.locals.user._id;
|
||||||
// Authenticate a future subscription of ~5 USD
|
// Authenticate a future subscription of ~5 USD
|
||||||
|
|
|
||||||
|
|
@ -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('/paypal/ipn', i18n.getUserLanguage, payments.paypalIPN); // misc ipn handling
|
||||||
|
|
||||||
router.post("/stripe/checkout", auth.auth, i18n.getUserLanguage, payments.stripeCheckout);
|
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", 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);
|
router.get("/stripe/subscribe/cancel", auth.authWithUrl, i18n.getUserLanguage, payments.stripeSubscribeCancel);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -207,4 +207,5 @@ script(id='partials/options.settings.subscription.html',type='text/ng-template')
|
||||||
|
|
|
|
||||||
span.glyphicon.glyphicon-ok
|
span.glyphicon.glyphicon-ok
|
||||||
div(ng-include="'partials/options.settings.subscription.perks.html'")
|
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')
|
.btn.btn-sm.btn-danger(ng-if='!user.purchased.plan.dateTerminated', ng-click='cancelSubscription()')=env.t('cancelSub')
|
||||||
Loading…
Reference in a new issue