From c916babe25b3c4ceec4f87c3b40d19fd5bcf422e Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Thu, 13 Oct 2016 17:49:48 -0300 Subject: [PATCH] Minor bug fixes. New bitauth method for getting token --- src/js/controllers/bitpayCard.js | 38 ++++++++----- src/js/controllers/confirm.js | 13 ++++- src/js/controllers/tab-home.js | 12 ++++- src/js/services/bitpayCardService.js | 80 ++++++++++++++++------------ www/views/bitpayCard.html | 24 ++++----- www/views/confirm.html | 7 +-- 6 files changed, 105 insertions(+), 69 deletions(-) diff --git a/src/js/controllers/bitpayCard.js b/src/js/controllers/bitpayCard.js index 80d15b697..ef5364b0b 100644 --- a/src/js/controllers/bitpayCard.js +++ b/src/js/controllers/bitpayCard.js @@ -3,7 +3,7 @@ angular.module('copayApp.controllers').controller('bitpayCardController', function($scope, $timeout, $log, $state, lodash, bitpayCardService, moment, popupService, gettextCatalog, $ionicHistory) { var self = this; - $scope.dateRange = 'last30Days'; + $scope.dateRange = { value: 'last30Days'}; $scope.network = bitpayCardService.getEnvironment(); var getFromCache = function(cb) { @@ -42,7 +42,7 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi }; this.update = function() { - var dateRange = setDateRange($scope.dateRange); + var dateRange = setDateRange($scope.dateRange.value); $scope.loadingHistory = true; bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) { @@ -53,39 +53,49 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi return; } - self.bitpayCardTransactionHistory = history.txs; + var txs = lodash.clone(history.txs); + for (var i = 0; i < txs.length; i++) { + txs[i] = _getMerchantInfo(txs[i]); + txs[i].icon = _getIconName(txs[i]); + txs[i].desc = _processDescription(txs[i]); + } + self.bitpayCardTransactionHistory = txs; self.bitpayCardCurrentBalance = history.currentCardBalance; - var cacheHistory = { - balance: self.bitpayCardCurrentBalance, - transactions: self.bitpayCardTransactionHistory - }; - bitpayCardService.setBitpayDebitCardsHistory($scope.cardId, cacheHistory, {}, function(err) { - if (err) $log.error(err); - $scope.historyCached = true; - }); + if ($scope.dateRange.value == 'last30Days') { + $log.debug('BitPay Card: store cache history'); + var cacheHistory = { + balance: history.currentCardBalance, + transactions: history.txs + }; + bitpayCardService.setBitpayDebitCardsHistory($scope.cardId, cacheHistory, {}, function(err) { + if (err) $log.error(err); + $scope.historyCached = true; + }); + } $timeout(function() { $scope.$apply(); }); }); }; - this.getMerchantInfo = function(tx) { + var _getMerchantInfo = function(tx) { var bpTranCodes = bitpayCardService.bpTranCodes; lodash.keys(bpTranCodes).forEach(function(code) { if (tx.type.indexOf(code) === 0) { lodash.assign(tx, bpTranCodes[code]); } }); + return tx; }; - this.getIconName = function(tx) { + var _getIconName = function(tx) { var icon = tx.mcc || tx.category || null; if (!icon) return 'default'; return bitpayCardService.iconMap[icon]; }; - this.processDescription = function(tx) { + var _processDescription = function(tx) { if (lodash.isArray(tx.description)) { return tx.description[0]; } diff --git a/src/js/controllers/confirm.js b/src/js/controllers/confirm.js index 5ee30b1b9..0b8949f3a 100644 --- a/src/js/controllers/confirm.js +++ b/src/js/controllers/confirm.js @@ -374,11 +374,22 @@ angular.module('copayApp.controllers').controller('confirmController', function( }; $scope.onSuccessConfirm = function() { + var previousView = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName; + var fromBitPayCard = previousView.match(/tabs.bitpayCard/) ? true : false; + $ionicHistory.nextViewOptions({ disableAnimate: true }); + $ionicHistory.removeBackView(); $scope.sendStatus = ''; - $state.go('tabs.send'); + + if (fromBitPayCard) { + $timeout(function() { + $state.transitionTo('tabs.bitpayCard', {id: $stateParams.cardId}); + }, 100); + } else { + $state.go('tabs.send'); + } }; function publishAndSign(wallet, txp, onSendStatusChange) { diff --git a/src/js/controllers/tab-home.js b/src/js/controllers/tab-home.js index 3e8f5cdc9..3d67a56f4 100644 --- a/src/js/controllers/tab-home.js +++ b/src/js/controllers/tab-home.js @@ -203,11 +203,19 @@ angular.module('copayApp.controllers').controller('tabHomeController', var bitpayCardCache = function() { bitpayCardService.getBitpayDebitCards(function(err, data) { - if (err || lodash.isEmpty(data)) return; + if (err) return; + if (lodash.isEmpty(data)) { + $scope.bitpayCards = null; + return; + } $scope.bitpayCards = data.cards; }); bitpayCardService.getBitpayDebitCardsHistory(null, function(err, data) { - if (err || lodash.isEmpty(data)) return; + if (err) return; + if (lodash.isEmpty(data)) { + $scope.cardsHistory = null; + return; + } $scope.cardsHistory = data; }); }; diff --git a/src/js/services/bitpayCardService.js b/src/js/services/bitpayCardService.js index b0f9c5882..884b57cbe 100644 --- a/src/js/services/bitpayCardService.js +++ b/src/js/services/bitpayCardService.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('bitpayCardService', function($http, $log, lodash, storageService, bitauthService, platformInfo) { +angular.module('copayApp.services').factory('bitpayCardService', function($http, $log, lodash, storageService, bitauthService, platformInfo, moment) { var root = {}; var BITPAY_CARD_NETWORK = 'livenet'; var BITPAY_CARD_API_URL = BITPAY_CARD_NETWORK == 'livenet' ? 'https://bitpay.com' : 'https://test.bitpay.com'; @@ -47,7 +47,6 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http, }; }; - var _post = function(endpoint, json, credentials) { var dataToSign = BITPAY_CARD_API_URL + endpoint + JSON.stringify(json); var signedData = bitauthService.sign(dataToSign, credentials.priv); @@ -64,26 +63,32 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http, }; }; - var _afterBitAuthSuccess = function(obj, credentials, cb) { - var json = { - method: 'getTokens' + var _postAuth = function(endpoint, json, credentials) { + json['params'].signature = bitauthService.sign(JSON.stringify(json.params), credentials.priv); + json['params'].pubkey = credentials.pub; + json['params'] = JSON.stringify(json.params); + + return { + method: 'POST', + url: BITPAY_CARD_API_URL + endpoint, + headers: { + 'content-type': 'application/json' + }, + data: json }; - // Get tokens - $http(_post('/api/v2/', json, credentials)).then(function(data) { - $log.info('BitPay Get Tokens: SUCCESS'); - var token = lodash.find(data.data.data, 'visaUser'); - if (lodash.isEmpty(token)) return cb(_setError('No token for visaUser')); - token = token.visaUser; - json['method'] = 'getDebitCards'; - // Get Debit Cards - $http(_post('/api/v2/' + token, json, credentials)).then(function(data) { - $log.info('BitPay Get Debit Cards: SUCCESS'); - return cb(data.data.error, {token: token, cards: data.data.data, email: obj.email}); - }, function(data) { - return cb(_setError('BitPay Card Error: Get Debit Cards', data)); - }); + }; + + var _afterBitAuthSuccess = function(token, obj, credentials, cb) { + var json = { + method: 'getDebitCards' + }; + // Get Debit Cards + $http(_post('/api/v2/' + token, json, credentials)).then(function(data) { + if (data && data.data.error) return cb(data.data.error); + $log.info('BitPay Get Debit Cards: SUCCESS'); + return cb(data.data.error, {token: token, cards: data.data.data, email: obj.email}); }, function(data) { - return cb(_setError('BitPay Card Error: Get Token', data)); + return cb(_setError('BitPay Card Error: Get Debit Cards', data)); }); }; @@ -96,16 +101,22 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http, matched = true; } } - if (!matched && ['paid', 'confirmed', 'complete'].indexOf(invoices[i].status) > -1) { + var isInvoiceLessThanOneDayOld = moment() < moment(new Date(invoices[i].invoiceTime)).add(1, 'day'); + if (!matched && isInvoiceLessThanOneDayOld) { + var isInvoiceUnderpaid = invoices[i].exceptionStatus === 'paidPartial'; - history.unshift({ - timestamp: invoices[i].invoiceTime, - description: invoices[i].itemDesc, - amount: invoices[i].price, - type: '00611 = Client Funded Deposit', - pending: true, - status: invoices[i].status - }); + if(['paid', 'confirmed', 'complete'].indexOf(invoices[i].status) >= 0 + || (invoices[i].status === 'invalid' || isInvoiceUnderpaid)) { + + history.unshift({ + timestamp: new Date(invoices[i].invoiceTime), + description: invoices[i].itemDesc, + amount: invoices[i].price, + type: '00611 = Client Funded Deposit', + pending: true, + status: invoices[i].status + }); + } } } return history; @@ -139,11 +150,12 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http, }; _getCredentials(function(err, credentials) { if (err) return cb(err); - $http(_post('/api/v2/', json, credentials)).then(function(data) { - $log.info('BitPay Card BitAuth: SUCCESS'); - _afterBitAuthSuccess(obj, credentials, cb); + $http(_postAuth('/api/v2/', json, credentials)).then(function(data) { + if (data && data.data.error) return cb(data.data.error); + $log.info('BitPay Card BitAuth Create Token: SUCCESS'); + _afterBitAuthSuccess(data.data.data, obj, credentials, cb); }, function(data) { - return cb(_setError('BitPay Card Error: BitAuth', data)); + return cb(_setError('BitPay Card Error Create Token: BitAuth', data)); }); }); }; @@ -164,7 +176,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http, // Get invoices $http(_post('/api/v2/' + card.token, json, credentials)).then(function(data) { $log.info('BitPay Get Invoices: SUCCESS'); - invoices = data.data.data; + invoices = data.data.data || []; if (lodash.isEmpty(invoices)) $log.info('No invoices'); json = { method: 'getTransactionHistory', diff --git a/www/views/bitpayCard.html b/www/views/bitpayCard.html index f0bff2c17..557b06869 100644 --- a/www/views/bitpayCard.html +++ b/www/views/bitpayCard.html @@ -21,7 +21,7 @@
${{bitpayCard.bitpayCardCurrentBalance}}
- {{'Add Funds'|translate}} + {{'Add Funds'|translate}}
@@ -45,20 +45,19 @@

Your BitPay Card is ready. Add funds to your card to start using your card at stores and ATMs worldwide.

-
+
-
-
- + ng-repeat="tx in bitpayCard.bitpayCardTransactionHistory | orderBy: ['pending','-timestamp']" + class="item row"> +
+
@@ -69,10 +68,8 @@ {{tx.merchant.city}}, {{tx.merchant.state}}
-
- {{desc}} +
+ {{tx.desc}}
@@ -85,7 +82,8 @@ 'text-gray': tx.amount.indexOf('-') == -1 && tx.pending}"> {{tx.amount | currency:'$':2 }}
- + + Pending
diff --git a/www/views/confirm.html b/www/views/confirm.html index 531a397b5..49c70ce85 100644 --- a/www/views/confirm.html +++ b/www/views/confirm.html @@ -25,11 +25,8 @@ - -
-
- {{toName}} - {{toAddress}} + {{toName}} + {{toAddress}}