diff --git a/src/js/controllers/bitpayCardIntro.js b/src/js/controllers/bitpayCardIntro.js index ca712a22b..dfc903e18 100644 --- a/src/js/controllers/bitpayCardIntro.js +++ b/src/js/controllers/bitpayCardIntro.js @@ -15,7 +15,7 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f return; } if (paired) { - bitpayCardService.sync(apiContext, function(err, data) { + bitpayCardService.sync(apiContext, function(err, cards) { if (err) { popupService.showAlert(gettextCatalog.getString('Error updating Debit Cards'), err); return; @@ -26,9 +26,9 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f disableAnimate: true }); $state.go('tabs.home').then(function() { - if (data.cards[0]) { + if (cards[0]) { $state.transitionTo('tabs.bitpayCard', { - id: data.cards[0].id + id: cards[0].id }); } }); @@ -78,7 +78,7 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f if (account == undefined) { startPairBitPayAccount(); } else { - bitpayCardService.fetchBitpayDebitCards(account.apiContext, function(err, data) { + bitpayCardService.sync(account.apiContext, function(err, data) { if (err) { popupService.showAlert(gettextCatalog.getString('Error'), err); return; diff --git a/src/js/controllers/preferencesBitpayServices.js b/src/js/controllers/preferencesBitpayServices.js index c6e0343f5..7ce638def 100644 --- a/src/js/controllers/preferencesBitpayServices.js +++ b/src/js/controllers/preferencesBitpayServices.js @@ -45,7 +45,7 @@ angular.module('copayApp.controllers').controller('preferencesBitpayServicesCont }; var removeCard = function(card) { - bitpayCardService.removeCard(card, function(err) { + bitpayCardService.remove(card.id, function(err) { if (err) { return popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not remove card')); } @@ -54,13 +54,13 @@ angular.module('copayApp.controllers').controller('preferencesBitpayServicesCont }; var setScope = function(cb) { - bitpayAccountService.getAccounts(function(err, data) { + bitpayAccountService.getAccounts(function(err, accounts) { if (err) return; - $scope.bitpayAccounts = data; + $scope.bitpayAccounts = accounts; - bitpayCardService.getBitpayDebitCards(function(err, data) { + bitpayCardService.getCards(function(err, cards) { if (err) return; - $scope.bitpayCards = data; + $scope.bitpayCards = cards; if (cb) { cb(); } diff --git a/src/js/services/bitpayAccountService.js b/src/js/services/bitpayAccountService.js index 5ddefb9f5..e32e84727 100644 --- a/src/js/services/bitpayAccountService.js +++ b/src/js/services/bitpayAccountService.js @@ -133,7 +133,7 @@ angular.module('copayApp.services').factory('bitpayAccountService', function($lo // from the server for each account (apiContext). root.getAccounts = function(cb) { root.getAccountsAsStored(function(err, accounts) { - if (err || !accounts) { + if (err || lodash.isEmpty(accounts)) { return cb(err, []); } appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) { @@ -143,10 +143,10 @@ angular.module('copayApp.services').factory('bitpayAccountService', function($lo var accountsArray = []; lodash.forEach(Object.keys(accounts), function(key) { - accounts[key].bitpayDebitCards = accounts[key].cards; + accounts[key].cards = accounts[key].cards; accounts[key].email = key; - accounts[key].firstName = accounts[key].givenName || ''; - accounts[key].lastName = accounts[key].familyName || ''; + accounts[key].givenName = accounts[key].givenName || ''; + accounts[key].familyName = accounts[key].familyName || ''; accounts[key].apiContext = { token: accounts[key].token, pairData: { @@ -164,29 +164,14 @@ angular.module('copayApp.services').factory('bitpayAccountService', function($lo var setBitpayAccount = function(account, cb) { storageService.setBitpayAccount(bitpayService.getEnvironment().network, account, function(err) { - if (err) { - return cb(err); - } - return cb(); + return cb(err); }); }; root.removeAccount = function(account, cb) { storageService.removeBitpayAccount(bitpayService.getEnvironment().network, account, function(err) { - if (err) { - $log.error('Error removing BitPay account: ' + err); - // Continue, try to remove next step if necessary - } - storageService.getBitpayDebitCards(bitpayService.getEnvironment().network, function(err, cards) { - if (err) { - $log.error('Error attempting to get BitPay debit cards after account removal: ' + err); - } - if (cards.length == 0) { - storageService.removeNextStep('BitpayCard', cb); - } else { - cb(); - } - }); + bitpayCardService.registerNextStep(); + cb(err); }); }; diff --git a/src/js/services/bitpayCardService.js b/src/js/services/bitpayCardService.js index 0144ee0b3..c9b85d9b6 100644 --- a/src/js/services/bitpayCardService.js +++ b/src/js/services/bitpayCardService.js @@ -47,13 +47,27 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log, bitpayService.post('/api/v2/' + apiContext.token, json, function(data) { if (data && data.data.error) return cb(data.data.error); $log.info('BitPay Get Debit Cards: SUCCESS'); - // Cache card data in storage - var cardData = { - cards: data.data.data, - email: apiContext.pairData.email - } - storageService.setBitpayDebitCards(bitpayService.getEnvironment().network, cardData, function(err) { - return cb(err, {token: apiContext.token, cards: data.data.data, email: apiContext.pairData.email}); + + var cards = []; + + lodash.each(data.data.data, function(x) { + var n = {}; + + if (!x.eid || !x.id || !x.lastFourDigits || !x.token) { + $log.warn('BAD data from Bitpay card' + JSON.stringify(x)); + return; + } + + n.eid = x.eid; + n.id = x.id; + n.lastFourDigits = x.lastFourDigits; + n.token = x.token; + cards.push(n); + }); + + storageService.setBitpayDebitCards(bitpayService.getEnvironment().network, apiContext.pairData.email, cards, function(err) { + root.registerNextStep(); + return cb(err, cards); }); }, function(data) { return cb(_setError('BitPay Card Error: Get Debit Cards', data)); @@ -186,36 +200,17 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log, }, cb); }; - root.removeCard = function(card, cb) { - storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, card, function(err) { + root.remove = function(cardId, cb) { + storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, cardId, function(err) { if (err) { $log.error('Error removing BitPay debit card: ' + err); - // Continue, try to remove/cleanup next step and card history + return cb(err); } - // Next two items in parallel - // - // If there are no more cards in storage then re-enable the next step entry - storageService.getBitpayDebitCards(bitpayService.getEnvironment().network, function(err, cards) { - if (err) { - $log.error('Error getting BitPay debit cards after remove: ' + err); - // Continue, try to remove next step if necessary - } - if (cards.length == 0) { - storageService.removeNextStep('BitpayCard', cb); - } - }); - storageService.removeBitpayDebitCardHistory(bitpayService.getEnvironment().network, card, function(err) { - if (err) { - $log.error('Error removing BitPay debit card transaction history: ' + err); - return cb(err); - } - $log.info('Successfully removed BitPay debit card'); - return cb(); - }); + root.registerNextStep(); + storageService.removeBalanceCache(cardId, cb); }); }; - root.getRates = function(currency, cb) { bitpayService.get('/rates/' + currency, function(data) { $log.info('BitPay Get Rates: SUCCESS'); @@ -1303,7 +1298,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log, }; - var register = function() { + root.registerNextStep = function() { root.getCards(function(err, cards) { if (lodash.isEmpty(cards)) { nextStepsService.register(nextStepItem); @@ -1313,7 +1308,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log, }); }; - register(); + root.registerNextStep(); return root; }); diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index 57ae94c0d..deb8a65c1 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -356,33 +356,22 @@ angular.module('copayApp.services') storage.remove('balanceCache-' + cardId, cb); }; - // data: { - // cards: [ - // eid: card id - // id: card id - // lastFourDigits: card number - // token: card token - // ] - // email: account email - // token: account token - // } - root.setBitpayDebitCards = function(network, data, cb) { - if (lodash.isString(data)) { - data = JSON.parse(data); - } - data = data || {}; - if (lodash.isEmpty(data) || !data.email) return cb('Cannot set cards: no account to set'); - + // cards: [ + // eid: card id + // id: card id + // lastFourDigits: card number + // token: card token + // ] + root.setBitpayDebitCards = function(network, email, cards, cb) { root.getBitpayAccounts(network, function(err, allAccounts) { if (err) return cb(err); - allAccounts = allAccounts || {}; - if (!allAccounts[data.email]) { - return cb('Cannot set cards for unknown account ' + data.email); + if (!allAccounts[email]) { + return cb('Cannot set cards for unknown account ' + email); } - allAccounts[data.email] = allAccounts[data.email] || {}; - allAccounts[data.email]['bitpayDebitCards-' + network] = data.cards; - storage.set('bitpayAccounts-v2-' + network, JSON.stringify(allAccounts), cb); + + allAccounts[email].cards = cards; + storage.set('bitpayAccounts-v2-' + network, allAccounts, cb); }); }; @@ -403,7 +392,7 @@ angular.module('copayApp.services') lodash.each(allAccounts, function(account, email) { if (account.cards) { - // Add account's email to card list, for convenience + // Add account's email to each card var cards = lodash.clone(account.cards); lodash.each(cards, function(x) { x.email = email; @@ -417,14 +406,7 @@ angular.module('copayApp.services') }); }; - // card: { - // eid: card id - // id: card id - // lastFourDigits: card number - // token: card token - // } root.removeBitpayDebitCard = function(network, cardEid, cb) { - root.getBitpayAccounts(network, function(err, allAccounts) { lodash.each(allAccounts, function(account) { @@ -500,7 +482,6 @@ angular.module('copayApp.services') }); }; - // data: { // email: account email // token: account token @@ -530,7 +511,7 @@ angular.module('copayApp.services') // account: { // email: account email // apiContext: the context needed for making future api calls - // bitpayDebitCards: an array of cards + // cards: an array of cards // } root.removeBitpayAccount = function(network, account, cb) { if (lodash.isString(account)) { @@ -538,36 +519,14 @@ angular.module('copayApp.services') } account = account || {}; if (lodash.isEmpty(account)) return cb('No account to remove'); - storage.get('bitpayAccounts-v3-' + network, function(err, bitpayAccounts) { + storage.get('bitpayAccounts-v2-' + network, function(err, bitpayAccounts) { if (err) cb(err); if (lodash.isString(bitpayAccounts)) { bitpayAccounts = JSON.parse(bitpayAccounts); } bitpayAccounts = bitpayAccounts || {}; delete bitpayAccounts[account.email]; - storage.set('bitpayAccounts-v3-' + network, JSON.stringify(bitpayAccounts), cb); - }); - }; - - // account: { - // email: account email - // apiContext: the context needed for making future api calls - // bitpayDebitCards: an array of cards - // } - root.removeBitpayAccount = function(network, account, cb) { - if (lodash.isString(account)) { - account = JSON.parse(account); - } - account = account || {}; - if (lodash.isEmpty(account)) return cb('No account to remove'); - storage.get('bitpayAccounts-v3-' + network, function(err, bitpayAccounts) { - if (err) cb(err); - if (lodash.isString(bitpayAccounts)) { - bitpayAccounts = JSON.parse(bitpayAccounts); - } - bitpayAccounts = bitpayAccounts || {}; - delete bitpayAccounts[account.email]; - storage.set('bitpayAccounts-v3-' + network, JSON.stringify(bitpayAccounts), cb); + storage.set('bitpayAccounts-v2-' + network, JSON.stringify(bitpayAccounts), cb); }); }; diff --git a/www/views/includes/accountSelector.html b/www/views/includes/accountSelector.html index a8da1fb1e..862524798 100644 --- a/www/views/includes/accountSelector.html +++ b/www/views/includes/accountSelector.html @@ -9,7 +9,7 @@
- {{account.firstName}} {{account.lastName}} + {{account.givenName}} {{account.familyName}} {{account.email}}