diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index 74f9d7c88..ff3e8beb9 100644 --- a/src/js/controllers/amount.js +++ b/src/js/controllers/amount.js @@ -340,6 +340,7 @@ angular.module('copayApp.controllers').controller('amountController', function($ }); }); } else if ($scope.isGlidera) { + var amount = $scope.showAlternativeAmount ? fromFiat(_amount) : _amount; $state.transitionTo('tabs.buyandsell.glidera.confirm', { toAmount: (amount * unitToSatoshi).toFixed(0), glideraBuy: $scope.glideraBuy, diff --git a/src/js/controllers/buyGlidera.js b/src/js/controllers/buyGlidera.js index 2bdcd049d..383c77688 100644 --- a/src/js/controllers/buyGlidera.js +++ b/src/js/controllers/buyGlidera.js @@ -9,80 +9,80 @@ angular.module('copayApp.controllers').controller('buyGlideraController', this.success = null; $scope.network = glideraService.getEnvironment(); - // $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); - // }); - // - // $scope.update = function(opts) { - // if (!$scope.token || !$scope.permissions) return; - // $log.debug('Updating Glidera Account...'); - // var accessToken = $scope.token; - // var permissions = $scope.permissions; - // - // opts = opts || {}; - // - // glideraService.getStatus(accessToken, function(err, data) { - // $scope.status = data; - // }); - // - // glideraService.getLimits(accessToken, function(err, limits) { - // $scope.limits = limits; - // }); - // - // if (permissions.transaction_history) { - // glideraService.getTransactions(accessToken, function(err, data) { - // $scope.txs = data; - // }); - // } - // - // if (permissions.view_email_address && opts.fullUpdate) { - // glideraService.getEmail(accessToken, function(err, data) { - // $scope.email = data.email; - // }); - // } - // if (permissions.personal_info && opts.fullUpdate) { - // glideraService.getPersonalInfo(accessToken, function(err, data) { - // $scope.personalInfo = data; - // }); - // } - // }; - // - // this.getBuyPrice = function(token, price) { - // var self = this; - // if (!price || (price && !price.qty && !price.fiat)) { - // this.buyPrice = null; - // return; - // } - // this.gettingBuyPrice = true; - // glideraService.buyPrice(token, price, function(err, buyPrice) { - // self.gettingBuyPrice = false; - // if (err) { - // popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again')); - // return; - // } - // self.buyPrice = buyPrice; - // }); - // }; + $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); + }); - // this.get2faCode = function(token) { - // var self = this; - // ongoingProcess.set('Sending 2FA code...', true); - // $timeout(function() { - // glideraService.get2faCode(token, function(err, sent) { - // ongoingProcess.set('Sending 2FA code...', false); - // if (err) { - // popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone')); - // return; - // } - // self.show2faCodeInput = sent; - // }); - // }, 100); - // }; + $scope.update = function(opts) { + if (!$scope.token || !$scope.permissions) return; + $log.debug('Updating Glidera Account...'); + var accessToken = $scope.token; + var permissions = $scope.permissions; + + opts = opts || {}; + + glideraService.getStatus(accessToken, function(err, data) { + $scope.status = data; + }); + + glideraService.getLimits(accessToken, function(err, limits) { + $scope.limits = limits; + }); + + if (permissions.transaction_history) { + glideraService.getTransactions(accessToken, function(err, data) { + $scope.txs = data; + }); + } + + if (permissions.view_email_address && opts.fullUpdate) { + glideraService.getEmail(accessToken, function(err, data) { + $scope.email = data.email; + }); + } + if (permissions.personal_info && opts.fullUpdate) { + glideraService.getPersonalInfo(accessToken, function(err, data) { + $scope.personalInfo = data; + }); + } + }; + + this.getBuyPrice = function(token, price) { + var self = this; + if (!price || (price && !price.qty && !price.fiat)) { + this.buyPrice = null; + return; + } + this.gettingBuyPrice = true; + glideraService.buyPrice(token, price, function(err, buyPrice) { + self.gettingBuyPrice = false; + if (err) { + popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again')); + return; + } + self.buyPrice = buyPrice; + }); + }; + + this.get2faCode = function(token) { + var self = this; + ongoingProcess.set('Sending 2FA code...', true); + $timeout(function() { + glideraService.get2faCode(token, function(err, sent) { + ongoingProcess.set('Sending 2FA code...', false); + if (err) { + popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone')); + return; + } + self.show2faCodeInput = sent; + }); + }, 100); + }; this.sendRequest = function(token, permissions, twoFaCode) { var self = this; diff --git a/src/js/controllers/confirm.js b/src/js/controllers/confirm.js index 3e762942d..7ddc0119a 100644 --- a/src/js/controllers/confirm.js +++ b/src/js/controllers/confirm.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('confirmController', function($rootScope, $scope, $interval, $filter, $timeout, $ionicScrollDelegate, gettextCatalog, walletService, platformInfo, lodash, configService, rateService, $stateParams, $window, $state, $log, profileService, bitcore, gettext, txFormatService, ongoingProcess, $ionicModal, popupService, $ionicHistory, $ionicConfig, payproService, feeService, amazonService) { +angular.module('copayApp.controllers').controller('confirmController', function($rootScope, $scope, $interval, $filter, $timeout, $ionicScrollDelegate, gettextCatalog, walletService, platformInfo, lodash, configService, rateService, $stateParams, $window, $state, $log, profileService, bitcore, gettext, txFormatService, ongoingProcess, $ionicModal, popupService, $ionicHistory, $ionicConfig, payproService, feeService, amazonService, glideraService, bwcError) { var cachedTxp = {}; var toAmount; var isChromeApp = platformInfo.isChromeApp; @@ -24,6 +24,8 @@ angular.module('copayApp.controllers').controller('confirmController', function( // Glidera parameters $scope.isGlidera = data.stateParams.isGlidera; $scope.glideraAccessToken = data.stateParams.glideraAccessToken; + $scope.glideraBuy = data.stateParams.glideraBuy; + $scope.glideraSell = data.stateParams.glideraSell; toAmount = data.stateParams.toAmount; cachedSendMax = {}; @@ -46,7 +48,8 @@ angular.module('copayApp.controllers').controller('confirmController', function( var config = configService.getSync().wallet; $scope.feeLevel = config.settings && config.settings.feeLevel ? config.settings.feeLevel : 'normal'; - $scope.network = (new bitcore.Address($scope.toAddress)).network.name; + if ($scope.isGlidera) $scope.network = glideraService.getEnvironment(); + else $scope.network = (new bitcore.Address($scope.toAddress)).network.name; resetValues(); setwallets(); }); @@ -129,29 +132,13 @@ angular.module('copayApp.controllers').controller('confirmController', function( txFormatService.formatAlternativeStr(toAmount, function(v) { $scope.alternativeAmountStr = v; }); + if ($scope.isGlidera && $scope.glideraBuy) $scope.getBuyPrice(); }; function resetValues() { $scope.displayAmount = $scope.displayUnit = $scope.fee = $scope.alternativeAmountStr = $scope.insufficientFunds = $scope.noMatchingWallet = null; }; - $scope.getBuyPrice = function(token, price) { - - if (!price || (price && !price.qty && !price.fiat)) { - $scope.buyPrice = null; - return; - } - $scope.gettingBuyPrice = true; - glideraService.buyPrice(token, price, function(err, buyPrice) { - $scope.gettingBuyPrice = false; - if (err) { - popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again')); - return; - } - $scope.buyPrice = buyPrice; - }); - }; - $scope.getSendMaxInfo = function() { resetValues(); @@ -330,6 +317,7 @@ angular.module('copayApp.controllers').controller('confirmController', function( $scope.wallet = wallet; $scope.fee = $scope.txp = null; + if ($scope.isGlidera) return; if (stop) { $timeout.cancel(stop); stop = null; @@ -445,6 +433,32 @@ angular.module('copayApp.controllers').controller('confirmController', function( return; } + if ($scope.isGlidera) { + $scope.get2faCode(function(err, sent) { + if (err) { + popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone')); + return; + } + if (sent) { + var title = gettextCatalog.getString("Please, enter the code below"); + var message = gettextCatalog.getString("A SMS containing a confirmation code was sent to your phone."); + popupService.showPrompt(title, message, null, function(twoFaCode) { + $scope.sendRequest(twoFaCode, function(err, data) { + if (err) { + popupService.showAlert(gettextCatalog.getString('Error'), err); + return; + } + $scope.success = data; + $timeout(function() { + $scope.$digest(); + }); + }) + }); + } + }); + return; + } + var wallet = $scope.wallet; if (!wallet) { return setSendError(gettextCatalog.getString('No wallet selected')); @@ -558,24 +572,53 @@ angular.module('copayApp.controllers').controller('confirmController', function( } }; - $scope.get2faCode = function(token, cb) { + $scope.get2faCode = function(cb) { ongoingProcess.set('Sending 2FA code...', true); $timeout(function() { - glideraService.get2faCode(token, function(err, sent) { + glideraService.get2faCode($scope.glideraAccessToken, function(err, sent) { ongoingProcess.set('Sending 2FA code...', false); + return cb(err, sent); + }); + }, 100); + }; + + $scope.sendRequest = function(twoFaCode, cb) { + ongoingProcess.set('Buying Bitcoin...', true); + $timeout(function() { + walletService.getAddress($scope.wallet, false, function(err, walletAddr) { if (err) { - popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone')); + ongoingProcess.set('Buying Bitcoin...', false); + popupService.showAlert(gettextCatalog.getString('Error'), bwcError.cb(err, 'Could not create address')); return; } - var title = gettextCatalog.getString("Please, enter the code below"); - var message = gettextCatalog.getString("A SMS containing a confirmation code was sent to your phone."); - popupService.showPrompt(title, message, null, function(code) { - return cb(code); + var data = { + destinationAddress: walletAddr, + qty: $scope.buyPrice.qty, + priceUuid: $scope.buyPrice.priceUuid, + useCurrentPrice: false, + ip: null + }; + glideraService.buy($scope.glideraAccessToken, twoFaCode, data, function(err, data) { + ongoingProcess.set('Buying Bitcoin...', false); + return cb(err, data) }); }); }, 100); }; + $scope.getBuyPrice = function() { + var satToBtc = 1 / 100000000; + var price = {}; + price.qty = (toAmount * satToBtc).toFixed(8); + glideraService.buyPrice($scope.glideraAccessToken, price, function(err, buyPrice) { + if (err) { + popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again')); + return; + } + $scope.buyPrice = buyPrice; + }); + }; + function publishAndSign(wallet, txp, onSendStatusChange) { walletService.publishAndSign(wallet, txp, function(err, txp) { if (err) return setSendError(err); diff --git a/src/js/routes.js b/src/js/routes.js index 4e49e6fa8..031a66000 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -924,7 +924,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr } }) .state('tabs.buyandsell.glidera.confirm', { - url: '/confirm/:toAmount/:glideraBuy/:glideraAccessToken', + url: '/confirm/:toAmount/:glideraBuy/:glideraSell/:glideraAccessToken', views: { 'tab-home@tabs': { controller: 'confirmController', diff --git a/src/js/services/glideraService.js b/src/js/services/glideraService.js index 2bf89ffb6..9673efd6a 100644 --- a/src/js/services/glideraService.js +++ b/src/js/services/glideraService.js @@ -192,8 +192,13 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l }; root.get2faCode = function(token, cb) { - if (!token) return cb('Invalid Token'); + if (!token) { + $log.error('Glidera Sent 2FA code by SMS: ERROR Invalid Token'); + return cb('Invalid Token'); + } + $http(_get('/authentication/get2faCode', token)).then(function(data) { + $log.info('Glidera Sent 2FA code by SMS: SUCCESS'); return cb(null, data.status == 200 ? true : false); }, function(data) { diff --git a/src/sass/views/includes/txp-details.scss b/src/sass/views/includes/txp-details.scss index 137573dae..83d375def 100644 --- a/src/sass/views/includes/txp-details.scss +++ b/src/sass/views/includes/txp-details.scss @@ -47,6 +47,11 @@ color: #9B9B9B; } } + .buy-price { + bottom: 20px; + position: absolute; + color: $light-gray; + } } .item { border-color: $item-border-color; diff --git a/www/views/confirm.html b/www/views/confirm.html index 5f5ebae63..433f8944c 100644 --- a/www/views/confirm.html +++ b/www/views/confirm.html @@ -19,6 +19,11 @@
{{displayAmount || '...'}} {{displayUnit}}
{{alternativeAmountStr || '...'}}
+
+
+ Buy {{buyPrice.subtotal|currency:'':2}} {{buyPrice.currency}} in Bitcoin at {{buyPrice.price}} {{buyPrice.currency}}/BTC +
+
@@ -29,11 +34,12 @@
To - +
+
@@ -67,7 +73,7 @@ -
+
Fee: {{feeLevel}} {{fee || '...'}}