2016-08-10 15:29:31 -03:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
2017-05-24 20:20:11 -03:00
|
|
|
|
angular.module('copayApp.controllers').controller('bitpayCardController', function($scope, $timeout, $log, $state, lodash, bitpayCardService, moment, popupService, gettextCatalog, $ionicHistory, bitpayService, externalLinkService) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
|
|
|
|
|
var self = this;
|
2016-10-21 20:33:28 -03:00
|
|
|
|
var runningBalance;
|
2016-10-26 17:10:21 -03:00
|
|
|
|
$scope.dateRange = {
|
|
|
|
|
|
value: 'last30Days'
|
|
|
|
|
|
};
|
2017-01-05 16:29:17 -05:00
|
|
|
|
$scope.network = bitpayService.getEnvironment().network;
|
2016-09-28 21:09:41 -03:00
|
|
|
|
|
2016-08-10 15:29:31 -03:00
|
|
|
|
var setDateRange = function(preset) {
|
|
|
|
|
|
var startDate, endDate;
|
2016-10-26 17:10:21 -03:00
|
|
|
|
preset = preset || 'last30Days';
|
|
|
|
|
|
switch (preset) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
case 'last30Days':
|
|
|
|
|
|
startDate = moment().subtract(30, 'days').toISOString();
|
|
|
|
|
|
endDate = moment().toISOString();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'lastMonth':
|
|
|
|
|
|
startDate = moment().startOf('month').subtract(1, 'month').toISOString();
|
|
|
|
|
|
endDate = moment().startOf('month').toISOString();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'all':
|
|
|
|
|
|
startDate = null;
|
|
|
|
|
|
endDate = null;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
startDate: startDate,
|
|
|
|
|
|
endDate: endDate
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-17 10:01:51 -03:00
|
|
|
|
var setGetStarted = function(history, cb) {
|
2017-01-31 14:24:13 -03:00
|
|
|
|
|
|
|
|
|
|
// Is the card new?
|
2017-05-24 20:20:11 -03:00
|
|
|
|
if (!lodash.isEmpty(history.transactionList))
|
2017-01-31 14:24:13 -03:00
|
|
|
|
return cb();
|
|
|
|
|
|
|
|
|
|
|
|
var dateRange = setDateRange('all');
|
|
|
|
|
|
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
|
|
|
|
|
|
|
2017-05-24 20:20:11 -03:00
|
|
|
|
if (!err && lodash.isEmpty(history.transactionList))
|
|
|
|
|
|
self.getStarted = true;
|
|
|
|
|
|
|
2017-01-31 14:24:13 -03:00
|
|
|
|
return cb();
|
|
|
|
|
|
});
|
2016-10-16 21:22:54 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-10 15:29:31 -03:00
|
|
|
|
this.update = function() {
|
2016-10-13 17:49:48 -03:00
|
|
|
|
var dateRange = setDateRange($scope.dateRange.value);
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
2016-10-06 19:23:39 -03:00
|
|
|
|
$scope.loadingHistory = true;
|
|
|
|
|
|
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
|
2017-01-31 14:24:13 -03:00
|
|
|
|
|
2016-10-06 19:23:39 -03:00
|
|
|
|
$scope.loadingHistory = false;
|
2016-10-16 21:22:54 -03:00
|
|
|
|
|
2016-10-11 14:50:35 -03:00
|
|
|
|
if (err) {
|
|
|
|
|
|
$log.error(err);
|
2017-05-24 20:20:11 -03:00
|
|
|
|
self.bitpayCardTransactionHistoryCompleted = null;
|
|
|
|
|
|
self.bitpayCardTransactionHistoryConfirming = null;
|
|
|
|
|
|
self.bitpayCardTransactionHistoryPreAuth = null;
|
|
|
|
|
|
self.underpaidInvoiceInList = null;
|
|
|
|
|
|
self.delayedInvoiceInList = null;
|
2017-01-31 14:24:13 -03:00
|
|
|
|
self.balance = null;
|
2016-11-21 17:24:42 -05:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get transactions'));
|
2016-08-10 15:29:31 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-31 14:24:13 -03:00
|
|
|
|
setGetStarted(history, function() {
|
2016-10-17 10:01:51 -03:00
|
|
|
|
|
|
|
|
|
|
var txs = lodash.clone(history.txs);
|
2016-10-21 20:33:28 -03:00
|
|
|
|
runningBalance = parseFloat(history.endingBalance);
|
2016-10-17 10:01:51 -03:00
|
|
|
|
for (var i = 0; i < txs.length; i++) {
|
|
|
|
|
|
txs[i] = _getMerchantInfo(txs[i]);
|
|
|
|
|
|
txs[i].icon = _getIconName(txs[i]);
|
|
|
|
|
|
txs[i].desc = _processDescription(txs[i]);
|
2016-10-21 20:33:28 -03:00
|
|
|
|
txs[i].price = _price(txs[i]);
|
|
|
|
|
|
txs[i].runningBalance = runningBalance;
|
|
|
|
|
|
_runningBalance(txs[i]);
|
2017-05-26 10:15:24 -04:00
|
|
|
|
|
|
|
|
|
|
if (txs[i].merchant.city && txs[i].merchant.state) {
|
|
|
|
|
|
txs[i].merchant.location = txs[i].merchant.city + ', ' + txs[i].merchant.state;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
txs[i].merchant.location = txs[i].merchant.city || txs[i].merchant.state || '';
|
|
|
|
|
|
}
|
2016-10-17 10:01:51 -03:00
|
|
|
|
}
|
2017-05-24 20:20:11 -03:00
|
|
|
|
self.bitpayCardTransactionHistoryCompleted = lodash.filter(txs, function(tx) {
|
|
|
|
|
|
return !tx.pending && tx.type.indexOf('93') == -1;
|
|
|
|
|
|
});
|
|
|
|
|
|
self.bitpayCardTransactionHistoryConfirming = lodash.filter(txs, function(tx) {
|
|
|
|
|
|
return tx.pending;
|
|
|
|
|
|
});
|
|
|
|
|
|
self.bitpayCardTransactionHistoryPreAuth = lodash.includes(txs, function(tx) {
|
|
|
|
|
|
return tx.type.indexOf('93') > -1;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
lodash.forEach(self.bitpayCardTransactionHistoryConfirming, function(tx) {
|
|
|
|
|
|
if (lodash.includes(tx, 'paidPartial'))
|
|
|
|
|
|
self.underpaidInvoiceInList = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
lodash.forEach(self.bitpayCardTransactionHistoryConfirming, function(tx) {
|
|
|
|
|
|
if (lodash.includes(tx, 'paid') || lodash.includes(tx, 'invalid'))
|
|
|
|
|
|
self.delayedInvoiceInList = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2017-01-31 14:24:13 -03:00
|
|
|
|
self.balance = history.currentCardBalance;
|
|
|
|
|
|
self.updatedOn = null;
|
2016-10-17 10:01:51 -03:00
|
|
|
|
|
|
|
|
|
|
if ($scope.dateRange.value == 'last30Days') {
|
2017-01-27 17:54:41 -03:00
|
|
|
|
|
2017-01-31 14:24:13 -03:00
|
|
|
|
// TODO?
|
|
|
|
|
|
// $log.debug('BitPay Card: storing cache history');
|
2017-01-27 17:54:41 -03:00
|
|
|
|
// var cacheHistory = {
|
|
|
|
|
|
// balance: history.currentCardBalance,
|
|
|
|
|
|
// transactions: history.txs
|
|
|
|
|
|
// };
|
|
|
|
|
|
// bitpayCardService.setHistory($scope.cardId, cacheHistory, {}, function(err) {
|
|
|
|
|
|
// if (err) $log.error(err);
|
|
|
|
|
|
// $scope.historyCached = true;
|
|
|
|
|
|
// });
|
2016-10-17 10:01:51 -03:00
|
|
|
|
}
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$apply();
|
2016-10-13 17:49:48 -03:00
|
|
|
|
});
|
2016-10-07 13:51:55 -03:00
|
|
|
|
});
|
2016-08-10 15:29:31 -03:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-13 17:49:48 -03:00
|
|
|
|
var _getMerchantInfo = function(tx) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
var bpTranCodes = bitpayCardService.bpTranCodes;
|
|
|
|
|
|
lodash.keys(bpTranCodes).forEach(function(code) {
|
|
|
|
|
|
if (tx.type.indexOf(code) === 0) {
|
|
|
|
|
|
lodash.assign(tx, bpTranCodes[code]);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2016-10-13 17:49:48 -03:00
|
|
|
|
return tx;
|
2016-08-10 15:29:31 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-13 17:49:48 -03:00
|
|
|
|
var _getIconName = function(tx) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
var icon = tx.mcc || tx.category || null;
|
2017-06-02 16:05:38 -04:00
|
|
|
|
if (!icon || bitpayCardService.iconMap[icon] == undefined) return 'default';
|
2016-08-10 15:29:31 -03:00
|
|
|
|
return bitpayCardService.iconMap[icon];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-13 17:49:48 -03:00
|
|
|
|
var _processDescription = function(tx) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
if (lodash.isArray(tx.description)) {
|
|
|
|
|
|
return tx.description[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
return tx.description;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-21 20:33:28 -03:00
|
|
|
|
var _price = function(tx) {
|
2017-05-24 20:20:11 -03:00
|
|
|
|
var price = tx.fee ? parseFloat(tx.amount) + parseFloat(tx.fee) : parseFloat(tx.amount);
|
|
|
|
|
|
return price;
|
2016-10-21 20:33:28 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var _runningBalance = function(tx) {
|
|
|
|
|
|
runningBalance -= parseFloat(tx.amount);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-05-24 20:20:11 -03:00
|
|
|
|
this.openExternalLink = function(url) {
|
|
|
|
|
|
var optIn = true;
|
|
|
|
|
|
var title = null;
|
|
|
|
|
|
var message = gettextCatalog.getString('Help and support information is available at the website.');
|
|
|
|
|
|
var okText = gettextCatalog.getString('Open');
|
|
|
|
|
|
var cancelText = gettextCatalog.getString('Go Back');
|
|
|
|
|
|
externalLinkService.open(url, optIn, title, message, okText, cancelText);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.viewOnBlockchain = function(transactionId) {
|
|
|
|
|
|
var url = 'https://insight.bitpay.com/tx/' + transactionId;
|
|
|
|
|
|
var optIn = true;
|
|
|
|
|
|
var title = null;
|
|
|
|
|
|
var message = gettextCatalog.getString('View Transaction on Insight');
|
|
|
|
|
|
var okText = gettextCatalog.getString('Open Insight');
|
|
|
|
|
|
var cancelText = gettextCatalog.getString('Go Back');
|
|
|
|
|
|
externalLinkService.open(url, optIn, title, message, okText, cancelText);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-06 19:23:39 -03:00
|
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
|
|
|
|
|
$scope.cardId = data.stateParams.id;
|
2017-06-01 17:16:42 -03:00
|
|
|
|
$scope.currency = bitpayCardService.getAvailableCurrency();
|
2017-01-31 14:24:13 -03:00
|
|
|
|
|
2016-10-06 19:23:39 -03:00
|
|
|
|
if (!$scope.cardId) {
|
|
|
|
|
|
$ionicHistory.nextViewOptions({
|
|
|
|
|
|
disableAnimate: true
|
|
|
|
|
|
});
|
|
|
|
|
|
$state.go('tabs.home');
|
|
|
|
|
|
}
|
2016-09-28 11:08:08 -03:00
|
|
|
|
|
2017-05-24 20:20:11 -03:00
|
|
|
|
|
2017-01-31 14:24:13 -03:00
|
|
|
|
bitpayCardService.get({
|
|
|
|
|
|
cardId: $scope.cardId,
|
|
|
|
|
|
noRefresh: true,
|
|
|
|
|
|
}, function(err, cards) {
|
|
|
|
|
|
|
|
|
|
|
|
if (cards && cards[0]) {
|
|
|
|
|
|
self.lastFourDigits = cards[0].lastFourDigits;
|
|
|
|
|
|
self.balance = cards[0].balance;
|
2017-06-02 16:05:38 -04:00
|
|
|
|
self.currencySymbol = cards[0].currencySymbol;
|
2017-01-31 14:24:13 -03:00
|
|
|
|
self.updatedOn = cards[0].updatedOn;
|
|
|
|
|
|
}
|
|
|
|
|
|
self.update();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2016-08-10 15:29:31 -03:00
|
|
|
|
});
|