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-08-24 19:00:50 -03:00
|
|
|
|
var wallet;
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
2016-08-29 17:01:34 -03:00
|
|
|
|
$scope.$on('Wallet/Changed', function(event, w) {
|
|
|
|
|
|
if (lodash.isEmpty(w)) {
|
|
|
|
|
|
$log.debug('No wallet provided');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
wallet = w;
|
|
|
|
|
|
$log.debug('Wallet changed: ' + w.name);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.init = function() {
|
|
|
|
|
|
$scope.dateRange = 'last30Days';
|
|
|
|
|
|
|
2016-08-24 19:00:50 -03:00
|
|
|
|
$scope.network = bitpayCardService.getEnvironment();
|
2016-08-29 17:01:34 -03:00
|
|
|
|
$scope.wallets = profileService.getWallets({
|
2016-08-24 19:00:50 -03:00
|
|
|
|
network: $scope.network,
|
2016-08-22 17:43:31 -03:00
|
|
|
|
onlyComplete: true
|
|
|
|
|
|
});
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
2016-08-24 19:00:50 -03:00
|
|
|
|
self.update();
|
|
|
|
|
|
|
2016-08-29 17:01:34 -03:00
|
|
|
|
wallet = $scope.wallets[0];
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
2016-08-29 17:01:34 -03:00
|
|
|
|
if (wallet && wallet.credentials.n > 1)
|
2016-08-10 15:29:31 -03:00
|
|
|
|
self.isMultisigWallet = true;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.sendFunds = function() {
|
2016-08-29 17:01:34 -03:00
|
|
|
|
if (lodash.isEmpty(wallet)) return;
|
|
|
|
|
|
|
2016-08-24 19:00:50 -03:00
|
|
|
|
if (!wallet.canSign() && !wallet.isPrivKeyExternal()) {
|
|
|
|
|
|
$log.info('No signing proposal: No private key');
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg('MISSING_PRIVATE_KEY'));
|
2016-08-24 19:00:50 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-10 15:29:31 -03:00
|
|
|
|
var dataSrc = {
|
|
|
|
|
|
amount: $scope.fiat,
|
|
|
|
|
|
currency: 'USD'
|
|
|
|
|
|
};
|
|
|
|
|
|
var outputs = [];
|
|
|
|
|
|
var config = configService.getSync();
|
|
|
|
|
|
var configWallet = config.wallet;
|
|
|
|
|
|
var walletSettings = configWallet.settings;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ongoingProcess.set('Processing Transaction...', true);
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
|
|
|
|
|
|
bitpayCardService.topUp(dataSrc, function(err, invoiceId) {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
ongoingProcess.set('Processing Transaction...', false);
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err));
|
2016-08-10 15:29:31 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bitpayCardService.getInvoice(invoiceId, function(err, data) {
|
2016-08-24 19:00:50 -03:00
|
|
|
|
var address, comment, amount;
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
2016-08-24 19:00:50 -03:00
|
|
|
|
address = data.bitcoinAddress;
|
|
|
|
|
|
amount = parseInt((data.btcPrice * 100000000).toFixed(0));
|
|
|
|
|
|
comment = data.itemDesc;
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
2016-08-24 19:00:50 -03:00
|
|
|
|
outputs.push({
|
|
|
|
|
|
'toAddress': address,
|
|
|
|
|
|
'amount': amount,
|
|
|
|
|
|
'message': comment
|
|
|
|
|
|
});
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
2016-08-24 19:00:50 -03:00
|
|
|
|
var txp = {
|
|
|
|
|
|
toAddress: address,
|
|
|
|
|
|
amount: amount,
|
|
|
|
|
|
outputs: outputs,
|
|
|
|
|
|
message: comment,
|
|
|
|
|
|
payProUrl: null,
|
|
|
|
|
|
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
|
|
|
|
|
|
feeLevel: walletSettings.feeLevel || 'normal'
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
walletService.createTx(wallet, txp, function(err, createdTxp) {
|
2016-09-06 15:14:07 -03:00
|
|
|
|
ongoingProcess.set('Processing Transaction...', false);
|
2016-08-24 19:00:50 -03:00
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err));
|
2016-08-24 19:00:50 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-08-29 18:54:14 -03:00
|
|
|
|
walletService.publishAndSign(wallet, createdTxp, function(err, tx) {
|
2016-08-24 19:00:50 -03:00
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err));
|
2016-08-24 19:00:50 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
self.update();
|
|
|
|
|
|
$scope.addFunds = false;
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$digest();
|
2016-08-10 15:29:31 -03:00
|
|
|
|
});
|
2016-08-24 19:00:50 -03:00
|
|
|
|
});
|
2016-08-10 15:29:31 -03:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.authenticate = function() {
|
|
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
|
emailAddress : $scope.email,
|
2016-08-10 16:08:11 -03:00
|
|
|
|
hashedPassword : pbkdf2Service.pbkdf2Sync($scope.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);
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.authenticate2FA = function() {
|
|
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
|
twoFactorCode : $scope.twoFactorCode
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|