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

@ -1,5 +1,5 @@
'use strict';
angular.module('copayApp.controllers').controller('bitpayCardIntroController', function($scope, $log, $state, $ionicHistory, storageService, externalLinkService, bitpayCardService, gettextCatalog, popupService, appIdentityService, bitpayService, lodash) {
angular.module('copayApp.controllers').controller('bitpayCardIntroController', function($scope, $log, $state, $ionicHistory, storageService, externalLinkService, bitpayCardService, gettextCatalog, popupService, bitpayAccountService) {
$scope.$on("$ionicView.beforeEnter", function(event, data) {
if (data.stateParams && data.stateParams.secret) {
@ -8,11 +8,8 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
email: data.stateParams.email,
otp: data.stateParams.otp
};
var pairingReason = gettextCatalog.getString('BitPay Visa card');
bitpayService.pair(pairData, pairingReason, function(err, paired, apiContext) {
var pairingReason = gettextCatalog.getString('add your BitPay Visa card(s)');
bitpayAccountService.pair(pairData, pairingReason, function(err, paired, apiContext) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error pairing Bitpay Account'), err);
return;
@ -25,26 +22,28 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
}
// Set flag for nextStep
storageService.setNextStep('BitpayCard', 'true', function(err) {});
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$state.go('tabs.home').then(function() {
if (cards[0]) {
if (data.cards[0]) {
$state.transitionTo('tabs.bitpayCard', {
id: cards[0].id
id: data.cards[0].id
});
}
});
});
}
});
} else {
appIdentityService.getIdentity(bitpayService.getEnvironment().network, function(err, appIdentity) {
if (err) popupService.showAlert(null, err);
else $log.info('App identity: OK');
});
}
bitpayAccountService.getAccounts(function(err, accounts) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err);
return;
}
$scope.accounts = accounts;
});
});
$scope.bitPayCardInfo = function() {
@ -58,7 +57,37 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f
};
$scope.connectBitPayCard = function() {
var url = 'https://bitpay.com/visa/dashboard/add-to-bitpay-wallet-confirm';
externalLinkService.open(url);
if ($scope.accounts.length == 0) {
startPairBitPayAccount();
} else {
showAccountSelector();
}
};
var startPairBitPayAccount = function() {
var url = 'https://bitpay.com/visa/dashboard/add-to-bitpay-wallet-confirm';
externalLinkService.open(url);
};
var showAccountSelector = function() {
$scope.accountSelectorTitle = gettextCatalog.getString('From BitPay account');
$scope.showAccounts = ($scope.accounts != undefined);
};
$scope.onAccountSelect = function(account) {
if (account == undefined) {
startPairBitPayAccount();
} else {
bitpayCardService.fetchBitpayDebitCards(account.apiContext, function(err, data) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err);
return;
}
storageService.setNextStep('BitpayCard', 'true', function(err) {
$state.go('tabs.home');
});
});
}
};
});

View file

@ -0,0 +1,75 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesBitpayServicesController',
function($rootScope, $scope, $state, $timeout, $ionicHistory, bitpayAccountService, bitpayCardService, popupService, gettextCatalog) {
$scope.removeAccount = function(account) {
var title = gettextCatalog.getString('Remove BitPay Account?');
var msg = gettextCatalog.getString('Removing your BitPay account will remove all associated BitPay account data from this device.<br/><br/>Are you sure you would like to remove your BitPay Account ({{email}}) from this device?', {
email: account.email
});
popupService.showConfirm(title, msg, null, null, function(res) {
if (res) {
removeAccount(account);
}
});
};
$scope.removeCard = function(card) {
var title = gettextCatalog.getString('Remove BitPay Card?');
var msg = gettextCatalog.getString('Are you sure you would like to remove your BitPay Card ({{lastFourDigits}}) from this device?', {
lastFourDigits: card.lastFourDigits
});
popupService.showConfirm(title, msg, null, null, function(res) {
if (res) {
removeCard(card);
}
});
};
var removeAccount = function(account) {
bitpayAccountService.removeAccount(account, function(err) {
if (err) {
return popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not remove account'));
}
setScope(function() {
// If there are no paired accounts then change views.
if ($scope.bitpayAccounts.length == 0) {
$state.go('tabs.settings').then(function() {
$ionicHistory.clearHistory();
$state.go('tabs.home');
});
}
});
});
};
var removeCard = function(card) {
bitpayCardService.removeCard(card, function(err) {
if (err) {
return popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not remove card'));
}
setScope();
});
};
var setScope = function(cb) {
bitpayAccountService.getAccounts(function(err, data) {
if (err) return;
$scope.bitpayAccounts = data;
bitpayCardService.getBitpayDebitCards(function(err, data) {
if (err) return;
$scope.bitpayCards = data;
if (cb) {
cb();
}
});
});
};
$scope.$on("$ionicView.beforeEnter", function(event, data) {
setScope();
});
});

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('tabSettingsController', function($scope, appConfigService, $log, lodash, uxLanguage, platformInfo, profileService, feeService, configService, externalLinkService, bitpayCardService, storageService, glideraService, coinbaseService, gettextCatalog, buyAndSellService) {
angular.module('copayApp.controllers').controller('tabSettingsController', function($scope, appConfigService, $ionicModal, $log, lodash, uxLanguage, platformInfo, profileService, feeService, configService, externalLinkService, bitpayAccountService, bitpayCardService, storageService, glideraService, gettextCatalog) {
var updateConfig = function() {
var isCordova = platformInfo.isCordova;
@ -25,12 +25,27 @@ angular.module('copayApp.controllers').controller('tabSettingsController', funct
isoCode: config.wallet.settings.alternativeIsoCode
};
// TODO move this to a generic service
bitpayCardService.getCards(function(err, cards) {
$scope.bitpayCardEnabled = config.bitpayCard.enabled;
$scope.glideraEnabled = config.glidera.enabled && !isWindowsPhoneApp;
bitpayAccountService.getAccounts(function(err, data) {
if (err) $log.error(err);
$scope.bitpayCards = cards && cards.length > 0;
$scope.bitpayAccounts = !lodash.isEmpty(data);
});
if ($scope.bitpayCardEnabled) {
bitpayCardService.getBitpayDebitCards(function(err, cards) {
if (err) $log.error(err);
$scope.bitpayCards = cards && cards.length > 0;
});
}
if ($scope.glideraEnabled) {
storageService.getGlideraToken(glideraService.getEnvironment(), function(err, token) {
if (err) $log.error(err);
$scope.glideraToken = token;
});
}
});
};