diff --git a/js/controllers/send.js b/js/controllers/send.js index 683b6c71c..d12868d40 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -465,6 +465,40 @@ angular.module('copayApp.controllers').controller('SendController', $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() { var scope = $scope; @@ -552,31 +586,8 @@ angular.module('copayApp.controllers').controller('SendController', // If the address changes to a non-payment-protocol one, // delete the `merchant` property from the scope. - var unregister = scope.$watch('address', function() { - 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(); - unregister(); - if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') { - $rootScope.$apply(); - } - } + var unregister = $rootScope.$watch(function() { + $scope.clearMerchant(unregister); }); if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') { diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 61e81b137..63ffebafa 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -1295,7 +1295,7 @@ Wallet.prototype.createPaymentTx = function(options, cb) { return self.receivePaymentRequest(options, pr, cb); }) .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, memo: memo || 'This server would like some BTC from you.', 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'), ca: trust.caName, @@ -1572,7 +1575,7 @@ Wallet.prototype.sendPaymentTx = function(ntxid, options, cb) { return self.receivePaymentRequestACK(ntxid, tx, txp, ack, cb); }) .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; } -// 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 * @TODO: This shouldn't be a wallet responsibility @@ -2557,7 +2551,13 @@ Wallet.request = function(options, callback) { }; 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) {