diff --git a/src/js/controllers/glidera.js b/src/js/controllers/glidera.js index d47721c67..c1cf086da 100644 --- a/src/js/controllers/glidera.js +++ b/src/js/controllers/glidera.js @@ -1,72 +1,37 @@ 'use strict'; angular.module('copayApp.controllers').controller('glideraController', - function($scope, $timeout, $ionicModal, $log, storageService, glideraService, ongoingProcess, platformInfo, externalLinkService, popupService, gettextCatalog) { - - $scope.network = glideraService.getEnvironment(); + function($scope, $timeout, $ionicModal, $log, storageService, glideraService, ongoingProcess, platformInfo, externalLinkService, popupService, lodash) { $scope.openExternalLink = function(url) { externalLinkService.open(url); }; - var initGlidera = function(accessToken) { - $scope.token = accessToken; - $scope.permissions = null; - $scope.email = null; - $scope.personalInfo = null; - $scope.txs = null; - $scope.status = null; - $scope.limits = null; + var init = function() { + glideraService.init(function(err, data) { + if (err || lodash.isEmpty(data)) return; - $scope.connectingGlidera = true; - ongoingProcess.set('connectingGlidera', true); - glideraService.init($scope.token, function(err, glidera) { - ongoingProcess.set('connectingGlidera'); - $scope.connectingGlidera = false; - if (err || !glidera) { - if (err) popupService.showAlert(gettextCatalog.getString('Error'), err); - return; - } - $scope.token = glidera.token; - $scope.permissions = glidera.permissions; - $scope.update({ - fullUpdate: true + $scope.account['token'] = data.token; + $scope.account['status'] = data.status; + $scope.account['price'] = {}; + + glideraService.buyPrice($scope.account.token, {qty: 1}, function(err, buy) { + $scope.account.price['buy'] = buy.price; + }); + glideraService.sellPrice($scope.account.token, {qty: 1}, function(err, sell) { + $scope.account.price['sell'] = sell.price; + }); + + $timeout(function() { + $scope.$digest(); + $scope.update(); }); }); }; $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; - }); - } + $log.debug('Updating Glidera...'); + glideraService.updateStatus($scope.account); }; $scope.getAuthenticateUrl = function() { @@ -75,29 +40,22 @@ angular.module('copayApp.controllers').controller('glideraController', $scope.submitOauthCode = function(code) { ongoingProcess.set('connectingGlidera', true); - $timeout(function() { - glideraService.getToken(code, function(err, data) { - ongoingProcess.set('connectingGlidera', false); - if (err) { - popupService.showAlert(gettextCatalog.getString('Error'), err); - } else if (data && data.access_token) { - storageService.setGlideraToken($scope.network, data.access_token, function() { - initGlidera(data.access_token); - $timeout(function() { - $scope.$apply(); - }, 100); - }); - } - }); - }, 100); + glideraService.authorize(code, function(err, data) { + ongoingProcess.set('connectingGlidera', false); + if (err) { + popupService.showAlert('Authorisation error', err); + return; + } + init(); + }); }; - $scope.openTxModal = function(token, tx) { + $scope.openTxModal = function(tx) { $scope.tx = tx; - glideraService.getTransaction(token, tx.transactionUuid, function(err, tx) { + glideraService.getTransaction($scope.account.token, tx.transactionUuid, function(err, tx) { if (err) { - popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get transactions')); + popupService.showAlert('Error getting transaction', 'Could not get transactions'); return; } $scope.tx = tx; @@ -127,10 +85,10 @@ angular.module('copayApp.controllers').controller('glideraController', $scope.openSupportWindow = function() { var url = glideraService.getSupportUrl(); var optIn = true; - var title = gettextCatalog.getString('Glidera Support'); - var message = gettextCatalog.getString('You can email glidera at support@glidera.io for direct support, or you can contact Glidera on Twitter.'); - var okText = gettextCatalog.getString('Tweet @GlideraInc'); - var cancelText = gettextCatalog.getString('Go Back'); + var title = 'Glidera Support'; + var message = 'You can email glidera at support@glidera.io for direct support, or you can contact Glidera on Twitter.'; + var okText = 'Tweet @GlideraInc'; + var cancelText = 'Go Back'; externalLinkService.open(url, optIn, title, message, okText, cancelText); } @@ -147,8 +105,10 @@ angular.module('copayApp.controllers').controller('glideraController', } $scope.$on("$ionicView.beforeEnter", function(event, data) { + $scope.network = glideraService.getNetwork(); $scope.showOauthForm = false; - initGlidera(); + $scope.account = {}; + init(); }); }); diff --git a/src/js/controllers/preferencesGlidera.js b/src/js/controllers/preferencesGlidera.js index 8d18005a2..12469e00f 100644 --- a/src/js/controllers/preferencesGlidera.js +++ b/src/js/controllers/preferencesGlidera.js @@ -1,46 +1,12 @@ 'use strict'; angular.module('copayApp.controllers').controller('preferencesGlideraController', - function($scope, $log, $timeout, $state, $ionicHistory, ongoingProcess, glideraService, popupService, gettextCatalog) { - - $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; - }); - } - if (permissions.personal_info && opts.fullUpdate) { - glideraService.getPersonalInfo(accessToken, function(err, data) { - $scope.personalInfo = data; - }); - } - }; + function($scope, $timeout, $state, $ionicHistory, glideraService, popupService) { $scope.revokeToken = function() { popupService.showConfirm('Glidera', 'Are you sure you would like to log out of your Glidera account?', null, null, function(res) { if (res) { - glideraService.removeToken(function() { + glideraService.remove(function() { $ionicHistory.clearHistory(); $timeout(function() { $state.go('tabs.home'); @@ -50,21 +16,20 @@ angular.module('copayApp.controllers').controller('preferencesGlideraController' }); }; - $scope.$on("$ionicView.enter", function(event, data){ - $scope.network = glideraService.getEnvironment(); + $scope.$on("$ionicView.afterEnter", function(event, data){ + glideraService.updateStatus($scope.account); + }); - ongoingProcess.set('connectingGlidera', true); - glideraService.init($scope.token, function(err, glidera) { - ongoingProcess.set('connectingGlidera'); + $scope.$on("$ionicView.beforeEnter", function(event, data){ + $scope.account = {}; + glideraService.init(function(err, glidera) { if (err || !glidera) { - if (err) popupService.showAlert(gettextCatalog.getString('Error'), err); + if (err) popupService.showAlert('Error connecting Glidera', err); return; } - $scope.token = glidera.token; - $scope.permissions = glidera.permissions; - $scope.update({ - fullUpdate: true - }); + $scope.account['token'] = glidera.token; + $scope.account['permissions'] = glidera.permissions; + $scope.account['status'] = glidera.status; }); }); diff --git a/src/js/services/glideraService.js b/src/js/services/glideraService.js index 987ae5845..08d227733 100644 --- a/src/js/services/glideraService.js +++ b/src/js/services/glideraService.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('glideraService', function($http, $log, $window, platformInfo, storageService, buyAndSellService) { +angular.module('copayApp.services').factory('glideraService', function($http, $log, $window, platformInfo, storageService, buyAndSellService, lodash) { var root = {}; var credentials = {}; var isCordova = platformInfo.isCordova; @@ -45,7 +45,7 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l }; }; - root.getEnvironment = function() { + root.getNetwork = function() { return credentials.NETWORK; }; @@ -61,10 +61,14 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l return credentials.HOST + '/oauth2/auth?response_type=code&client_id=' + credentials.CLIENT_ID + '&redirect_uri=' + credentials.REDIRECT_URI; }; - root.removeToken = function(cb) { + root.remove = function(cb) { storageService.removeGlideraToken(credentials.NETWORK, function() { - buyAndSellService.updateLink('glidera', false); - return cb(); + storageService.removeGlideraPermissions(credentials.NETWORK, function() { + storageService.removeGlideraStatus(credentials.NETWORK, function() { + buyAndSellService.updateLink('glidera', false); + return cb(); + }); + }); }); }; @@ -87,8 +91,6 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l $http(req).then(function(data) { $log.info('Glidera Authorization Access Token: SUCCESS'); - // Show pending task from the UI - storageService.setNextStep('BuyAndSell', 'true', function(err) {}); return cb(null, data.data); }, function(data) { $log.error('Glidera Authorization Access Token: ERROR ' + data.statusText); @@ -96,6 +98,31 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l }); }; + root.authorize = function(code, cb) { + root.getToken(code, function(err, data) { + if (err) return cb(err); + if (data && !data.access_token) return cb('No access token'); + var accessToken = data.access_token; + root.getAccessTokenPermissions(accessToken, function(err, p) { + if (err) return cb(err); + root.getStatus(accessToken, function(err, status) { + if (err) $log.error(err); + storageService.setGlideraToken(credentials.NETWORK, accessToken, function() { + storageService.setGlideraPermissions(credentials.NETWORK, JSON.stringify(p), function() { + storageService.setGlideraStatus(credentials.NETWORK, JSON.stringify(status), function() { + return cb(null, { + token: accessToken, + permissions: p, + status: status + }); + }); + }); + }); + }); + }); + }); + }; + var _get = function(endpoint, token) { return { method: 'GET', @@ -289,40 +316,65 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l }); }; - root.init = function(accessToken, cb) { - $log.debug('Init Glidera...'); - - var glidera = { - token: null, - permissions: null + root.init = function(cb) { + if (lodash.isEmpty(credentials.CLIENT_ID)) { + return cb('Glidera is Disabled'); } + $log.debug('Trying to initialise Glidera...'); - var getToken = function(cb) { - if (accessToken) { - cb(null, accessToken); - } else { - storageService.getGlideraToken(credentials.NETWORK, cb); - } - }; - - getToken(function(err, accessToken) { - if (err || !accessToken) return cb(); - else { - buyAndSellService.updateLink('glidera', true); - - root.getAccessTokenPermissions(accessToken, function(err, p) { - if (err) { - return cb(err); - } else { - glidera.token = accessToken; - glidera.permissions = p; - return cb(null, glidera); - } + storageService.getGlideraToken(credentials.NETWORK, function(err, accessToken) { + if (err || lodash.isEmpty(accessToken)) return cb(); + + storageService.getGlideraPermissions(credentials.NETWORK, function(err, permissions) { + if (lodash.isString(permissions)) permissions = JSON.parse(permissions); + storageService.getGlideraStatus(credentials.NETWORK, function(err, status) { + if (lodash.isString(status)) status = JSON.parse(status); + buyAndSellService.updateLink('glidera', true); + return cb(null, { + token: accessToken, + permissions: permissions, + status: status + }); }); - } + }); }); }; + root.updateStatus = function(data) { + storageService.getGlideraToken(credentials.NETWORK, function(err, accessToken) { + if (err) return; + root.getAccessTokenPermissions(accessToken, function(err, permissions) { + if (err) return; + storageService.setGlideraPermissions(credentials.NETWORK, JSON.stringify(permissions), function() {}); + data.permissions = permissions; + root.getStatus(accessToken, function(err, status) { + data.status = status; + storageService.setGlideraStatus(credentials.NETWORK, JSON.stringify(status), function() {}); + + root.getLimits(accessToken, function(err, limits) { + data.limits = limits; + + if (permissions.transaction_history) { + root.getTransactions(accessToken, function(err, txs) { + data.txs = txs; + }); + } + + if (permissions.view_email_address) { + root.getEmail(accessToken, function(err, email) { + data.email = email; + }); + } + if (permissions.personal_info) { + root.getPersonalInfo(accessToken, function(err, info) { + data.personalInfo = info; + }); + } + }); + }); + }); + }); + }; var register = function() { if (isWindowsPhoneApp) return; diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index 668e8c66a..3080b1b52 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -247,6 +247,30 @@ angular.module('copayApp.services') storage.remove('glideraToken-' + network, cb); }; + root.setGlideraPermissions = function(network, p, cb) { + storage.set('glideraPermissions-' + network, p, cb); + }; + + root.getGlideraPermissions = function(network, cb) { + storage.get('glideraPermissions-' + network, cb); + }; + + root.removeGlideraPermissions = function(network, cb) { + storage.remove('glideraPermissions-' + network, cb); + }; + + root.setGlideraStatus = function(network, status, cb) { + storage.set('glideraStatus-' + network, status, cb); + }; + + root.getGlideraStatus = function(network, cb) { + storage.get('glideraStatus-' + network, cb); + }; + + root.removeGlideraStatus = function(network, cb) { + storage.remove('glideraStatus-' + network, cb); + }; + root.setCoinbaseRefreshToken = function(network, token, cb) { storage.set('coinbaseRefreshToken-' + network, token, cb); }; diff --git a/www/views/glidera.html b/www/views/glidera.html index eea690702..d74c8d56c 100644 --- a/www/views/glidera.html +++ b/www/views/glidera.html @@ -4,30 +4,24 @@ Glidera - - - -
- The Glidera integration is currently disabled. -
-
- Testnet wallets only work with Glidera Sandbox Accounts. -
+
-
Glidera's exchange service is available in the United States, and can take 1-2 weeks to buy or sell bitcoin.
-
If you have trouble, contact Glidera support for direct assistance.
+
Glidera's exchange service is available in the United States, and can take 1-2 weeks to buy or sell bitcoin.
+
If you have trouble, contact Glidera support for direct assistance.
- - + +
@@ -43,13 +37,13 @@ - +
- +
- +
- -
+
Activity
- diff --git a/www/views/preferencesGlidera.html b/www/views/preferencesGlidera.html index a93bd1f80..99de7e7ac 100644 --- a/www/views/preferencesGlidera.html +++ b/www/views/preferencesGlidera.html @@ -8,55 +8,55 @@
    -
    +
    Permissions
  • Email - {{permissions.view_email_address}} + {{account.permissions.view_email_address}}
  • Personal Information - {{permissions.personal_info}} + {{account.permissions.personal_info}}
  • Buy/Sell - {{permissions.transact}} + {{account.permissions.transact}}
  • Transaction History - {{permissions.transaction_history}} + {{account.permissions.transaction_history}}
  • -
    +
    Email
  • Email - {{email.email}} + {{account.email.email}}
  • Active - {{email.userEmailIsSetup}} + {{account.email.userEmailIsSetup}}
  • -
    +
    Personal Information
    @@ -64,73 +64,73 @@
  • First Name - {{personalInfo.firstName}} + {{account.personalInfo.firstName}}
  • Middle Name - {{personalInfo.middleName}} + {{account.personalInfo.middleName}}
  • Last Name - {{personalInfo.lastName}} + {{account.personalInfo.lastName}}
  • Birth Date - {{personalInfo.birthDate}} + {{account.personalInfo.birthDate}}
  • Address 1 - {{personalInfo.address1}} + {{account.personalInfo.address1}}
  • Address 2 - {{personalInfo.address2}} + {{account.personalInfo.address2}}
  • City - {{personalInfo.city}} + {{account.personalInfo.city}}
  • State - {{personalInfo.state}} + {{account.personalInfo.state}}
  • ZIP Code - {{personalInfo.zipCode}} + {{account.personalInfo.zipCode}}
  • Country - {{personalInfo.countryCode}} + {{account.personalInfo.countryCode}}
  • Occupation - {{personalInfo.occupation}} + {{account.personalInfo.occupation}}
  • Basic Information State - {{personalInfo.basicInfoState}} + {{account.personalInfo.basicInfoState}}
  • @@ -143,56 +143,56 @@
  • Buy/Sell - {{status.userCanTransact}} + {{account.status.userCanTransact}}
  • Buy - {{status.userCanBuy}} + {{account.status.userCanBuy}}
  • Sell - {{status.userCanSell}} + {{account.status.userCanSell}}
  • Email Is Setup - {{status.userEmailIsSetup}} + {{account.status.userEmailIsSetup}}
  • Phone Is Setup - {{status.userPhoneIsSetup}} + {{account.status.userPhoneIsSetup}}
  • Bank Account Is Setup - {{status.userBankAccountIsSetup}} + {{account.status.userBankAccountIsSetup}}
  • Personal Information State - {{status.personalInfoState}} + {{account.status.personalInfoState}}
  • Bank Account State - {{status.bankAccountState}} + {{account.status.bankAccountState}}
  • Country - {{status.country}} + {{account.status.country}}
  • @@ -205,55 +205,55 @@
  • Daily Buy - {{limits.dailyBuy|currency:'':2}} {{limits.currency}} + {{account.limits.dailyBuy|currency:'':2}} {{limits.currency}}
  • Daily Sell - {{limits.dailySell|currency:'':2}} {{limits.currency}} + {{account.limits.dailySell|currency:'':2}} {{limits.currency}}
  • Monthly Buy - {{limits.monthlyBuy|currency:'':2}} {{limits.currency}} + {{account.limits.monthlyBuy|currency:'':2}} {{limits.currency}}
  • Monthly Sell - {{limits.monthlySell|currency:'':2}} {{limits.currency}} + {{account.limits.monthlySell|currency:'':2}} {{limits.currency}}
  • Daily Buy Remaining - {{limits.dailyBuyRemaining|currency:'':2}} {{limits.currency}} + {{account.limits.dailyBuyRemaining|currency:'':2}} {{limits.currency}}
  • Daily Sell Remaining - {{limits.dailySellRemaining|currency:'':2}} {{limits.currency}} + {{account.limits.dailySellRemaining|currency:'':2}} {{limits.currency}}
  • Monthly Buy Remaining - {{limits.monthlyBuyRemaining|currency:'':2}} {{limits.currency}} + {{account.limits.monthlyBuyRemaining|currency:'':2}} {{limits.currency}}
  • Monthly Sell Remaining - {{limits.monthlySellRemaining|currency:'':2}} {{limits.currency}} + {{account.limits.monthlySellRemaining|currency:'':2}} {{limits.currency}}
  • Buy/Sell Disabled (pending first transaction) - {{limits.transactDisabledPendingFirstTransaction}} + {{account.limits.transactDisabledPendingFirstTransaction}}