Improved bitpay account pairing and management of paired state and data.

This commit is contained in:
Andy Phillipson 2017-01-06 12:11:47 -05:00
commit 63bc3d8f63
23 changed files with 691 additions and 208 deletions

View file

@ -47,28 +47,13 @@ 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');
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) {
register();
return cb(err, cards);
// Cache card data in storage
var cardData = {
cards: data.data.data,
email: apiContext.pairData.email
}
root.setBitpayDebitCards(cardData, function(err) {
return cb(err, {token: apiContext.token, cards: data.data.data, email: apiContext.pairData.email});
});
}, function(data) {
return cb(_setError('BitPay Card Error: Get Debit Cards', data));
@ -201,15 +186,32 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
}, cb);
};
root.remove = function(cardId, cb) {
storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, cardId, function(err) {
root.removeCard = function(card, cb) {
storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, card, function(err) {
if (err) {
$log.error('Error removing BitPay debit card: ' + err);
return cb(err);
// Continue, try to remove/cleanup next step and card history
}
register();
storageService.removeBalanceCache(cardId, cb);
// 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();
});
});
};