diff --git a/src/js/controllers/topup.js b/src/js/controllers/topup.js index 1cf81ab4df4..44c8e41484c 100644 --- a/src/js/controllers/topup.js +++ b/src/js/controllers/topup.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('topUpController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, bitpayService, bitpayCardService, payproService) { +angular.module('copayApp.controllers').controller('topUpController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, bitpayService, bitpayCardService, payproService, bwcError) { var amount; var currency; @@ -17,11 +17,11 @@ angular.module('copayApp.controllers').controller('topUpController', function($s }); }; - var showError = function(err) { + var showError = function(title, msg) { $scope.sendStatus = ''; - $log.error(err); - err = err.errors ? err.errors[0].message : err; - popupService.showAlert('Error', err); + $log.error(msg); + msg = msg.errors ? msg.errors[0].message : msg; + popupService.showAlert(title, msg); }; var publishAndSign = function (wallet, txp, onSendStatusChange, cb) { @@ -68,11 +68,17 @@ angular.module('copayApp.controllers').controller('topUpController', function($s $scope.network = bitpayService.getEnvironment().network; $scope.wallets = profileService.getWallets({ - m: 1, // Only 1-signature wallet onlyComplete: true, - network: $scope.network + network: $scope.network, + hasFunds: true, + minAmount: parsedAmount.amountSat }); - $scope.wallet = $scope.wallets[0]; // Default first wallet + + if (lodash.isEmpty($scope.wallets)) { + showErrorAndBack('Insufficient funds'); + return; + } + $scope.onWalletSelect($scope.wallets[0]); // Default first wallet bitpayCardService.getRates(currency, function(err, data) { if (err) $log.error(err); @@ -109,14 +115,14 @@ angular.module('copayApp.controllers').controller('topUpController', function($s bitpayCardService.topUp(cardId, dataSrc, function(err, invoiceId) { if (err) { ongoingProcess.set('topup', false, statusChangeHandler); - showError(err); + showError('Could not create the invoice', err); return; } bitpayCardService.getInvoice(invoiceId, function(err, invoice) { if (err) { ongoingProcess.set('topup', false, statusChangeHandler); - showError(err); + showError('Could not get the invoice', err); return; } @@ -124,14 +130,14 @@ angular.module('copayApp.controllers').controller('topUpController', function($s if (!payProUrl) { ongoingProcess.set('topup', false, statusChangeHandler); - showError('Error fetching invoice'); + showError('Error in Payment Protocol', 'Invalid URL'); return; } payproService.getPayProDetails(payProUrl, function(err, payProDetails) { if (err) { ongoingProcess.set('topup', false, statusChangeHandler); - showError(err); + showError('Error fetching invoice', err); return; } diff --git a/src/js/services/bitpayCardService.js b/src/js/services/bitpayCardService.js index 7131d5c2fea..ed3157cf6cc 100644 --- a/src/js/services/bitpayCardService.js +++ b/src/js/services/bitpayCardService.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('bitpayCardService', function($log, $rootScope, $filter, lodash, storageService, bitauthService, platformInfo, moment, appIdentityService, bitpayService, nextStepsService, configService, txFormatService, appConfigService) { +angular.module('copayApp.services').factory('bitpayCardService', function($log, $rootScope, $filter, lodash, storageService, bitauthService, platformInfo, moment, appIdentityService, bitpayService, nextStepsService, configService, txFormatService, appConfigService, rateService) { var root = {}; var _setError = function(msg, e) { @@ -44,12 +44,14 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log, var satToBtc = 1 / 100000000; var unitToSatoshi = config.unitToSatoshi; var amountUnitStr; + var amountSat; // IF 'USD' if (currency) { amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency; + amountSat = rateService.fromFiat(amount, currency).toFixed(0); } else { - var amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); + amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); amountUnitStr = txFormatService.formatAmountStr(amountSat); // convert unit to BTC amount = (amountSat * satToBtc).toFixed(8); @@ -59,6 +61,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log, return { amount: amount, currency: currency, + amountSat: amountSat, amountUnitStr: amountUnitStr }; }; diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index cb9a9b327e3..e5af8bc2255 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -762,6 +762,18 @@ angular.module('copayApp.services') }); } + if (opts.hasFunds) { + ret = lodash.filter(ret, function(w) { + return (w.status.availableBalanceSat > 0); + }); + } + + if (opts.minAmount) { + ret = lodash.filter(ret, function(w) { + return (w.status.availableBalanceSat > opts.minAmount); + }); + } + if (opts.onlyComplete) { ret = lodash.filter(ret, function(w) { return w.isComplete(); diff --git a/www/views/topup.html b/www/views/topup.html index d5da3031648..28ae3d32d60 100644 --- a/www/views/topup.html +++ b/www/views/topup.html @@ -63,7 +63,7 @@ ng-if="!isCordova && cardInfo" click-send-status="sendStatus" has-wallet-chosen="wallet" - insufficient-funds="false" + insufficient-funds="insufficientFunds" no-matching-wallet="!cardInfo"> Add funds @@ -73,7 +73,7 @@ slide-on-confirm="topUpConfirm()" slide-send-status="sendStatus" has-wallet-chosen="wallet" - insufficient-funds="false" + insufficient-funds="insufficientFunds" no-matching-wallet="!cardInfo"> Slide to confirm @@ -81,9 +81,11 @@ slide-success-show="sendStatus === 'success'" slide-success-on-confirm="goBackHome()" slide-success-hide-on-confirm="true"> - Sent + Sent + Success