From bc2d530e906205537cbd418504e468520f861370 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Mon, 27 Mar 2017 11:31:46 -0300 Subject: [PATCH] Enable input in bits/BTC to reload the debit card --- src/js/controllers/amount.js | 2 +- src/js/controllers/buyAmazon.js | 7 ++++++- src/js/controllers/buyCoinbase.js | 11 ++++++++--- src/js/controllers/buyGlidera.js | 11 ++++++++--- src/js/controllers/sellCoinbase.js | 15 ++++++++++---- src/js/controllers/sellGlidera.js | 15 ++++++++++---- src/js/controllers/topup.js | 10 +++++----- src/js/routes.js | 3 +-- src/js/services/bitpayCardService.js | 29 +--------------------------- src/js/services/coinbaseService.js | 24 ----------------------- src/js/services/glideraService.js | 24 ----------------------- src/js/services/txFormatService.js | 29 ++++++++++++++++++++++++++++ www/views/topup.html | 5 +++-- 13 files changed, 84 insertions(+), 101 deletions(-) diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index 4202b8e01..777513df4 100644 --- a/src/js/controllers/amount.js +++ b/src/js/controllers/amount.js @@ -224,7 +224,7 @@ angular.module('copayApp.controllers').controller('amountController', function($ $state.transitionTo($scope.nextStep, { id: _cardId, amount: _amount, - currency: $scope.showAlternativeAmount ? $scope.alternativeIsoCode : '' + currency: $scope.showAlternativeAmount ? $scope.alternativeIsoCode : $scope.unitName }); } else { var amount = $scope.showAlternativeAmount ? fromFiat(_amount) : _amount; diff --git a/src/js/controllers/buyAmazon.js b/src/js/controllers/buyAmazon.js index b51d34763..a281ed342 100644 --- a/src/js/controllers/buyAmazon.js +++ b/src/js/controllers/buyAmazon.js @@ -112,8 +112,13 @@ angular.module('copayApp.controllers').controller('buyAmazonController', functio $scope.network = amazonService.getNetwork(); $scope.wallets = profileService.getWallets({ onlyComplete: true, - network: $scope.network + network: $scope.network, + hasFunds: true }); + if (lodash.isEmpty($scope.wallets)) { + showErrorAndBack('No wallets with funds'); + return; + } $scope.wallet = $scope.wallets[0]; // Default first wallet }); diff --git a/src/js/controllers/buyCoinbase.js b/src/js/controllers/buyCoinbase.js index b259552cd..8c1bd2ea9 100644 --- a/src/js/controllers/buyCoinbase.js +++ b/src/js/controllers/buyCoinbase.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('buyCoinbaseController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicScrollDelegate, lodash, coinbaseService, popupService, profileService, ongoingProcess, walletService) { +angular.module('copayApp.controllers').controller('buyCoinbaseController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicScrollDelegate, lodash, coinbaseService, popupService, profileService, ongoingProcess, walletService, txFormatService) { var amount; var currency; @@ -34,8 +34,8 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct }; $scope.$on("$ionicView.beforeEnter", function(event, data) { - $scope.isFiat = data.stateParams.currency ? true : false; - var parsedAmount = coinbaseService.parseAmount( + $scope.isFiat = data.stateParams.currency != 'bits' && data.stateParams.currency != 'BTC' ? true : false; + var parsedAmount = txFormatService.parseAmount( data.stateParams.amount, data.stateParams.currency); @@ -48,6 +48,11 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct onlyComplete: true, network: $scope.network }); + + if (lodash.isEmpty($scope.wallets)) { + showErrorAndBack('No wallets available'); + return; + } $scope.wallet = $scope.wallets[0]; // Default first wallet ongoingProcess.set('connectingCoinbase', true); diff --git a/src/js/controllers/buyGlidera.js b/src/js/controllers/buyGlidera.js index 7839685cc..33afa5c00 100644 --- a/src/js/controllers/buyGlidera.js +++ b/src/js/controllers/buyGlidera.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('buyGlideraController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, glideraService, popupService, profileService, ongoingProcess, walletService, platformInfo) { +angular.module('copayApp.controllers').controller('buyGlideraController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, glideraService, popupService, profileService, ongoingProcess, walletService, platformInfo, txFormatService) { var amount; var currency; @@ -36,8 +36,8 @@ angular.module('copayApp.controllers').controller('buyGlideraController', functi }; $scope.$on("$ionicView.beforeEnter", function(event, data) { - $scope.isFiat = data.stateParams.currency ? true : false; - var parsedAmount = glideraService.parseAmount( + $scope.isFiat = data.stateParams.currency != 'bits' && data.stateParams.currency != 'BTC' ? true : false; + var parsedAmount = txFormatService.parseAmount( data.stateParams.amount, data.stateParams.currency); @@ -50,6 +50,11 @@ angular.module('copayApp.controllers').controller('buyGlideraController', functi onlyComplete: true, network: $scope.network }); + + if (lodash.isEmpty($scope.wallets)) { + showErrorAndBack('No wallets available'); + return; + } $scope.wallet = $scope.wallets[0]; // Default first wallet ongoingProcess.set('connectingGlidera', true); diff --git a/src/js/controllers/sellCoinbase.js b/src/js/controllers/sellCoinbase.js index a336a4ac6..9e6bf2cd8 100644 --- a/src/js/controllers/sellCoinbase.js +++ b/src/js/controllers/sellCoinbase.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('sellCoinbaseController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicScrollDelegate, lodash, coinbaseService, popupService, profileService, ongoingProcess, walletService, appConfigService, configService) { +angular.module('copayApp.controllers').controller('sellCoinbaseController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicScrollDelegate, lodash, coinbaseService, popupService, profileService, ongoingProcess, walletService, appConfigService, configService, txFormatService) { var amount; var currency; @@ -117,8 +117,8 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController', func }; $scope.$on("$ionicView.beforeEnter", function(event, data) { - $scope.isFiat = data.stateParams.currency ? true : false; - var parsedAmount = coinbaseService.parseAmount( + $scope.isFiat = data.stateParams.currency != 'bits' && data.stateParams.currency != 'BTC' ? true : false; + var parsedAmount = txFormatService.parseAmount( data.stateParams.amount, data.stateParams.currency); @@ -133,8 +133,15 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController', func $scope.wallets = profileService.getWallets({ m: 1, // Only 1-signature wallet onlyComplete: true, - network: $scope.network + network: $scope.network, + hasFunds: true, + minAmount: parsedAmount.amountSat }); + + if (lodash.isEmpty($scope.wallets)) { + showErrorAndBack('Insufficient funds'); + return; + } $scope.wallet = $scope.wallets[0]; // Default first wallet ongoingProcess.set('connectingCoinbase', true); diff --git a/src/js/controllers/sellGlidera.js b/src/js/controllers/sellGlidera.js index 3028e0df3..119dac2fa 100644 --- a/src/js/controllers/sellGlidera.js +++ b/src/js/controllers/sellGlidera.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('sellGlideraController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, glideraService, popupService, profileService, ongoingProcess, walletService, configService, platformInfo) { +angular.module('copayApp.controllers').controller('sellGlideraController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, glideraService, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, txFormatService) { var amount; var currency; @@ -36,8 +36,8 @@ angular.module('copayApp.controllers').controller('sellGlideraController', funct }; $scope.$on("$ionicView.beforeEnter", function(event, data) { - $scope.isFiat = data.stateParams.currency ? true : false; - var parsedAmount = glideraService.parseAmount( + $scope.isFiat = data.stateParams.currency != 'bits' && data.stateParams.currency != 'BTC' ? true : false; + var parsedAmount = txFormatService.parseAmount( data.stateParams.amount, data.stateParams.currency); @@ -49,8 +49,15 @@ angular.module('copayApp.controllers').controller('sellGlideraController', funct $scope.wallets = profileService.getWallets({ m: 1, // Only 1-signature wallet onlyComplete: true, - network: $scope.network + network: $scope.network, + hasFunds: true, + minAmount: parsedAmount.amountSat }); + + if (lodash.isEmpty($scope.wallets)) { + showErrorAndBack('Insufficient funds'); + return; + } $scope.wallet = $scope.wallets[0]; // Default first wallet ongoingProcess.set('connectingGlidera', true); diff --git a/src/js/controllers/topup.js b/src/js/controllers/topup.js index 44c8e4148..b90794b15 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, bwcError) { +angular.module('copayApp.controllers').controller('topUpController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, bitpayService, bitpayCardService, payproService, bwcError, txFormatService) { var amount; var currency; @@ -50,21 +50,21 @@ angular.module('copayApp.controllers').controller('topUpController', function($s }; $scope.$on("$ionicView.beforeEnter", function(event, data) { - $scope.isFiat = data.stateParams.currency ? true : false; cardId = data.stateParams.id; if (!cardId) { showErrorAndBack('No card selected'); return; } - - var parsedAmount = bitpayCardService.parseAmount( + + var parsedAmount = txFormatService.parseAmount( data.stateParams.amount, data.stateParams.currency); amount = parsedAmount.amount; currency = parsedAmount.currency; $scope.amountUnitStr = parsedAmount.amountUnitStr; + $scope.alternativeIsoCode = parsedAmount.alternativeIsoCode; $scope.network = bitpayService.getEnvironment().network; $scope.wallets = profileService.getWallets({ @@ -80,7 +80,7 @@ angular.module('copayApp.controllers').controller('topUpController', function($s } $scope.onWalletSelect($scope.wallets[0]); // Default first wallet - bitpayCardService.getRates(currency, function(err, data) { + bitpayCardService.getRates($scope.alternativeIsoCode, function(err, data) { if (err) $log.error(err); $scope.rate = data.rate; }); diff --git a/src/js/routes.js b/src/js/routes.js index e95c63bb4..4fc657bd4 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -1060,8 +1060,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr }, params: { id: null, - currency: 'USD', - forceCurrency: true + currency: 'USD' } }) .state('tabs.bitpayCard.amount', { diff --git a/src/js/services/bitpayCardService.js b/src/js/services/bitpayCardService.js index ed3157cf6..d1aea6b19 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, rateService) { +angular.module('copayApp.services').factory('bitpayCardService', function($log, $rootScope, $filter, lodash, storageService, bitauthService, platformInfo, moment, appIdentityService, bitpayService, nextStepsService, configService, txFormatService, appConfigService) { var root = {}; var _setError = function(msg, e) { @@ -39,33 +39,6 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log, return history; }; - root.parseAmount = function(amount, currency) { - var config = configService.getSync().wallet.settings; - 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 { - amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); - amountUnitStr = txFormatService.formatAmountStr(amountSat); - // convert unit to BTC - amount = (amountSat * satToBtc).toFixed(8); - currency = 'BTC'; - } - - return { - amount: amount, - currency: currency, - amountSat: amountSat, - amountUnitStr: amountUnitStr - }; - }; - root.sync = function(apiContext, cb) { var json = { method: 'getDebitCards' diff --git a/src/js/services/coinbaseService.js b/src/js/services/coinbaseService.js index 07aca04c8..ac3bca0aa 100644 --- a/src/js/services/coinbaseService.js +++ b/src/js/services/coinbaseService.js @@ -107,30 +107,6 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ }; }; - root.parseAmount = function(amount, currency) { - var config = configService.getSync().wallet.settings; - var satToBtc = 1 / 100000000; - var unitToSatoshi = config.unitToSatoshi; - var amountUnitStr; - - // IF 'USD' - if (currency) { - amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency; - } else { - var amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); - amountUnitStr = txFormatService.formatAmountStr(amountSat); - // convert unit to BTC - amount = (amountSat * satToBtc).toFixed(8); - currency = 'BTC'; - } - - return { - amount: amount, - currency: currency, - amountUnitStr: amountUnitStr - }; - }; - root.getSignupUrl = function() { return credentials.HOST + '/signup'; } diff --git a/src/js/services/glideraService.js b/src/js/services/glideraService.js index 79bee5f38..f60230cdf 100644 --- a/src/js/services/glideraService.js +++ b/src/js/services/glideraService.js @@ -53,30 +53,6 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l return 'USD'; }; - root.parseAmount = function(amount, currency) { - var config = configService.getSync().wallet.settings; - var satToBtc = 1 / 100000000; - var unitToSatoshi = config.unitToSatoshi; - var amountUnitStr; - - // IF 'USD' - if (currency) { - amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency; - } else { - var amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); - amountUnitStr = txFormatService.formatAmountStr(amountSat); - // convert unit to BTC - amount = (amountSat * satToBtc).toFixed(8); - currency = 'BTC'; - } - - return { - amount: amount, - currency: currency, - amountUnitStr: amountUnitStr - }; - }; - root.getSignupUrl = function() { return credentials.HOST + '/register'; } diff --git a/src/js/services/txFormatService.js b/src/js/services/txFormatService.js index 77533b692..a28079d3f 100644 --- a/src/js/services/txFormatService.js +++ b/src/js/services/txFormatService.js @@ -155,5 +155,34 @@ angular.module('copayApp.services').factory('txFormatService', function($filter, return txps; }; + root.parseAmount = function(amount, currency) { + var config = configService.getSync().wallet.settings; + var satToBtc = 1 / 100000000; + var unitToSatoshi = config.unitToSatoshi; + var amountUnitStr; + var amountSat; + var alternativeIsoCode = config.alternativeIsoCode; + + // If fiat currency + if (currency != 'bits' && currency != 'BTC') { + amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency; + amountSat = rateService.fromFiat(amount, currency).toFixed(0); + } else { + amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); + amountUnitStr = root.formatAmountStr(amountSat); + // convert unit to BTC + amount = (amountSat * satToBtc).toFixed(8); + currency = 'BTC'; + } + + return { + amount: amount, + currency: currency, + alternativeIsoCode: alternativeIsoCode, + amountSat: amountSat, + amountUnitStr: amountUnitStr + }; + }; + return root; }); diff --git a/www/views/topup.html b/www/views/topup.html index 28ae3d32d..4c003aa1a 100644 --- a/www/views/topup.html +++ b/www/views/topup.html @@ -18,8 +18,9 @@
{{amountUnitStr}}
-
- @ {{rate | currency:'$':2}} per BTC +
+ @ {{rate | currency:'$':2}} per BTC + ...