Files
habitica/website/client-old/js/services/paymentServices.js
Keith Holliday ea24eeb019 Thehollidayinn/group plans part 2 (#8262)
* Added all ui components back

* Added group ui items back and initial group approval directive

* Added approval list view with approving functionality

* Added notification display for group approvals

* Fixed linting issues

* Removed expectation from beforeEach

* Moved string to locale

* Added per use group plan for stripe

* Added tests for stripe group plan upgrade

* Removed paypal option

* Abstract sub blocks. Hit group sub block from user settings page. Added group subscriptin beneifts display

* Fixed lint issue

* Added pricing and adjusted styles

* Moved text to translations

* Added group email types

* Fixed typo

* Fixed group plan abstraction and other style issues

* Fixed email unit test

* Added type to group plan to filter our group plans

* Removed dev protection from routes

* Removed hard coding and fixed upgrade plan

* Added error when group has subscription and tries to remove

* Fixed payment unit tests

* Added custom string and moved subscription check up in the logic

* Added ability for old leader to delete subscription the created

* Allowed old guild leader to edit their group subscription

* Fixed linting and tests

* Added group sub page to user sub settings

* Added approval and group tasks requests back. Hid user group sub on profile

* Added group tasks sync after adding to allow for editing

* Fixed promise chain when resolving group

* Added approvals to group promise chain

* Ensured compelted group todos are not delted at cron

* Updated copy and other minor styles

* Added group field to tags and recolored group tag.

* Added chat message when task is claimed

* Preventing task scoring when approval is needed

* Added approval requested indicator

* Updated column with for tasks on group page

* Added checklist sync on assign

* Added sync for checklist items

* Added checkilist sync when task is updated

* Added checklist sync remove

* Sanatized group tasks when updated

* Fixed lint issues

* Added instant scoring of approved task

* Added task modal

* Fixed editing of challenge and group tasks

* Added cancel button

* Added add new checklist option to update sync

* Added remove for checklist

* Added checklist update

* Added difference check and sync for checklist if there is a diff

* Fixed task syncing

* Fixed linting issues

* Fixed styles and karma tests

* Fixed minor style issues

* Fixed obj transfer on scope

* Fixed broken tests

* Added new benefits page

* Updated group page styles

* Updated benefits page style

* Added translations

* Prevented sync with empty trask list

* Added task title to edit modal

* Added new group plans page and upgrade redirect

* Added group plans redirect to upgrade

* Fixed party home page being hidden and home button click

* Fixed dynamic changing of task status and grey popup

* Fixed tag editing

* Hid benifites information if group has subscription

* Added quotes to task name

* Fixed issue with assigning multiple users

* Added new group plans ctrl

* Hid menu from public guilds

* Fixed task sync issue

* Updated placeholder for assign field

* Added correct cost to subscribe details

* Hid create, edit, delete task options from non group leaders

* Prevented some front end modifications to group tasks

* Hid tags option from group original task

* Added refresh for approvals and group tasks

* Prepend new group tasks

* Fix last checklist item sync

* Fixed casing issue with tags

* Added claimed by message on hover

* Prevent user from deleting assigned task

* Added single route for group plan sign up and payments

* Abstracted stripe payments and added initial tests

* Abstracted amazon and added initial tests

* Fixed create group message

* Update group id check and return group

* Updated to use the new returned group

* Fixed linting and promise issues

* Fixed broken leave test after merge issue

* Fixed undefined approval error and editing/deleting challenge tasks

* Add pricing to group plans, removed confirmation, and fixed redirect after payment

* Updated group plan cost text
2016-12-21 13:45:45 -06:00

332 lines
10 KiB
JavaScript

'use strict';
angular.module('habitrpg').factory('Payments',
['$rootScope', 'User', '$http', 'Content',
function($rootScope, User, $http, Content) {
var Payments = {};
var isAmazonReady = false;
window.onAmazonLoginReady = function(){
isAmazonReady = true;
amazon.Login.setClientId(window.env.AMAZON_PAYMENTS.CLIENT_ID);
};
Payments.showStripe = function(data) {
var sub = false;
if (data.subscription) {
sub = data.subscription;
} else if (data.gift && data.gift.type=='subscription') {
sub = data.gift.subscription.key;
}
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,
name: 'Habitica',
description: sub ? window.env.t('subscribe') : window.env.t('checkout'),
image: "/apple-touch-icon-144-precomposed.png",
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
if (data.groupToCreate) {
url = '/api/v3/groups/create-plan?a=a';
res.groupToCreate = data.groupToCreate;
res.paymentType = 'Stripe';
}
if (data.gift) url += '&gift=' + Payments.encodeGift(data.uuid, data.gift);
if (data.subscription) url += '&sub='+sub.key;
if (data.coupon) url += '&coupon='+data.coupon;
if (data.groupId) url += '&groupId=' + data.groupId;
$http.post(url, res).success(function(response) {
if (response && response.data && response.data._id) {
$rootScope.hardRedirect('/#/options/groups/guilds/' + response.data._id);
} else {
window.location.reload(true);
}
}).error(function(res) {
alert(res.message);
});
}
});
}
Payments.showStripeEdit = function(config) {
var groupId;
if (config && config.groupId) {
groupId = config.groupId;
}
StripeCheckout.open({
key: window.env.STRIPE_PUB_KEY,
address: false,
name: window.env.t('subUpdateTitle'),
description: window.env.t('subUpdateDescription'),
panelLabel: window.env.t('subUpdateCard'),
token: function(data) {
data.groupId = groupId;
var url = '/stripe/subscribe/edit';
$http.post(url, data).success(function() {
window.location.reload(true);
}).error(function(data) {
alert(data.message);
});
}
});
}
var amazonOnError = function(error){
console.error(error);
console.log(error.getErrorMessage(), error.getErrorCode());
alert(error.getErrorMessage());
Payments.amazonPayments.reset();
};
Payments.amazonPayments = {};
Payments.amazonPayments.reset = function(){
Payments.amazonPayments.modal.close();
Payments.amazonPayments.modal = null;
Payments.amazonPayments.type = null;
Payments.amazonPayments.loggedIn = false;
Payments.amazonPayments.gift = null;
Payments.amazonPayments.billingAgreementId = null;
Payments.amazonPayments.orderReferenceId = null;
Payments.amazonPayments.paymentSelected = false;
Payments.amazonPayments.recurringConsent = false;
Payments.amazonPayments.subscription = null;
Payments.amazonPayments.coupon = null;
};
// Needs to be called everytime the modal/router is accessed
Payments.amazonPayments.init = function(data) {
if(!isAmazonReady) return;
if(data.type !== 'single' && data.type !== 'subscription') return;
if (data.gift) {
if(data.gift.gems && data.gift.gems.amount && data.gift.gems.amount <= 0) return;
data.gift.uuid = data.giftedTo;
}
if (data.subscription) {
Payments.amazonPayments.subscription = data.subscription;
Payments.amazonPayments.coupon = data.coupon;
}
if (data.groupId) {
Payments.amazonPayments.groupId = data.groupId;
}
if (data.groupToCreate) {
Payments.amazonPayments.groupToCreate = data.groupToCreate;
}
Payments.amazonPayments.gift = data.gift;
Payments.amazonPayments.type = data.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'
});
modal.rendered.then(function(){
OffAmazonPayments.Button('AmazonPayButton', window.env.AMAZON_PAYMENTS.SELLER_ID, {
type: 'PwA',
color: 'Gold',
size: 'small',
agreementType: 'BillingAgreement',
onSignIn: function(contract){
Payments.amazonPayments.billingAgreementId = contract.getAmazonBillingAgreementId();
if (Payments.amazonPayments.type === 'subscription') {
Payments.amazonPayments.loggedIn = true;
Payments.amazonPayments.initWidgets();
} else {
var url = '/amazon/createOrderReferenceId'
$http.post(url, {
billingAgreementId: Payments.amazonPayments.billingAgreementId
}).success(function(res){
Payments.amazonPayments.loggedIn = true;
Payments.amazonPayments.orderReferenceId = res.data.orderReferenceId;
Payments.amazonPayments.initWidgets();
}).error(function(res){
alert(res.message);
});
}
},
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).error(function(res){
alert(res.message);
});
});
},
onError: amazonOnError
});
});
}
Payments.amazonPayments.canCheckout = function(){
if(Payments.amazonPayments.type === 'single'){
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 = {
sellerId: window.env.AMAZON_PAYMENTS.SELLER_ID,
design: {
designMode: 'responsive'
},
onPaymentSelect: function() {
$rootScope.$apply(function() {
Payments.amazonPayments.paymentSelected = true;
});
},
onError: amazonOnError
}
if (Payments.amazonPayments.type === 'subscription') {
walletParams.agreementType = 'BillingAgreement';
console.log(Payments.amazonPayments.billingAgreementId);
walletParams.billingAgreementId = Payments.amazonPayments.billingAgreementId;
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(){
var getConsent = consent.getConsentStatus
Payments.amazonPayments.recurringConsent = getConsent ? getConsent() : false;
});
},
onConsent: function(consent){
$rootScope.$apply(function(){
Payments.amazonPayments.recurringConsent = consent.getConsentStatus();
});
},
onError: amazonOnError
}).bind('AmazonPayRecurring');
}
} else {
walletParams.amazonOrderReferenceId = Payments.amazonPayments.orderReferenceId;
}
new OffAmazonPayments.Widgets.Wallet(walletParams).bind('AmazonPayWallet');
}
Payments.amazonPayments.checkout = function() {
if(Payments.amazonPayments.type === 'single'){
var url = '/amazon/checkout';
$http.post(url, {
orderReferenceId: Payments.amazonPayments.orderReferenceId,
gift: Payments.amazonPayments.gift
}).success(function(){
Payments.amazonPayments.reset();
window.location.reload(true);
}).error(function(res){
alert(res.message);
Payments.amazonPayments.reset();
});
} else if(Payments.amazonPayments.type === 'subscription') {
var url = '/amazon/subscribe';
if (Payments.amazonPayments.groupToCreate) {
url = '/api/v3/groups/create-plan';
}
$http.post(url, {
billingAgreementId: Payments.amazonPayments.billingAgreementId,
subscription: Payments.amazonPayments.subscription,
coupon: Payments.amazonPayments.coupon,
groupId: Payments.amazonPayments.groupId,
groupToCreate: Payments.amazonPayments.groupToCreate,
paymentType: 'Amazon',
}).success(function(response) {
Payments.amazonPayments.reset();
if (response && response.data && response.data._id) {
$rootScope.hardRedirect('/#/options/groups/guilds/' + response.data._id);
} else {
window.location.reload(true);
}
}).error(function(res){
alert(res.message);
Payments.amazonPayments.reset();
});
}
}
Payments.cancelSubscription = function(config) {
if (!confirm(window.env.t('sureCancelSub'))) return;
var group;
if (config && config.group) {
group = config.group;
}
var paymentMethod = User.user.purchased.plan.paymentMethod;
if (group) {
paymentMethod = group.purchased.plan.paymentMethod;
}
if (paymentMethod === 'Amazon Payments') {
paymentMethod = 'amazon';
} else {
paymentMethod = paymentMethod.toLowerCase();
}
var cancelUrl = '/' + paymentMethod + '/subscribe/cancel?_id=' + User.user._id + '&apiToken=' + User.settings.auth.apiToken;
if (group) {
cancelUrl += '&groupId=' + group._id;
}
window.location.href = cancelUrl;
}
Payments.encodeGift = function(uuid, gift) {
gift.uuid = uuid;
var encodedString = JSON.stringify(gift);
return encodeURIComponent(encodedString);
}
return Payments;
}]);