2014-11-30 21:01:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-01-18 01:43:37 +00:00
|
|
|
angular.module('habitrpg').factory('Payments',
|
2014-11-30 21:01:58 +00:00
|
|
|
['$rootScope', 'User', '$http', 'Content',
|
|
|
|
|
function($rootScope, User, $http, Content) {
|
|
|
|
|
var Payments = {};
|
2015-06-08 16:55:15 +00:00
|
|
|
var isAmazonReady = false;
|
|
|
|
|
|
|
|
|
|
window.onAmazonLoginReady = function(){
|
|
|
|
|
isAmazonReady = true;
|
|
|
|
|
amazon.Login.setClientId(window.env.AMAZON_PAYMENTS.CLIENT_ID);
|
|
|
|
|
};
|
2014-11-30 21:01:58 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-23 15:30:40 +00:00
|
|
|
var amazonOnError = function(error){
|
|
|
|
|
console.error(error);
|
|
|
|
|
console.log(error.getErrorMessage(), error.getErrorCode());
|
|
|
|
|
alert(error.getErrorMessage());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Payments.amazonPayments = {};
|
|
|
|
|
|
|
|
|
|
Payments.amazonPayments.reset = function(){
|
|
|
|
|
Payments.amazonPayments.modal.close();
|
|
|
|
|
// TODO this is needed because if we do not logout
|
|
|
|
|
// users then if they use donation & then subscription
|
|
|
|
|
// the billing agreement will be wrong
|
|
|
|
|
amazon.Login.logout();
|
|
|
|
|
Payments.amazonPayments.modal = null;
|
|
|
|
|
Payments.amazonPayments.type = null;
|
|
|
|
|
Payments.amazonPayments.loggedIn = false;
|
|
|
|
|
Payments.amazonPayments.orderReferenceId = null;
|
|
|
|
|
Payments.amazonPayments.billingAgreementId = null;
|
|
|
|
|
Payments.amazonPayments.paymentSelected = false;
|
|
|
|
|
Payments.amazonPayments.recurringConsent = false;
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-08 16:55:15 +00:00
|
|
|
// Needs to be called everytime the modal/router is accessed
|
2015-06-23 15:30:40 +00:00
|
|
|
Payments.amazonPayments.init = function(type){
|
2015-06-08 16:55:15 +00:00
|
|
|
if(!isAmazonReady) return;
|
2015-06-23 15:30:40 +00:00
|
|
|
if(type !== 'donation' && type !== 'subscription') return;
|
|
|
|
|
Payments.amazonPayments.type = type;
|
|
|
|
|
|
|
|
|
|
var modal = Payments.amazonPayments.modal = $rootScope.openModal('amazonPayments', {
|
|
|
|
|
// Allow the modal to be closed only by pressing cancel
|
|
|
|
|
// because no easy method to intercept those types of closings
|
|
|
|
|
// and we need to make some cleanup
|
|
|
|
|
keyboard: false,
|
|
|
|
|
backdrop: 'static'
|
|
|
|
|
});
|
2015-06-08 16:55:15 +00:00
|
|
|
|
2015-06-23 15:30:40 +00:00
|
|
|
modal.rendered.then(function(){
|
|
|
|
|
OffAmazonPayments.Button('AmazonPayButton', window.env.AMAZON_PAYMENTS.SELLER_ID, {
|
|
|
|
|
type: 'PwA',
|
|
|
|
|
color: 'Gold',
|
|
|
|
|
size: 'small',
|
|
|
|
|
|
|
|
|
|
authorization: function(){
|
|
|
|
|
amazon.Login.authorize({
|
|
|
|
|
scope: 'payments:widget',
|
|
|
|
|
popup: true
|
|
|
|
|
}, function(response){
|
|
|
|
|
if(response.error) return alert(response.error);
|
|
|
|
|
|
|
|
|
|
var url = '/amazon/verifyAccessToken'
|
|
|
|
|
$http.post(url, response).success(function(){
|
|
|
|
|
Payments.amazonPayments.loggedIn = true;
|
|
|
|
|
Payments.amazonPayments.initWidgets();
|
|
|
|
|
}).error(function(res){
|
|
|
|
|
alert(res.err);
|
|
|
|
|
});
|
2015-06-08 16:55:15 +00:00
|
|
|
});
|
2015-06-23 15:30:40 +00:00
|
|
|
},
|
2015-06-08 16:55:15 +00:00
|
|
|
|
2015-06-23 15:30:40 +00:00
|
|
|
onError: amazonOnError
|
|
|
|
|
});
|
2015-06-08 16:55:15 +00:00
|
|
|
});
|
|
|
|
|
|
2015-06-23 15:30:40 +00:00
|
|
|
}
|
2015-06-09 14:55:13 +00:00
|
|
|
|
2015-06-23 15:30:40 +00:00
|
|
|
Payments.amazonPayments.canCheckout = function(){
|
|
|
|
|
if(Payments.amazonPayments.type === 'donation'){
|
|
|
|
|
return Payments.amazonPayments.paymentSelected === true;
|
|
|
|
|
}else if(Payments.amazonPayments.type === 'subscription'){
|
|
|
|
|
return Payments.amazonPayments.paymentSelected === true &&
|
|
|
|
|
// Mah.. one is a boolean the other a string...
|
|
|
|
|
Payments.amazonPayments.recurringConsent === 'true';
|
|
|
|
|
}else{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
Payments.amazonPayments.initWidgets = function(){
|
|
|
|
|
var walletParams = {
|
2015-06-09 14:55:13 +00:00
|
|
|
sellerId: window.env.AMAZON_PAYMENTS.SELLER_ID,
|
2015-06-23 15:30:40 +00:00
|
|
|
design: {
|
|
|
|
|
designMode: 'responsive'
|
2015-06-09 14:55:13 +00:00
|
|
|
},
|
2015-06-23 15:30:40 +00:00
|
|
|
|
2015-06-09 14:55:13 +00:00
|
|
|
onPaymentSelect: function(orderReference) {
|
|
|
|
|
$rootScope.$apply(function(){
|
2015-06-23 15:30:40 +00:00
|
|
|
Payments.amazonPayments.paymentSelected = true;
|
2015-06-09 14:55:13 +00:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2015-06-23 15:30:40 +00:00
|
|
|
onError: amazonOnError
|
|
|
|
|
};
|
2015-06-09 14:55:13 +00:00
|
|
|
|
2015-06-23 15:30:40 +00:00
|
|
|
if(Payments.amazonPayments.type === 'donation'){
|
|
|
|
|
walletParams.onOrderReferenceCreate = function(orderReference) {
|
|
|
|
|
Payments.amazonPayments.orderReferenceId = orderReference.getAmazonOrderReferenceId();
|
|
|
|
|
}
|
|
|
|
|
}else if(Payments.amazonPayments.type === 'subscription'){
|
|
|
|
|
walletParams.onReady = function(billingAgreement) {
|
|
|
|
|
Payments.amazonPayments.billingAgreementId = billingAgreement.getAmazonBillingAgreementId();
|
|
|
|
|
|
|
|
|
|
new OffAmazonPayments.Widgets.Consent({
|
|
|
|
|
sellerId: window.env.AMAZON_PAYMENTS.SELLER_ID,
|
|
|
|
|
amazonBillingAgreementId: Payments.amazonPayments.billingAgreementId,
|
|
|
|
|
design: {
|
|
|
|
|
designMode: 'responsive'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onReady: function(consent){
|
|
|
|
|
$rootScope.$apply(function(){
|
|
|
|
|
Payments.amazonPayments.recurringConsent = consent.getConsentStatus();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onConsent: function(consent){
|
|
|
|
|
$rootScope.$apply(function(){
|
|
|
|
|
Payments.amazonPayments.recurringConsent = consent.getConsentStatus();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onError: amazonOnError
|
|
|
|
|
}).bind('AmazonPayRecurring');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
walletParams.agreementType = 'BillingAgreement';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new OffAmazonPayments.Widgets.Wallet(walletParams).bind('AmazonPayWallet');
|
|
|
|
|
}
|
2015-06-08 16:55:15 +00:00
|
|
|
|
2015-06-23 15:30:40 +00:00
|
|
|
Payments.amazonPayments.checkout = function(){
|
|
|
|
|
if(Payments.amazonPayments.type === 'donation'){
|
|
|
|
|
var url = '/amazon/checkout'
|
|
|
|
|
$http.post(url, {orderReferenceId: Payments.amazonPayments.orderReferenceId}).success(function(){
|
|
|
|
|
console.log(arguments);
|
|
|
|
|
Payments.amazonPayments.reset();
|
|
|
|
|
}).error(function(res){
|
|
|
|
|
alert(res.err);
|
|
|
|
|
Payments.amazonPayments.reset();
|
|
|
|
|
});
|
|
|
|
|
}else if(Payments.amazonPayments.type === 'subscription'){
|
|
|
|
|
return false
|
|
|
|
|
}
|
2015-06-08 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-30 21:01:58 +00:00
|
|
|
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;
|
|
|
|
|
}]);
|