Skip to content

Commit

Permalink
Merge pull request #5794 from cmgustavo/bug/bitpay-card-top-up
Browse files Browse the repository at this point in the history
Show wallets with sufficient funds in order to top-up the bitpay card…
  • Loading branch information
matiu authored Mar 20, 2017
2 parents 4c0da8c + 7c14c61 commit 31f8a01
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
30 changes: 18 additions & 12 deletions src/js/controllers/topup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

angular.module('copayApp.controllers').controller('topUpController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, bitpayService, bitpayCardService, payproService) {
angular.module('copayApp.controllers').controller('topUpController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, bitpayService, bitpayCardService, payproService, bwcError) {

var amount;
var currency;
Expand All @@ -17,11 +17,11 @@ angular.module('copayApp.controllers').controller('topUpController', function($s
});
};

var showError = function(err) {
var showError = function(title, msg) {
$scope.sendStatus = '';
$log.error(err);
err = err.errors ? err.errors[0].message : err;
popupService.showAlert('Error', err);
$log.error(msg);
msg = msg.errors ? msg.errors[0].message : msg;
popupService.showAlert(title, msg);
};

var publishAndSign = function (wallet, txp, onSendStatusChange, cb) {
Expand Down Expand Up @@ -68,11 +68,17 @@ angular.module('copayApp.controllers').controller('topUpController', function($s

$scope.network = bitpayService.getEnvironment().network;
$scope.wallets = profileService.getWallets({
m: 1, // Only 1-signature wallet
onlyComplete: true,
network: $scope.network
network: $scope.network,
hasFunds: true,
minAmount: parsedAmount.amountSat
});
$scope.wallet = $scope.wallets[0]; // Default first wallet

if (lodash.isEmpty($scope.wallets)) {
showErrorAndBack('Insufficient funds');
return;
}
$scope.onWalletSelect($scope.wallets[0]); // Default first wallet

bitpayCardService.getRates(currency, function(err, data) {
if (err) $log.error(err);
Expand Down Expand Up @@ -109,29 +115,29 @@ angular.module('copayApp.controllers').controller('topUpController', function($s
bitpayCardService.topUp(cardId, dataSrc, function(err, invoiceId) {
if (err) {
ongoingProcess.set('topup', false, statusChangeHandler);
showError(err);
showError('Could not create the invoice', err);
return;
}

bitpayCardService.getInvoice(invoiceId, function(err, invoice) {
if (err) {
ongoingProcess.set('topup', false, statusChangeHandler);
showError(err);
showError('Could not get the invoice', err);
return;
}

var payProUrl = (invoice && invoice.paymentUrls) ? invoice.paymentUrls.BIP73 : null;

if (!payProUrl) {
ongoingProcess.set('topup', false, statusChangeHandler);
showError('Error fetching invoice');
showError('Error in Payment Protocol', 'Invalid URL');
return;
}

payproService.getPayProDetails(payProUrl, function(err, payProDetails) {
if (err) {
ongoingProcess.set('topup', false, statusChangeHandler);
showError(err);
showError('Error fetching invoice', err);
return;
}

Expand Down
7 changes: 5 additions & 2 deletions src/js/services/bitpayCardService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

angular.module('copayApp.services').factory('bitpayCardService', function($log, $rootScope, $filter, lodash, storageService, bitauthService, platformInfo, moment, appIdentityService, bitpayService, nextStepsService, configService, txFormatService, appConfigService) {
angular.module('copayApp.services').factory('bitpayCardService', function($log, $rootScope, $filter, lodash, storageService, bitauthService, platformInfo, moment, appIdentityService, bitpayService, nextStepsService, configService, txFormatService, appConfigService, rateService) {
var root = {};

var _setError = function(msg, e) {
Expand Down Expand Up @@ -44,12 +44,14 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
var satToBtc = 1 / 100000000;
var unitToSatoshi = config.unitToSatoshi;
var amountUnitStr;
var amountSat;

// IF 'USD'
if (currency) {
amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency;
amountSat = rateService.fromFiat(amount, currency).toFixed(0);
} else {
var amountSat = parseInt((amount * unitToSatoshi).toFixed(0));
amountSat = parseInt((amount * unitToSatoshi).toFixed(0));
amountUnitStr = txFormatService.formatAmountStr(amountSat);
// convert unit to BTC
amount = (amountSat * satToBtc).toFixed(8);
Expand All @@ -59,6 +61,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
return {
amount: amount,
currency: currency,
amountSat: amountSat,
amountUnitStr: amountUnitStr
};
};
Expand Down
12 changes: 12 additions & 0 deletions src/js/services/profileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,18 @@ angular.module('copayApp.services')
});
}

if (opts.hasFunds) {
ret = lodash.filter(ret, function(w) {
return (w.status.availableBalanceSat > 0);
});
}

if (opts.minAmount) {
ret = lodash.filter(ret, function(w) {
return (w.status.availableBalanceSat > opts.minAmount);
});
}

if (opts.onlyComplete) {
ret = lodash.filter(ret, function(w) {
return w.isComplete();
Expand Down
10 changes: 6 additions & 4 deletions www/views/topup.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
ng-if="!isCordova && cardInfo"
click-send-status="sendStatus"
has-wallet-chosen="wallet"
insufficient-funds="false"
insufficient-funds="insufficientFunds"
no-matching-wallet="!cardInfo">
Add funds
</click-to-accept>
Expand All @@ -73,17 +73,19 @@
slide-on-confirm="topUpConfirm()"
slide-send-status="sendStatus"
has-wallet-chosen="wallet"
insufficient-funds="false"
insufficient-funds="insufficientFunds"
no-matching-wallet="!cardInfo">
Slide to confirm
</slide-to-accept>
<slide-to-accept-success
slide-success-show="sendStatus === 'success'"
slide-success-on-confirm="goBackHome()"
slide-success-hide-on-confirm="true">
<span>Sent</span>
<span ng-if="wallet.credentials.m == 1">Sent</span>
<span ng-if="wallet.credentials.m > 1">Success</span>
<div class="m10 size-14">
Funds were added to debit card
<span ng-if="wallet.credentials.m == 1">Funds were added to debit card</span>
<span ng-if="wallet.credentials.m > 1">Transaction initiated</span>
</div>
</slide-to-accept-success>

Expand Down

0 comments on commit 31f8a01

Please sign in to comment.