Adds limits 500usd per day per user

This commit is contained in:
Gustavo Maximiliano Cortez 2016-06-06 12:07:47 -03:00
parent 39c3a85bd3
commit d25cb3d02e
No known key found for this signature in database
GPG Key ID: 15EDAD8D9F2EB1AF
4 changed files with 76 additions and 12 deletions

View File

@ -6,10 +6,10 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
window.ignoreMobilePause = true;
var self = this;
var fc;
var minimumAmount = 1;
var minimumAmount = 5;
var stepAmount = 1;
var multiplierAmount = 2;
var maximumAmount = 10;
var multiplierAmount = 5;
var maximumAmount = 500;
var otherWallets = function(network) {
return lodash.filter(profileService.getWallets(network), function(w) {
@ -26,7 +26,7 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
};
this.init = function() {
$scope.fiat = minimumAmount * multiplierAmount;
$scope.fiat = minimumAmount;
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
amazonService.setCredentials(network);
self.otherWallets = otherWallets(network);
@ -99,7 +99,7 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
if (plus && $scope.fiat < maximumAmount ) {
stepAmount = stepAmount + 1;
$scope.fiat = stepAmount * multiplierAmount;
} else if (!plus && $scope.fiat > minimumAmount * multiplierAmount) {
} else if (!plus && $scope.fiat > minimumAmount) {
stepAmount = stepAmount - 1;
$scope.fiat = stepAmount * multiplierAmount;
}
@ -126,7 +126,7 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
amazonService.createBitPayInvoice(dataSrc, function(err, data) {
if (err) {
self.loading = null;
self.error = err;
self.error = bwsError.msg(err);
return;
}
@ -185,6 +185,7 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
self.errorInfo = gift;
return;
}
amazonService.setAmountByDay(dataSrc.price);
self.giftCard = giftCard;
$timeout(function() {
$scope.$digest();

View File

@ -3,6 +3,7 @@
angular.module('copayApp.services').factory('amazonService', function($http, $log, lodash, moment, storageService, configService) {
var root = {};
var credentials = {};
var LIMIT = 500;
root.setCredentials = function(network) {
credentials.AMAZON_SANDBOX = network == 'testnet' ? true : false;
@ -28,6 +29,50 @@ angular.module('copayApp.services').factory('amazonService', function($http, $lo
};
};
var _checkLimit = function(amount, cb) {
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
var dateStamp = moment.utc().format('YYYY-MM-DD');
storageService.getAmazonLimits(network, function(err, limits) {
if (err) $log.error(err);
if (lodash.isEmpty(limits) && amount <= LIMIT) return cb();
if (lodash.isEmpty(limits) || amount > LIMIT) return cb('EXCEEDED_DAYLY_LIMIT');
if (lodash.isString(limits)) {
limits = JSON.parse(limits);
}
if (limits.date == dateStamp && (limits.amount + amount) > LIMIT)
return cb('EXCEEDED_DAYLY_LIMIT');
return cb();
});
};
root.setAmountByDay = function(amount) {
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
var dateStamp = moment.utc().format('YYYY-MM-DD');
storageService.getAmazonLimits(network, function(err, limits) {
if (err) $log.error(err);
if (lodash.isEmpty(limits)) limits = { date: dateStamp, amount: 0 };
if (lodash.isString(limits)) {
limits = JSON.parse(limits);
}
if (limits.date == dateStamp) {
limits.amount = limits.amount + amount;
} else {
limits = { date: dateStamp, amount: amount };
}
limits = JSON.stringify(limits);
storageService.setAmazonLimits(network, limits, function(err) {
if (err) $log.error(err);
});
});
};
var _getSignatureKey = function() {
var key = credentials.AMAZON_SECRET_KEY;
@ -116,6 +161,8 @@ angular.module('copayApp.services').factory('amazonService', function($http, $lo
price: data.price,
currency: data.currency
};
_checkLimit(data.price, function(err) {
if (err) return cb(err);
$http(_postBitPay('/invoices', data)).then(function(data) {
$log.info('BitPay Create Invoice: SUCCESS');
return cb(null, data.data);
@ -123,6 +170,7 @@ angular.module('copayApp.services').factory('amazonService', function($http, $lo
$log.error('BitPay Create Invoice: ERROR ' + data.data.error);
return cb(data.data.error);
});
});
};
root.getBitPayInvoice = function(id, cb) {

View File

@ -139,6 +139,9 @@ angular.module('copayApp.services')
case 'PASSWORD_INCORRECT':
body = gettextCatalog.getString('Wrong spending password');
break;
case 'EXCEEDED_DAYLY_LIMIT':
body = gettextCatalog.getString('Exceeded dayly limit of $500 per user');
break;
case 'ERROR':
body = (err.message || err.error);
break;

View File

@ -330,5 +330,17 @@ angular.module('copayApp.services')
storage.remove('amazonGiftCards-' + network, cb);
};
root.setAmazonLimits = function(network, limits, cb) {
storage.set('amazonLimits-' + network, limits, cb);
};
root.getAmazonLimits = function(network, cb) {
storage.get('amazonLimits-' + network, cb);
};
root.removeAmazonLimits = function(network, cb) {
storage.remove('amazonLimits-' + network, cb);
};
return root;
});