2014-11-30 21:01:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('paymentServices',[]).factory('Payments',
|
|
|
|
|
['$rootScope', 'User', '$http', 'Content',
|
|
|
|
|
function($rootScope, User, $http, Content) {
|
|
|
|
|
var Payments = {};
|
|
|
|
|
|
|
|
|
|
Payments.showStripe = function(data) {
|
|
|
|
|
var sub =
|
|
|
|
|
data.subscription ? data.subscription
|
2014-12-21 03:47:16 +00:00
|
|
|
: data.gift && data.gift.type=='subscription' ? data.gift.subscription.key
|
2014-11-30 21:01:58 +00:00
|
|
|
: false;
|
|
|
|
|
sub = sub && Content.subscriptionBlocks[sub];
|
|
|
|
|
var amount = // 500 = $5
|
|
|
|
|
sub ? sub.price*100
|
|
|
|
|
: data.gift && data.gift.type=='gems' ? data.gift.gems.amount/4*100
|
|
|
|
|
: 500;
|
|
|
|
|
StripeCheckout.open({
|
|
|
|
|
key: window.env.STRIPE_PUB_KEY,
|
|
|
|
|
address: false,
|
|
|
|
|
amount: amount,
|
2014-12-30 02:31:59 +00:00
|
|
|
name: 'HabitRPG',
|
|
|
|
|
description: sub ? window.env.t('subscribe') : window.env.t('checkout'),
|
2014-12-30 02:35:07 +00:00
|
|
|
image: "/apple-touch-icon-144-precomposed.png",
|
2014-11-30 21:01:58 +00:00
|
|
|
panelLabel: sub ? window.env.t('subscribe') : window.env.t('checkout'),
|
|
|
|
|
token: function(res) {
|
|
|
|
|
var url = '/stripe/checkout?a=a'; // just so I can concat &x=x below
|
2014-12-04 02:38:09 +00:00
|
|
|
if (data.gift) url += '&gift=' + Payments.encodeGift(data.uuid, data.gift);
|
2014-12-21 03:47:16 +00:00
|
|
|
if (data.subscription) url += '&sub='+sub.key;
|
|
|
|
|
if (data.coupon) url += '&coupon='+data.coupon;
|
2014-11-30 21:01:58 +00:00
|
|
|
$http.post(url, res).success(function() {
|
|
|
|
|
window.location.reload(true);
|
|
|
|
|
}).error(function(res) {
|
|
|
|
|
alert(res.err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Payments.showStripeEdit = function(){
|
|
|
|
|
StripeCheckout.open({
|
|
|
|
|
key: window.env.STRIPE_PUB_KEY,
|
|
|
|
|
address: false,
|
2014-12-23 23:14:08 +00:00
|
|
|
name: window.env.t('subUpdateTitle'),
|
|
|
|
|
description: window.env.t('subUpdateDescription'),
|
|
|
|
|
panelLabel: window.env.t('subUpdateCard'),
|
2014-11-30 21:01:58 +00:00
|
|
|
token: function(data) {
|
2014-12-01 00:18:54 +00:00
|
|
|
var url = '/stripe/subscribe/edit';
|
2014-11-30 21:01:58 +00:00
|
|
|
$http.post(url, data).success(function() {
|
|
|
|
|
window.location.reload(true);
|
|
|
|
|
}).error(function(data) {
|
|
|
|
|
alert(data.err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Payments.cancelSubscription = function(){
|
|
|
|
|
if (!confirm(window.env.t('sureCancelSub'))) return;
|
2014-12-21 03:47:16 +00:00
|
|
|
window.location.href = '/' + User.user.purchased.plan.paymentMethod.toLowerCase() + '/subscribe/cancel?_id=' + User.user._id + '&apiToken=' + User.user.apiToken;
|
2014-11-30 21:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 23:43:27 +00:00
|
|
|
Payments.encodeGift = function(uuid, gift){
|
|
|
|
|
gift.uuid = uuid;
|
|
|
|
|
return JSON.stringify(gift);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-30 21:01:58 +00:00
|
|
|
return Payments;
|
|
|
|
|
}]);
|