2017-02-17 13:36:51 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-06-05 10:37:35 -03:00
|
|
|
angular.module('copayApp.controllers').controller('topUpController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicConfig, lodash, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, bitpayService, bitpayCardService, payproService, bwcError, txFormatService, sendMaxService) {
|
2017-02-17 13:36:51 -03:00
|
|
|
|
|
|
|
|
var amount;
|
|
|
|
|
var currency;
|
|
|
|
|
var cardId;
|
2017-04-16 22:23:20 -03:00
|
|
|
var sendMax;
|
2017-02-17 13:36:51 -03:00
|
|
|
|
|
|
|
|
$scope.isCordova = platformInfo.isCordova;
|
|
|
|
|
|
2017-05-15 14:26:49 -03:00
|
|
|
$scope.$on("$ionicView.beforeLeave", function(event, data) {
|
|
|
|
|
$ionicConfig.views.swipeBackEnabled(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.enter", function(event, data) {
|
|
|
|
|
$ionicConfig.views.swipeBackEnabled(false);
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-20 13:27:15 -03:00
|
|
|
var showErrorAndBack = function(title, msg) {
|
|
|
|
|
title = title || 'Error';
|
2017-02-17 13:36:51 -03:00
|
|
|
$scope.sendStatus = '';
|
2017-04-20 13:27:15 -03:00
|
|
|
$log.error(msg);
|
|
|
|
|
msg = msg.errors ? msg.errors[0].message : msg;
|
|
|
|
|
popupService.showAlert(title, msg, function() {
|
2017-02-17 13:36:51 -03:00
|
|
|
$ionicHistory.goBack();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-03-20 14:53:18 -03:00
|
|
|
var showError = function(title, msg) {
|
2017-04-20 13:27:15 -03:00
|
|
|
title = title || 'Error';
|
2017-02-17 13:36:51 -03:00
|
|
|
$scope.sendStatus = '';
|
2017-03-20 14:53:18 -03:00
|
|
|
$log.error(msg);
|
|
|
|
|
msg = msg.errors ? msg.errors[0].message : msg;
|
|
|
|
|
popupService.showAlert(title, msg);
|
2017-02-17 13:36:51 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var publishAndSign = function (wallet, txp, onSendStatusChange, cb) {
|
|
|
|
|
if (!wallet.canSign() && !wallet.isPrivKeyExternal()) {
|
|
|
|
|
var err = 'No signing proposal: No private key';
|
|
|
|
|
$log.info(err);
|
|
|
|
|
return cb(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
walletService.publishAndSign(wallet, txp, function(err, txp) {
|
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
return cb(null, txp);
|
|
|
|
|
}, onSendStatusChange);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var statusChangeHandler = function (processName, showName, isOn) {
|
|
|
|
|
$log.debug('statusChangeHandler: ', processName, showName, isOn);
|
|
|
|
|
if ( processName == 'topup' && !isOn) {
|
|
|
|
|
$scope.sendStatus = 'success';
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
}, 100);
|
|
|
|
|
} else if (showName) {
|
|
|
|
|
$scope.sendStatus = showName;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
|
|
|
|
cardId = data.stateParams.id;
|
2017-04-16 22:23:20 -03:00
|
|
|
sendMax = data.stateParams.useSendMax;
|
2017-02-17 13:36:51 -03:00
|
|
|
|
|
|
|
|
if (!cardId) {
|
2017-04-20 13:27:15 -03:00
|
|
|
showErrorAndBack(null, 'No card selected');
|
2017-02-17 13:36:51 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2017-03-27 11:31:46 -03:00
|
|
|
|
|
|
|
|
var parsedAmount = txFormatService.parseAmount(
|
2017-02-17 13:36:51 -03:00
|
|
|
data.stateParams.amount,
|
|
|
|
|
data.stateParams.currency);
|
|
|
|
|
|
|
|
|
|
amount = parsedAmount.amount;
|
|
|
|
|
currency = parsedAmount.currency;
|
|
|
|
|
$scope.amountUnitStr = parsedAmount.amountUnitStr;
|
|
|
|
|
|
|
|
|
|
$scope.network = bitpayService.getEnvironment().network;
|
|
|
|
|
$scope.wallets = profileService.getWallets({
|
|
|
|
|
onlyComplete: true,
|
2017-03-20 14:53:18 -03:00
|
|
|
network: $scope.network,
|
|
|
|
|
hasFunds: true,
|
|
|
|
|
minAmount: parsedAmount.amountSat
|
2017-02-17 13:36:51 -03:00
|
|
|
});
|
2017-03-20 14:53:18 -03:00
|
|
|
|
|
|
|
|
if (lodash.isEmpty($scope.wallets)) {
|
2017-04-20 13:27:15 -03:00
|
|
|
showErrorAndBack(null, 'Insufficient funds');
|
2017-03-20 14:53:18 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$scope.onWalletSelect($scope.wallets[0]); // Default first wallet
|
2017-02-17 13:36:51 -03:00
|
|
|
|
2017-06-01 17:16:42 -03:00
|
|
|
var currencyCode = bitpayCardService.getAvailableCurrency();
|
|
|
|
|
var code;
|
|
|
|
|
switch(currencyCode) {
|
|
|
|
|
case 'EUR':
|
2017-06-05 10:37:35 -03:00
|
|
|
$scope.currencySymbol = '€';
|
2017-06-01 17:16:42 -03:00
|
|
|
break;
|
|
|
|
|
case 'GBP':
|
2017-06-05 10:37:35 -03:00
|
|
|
$scope.currencySymbol = '£';
|
2017-06-01 17:16:42 -03:00
|
|
|
break;
|
2017-06-05 10:37:35 -03:00
|
|
|
default : $scope.currencySymbol = '$';
|
2017-06-01 17:16:42 -03:00
|
|
|
};
|
|
|
|
|
bitpayCardService.getRates(currencyCode, function(err, data) {
|
2017-04-20 13:27:15 -03:00
|
|
|
if (err) $log.error(err);
|
|
|
|
|
$scope.rate = data.rate;
|
|
|
|
|
});
|
|
|
|
|
|
2017-02-17 13:36:51 -03:00
|
|
|
bitpayCardService.get({ cardId: cardId, noRefresh: true }, function(err, card) {
|
|
|
|
|
if (err) {
|
2017-04-20 13:27:15 -03:00
|
|
|
showErrorAndBack(null, err);
|
2017-02-17 13:36:51 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$scope.cardInfo = card[0];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.topUpConfirm = function() {
|
|
|
|
|
|
|
|
|
|
var config = configService.getSync();
|
|
|
|
|
var configWallet = config.wallet;
|
|
|
|
|
var walletSettings = configWallet.settings;
|
|
|
|
|
|
|
|
|
|
var message = 'Add ' + amount + ' ' + currency + ' to debit card';
|
|
|
|
|
var okText = 'Confirm';
|
|
|
|
|
var cancelText = 'Cancel';
|
|
|
|
|
popupService.showConfirm(null, message, okText, cancelText, function(ok) {
|
|
|
|
|
if (!ok) return;
|
|
|
|
|
|
|
|
|
|
var dataSrc = {
|
|
|
|
|
amount: amount,
|
|
|
|
|
currency: currency
|
|
|
|
|
};
|
|
|
|
|
ongoingProcess.set('topup', true, statusChangeHandler);
|
|
|
|
|
bitpayCardService.topUp(cardId, dataSrc, function(err, invoiceId) {
|
|
|
|
|
if (err) {
|
|
|
|
|
ongoingProcess.set('topup', false, statusChangeHandler);
|
2017-04-20 13:27:15 -03:00
|
|
|
showErrorAndBack('Could not create the invoice', err);
|
2017-02-17 13:36:51 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bitpayCardService.getInvoice(invoiceId, function(err, invoice) {
|
|
|
|
|
if (err) {
|
|
|
|
|
ongoingProcess.set('topup', false, statusChangeHandler);
|
2017-03-20 14:53:18 -03:00
|
|
|
showError('Could not get the invoice', err);
|
2017-02-17 13:36:51 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var payProUrl = (invoice && invoice.paymentUrls) ? invoice.paymentUrls.BIP73 : null;
|
|
|
|
|
|
|
|
|
|
if (!payProUrl) {
|
|
|
|
|
ongoingProcess.set('topup', false, statusChangeHandler);
|
2017-03-20 14:53:18 -03:00
|
|
|
showError('Error in Payment Protocol', 'Invalid URL');
|
2017-02-17 13:36:51 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
payproService.getPayProDetails(payProUrl, function(err, payProDetails) {
|
|
|
|
|
if (err) {
|
|
|
|
|
ongoingProcess.set('topup', false, statusChangeHandler);
|
2017-03-20 14:53:18 -03:00
|
|
|
showError('Error fetching invoice', err);
|
2017-02-17 13:36:51 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var outputs = [];
|
|
|
|
|
var toAddress = payProDetails.toAddress;
|
|
|
|
|
var amountSat = payProDetails.amount;
|
2017-04-23 22:30:04 -03:00
|
|
|
var comment = 'Top up ' + amount + ' ' + currency + ' to Debit Card (' + $scope.cardInfo.lastFourDigits + ')';
|
2017-02-17 13:36:51 -03:00
|
|
|
|
|
|
|
|
outputs.push({
|
|
|
|
|
'toAddress': toAddress,
|
|
|
|
|
'amount': amountSat,
|
|
|
|
|
'message': comment
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var txp = {
|
|
|
|
|
toAddress: toAddress,
|
|
|
|
|
amount: amountSat,
|
|
|
|
|
outputs: outputs,
|
|
|
|
|
message: comment,
|
|
|
|
|
payProUrl: payProUrl,
|
|
|
|
|
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
|
|
|
|
|
feeLevel: walletSettings.feeLevel || 'normal'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
walletService.createTx($scope.wallet, txp, function(err, ctxp) {
|
|
|
|
|
if (err) {
|
|
|
|
|
ongoingProcess.set('topup', false, statusChangeHandler);
|
|
|
|
|
showError('Could not create transaction', bwcError.msg(err));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
publishAndSign($scope.wallet, ctxp, function() {}, function(err, txSent) {
|
|
|
|
|
ongoingProcess.set('topup', false, statusChangeHandler);
|
|
|
|
|
if (err) {
|
|
|
|
|
showError('Could not send transaction', err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}, true); // Disable loader
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.showWalletSelector = function() {
|
|
|
|
|
$scope.walletSelectorTitle = 'From';
|
|
|
|
|
$scope.showWallets = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.onWalletSelect = function(wallet) {
|
|
|
|
|
$scope.wallet = wallet;
|
2017-04-16 22:23:20 -03:00
|
|
|
if (sendMax) {
|
|
|
|
|
ongoingProcess.set('retrievingInputs', true);
|
|
|
|
|
sendMaxService.getInfo($scope.wallet, function(err, values) {
|
|
|
|
|
ongoingProcess.set('retrievingInputs', false);
|
|
|
|
|
if (err) {
|
2017-04-20 13:27:15 -03:00
|
|
|
showErrorAndBack(null, err);
|
2017-04-16 22:23:20 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var config = configService.getSync().wallet.settings;
|
|
|
|
|
var unitName = config.unitName;
|
|
|
|
|
var amountUnit = txFormatService.satToUnit(values.amount);
|
|
|
|
|
var parsedAmount = txFormatService.parseAmount(
|
|
|
|
|
amountUnit,
|
|
|
|
|
unitName);
|
|
|
|
|
|
|
|
|
|
amount = parsedAmount.amount;
|
|
|
|
|
currency = parsedAmount.currency;
|
|
|
|
|
$scope.amountUnitStr = parsedAmount.amountUnitStr;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
}, 100);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-02-17 13:36:51 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.goBackHome = function() {
|
|
|
|
|
$scope.sendStatus = '';
|
|
|
|
|
$ionicHistory.nextViewOptions({
|
|
|
|
|
disableAnimate: true,
|
|
|
|
|
historyRoot: true
|
|
|
|
|
});
|
|
|
|
|
$ionicHistory.clearHistory();
|
|
|
|
|
$state.go('tabs.home').then(function() {
|
|
|
|
|
$state.transitionTo('tabs.bitpayCard', {id: cardId});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
});
|