Updated for 1.3.6.

This commit is contained in:
Andy Phillipson 2017-02-09 14:58:47 -05:00
parent 7a2d386f34
commit ff56fc2bee
7 changed files with 62 additions and 123 deletions

View File

@ -15,7 +15,7 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
return; return;
} }
if (paired) { if (paired) {
bitpayCardService.sync(apiContext, function(err, data) { bitpayCardService.sync(apiContext, function(err, cards) {
if (err) { if (err) {
popupService.showAlert(gettextCatalog.getString('Error updating Debit Cards'), err); popupService.showAlert(gettextCatalog.getString('Error updating Debit Cards'), err);
return; return;
@ -26,9 +26,9 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
disableAnimate: true disableAnimate: true
}); });
$state.go('tabs.home').then(function() { $state.go('tabs.home').then(function() {
if (data.cards[0]) { if (cards[0]) {
$state.transitionTo('tabs.bitpayCard', { $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) { if (account == undefined) {
startPairBitPayAccount(); startPairBitPayAccount();
} else { } else {
bitpayCardService.fetchBitpayDebitCards(account.apiContext, function(err, data) { bitpayCardService.sync(account.apiContext, function(err, data) {
if (err) { if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err); popupService.showAlert(gettextCatalog.getString('Error'), err);
return; return;

View File

@ -45,7 +45,7 @@ angular.module('copayApp.controllers').controller('preferencesBitpayServicesCont
}; };
var removeCard = function(card) { var removeCard = function(card) {
bitpayCardService.removeCard(card, function(err) { bitpayCardService.remove(card.id, function(err) {
if (err) { if (err) {
return popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not remove card')); 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) { var setScope = function(cb) {
bitpayAccountService.getAccounts(function(err, data) { bitpayAccountService.getAccounts(function(err, accounts) {
if (err) return; if (err) return;
$scope.bitpayAccounts = data; $scope.bitpayAccounts = accounts;
bitpayCardService.getBitpayDebitCards(function(err, data) { bitpayCardService.getCards(function(err, cards) {
if (err) return; if (err) return;
$scope.bitpayCards = data; $scope.bitpayCards = cards;
if (cb) { if (cb) {
cb(); cb();
} }

View File

@ -133,7 +133,7 @@ angular.module('copayApp.services').factory('bitpayAccountService', function($lo
// from the server for each account (apiContext). // from the server for each account (apiContext).
root.getAccounts = function(cb) { root.getAccounts = function(cb) {
root.getAccountsAsStored(function(err, accounts) { root.getAccountsAsStored(function(err, accounts) {
if (err || !accounts) { if (err || lodash.isEmpty(accounts)) {
return cb(err, []); return cb(err, []);
} }
appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) { appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) {
@ -143,10 +143,10 @@ angular.module('copayApp.services').factory('bitpayAccountService', function($lo
var accountsArray = []; var accountsArray = [];
lodash.forEach(Object.keys(accounts), function(key) { 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].email = key;
accounts[key].firstName = accounts[key].givenName || ''; accounts[key].givenName = accounts[key].givenName || '';
accounts[key].lastName = accounts[key].familyName || ''; accounts[key].familyName = accounts[key].familyName || '';
accounts[key].apiContext = { accounts[key].apiContext = {
token: accounts[key].token, token: accounts[key].token,
pairData: { pairData: {
@ -164,29 +164,14 @@ angular.module('copayApp.services').factory('bitpayAccountService', function($lo
var setBitpayAccount = function(account, cb) { var setBitpayAccount = function(account, cb) {
storageService.setBitpayAccount(bitpayService.getEnvironment().network, account, function(err) { storageService.setBitpayAccount(bitpayService.getEnvironment().network, account, function(err) {
if (err) { return cb(err);
return cb(err);
}
return cb();
}); });
}; };
root.removeAccount = function(account, cb) { root.removeAccount = function(account, cb) {
storageService.removeBitpayAccount(bitpayService.getEnvironment().network, account, function(err) { storageService.removeBitpayAccount(bitpayService.getEnvironment().network, account, function(err) {
if (err) { bitpayCardService.registerNextStep();
$log.error('Error removing BitPay account: ' + err); cb(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();
}
});
}); });
}; };

View File

@ -47,13 +47,27 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
bitpayService.post('/api/v2/' + apiContext.token, json, function(data) { bitpayService.post('/api/v2/' + apiContext.token, json, function(data) {
if (data && data.data.error) return cb(data.data.error); if (data && data.data.error) return cb(data.data.error);
$log.info('BitPay Get Debit Cards: SUCCESS'); $log.info('BitPay Get Debit Cards: SUCCESS');
// Cache card data in storage
var cardData = { var cards = [];
cards: data.data.data,
email: apiContext.pairData.email lodash.each(data.data.data, function(x) {
} var n = {};
storageService.setBitpayDebitCards(bitpayService.getEnvironment().network, cardData, function(err) {
return cb(err, {token: apiContext.token, cards: data.data.data, email: apiContext.pairData.email}); 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) { }, function(data) {
return cb(_setError('BitPay Card Error: Get Debit Cards', data)); return cb(_setError('BitPay Card Error: Get Debit Cards', data));
@ -186,36 +200,17 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
}, cb); }, cb);
}; };
root.removeCard = function(card, cb) { root.remove = function(cardId, cb) {
storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, card, function(err) { storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, cardId, function(err) {
if (err) { if (err) {
$log.error('Error removing BitPay debit card: ' + 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 root.registerNextStep();
// storageService.removeBalanceCache(cardId, cb);
// 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.getRates = function(currency, cb) { root.getRates = function(currency, cb) {
bitpayService.get('/rates/' + currency, function(data) { bitpayService.get('/rates/' + currency, function(data) {
$log.info('BitPay Get Rates: SUCCESS'); $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) { root.getCards(function(err, cards) {
if (lodash.isEmpty(cards)) { if (lodash.isEmpty(cards)) {
nextStepsService.register(nextStepItem); nextStepsService.register(nextStepItem);
@ -1313,7 +1308,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
}); });
}; };
register(); root.registerNextStep();
return root; return root;
}); });

View File

@ -356,33 +356,22 @@ angular.module('copayApp.services')
storage.remove('balanceCache-' + cardId, cb); storage.remove('balanceCache-' + cardId, cb);
}; };
// data: { // cards: [
// cards: [ // eid: card id
// eid: card id // id: card id
// id: card id // lastFourDigits: card number
// lastFourDigits: card number // token: card token
// token: card token // ]
// ] root.setBitpayDebitCards = function(network, email, cards, cb) {
// 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');
root.getBitpayAccounts(network, function(err, allAccounts) { root.getBitpayAccounts(network, function(err, allAccounts) {
if (err) return cb(err); if (err) return cb(err);
allAccounts = allAccounts || {}; if (!allAccounts[email]) {
if (!allAccounts[data.email]) { return cb('Cannot set cards for unknown account ' + email);
return cb('Cannot set cards for unknown account ' + data.email);
} }
allAccounts[data.email] = allAccounts[data.email] || {};
allAccounts[data.email]['bitpayDebitCards-' + network] = data.cards; allAccounts[email].cards = cards;
storage.set('bitpayAccounts-v2-' + network, JSON.stringify(allAccounts), cb); storage.set('bitpayAccounts-v2-' + network, allAccounts, cb);
}); });
}; };
@ -403,7 +392,7 @@ angular.module('copayApp.services')
lodash.each(allAccounts, function(account, email) { lodash.each(allAccounts, function(account, email) {
if (account.cards) { 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); var cards = lodash.clone(account.cards);
lodash.each(cards, function(x) { lodash.each(cards, function(x) {
x.email = email; 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.removeBitpayDebitCard = function(network, cardEid, cb) {
root.getBitpayAccounts(network, function(err, allAccounts) { root.getBitpayAccounts(network, function(err, allAccounts) {
lodash.each(allAccounts, function(account) { lodash.each(allAccounts, function(account) {
@ -500,7 +482,6 @@ angular.module('copayApp.services')
}); });
}; };
// data: { // data: {
// email: account email // email: account email
// token: account token // token: account token
@ -530,7 +511,7 @@ angular.module('copayApp.services')
// account: { // account: {
// email: account email // email: account email
// apiContext: the context needed for making future api calls // 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) { root.removeBitpayAccount = function(network, account, cb) {
if (lodash.isString(account)) { if (lodash.isString(account)) {
@ -538,36 +519,14 @@ angular.module('copayApp.services')
} }
account = account || {}; account = account || {};
if (lodash.isEmpty(account)) return cb('No account to remove'); 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 (err) cb(err);
if (lodash.isString(bitpayAccounts)) { if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts); bitpayAccounts = JSON.parse(bitpayAccounts);
} }
bitpayAccounts = bitpayAccounts || {}; bitpayAccounts = bitpayAccounts || {};
delete bitpayAccounts[account.email]; delete bitpayAccounts[account.email];
storage.set('bitpayAccounts-v3-' + network, JSON.stringify(bitpayAccounts), cb); storage.set('bitpayAccounts-v2-' + 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);
}); });
}; };

View File

@ -9,7 +9,7 @@
<div class="account-inner"> <div class="account-inner">
<div class="account-details"> <div class="account-details">
<div class="account-name"> <div class="account-name">
{{a.firstName}} {{a.lastName}} {{a.givenName}} {{a.familyName}}
</div> </div>
<p class="account-email" ng-if="a.email"> <p class="account-email" ng-if="a.email">
<span>{{a.email}}</span> <span>{{a.email}}</span>

View File

@ -25,7 +25,7 @@
</div> </div>
<div class="item item-icon-right" ng-repeat="account in bitpayAccounts"> <div class="item item-icon-right" ng-repeat="account in bitpayAccounts">
<span class="item-title"> <span class="item-title">
{{account.firstName}} {{account.lastName}} {{account.givenName}} {{account.familyName}}
</span> </span>
<span class="item-subtitle"> <span class="item-subtitle">
{{account.email}} {{account.email}}