refactor bitpay card
This commit is contained in:
parent
5b1cbe0ab9
commit
19b3d9bb15
11 changed files with 242 additions and 261 deletions
|
|
@ -10,7 +10,11 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
|
||||||
$scope.network = bitpayService.getEnvironment().network;
|
$scope.network = bitpayService.getEnvironment().network;
|
||||||
|
|
||||||
var updateHistoryFromCache = function(cb) {
|
var updateHistoryFromCache = function(cb) {
|
||||||
bitpayCardService.getBitpayDebitCardsHistory($scope.cardId, function(err, data) {
|
// TODO no cache for now
|
||||||
|
$log.warn ('TODO: cache');
|
||||||
|
return cb();
|
||||||
|
|
||||||
|
bitpayCardService.getHistory($scope.cardId, function(err, data) {
|
||||||
if (err || lodash.isEmpty(data)) return cb();
|
if (err || lodash.isEmpty(data)) return cb();
|
||||||
$scope.historyCached = true;
|
$scope.historyCached = true;
|
||||||
self.bitpayCardTransactionHistory = data.transactions;
|
self.bitpayCardTransactionHistory = data.transactions;
|
||||||
|
|
@ -86,15 +90,18 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
|
||||||
self.bitpayCardCurrentBalance = history.currentCardBalance;
|
self.bitpayCardCurrentBalance = history.currentCardBalance;
|
||||||
|
|
||||||
if ($scope.dateRange.value == 'last30Days') {
|
if ($scope.dateRange.value == 'last30Days') {
|
||||||
$log.debug('BitPay Card: store cache history');
|
|
||||||
var cacheHistory = {
|
// TODO CACHE
|
||||||
balance: history.currentCardBalance,
|
//
|
||||||
transactions: history.txs
|
// $log.debug('BitPay Card: store cache history');
|
||||||
};
|
// var cacheHistory = {
|
||||||
bitpayCardService.setBitpayDebitCardsHistory($scope.cardId, cacheHistory, {}, function(err) {
|
// balance: history.currentCardBalance,
|
||||||
if (err) $log.error(err);
|
// transactions: history.txs
|
||||||
$scope.historyCached = true;
|
// };
|
||||||
});
|
// bitpayCardService.setHistory($scope.cardId, cacheHistory, {}, function(err) {
|
||||||
|
// if (err) $log.error(err);
|
||||||
|
// $scope.historyCached = true;
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
$timeout(function() {
|
$timeout(function() {
|
||||||
$scope.$apply();
|
$scope.$apply();
|
||||||
|
|
@ -147,7 +154,7 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
|
||||||
updateHistoryFromCache(function() {
|
updateHistoryFromCache(function() {
|
||||||
self.update();
|
self.update();
|
||||||
});
|
});
|
||||||
bitpayCardService.getBitpayDebitCards(function(err, cards) {
|
bitpayCardService.getCards(function(err, cards) {
|
||||||
if (err) return;
|
if (err) return;
|
||||||
$scope.card = lodash.find(cards, function(card) {
|
$scope.card = lodash.find(cards, function(card) {
|
||||||
return card.eid == $scope.cardId;
|
return card.eid == $scope.cardId;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
angular.module('copayApp.controllers').controller('bitpayCardIntroController', function($scope, $log, $state, $ionicHistory, storageService, externalLinkService, bitpayCardService, gettextCatalog, popupService, appIdentityService, bitpayService) {
|
angular.module('copayApp.controllers').controller('bitpayCardIntroController', function($scope, $log, $state, $ionicHistory, storageService, externalLinkService, bitpayCardService, gettextCatalog, popupService, appIdentityService, bitpayService, lodash) {
|
||||||
|
|
||||||
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
||||||
if (data.stateParams && data.stateParams.secret) {
|
if (data.stateParams && data.stateParams.secret) {
|
||||||
|
|
@ -18,27 +18,23 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (paired) {
|
if (paired) {
|
||||||
bitpayCardService.fetchBitpayDebitCards(apiContext, function(err, data) {
|
bitpayCardService.sync(apiContext, function(err, cards) {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
popupService.showAlert(gettextCatalog.getString('Error fetching Debit Cards'), err);
|
popupService.showAlert(gettextCatalog.getString('Error updating Debit Cards'), err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Set flag for nextStep
|
// Set flag for nextStep
|
||||||
storageService.setNextStep('BitpayCard', 'true', function(err) {});
|
storageService.setNextStep('BitpayCard', 'true', function(err) {});
|
||||||
// Save data
|
|
||||||
bitpayCardService.setBitpayDebitCards(data, function(err) {
|
$ionicHistory.nextViewOptions({
|
||||||
if (err) return;
|
disableAnimate: true
|
||||||
$ionicHistory.nextViewOptions({
|
});
|
||||||
disableAnimate: true
|
$state.go('tabs.home').then(function() {
|
||||||
});
|
if (cards[0]) {
|
||||||
$state.go('tabs.home').then(function() {
|
$state.transitionTo('tabs.bitpayCard', {
|
||||||
if (data.cards[0]) {
|
id: cards[0].id
|
||||||
$state.transitionTo('tabs.bitpayCard', {
|
});
|
||||||
id: data.cards[0].id
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,21 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('preferencesBitpayCardController',
|
angular.module('copayApp.controllers').controller('preferencesBitpayCardController',
|
||||||
function($scope, $state, $timeout, $ionicHistory, bitpayCardService, popupService, gettextCatalog) {
|
function($scope, $state, $timeout, $ionicHistory, bitpayCardService, popupService, gettextCatalog, $log) {
|
||||||
|
|
||||||
$scope.remove = function(card) {
|
$scope.remove = function(card) {
|
||||||
var msg = gettextCatalog.getString('Are you sure you would like to remove your BitPay Card ({{lastFourDigits}}) from this device?', {
|
var msg = gettextCatalog.getString('Are you sure you would like to remove your BitPay Card ({{lastFourDigits}}) from this device?', {
|
||||||
lastFourDigits: card.lastFourDigits
|
lastFourDigits: card.lastFourDigits
|
||||||
});
|
});
|
||||||
popupService.showConfirm(null, msg, null, null, function(res) {
|
popupService.showConfirm(null, msg, null, null, function(res) {
|
||||||
if (res) remove(card);
|
$log.info('Removing bitpay card:' + card.eid)
|
||||||
|
if (res)
|
||||||
|
remove(card.eid);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var remove = function(card) {
|
var remove = function(cardEid) {
|
||||||
bitpayCardService.remove(card, function(err) {
|
bitpayCardService.remove(cardEid, 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'));
|
||||||
}
|
}
|
||||||
|
|
@ -25,8 +27,9 @@ angular.module('copayApp.controllers').controller('preferencesBitpayCardControll
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
||||||
bitpayCardService.getBitpayDebitCards(function(err, data) {
|
bitpayCardService.getCards(function(err, data) {
|
||||||
if (err) return;
|
if (err) return;
|
||||||
|
|
||||||
$scope.bitpayCards = data;
|
$scope.bitpayCards = data;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -285,7 +285,7 @@ angular.module('copayApp.controllers').controller('tabHomeController',
|
||||||
};
|
};
|
||||||
|
|
||||||
var bitpayCardCache = function() {
|
var bitpayCardCache = function() {
|
||||||
bitpayCardService.getBitpayDebitCards(function(err, data) {
|
bitpayCardService.getCards(function(err, data) {
|
||||||
if (err) return;
|
if (err) return;
|
||||||
if (lodash.isEmpty(data)) {
|
if (lodash.isEmpty(data)) {
|
||||||
$scope.bitpayCards = null;
|
$scope.bitpayCards = null;
|
||||||
|
|
@ -296,14 +296,16 @@ angular.module('copayApp.controllers').controller('tabHomeController',
|
||||||
$scope.$digest();
|
$scope.$digest();
|
||||||
}, 100);
|
}, 100);
|
||||||
});
|
});
|
||||||
bitpayCardService.getBitpayDebitCardsHistory(null, function(err, data) {
|
|
||||||
if (err) return;
|
// TODO
|
||||||
if (lodash.isEmpty(data)) {
|
// bitpayCardService.getCardsHistoryCache(function(err, data) {
|
||||||
$scope.cardsHistory = null;
|
// if (err) return;
|
||||||
return;
|
// if (lodash.isEmpty(data)) {
|
||||||
}
|
// $scope.cardsHistory = null;
|
||||||
$scope.cardsHistory = data;
|
// return;
|
||||||
});
|
// }
|
||||||
|
// $scope.cardsHistory = data;
|
||||||
|
// });
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.onRefresh = function() {
|
$scope.onRefresh = function() {
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ angular.module('copayApp.controllers').controller('tabSettingsController', funct
|
||||||
$scope.coinbaseEnabled = config.coinbaseV2 && !isWindowsPhoneApp;
|
$scope.coinbaseEnabled = config.coinbaseV2 && !isWindowsPhoneApp;
|
||||||
|
|
||||||
if ($scope.bitpayCardEnabled) {
|
if ($scope.bitpayCardEnabled) {
|
||||||
bitpayCardService.getBitpayDebitCards(function(err, cards) {
|
bitpayCardService.getCards(function(err, cards) {
|
||||||
if (err) $log.error(err);
|
if (err) $log.error(err);
|
||||||
$scope.bitpayCards = cards && cards.length > 0;
|
$scope.bitpayCards = cards && cards.length > 0;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
|
||||||
};
|
};
|
||||||
|
|
||||||
var _processTransactions = function(invoices, history) {
|
var _processTransactions = function(invoices, history) {
|
||||||
invoices = invoices || [];
|
invoices = invoices || [];
|
||||||
for (var i = 0; i < invoices.length; i++) {
|
for (var i = 0; i < invoices.length; i++) {
|
||||||
var matched = false;
|
var matched = false;
|
||||||
for (var j = 0; j < history.length; j++) {
|
for (var j = 0; j < history.length; j++) {
|
||||||
|
|
@ -22,8 +22,8 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
|
||||||
if (!matched && isInvoiceLessThanOneDayOld) {
|
if (!matched && isInvoiceLessThanOneDayOld) {
|
||||||
var isInvoiceUnderpaid = invoices[i].exceptionStatus === 'paidPartial';
|
var isInvoiceUnderpaid = invoices[i].exceptionStatus === 'paidPartial';
|
||||||
|
|
||||||
if(['paid', 'confirmed', 'complete'].indexOf(invoices[i].status) >= 0
|
if (['paid', 'confirmed', 'complete'].indexOf(invoices[i].status) >= 0 ||
|
||||||
|| (invoices[i].status === 'invalid' || isInvoiceUnderpaid)) {
|
(invoices[i].status === 'invalid' || isInvoiceUnderpaid)) {
|
||||||
|
|
||||||
history.unshift({
|
history.unshift({
|
||||||
timestamp: new Date(invoices[i].invoiceTime),
|
timestamp: new Date(invoices[i].invoiceTime),
|
||||||
|
|
@ -39,7 +39,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
|
||||||
return history;
|
return history;
|
||||||
};
|
};
|
||||||
|
|
||||||
root.fetchBitpayDebitCards = function(apiContext, cb) {
|
root.sync = function(apiContext, cb) {
|
||||||
var json = {
|
var json = {
|
||||||
method: 'getDebitCards'
|
method: 'getDebitCards'
|
||||||
};
|
};
|
||||||
|
|
@ -47,65 +47,111 @@ 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');
|
||||||
return cb(data.data.error, {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) {
|
||||||
|
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));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
root.getHistory = function(cardId, params, cb) {
|
// opts: range
|
||||||
|
root.getHistory = function(cardId, opts, cb) {
|
||||||
var invoices, transactions;
|
var invoices, transactions;
|
||||||
params = params || {};
|
opts = opts || {};
|
||||||
|
|
||||||
var json = {
|
var json = {
|
||||||
method: 'getInvoiceHistory',
|
method: 'getInvoiceHistory',
|
||||||
params: JSON.stringify(params)
|
params: JSON.stringify(opts)
|
||||||
};
|
};
|
||||||
|
|
||||||
appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) {
|
appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
root.getBitpayDebitCards(function(err, data) {
|
|
||||||
|
root.getCards(function(err, data) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
var card = lodash.find(data, {id : cardId});
|
var card = lodash.find(data, {
|
||||||
if (!card) return cb(_setError('Card not found'));
|
id: cardId
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!card)
|
||||||
|
return cb(_setError('Card not found'));
|
||||||
|
|
||||||
// Get invoices
|
// Get invoices
|
||||||
bitpayService.post('/api/v2/' + card.token, json, function(data) {
|
bitpayService.post('/api/v2/' + card.token, json, function(data) {
|
||||||
$log.info('BitPay Get Invoices: SUCCESS');
|
$log.info('BitPay Get Invoices: SUCCESS');
|
||||||
invoices = data.data.data || [];
|
invoices = data.data.data || [];
|
||||||
if (lodash.isEmpty(invoices)) $log.info('No invoices');
|
|
||||||
|
if (lodash.isEmpty(invoices))
|
||||||
|
$log.info('No invoices');
|
||||||
|
|
||||||
json = {
|
json = {
|
||||||
method: 'getTransactionHistory',
|
method: 'getTransactionHistory',
|
||||||
params: JSON.stringify(params)
|
params: JSON.stringify(opts)
|
||||||
};
|
};
|
||||||
// Get transactions list
|
// Get transactions list
|
||||||
bitpayService.post('/api/v2/' + card.token, json, function(data) {
|
bitpayService.post('/api/v2/' + card.token, json, function(data) {
|
||||||
$log.info('BitPay Get Transactions: SUCCESS');
|
$log.info('BitPay Get Transactions: SUCCESS');
|
||||||
transactions = data.data.data || {};
|
transactions = data.data.data || {};
|
||||||
transactions['txs'] = _processTransactions(invoices, transactions.transactionList);
|
transactions['txs'] = _processTransactions(invoices, transactions.transactionList);
|
||||||
|
|
||||||
|
// TODO CACHE?
|
||||||
|
// update cache?
|
||||||
|
// if (lodash.isEmpty(opts)) {
|
||||||
|
// root.setHistoryCache(cardId, transactions, function() {});
|
||||||
|
// }
|
||||||
|
|
||||||
return cb(data.data.error, transactions);
|
return cb(data.data.error, transactions);
|
||||||
}, function(data) {
|
}, function(data) {
|
||||||
return cb(_setError('BitPay Card Error: Get Transactions', data));
|
return cb(_setError('BitPay Card Error: Get Transactions', data));
|
||||||
});
|
});
|
||||||
}, function(data) {
|
}, function(data) {
|
||||||
return cb(_setError('BitPay Card Error: Get Invoices', data));
|
return cb(_setError('BitPay Card Error: Get Invoices', data));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
root.topUp = function(cardId, params, cb) {
|
root.topUp = function(cardId, opts, cb) {
|
||||||
params = params || {};
|
opts = opts || {};
|
||||||
var json = {
|
var json = {
|
||||||
method: 'generateTopUpInvoice',
|
method: 'generateTopUpInvoice',
|
||||||
params: JSON.stringify(params)
|
params: JSON.stringify(opts)
|
||||||
};
|
};
|
||||||
appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) {
|
appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
root.getBitpayDebitCards(function(err, data) {
|
|
||||||
|
root.getCards(function(err, data) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
var card = lodash.find(data, {id : cardId});
|
|
||||||
if (!card) return cb(_setError('Card not found'));
|
var card = lodash.find(data, {
|
||||||
|
id: cardId
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!card)
|
||||||
|
return cb(_setError('Card not found'));
|
||||||
|
|
||||||
bitpayService.post('/api/v2/' + card.token, json, function(data) {
|
bitpayService.post('/api/v2/' + card.token, json, function(data) {
|
||||||
$log.info('BitPay TopUp: SUCCESS');
|
$log.info('BitPay TopUp: SUCCESS');
|
||||||
if(data.data.error) {
|
if (data.data.error) {
|
||||||
return cb(data.data.error);
|
return cb(data.data.error);
|
||||||
} else {
|
} else {
|
||||||
return cb(null, data.data.data.invoice);
|
return cb(null, data.data.data.invoice);
|
||||||
|
|
@ -126,75 +172,32 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
root.getBitpayDebitCards = function(cb) {
|
// get all cards, for all accounts.
|
||||||
storageService.getBitpayDebitCards(bitpayService.getEnvironment().network, function(err, data) {
|
root.getCards = function(cb) {
|
||||||
if (err) return cb(err);
|
storageService.getBitpayDebitCards(bitpayService.getEnvironment().network, cb);
|
||||||
if (lodash.isString(data)) {
|
|
||||||
data = JSON.parse(data);
|
|
||||||
}
|
|
||||||
data = data || {};
|
|
||||||
return cb(null, data);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
root.setBitpayDebitCards = function(data, cb) {
|
// TODO??
|
||||||
data = JSON.stringify(data);
|
// root.getHistoryCache = function(cardId, cb) {
|
||||||
storageService.setBitpayDebitCards(bitpayService.getEnvironment().network, data, function(err) {
|
// storageService.getBitpayDebitCardHistory(cardId, cb);
|
||||||
if (err) return cb(err);
|
// };
|
||||||
return cb();
|
//
|
||||||
});
|
// root.setHistoryCache = function(cardId, data, cb) {
|
||||||
};
|
// storageService.setBitpayDebitCardHistory(cardId, data, cb);
|
||||||
|
// };
|
||||||
root.getBitpayDebitCardsHistory = function(cardId, cb) {
|
//
|
||||||
storageService.getBitpayDebitCardsHistory(bitpayService.getEnvironment().network, function(err, data) {
|
|
||||||
if (err) return cb(err);
|
root.remove = function(cardId, cb) {
|
||||||
if (lodash.isString(data)) {
|
storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, cardId, function(err) {
|
||||||
data = JSON.parse(data);
|
|
||||||
}
|
|
||||||
data = data || {};
|
|
||||||
if (cardId) data = data[cardId];
|
|
||||||
return cb(null, data);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
root.setBitpayDebitCardsHistory = function(cardId, data, opts, cb) {
|
|
||||||
storageService.getBitpayDebitCardsHistory(bitpayService.getEnvironment().network, function(err, oldData) {
|
|
||||||
if (lodash.isString(oldData)) {
|
|
||||||
oldData = JSON.parse(oldData);
|
|
||||||
}
|
|
||||||
if (lodash.isString(data)) {
|
|
||||||
data = JSON.parse(data);
|
|
||||||
}
|
|
||||||
var inv = oldData || {};
|
|
||||||
inv[cardId] = data;
|
|
||||||
if (opts && opts.remove) {
|
|
||||||
delete(inv[cardId]);
|
|
||||||
}
|
|
||||||
inv = JSON.stringify(inv);
|
|
||||||
|
|
||||||
storageService.setBitpayDebitCardsHistory(bitpayService.getEnvironment().network, inv, function(err) {
|
|
||||||
return cb(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
root.remove = function(card, cb) {
|
|
||||||
storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, card, 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 card history
|
return cb(err);
|
||||||
}
|
}
|
||||||
storageService.removeBitpayDebitCardHistory(bitpayService.getEnvironment().network, card, function(err) {
|
storageService.removeBitpayDebitCardHistory(cardId, cb);
|
||||||
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');
|
||||||
|
|
|
||||||
|
|
@ -136,13 +136,10 @@ angular.module('copayApp.services').factory('bitpayService', function($log, $htt
|
||||||
};
|
};
|
||||||
|
|
||||||
var setBitpayAccount = function(accountData, cb) {
|
var setBitpayAccount = function(accountData, cb) {
|
||||||
var data = JSON.stringify(accountData);
|
storageService.setBitpayAccount(root.getEnvironment().network, accountData, cb);
|
||||||
storageService.setBitpayAccount(root.getEnvironment().network, data, function(err) {
|
|
||||||
if (err) return cb(err);
|
|
||||||
return cb();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
var _get = function(endpoint) {
|
var _get = function(endpoint) {
|
||||||
return {
|
return {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ angular.module('copayApp.services')
|
||||||
if (lodash.isObject(v))
|
if (lodash.isObject(v))
|
||||||
v = JSON.stringify(v);
|
v = JSON.stringify(v);
|
||||||
|
|
||||||
if (!lodash.isString(v)) {
|
if (v && !lodash.isString(v)) {
|
||||||
v = v.toString();
|
v = v.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,16 +43,17 @@ angular.module('copayApp.services')
|
||||||
};
|
};
|
||||||
|
|
||||||
root.set = function(k, v, cb) {
|
root.set = function(k, v, cb) {
|
||||||
|
|
||||||
|
if (lodash.isObject(v)) {
|
||||||
|
v = JSON.stringify(v);
|
||||||
|
}
|
||||||
|
if (v && !lodash.isString(v)) {
|
||||||
|
v = v.toString();
|
||||||
|
}
|
||||||
|
|
||||||
if (isChromeApp || isNW) {
|
if (isChromeApp || isNW) {
|
||||||
var obj = {};
|
var obj = {};
|
||||||
|
|
||||||
if (lodash.isObject(v)) {
|
|
||||||
v = JSON.stringify(v);
|
|
||||||
}
|
|
||||||
if (!lodash.isString(v)) {
|
|
||||||
v = v.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
obj[k] = v;
|
obj[k] = v;
|
||||||
|
|
||||||
chrome.storage.local.set(obj, cb);
|
chrome.storage.local.set(obj, cb);
|
||||||
|
|
@ -60,7 +61,6 @@ angular.module('copayApp.services')
|
||||||
ls.setItem(k, v);
|
ls.setItem(k, v);
|
||||||
return cb();
|
return cb();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
root.remove = function(k, cb) {
|
root.remove = function(k, cb) {
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,7 @@ angular.module('copayApp.services').factory('openURLService', function($rootScop
|
||||||
$log.debug('Registering Browser handlers base:' + base);
|
$log.debug('Registering Browser handlers base:' + base);
|
||||||
navigator.registerProtocolHandler('bitcoin', url, 'Copay Bitcoin Handler');
|
navigator.registerProtocolHandler('bitcoin', url, 'Copay Bitcoin Handler');
|
||||||
navigator.registerProtocolHandler('web+copay', url, 'Copay Wallet Handler');
|
navigator.registerProtocolHandler('web+copay', url, 'Copay Wallet Handler');
|
||||||
|
navigator.registerProtocolHandler('web+bitpay', url, 'Bitpay Wallet Handler');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@ angular.module('copayApp.services')
|
||||||
}, cb);
|
}, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// This is only used in Copay, we used to encrypt profile
|
||||||
|
// using device's UUID.
|
||||||
|
|
||||||
var decryptOnMobile = function(text, cb) {
|
var decryptOnMobile = function(text, cb) {
|
||||||
var json;
|
var json;
|
||||||
try {
|
try {
|
||||||
|
|
@ -74,7 +77,6 @@ angular.module('copayApp.services')
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// This is only use in Copay, for very old instalations
|
// This is only use in Copay, for very old instalations
|
||||||
// in which we use to use localStorage instead of fileStorage
|
// in which we use to use localStorage instead of fileStorage
|
||||||
root.tryToMigrate = function(cb) {
|
root.tryToMigrate = function(cb) {
|
||||||
|
|
@ -342,51 +344,35 @@ angular.module('copayApp.services')
|
||||||
storage.remove('coinbaseTxs-' + network, cb);
|
storage.remove('coinbaseTxs-' + network, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
root.setBitpayDebitCardsHistory = function(network, data, cb) {
|
root.setBitpayDebitCardHistory = function(cardId, data, cb) {
|
||||||
storage.set('bitpayDebitCardsHistory-' + network, data, cb);
|
storage.set('bitpayDebitCardHistory-' + cardId, data, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
root.getBitpayDebitCardsHistory = function(network, cb) {
|
root.getBitpayDebitCardHistory = function(cardId, cb) {
|
||||||
storage.get('bitpayDebitCardsHistory-' + network, cb);
|
storage.get('bitpayDebitCardHistory-' + cardId, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
root.removeBitpayDebitCardHistory = function(network, card, cb) {
|
root.removeBitpayDebitCardHistory = function(cardId, cb) {
|
||||||
root.getBitpayDebitCardsHistory(network, function(err, data) {
|
storage.remove('bitpayDebitCardHistory-' + cardId, cb);
|
||||||
if (err) return cb(err);
|
|
||||||
if (lodash.isString(data)) {
|
|
||||||
data = JSON.parse(data);
|
|
||||||
}
|
|
||||||
data = data || {};
|
|
||||||
delete data[card.eid];
|
|
||||||
root.setBitpayDebitCardsHistory(network, JSON.stringify(data), 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
|
||||||
// ]
|
// ]
|
||||||
// email: account email
|
root.setBitpayDebitCards = function(network, email, cards, cb) {
|
||||||
// token: account token
|
|
||||||
// }
|
root.getBitpayAccounts(network, function(err, allAccounts) {
|
||||||
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');
|
|
||||||
storage.get('bitpayAccounts-v2-' + network, function(err, bitpayAccounts) {
|
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (lodash.isString(bitpayAccounts)) {
|
|
||||||
bitpayAccounts = JSON.parse(bitpayAccounts);
|
if (!allAccounts[email]) {
|
||||||
|
return cb('Cannot set cards for unknown account ' + email);
|
||||||
}
|
}
|
||||||
bitpayAccounts = bitpayAccounts || {};
|
|
||||||
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
|
allAccounts[email].cards = cards;
|
||||||
bitpayAccounts[data.email]['bitpayDebitCards-' + network] = data.cards;
|
storage.set('bitpayAccounts-v2-' + network, allAccounts, cb);
|
||||||
storage.set('bitpayAccounts-v2-' + network, JSON.stringify(bitpayAccounts), cb);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -399,24 +385,24 @@ angular.module('copayApp.services')
|
||||||
// email: account email
|
// email: account email
|
||||||
// ]
|
// ]
|
||||||
root.getBitpayDebitCards = function(network, cb) {
|
root.getBitpayDebitCards = function(network, cb) {
|
||||||
storage.get('bitpayAccounts-v2-' + network, function(err, bitpayAccounts) {
|
|
||||||
if (lodash.isString(bitpayAccounts)) {
|
root.getBitpayAccounts(network, function(err, allAccounts) {
|
||||||
bitpayAccounts = JSON.parse(bitpayAccounts);
|
if (err) return cb(err);
|
||||||
}
|
|
||||||
bitpayAccounts = bitpayAccounts || {};
|
var allCards = [];
|
||||||
var cards = [];
|
|
||||||
_asyncEach(Object.keys(bitpayAccounts), function(email, callback) {
|
lodash.each(allAccounts, function(account) {
|
||||||
// For the UI, add the account email to the card object.
|
|
||||||
var acctCards = bitpayAccounts[email]['bitpayDebitCards-' + network] || [];
|
var cards = lodash.clone(account.cards);
|
||||||
for (var i = 0; i < acctCards.length; i++) {
|
|
||||||
acctCards[i].email = email;
|
lodash.each(allAccounts, function(x) {
|
||||||
}
|
x.email = account.email;
|
||||||
cards = cards.concat(acctCards);
|
});
|
||||||
callback();
|
|
||||||
}, function() {
|
allCards = allCards.concat(cards);
|
||||||
// done
|
|
||||||
cb(err, cards);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return cb(null, allCards);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -426,95 +412,84 @@ angular.module('copayApp.services')
|
||||||
// lastFourDigits: card number
|
// lastFourDigits: card number
|
||||||
// token: card token
|
// token: card token
|
||||||
// }
|
// }
|
||||||
root.removeBitpayDebitCard = function(network, card, cb) {
|
root.removeBitpayDebitCard = function(network, cardEid, cb) {
|
||||||
if (lodash.isString(card)) {
|
|
||||||
card = JSON.parse(card);
|
|
||||||
}
|
|
||||||
card = card || {};
|
|
||||||
if (lodash.isEmpty(card) || !card.eid) return cb('No card to remove');
|
|
||||||
storage.get('bitpayAccounts-v2-' + network, function(err, bitpayAccounts) {
|
|
||||||
if (err) cb(err);
|
|
||||||
if (lodash.isString(bitpayAccounts)) {
|
|
||||||
bitpayAccounts = JSON.parse(bitpayAccounts);
|
|
||||||
}
|
|
||||||
bitpayAccounts = bitpayAccounts || {};
|
|
||||||
_asyncEach(Object.keys(bitpayAccounts), function(email, callback) {
|
|
||||||
var data = bitpayAccounts[email]['bitpayDebitCards-' + network];
|
|
||||||
var newCards = lodash.reject(data, {
|
|
||||||
'eid': card.eid
|
|
||||||
});
|
|
||||||
data = {};
|
|
||||||
data.cards = newCards;
|
|
||||||
data.email = email;
|
|
||||||
root.setBitpayDebitCards(network, data, function(err) {
|
|
||||||
if (err) cb(err);
|
|
||||||
// If there are no more cards in storage then re-enable the next step entry.
|
|
||||||
root.getBitpayDebitCards(network, function(err, cards) {
|
|
||||||
if (err) cb(err);
|
|
||||||
if (cards.length == 0) {
|
|
||||||
root.removeNextStep('BitpayCard', callback());
|
|
||||||
} else {
|
|
||||||
callback()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}, function() {
|
|
||||||
// done
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// data: {
|
root.getBitpayAccounts(network, function(err, allAccounts){
|
||||||
// email: account email
|
|
||||||
// token: account token
|
lodash.each(allAccounts, function(account){
|
||||||
// }
|
account.cards = lodash.reject(account.cards, {
|
||||||
root.setBitpayAccount = function(network, data, cb) {
|
'eid': cardEid
|
||||||
if (lodash.isString(data)) {
|
});
|
||||||
data = JSON.parse(data);
|
});
|
||||||
}
|
|
||||||
data = data || {};
|
storage.set('bitpayAccounts-v2-' + network, allAccounts, cb);
|
||||||
if (lodash.isEmpty(data) || !data.email) return cb('No account to set');
|
|
||||||
storage.get('bitpayAccounts-v2-' + network, function(err, bitpayAccounts) {
|
|
||||||
if (err) return cb(err);
|
|
||||||
if (lodash.isString(bitpayAccounts)) {
|
|
||||||
bitpayAccounts = JSON.parse(bitpayAccounts);
|
|
||||||
}
|
|
||||||
bitpayAccounts = bitpayAccounts || {};
|
|
||||||
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
|
|
||||||
bitpayAccounts[data.email]['bitpayApi-' + network] = bitpayAccounts[data.email]['bitpayApi-' + network] || {};
|
|
||||||
bitpayAccounts[data.email]['bitpayApi-' + network].token = data.token;
|
|
||||||
storage.set('bitpayAccounts-v2-' + network, JSON.stringify(bitpayAccounts), cb);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// cb(err, accounts)
|
// cb(err, accounts)
|
||||||
// accounts: {
|
// accounts: {
|
||||||
// email_1: {
|
// email_1: {
|
||||||
// bitpayApi-<network>: {
|
// token: account token
|
||||||
// token: account token
|
// cards: {
|
||||||
// }
|
|
||||||
// bitpayDebitCards-<network>: {
|
|
||||||
// <card-data>
|
// <card-data>
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// ...
|
// ...
|
||||||
// email_n: {
|
// email_n: {
|
||||||
// bitpayApi-<network>: {
|
// token: account token
|
||||||
// token: account token
|
// cards: {
|
||||||
// }
|
|
||||||
// bitpayDebitCards-<network>: {
|
|
||||||
// <card-data>
|
// <card-data>
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
//
|
||||||
root.getBitpayAccounts = function(network, cb) {
|
root.getBitpayAccounts = function(network, cb) {
|
||||||
storage.get('bitpayAccounts-v2-' + network, function(err, bitpayAccounts) {
|
storage.get('bitpayAccounts-v2-' + network, function(err, allAccountsStr) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (lodash.isString(bitpayAccounts)) {
|
|
||||||
bitpayAccounts = JSON.parse(bitpayAccounts);
|
var allAccounts = {};
|
||||||
}
|
try {
|
||||||
cb(err, bitpayAccounts);
|
allAccounts = JSON.parse(allAccountsStr);
|
||||||
|
} catch (e) {};
|
||||||
|
|
||||||
|
lodash.each(allAccounts, function(account, email) {
|
||||||
|
|
||||||
|
// Migrate old `'bitpayApi-' + network` key, if exists
|
||||||
|
if (!account.token && account['bitpayApi-' + network].token) {
|
||||||
|
$log.info('Migrating all bitpayApi-network branch');
|
||||||
|
account = account['bitpayApi-' + network];
|
||||||
|
delete account['bitpayApi-' + network];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return cb(err, allAccounts);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// data: {
|
||||||
|
// email: account email
|
||||||
|
// token: account token
|
||||||
|
// }
|
||||||
|
root.setBitpayAccount = function(network, data, cb) {
|
||||||
|
|
||||||
|
if (!lodash.isObject(data) || !data.email || !data.token)
|
||||||
|
return cb('No account to set');
|
||||||
|
|
||||||
|
var email = data.email;
|
||||||
|
var token = data.token;
|
||||||
|
|
||||||
|
|
||||||
|
root.getBitpayAccounts(network, function(err, allAccounts) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
var account = allAccounts[email] || {};
|
||||||
|
account.token = token;
|
||||||
|
|
||||||
|
allAccounts[email] = account;
|
||||||
|
|
||||||
|
$log.info('Storing BitPay accounts with new account:' + email);
|
||||||
|
storage.set('bitpayAccounts-v2-' + network, allAccounts, cb);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -525,10 +500,7 @@ angular.module('copayApp.services')
|
||||||
root.getAppIdentity = function(network, cb) {
|
root.getAppIdentity = function(network, cb) {
|
||||||
storage.get('appIdentity-' + network, function(err, data) {
|
storage.get('appIdentity-' + network, function(err, data) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (lodash.isString(data)) {
|
cb(err, JSON.parse(data || '{}'));
|
||||||
data = JSON.parse(data);
|
|
||||||
}
|
|
||||||
cb(err, data);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue