continuation of PR #8161 Display error notification when attempting to purchase invalid amount of gems - fixes #8145 (#8688)

* Translation string for error notification

* Use function instead of a link for paypal

* Inject notification service, function to check the amount of gems to purchase, function to handle payments with paypal

* Throw error if amount of gems is zero or negative

* Add condition to raise error if amount is negative

* Added gem errors for gifts 0 or less

* Fixed linting and broken test

* Fixed test syntax

* Added back needed strings

* Fixed group locales
This commit is contained in:
Keith Holliday
2017-07-06 14:43:43 -06:00
committed by Sabe Jones
parent 48bbc22fb4
commit e901850a6f
9 changed files with 101 additions and 4 deletions

View File

@@ -1,8 +1,8 @@
'use strict';
angular.module('habitrpg').factory('Payments',
['$rootScope', 'User', '$http', 'Content',
function($rootScope, User, $http, Content) {
['$rootScope', 'User', '$http', 'Content', 'Notification',
function($rootScope, User, $http, Content, Notification) {
var Payments = {};
var isAmazonReady = false;
Payments.amazonButtonEnabled = true;
@@ -21,7 +21,18 @@ function($rootScope, User, $http, Content) {
amazon.Login.setClientId(window.env.AMAZON_PAYMENTS.CLIENT_ID);
};
Payments.checkGemAmount = function(data) {
if(data.gift.type === "gems" && !data.gift.gems.amount || data.gift.gems.amount === 0) {
Notification.error(window.env.t('badAmountOfGemsToPurchase'), true);
return false;
}
return true;
}
Payments.showStripe = function(data) {
if(!Payments.checkGemAmount(data)) return;
var sub = false;
if (data.subscription) {
@@ -121,6 +132,7 @@ function($rootScope, User, $http, Content) {
// Needs to be called everytime the modal/router is accessed
Payments.amazonPayments.init = function(data) {
if(!isAmazonReady) return;
if(!Payments.checkGemAmount(data)) return;
if(data.type !== 'single' && data.type !== 'subscription') return;
if (data.gift) {
@@ -345,6 +357,14 @@ function($rootScope, User, $http, Content) {
});
}
Payments.payPalPayment = function(data){
if(!Payments.checkGemAmount(data)) return;
var gift = Payments.encodeGift(data.giftedTo, data.gift);
var url = '/paypal/checkout?_id=' + User.user._id + '&apiToken=' + User.settings.auth.apiToken + '&gift=' + gift;
$http.get(url);
}
Payments.encodeGift = function(uuid, gift) {
gift.uuid = uuid;
var encodedString = JSON.stringify(gift);