Merge pull request #1406 from chjj/xhr_error_msg

paypro: fix xhr error messages. fix clearing $root.merchant on section switch. check for merchant_data.
This commit is contained in:
Matias Alejo Garcia 2014-09-15 21:03:31 -03:00
commit c665ccfb80
2 changed files with 49 additions and 38 deletions

View file

@ -465,6 +465,40 @@ angular.module('copayApp.controllers').controller('SendController',
$scope.loadTxs(); $scope.loadTxs();
}; };
$scope.clearMerchant = function(callback) {
var scope = $scope;
// TODO: Find a better way of detecting
// whether we're in the Send scope or not.
if (!scope.sendForm || !scope.sendForm.address) {
delete $rootScope.merchant;
if (callback) callback();
return;
}
var val = scope.sendForm.address.$viewValue || '';
var uri;
// If we're setting the domain, ignore the change.
if ($rootScope.merchant && $rootScope.merchant.domain && val === $rootScope.merchant.domain) {
uri = {
merchant: $rootScope.merchant.request_url
};
}
if (val.indexOf('bitcoin:') === 0) {
uri = new bitcore.BIP21(val).data;
} else if (/^https?:\/\//.test(val)) {
uri = {
merchant: val
};
}
if (!uri || !uri.merchant) {
delete $rootScope.merchant;
scope.sendForm.amount.$setViewValue('');
scope.sendForm.amount.$render();
if (callback) callback();
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
}
}
};
$scope.onChanged = function() { $scope.onChanged = function() {
var scope = $scope; var scope = $scope;
@ -552,31 +586,8 @@ angular.module('copayApp.controllers').controller('SendController',
// If the address changes to a non-payment-protocol one, // If the address changes to a non-payment-protocol one,
// delete the `merchant` property from the scope. // delete the `merchant` property from the scope.
var unregister = scope.$watch('address', function() { var unregister = $rootScope.$watch(function() {
var val = scope.sendForm.address.$viewValue || ''; $scope.clearMerchant(unregister);
var uri;
// If we're setting the domain, ignore the change.
if ($rootScope.merchant && $rootScope.merchant.domain && val === $rootScope.merchant.domain) {
uri = {
merchant: $rootScope.merchant.request_url
};
}
if (val.indexOf('bitcoin:') === 0) {
uri = new bitcore.BIP21(val).data;
} else if (/^https?:\/\//.test(val)) {
uri = {
merchant: val
};
}
if (!uri || !uri.merchant) {
delete $rootScope.merchant;
scope.sendForm.amount.$setViewValue('');
scope.sendForm.amount.$render();
unregister();
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {
$rootScope.$apply();
}
}
}); });
if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') { if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') {

View file

@ -1295,7 +1295,7 @@ Wallet.prototype.createPaymentTx = function(options, cb) {
return self.receivePaymentRequest(options, pr, cb); return self.receivePaymentRequest(options, pr, cb);
}) })
.error(function(data, status, headers, config) { .error(function(data, status, headers, config) {
return cb(new Error('Status: ' + JSON.stringify(status))); return cb(new Error('Status: ' + status));
}); });
}; };
@ -1408,7 +1408,10 @@ Wallet.prototype.receivePaymentRequest = function(options, pr, cb) {
expires: expires, expires: expires,
memo: memo || 'This server would like some BTC from you.', memo: memo || 'This server would like some BTC from you.',
payment_url: payment_url, payment_url: payment_url,
merchant_data: merchant_data.toString('hex') merchant_data: merchant_data
? merchant_data.toString('hex')
// : new Buffer('none', 'utf8').toString('hex')
: '00'
}, },
signature: sig.toString('hex'), signature: sig.toString('hex'),
ca: trust.caName, ca: trust.caName,
@ -1572,7 +1575,7 @@ Wallet.prototype.sendPaymentTx = function(ntxid, options, cb) {
return self.receivePaymentRequestACK(ntxid, tx, txp, ack, cb); return self.receivePaymentRequestACK(ntxid, tx, txp, ack, cb);
}) })
.error(function(data, status, headers, config) { .error(function(data, status, headers, config) {
return cb(new Error('Status: ' + JSON.stringify(status))); return cb(new Error('Status: ' + status));
}); });
}; };
@ -2483,15 +2486,6 @@ Wallet.prototype.verifySignedJson = function(senderId, payload, signature) {
return v; return v;
} }
// NOTE: Angular $http module does not send ArrayBuffers correctly, so we're
// not going to use it. We'll have to write our own. Otherwise, we could
// hex-encoded our messages and decode them on the other side, but that
// deviates from BIP-70.
// if (typeof angular !== 'undefined') {
// var $http = angular.bootstrap().get('$http');
// }
/** /**
* @desc Create a HTTP request * @desc Create a HTTP request
* @TODO: This shouldn't be a wallet responsibility * @TODO: This shouldn't be a wallet responsibility
@ -2557,7 +2551,13 @@ Wallet.request = function(options, callback) {
}; };
xhr.onerror = function(event) { xhr.onerror = function(event) {
return ret._error(null, new Error(event.message), null, options); var status;
if (xhr.status === 0 || !xhr.statusText) {
status = 'HTTP Request Error: This endpoint likely does not support cross-origin requests.';
} else {
status = xhr.statusText;
}
return ret._error(null, status, null, options);
}; };
if (req.body) { if (req.body) {