2016-08-10 15:29:31 -03:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
2016-09-06 15:14:07 -03:00
|
|
|
|
angular.module('copayApp.controllers').controller('bitpayCardController', function($scope, $timeout, $log, lodash, bitpayCardService, configService, profileService, walletService, ongoingProcess, pbkdf2Service, moment, popupService, gettextCatalog, bwcError) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
|
|
|
|
|
var self = this;
|
2016-09-28 21:09:41 -03:00
|
|
|
|
$scope.dateRange = 'last30Days';
|
|
|
|
|
|
$scope.network = bitpayCardService.getEnvironment();
|
|
|
|
|
|
|
|
|
|
|
|
bitpayCardService.getCacheData(function(err, data) {
|
|
|
|
|
|
if (err || lodash.isEmpty(data)) return;
|
|
|
|
|
|
$scope.bitpayCardCached = true;
|
|
|
|
|
|
self.bitpayCardTransactionHistory = data.transactions;
|
|
|
|
|
|
self.bitpayCardCurrentBalance = data.balance;
|
2016-08-29 17:01:34 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
2016-08-10 15:29:31 -03:00
|
|
|
|
var processTransactions = function(invoices, history) {
|
|
|
|
|
|
for (var i = 0; i < invoices.length; i++) {
|
|
|
|
|
|
var matched = false;
|
|
|
|
|
|
for (var j = 0; j < history.length; j++) {
|
|
|
|
|
|
if (history[j].description[0].indexOf(invoices[i].id) > -1) {
|
|
|
|
|
|
matched = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!matched && ['paid', 'confirmed', 'complete'].indexOf(invoices[i].status) > -1) {
|
|
|
|
|
|
|
|
|
|
|
|
history.unshift({
|
|
|
|
|
|
timestamp: invoices[i].invoiceTime,
|
|
|
|
|
|
description: invoices[i].itemDesc,
|
|
|
|
|
|
amount: invoices[i].price,
|
|
|
|
|
|
type: '00611 = Client Funded Deposit',
|
|
|
|
|
|
pending: true,
|
|
|
|
|
|
status: invoices[i].status
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return history;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var setDateRange = function(preset) {
|
|
|
|
|
|
var startDate, endDate;
|
|
|
|
|
|
preset = preset || 'last30Days';
|
|
|
|
|
|
switch(preset) {
|
|
|
|
|
|
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
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.update = function() {
|
|
|
|
|
|
var dateRange = setDateRange($scope.dateRange);
|
|
|
|
|
|
self.loadingSession = true;
|
|
|
|
|
|
bitpayCardService.isAuthenticated(function(err, bpSession) {
|
|
|
|
|
|
self.loadingSession = false;
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.bitpayCardAuthenticated = bpSession.isAuthenticated;
|
|
|
|
|
|
self.bitpayCardTwoFactorPending = bpSession.twoFactorPending ? true : false;
|
|
|
|
|
|
|
|
|
|
|
|
if (self.bitpayCardTwoFactorPending) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (self.bitpayCardAuthenticated) {
|
|
|
|
|
|
$scope.loadingHistory = true;
|
|
|
|
|
|
bitpayCardService.invoiceHistory(function(err, invoices) {
|
|
|
|
|
|
if (err) $log.error(err);
|
|
|
|
|
|
bitpayCardService.transactionHistory(dateRange, function(err, history) {
|
|
|
|
|
|
$scope.loadingHistory = false;
|
|
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get transactions'));
|
2016-08-10 15:29:31 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.bitpayCardTransactionHistory = processTransactions(invoices, history.transactionList);
|
|
|
|
|
|
self.bitpayCardCurrentBalance = history.currentCardBalance;
|
2016-09-28 21:09:41 -03:00
|
|
|
|
|
|
|
|
|
|
var cacheData = {
|
|
|
|
|
|
balance: self.bitpayCardCurrentBalance,
|
|
|
|
|
|
transactions: self.bitpayCardTransactionHistory
|
|
|
|
|
|
};
|
|
|
|
|
|
bitpayCardService.setCacheData(cacheData, function(err) {
|
2016-10-05 11:42:40 -03:00
|
|
|
|
$scope.bitpayCardCached = true;
|
2016-09-28 21:09:41 -03:00
|
|
|
|
if (err) $log.error(err);
|
|
|
|
|
|
});
|
2016-08-10 15:29:31 -03:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-09-28 21:09:41 -03:00
|
|
|
|
this.authenticate = function(email, password) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
|
|
|
|
|
var data = {
|
2016-09-28 21:09:41 -03:00
|
|
|
|
emailAddress : email,
|
|
|
|
|
|
hashedPassword : pbkdf2Service.pbkdf2Sync(password, '..............', 200, 64).toString('hex')
|
2016-08-10 15:29:31 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// POST /authenticate
|
|
|
|
|
|
// emailAddress:
|
|
|
|
|
|
// hashedPassword:
|
|
|
|
|
|
self.authenticating = true;
|
|
|
|
|
|
bitpayCardService.authenticate(data, function(err, auth) {
|
|
|
|
|
|
self.authenticating = false;
|
2016-08-29 17:01:34 -03:00
|
|
|
|
if (err && err.data && err.data.error && !err.data.error.twoFactorPending) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), err.statusText || err.data.error || 'Authentiation error');
|
2016-08-10 15:29:31 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.update();
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-09-28 21:09:41 -03:00
|
|
|
|
this.authenticate2FA = function(twoFactorCode) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
|
|
|
|
|
var data = {
|
2016-09-28 21:09:41 -03:00
|
|
|
|
twoFactorCode : twoFactorCode
|
2016-08-10 15:29:31 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
self.authenticating = true;
|
|
|
|
|
|
bitpayCardService.authenticate2FA(data, function(err, auth) {
|
|
|
|
|
|
self.authenticating = false;
|
|
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Authentiation error'));
|
2016-08-10 15:29:31 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.update();
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.getMerchantInfo = function(tx) {
|
|
|
|
|
|
var bpTranCodes = bitpayCardService.bpTranCodes;
|
|
|
|
|
|
lodash.keys(bpTranCodes).forEach(function(code) {
|
|
|
|
|
|
if (tx.type.indexOf(code) === 0) {
|
|
|
|
|
|
lodash.assign(tx, bpTranCodes[code]);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.getIconName = function(tx) {
|
|
|
|
|
|
var icon = tx.mcc || tx.category || null;
|
|
|
|
|
|
if (!icon) return 'default';
|
|
|
|
|
|
return bitpayCardService.iconMap[icon];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.processDescription = function(tx) {
|
|
|
|
|
|
if (lodash.isArray(tx.description)) {
|
|
|
|
|
|
return tx.description[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
return tx.description;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-09-28 11:08:08 -03:00
|
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data){
|
|
|
|
|
|
self.update();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2016-08-10 15:29:31 -03:00
|
|
|
|
});
|
|
|
|
|
|
|