From 5f6c9482b464ad9a557dc474666b5c06fc3bce7a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 9 Dec 2014 01:29:06 -0300 Subject: [PATCH] paypro working 1-1 --- TODO | 2 + css/src/main.css | 9 ++-- js/controllers/send.js | 79 ++++++++++++++++++++------------- js/models/Wallet.js | 36 ++++++++++----- views/modals/paypro.html | 19 ++++---- views/send.html | 96 ++++++++++++++++------------------------ 6 files changed, 127 insertions(+), 114 deletions(-) diff --git a/TODO b/TODO index 2d468a2e6..809f8701e 100644 --- a/TODO +++ b/TODO @@ -1,2 +1,4 @@ - join. on walletComplete! - paypro fetch-> return TIMEOUT +- yellow lock on paypro +- attack: change paypro ->no! in is cached diff --git a/css/src/main.css b/css/src/main.css index 1c76dbe45..2f476cf86 100644 --- a/css/src/main.css +++ b/css/src/main.css @@ -99,15 +99,14 @@ header .alt-currency { font-weight: 700; } -.green { - color: #1abc9c; +.color-greeni { + color: #1abc9c !important; } -.red { - color: #A02F23; +.color-yellowi { + color: yellow !important; } - .alt-currency.green { background: #1abc9c; } diff --git a/js/controllers/send.js b/js/controllers/send.js index e3052dd50..b2cb0e421 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -119,38 +119,44 @@ angular.module('copayApp.controllers').controller('SendController', if (msg.match('totalNeededAmount') || msg.match('unspent not set')) msg = 'Insufficient funds' + if (msg.match('expired')) + msg = 'The payment request has expired'; + var message = 'The transaction' + (w.isShared() ? ' proposal' : '') + ' could not be created: ' + msg; $scope.error = message; - $timeout(function(){ + $timeout(function() { $scope.$digest(); - },1); + }, 1); }; $scope.submitForm = function(form) { + if (form.$invalid) { $scope.error = 'Unable to send transaction proposal'; return; } $scope.loading = true; - - var url = $scope._url; - var address = form.address.$modelValue; - var amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0)); var comment = form.comment.$modelValue; + var merchantData = $scope._merchantData; + var address, amount; + if (!merchantData) { + address = form.address.$modelValue; + amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0)); + } w.spend({ + merchantData: merchantData, toAddress: address, amountSat: amount, comment: comment, - url: url, }, function(err, txid, status) { $scope.loading = false; - if (err) + if (err) return $scope.setError(err); $scope.resetForm(status); @@ -331,13 +337,15 @@ angular.module('copayApp.controllers').controller('SendController', $scope.setForm = function(to, amount, comment) { var form = $scope.sendForm; - form.address.$setViewValue(to); - form.address.$isValid = true; - form.address.$render(); - $scope.lockAddress = true; + if (to) { + form.address.$setViewValue(to); + form.address.$isValid = true; + form.address.$render(); + $scope.lockAddress = true; + } if (amount) { - form.amount.$setViewValue(""+amount); + form.amount.$setViewValue("" + amount); form.amount.$isValid = true; form.amount.$render(); $scope.lockAmount = true; @@ -353,28 +361,42 @@ angular.module('copayApp.controllers').controller('SendController', $scope.resetForm = function(status) { var form = $scope.sendForm; - form.address.$pristine = form.amount.$pristine = true; $scope.fetchingURL = null; + $scope._merchantData = $scope._domain = null; + $scope.lockAddress = false; $scope.lockAmount = false; - form.address.$setViewValue(''); - form.address.$render(); + + $scope._amount = $scope._address = null; + + form.amount.$pristine = true; form.amount.$setViewValue(''); form.amount.$render(); + form.comment.$setViewValue(''); form.comment.$render(); form.$setPristine(); $scope.notifyStatus(status); - $timeout(function(){ + $timeout(function() { + if (form.address) { + form.address.$pristine = true; + form.address.$setViewValue(''); + form.address.$render(); + } + $rootScope.$digest(); - },1); + }, 1); }; - - $scope.openPPModal = function(pp) { + var $oscope = $scope; + $scope.openPPModal = function(merchantData) { var ModalInstanceCtrl = function($scope, $modalInstance) { - $scope.pp = pp; + $scope.md = merchantData; + $scope.alternative = $oscope._alternative; + $scope.alternativeIsoCode = $oscope.alternativeIsoCode; + $scope.isRateAvailable = $oscope.isRateAvailable; + $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; @@ -393,9 +415,6 @@ angular.module('copayApp.controllers').controller('SendController', $scope.fetchingURL = uri; $scope.loading = true; - var balance = w.balanceInfo.availableBalance; - var available = +(balance * unitToSat).toFixed(0); - // Payment Protocol URI (BIP-72) w.fetchPaymentRequest({ url: uri @@ -404,15 +423,16 @@ angular.module('copayApp.controllers').controller('SendController', $scope.fetchingURL = null; if (err) { - if (err.match('TIMEOUT')) { + copay.logger.warn(err); + if (err.toString().match('TIMEOUT')) { $scope.resetForm('Payment server timed out'); } else { $scope.resetForm(err.toString()); } - } else if (merchantData && available < +merchantData.total) { - $scope.resetForm('Insufficient funds'); } else { - $scope.setForm(merchantData.domain, merchantData.unitTotal) + $scope._merchantData = merchantData; + $scope._domain = merchantData.domain; + $scope.setForm(null, merchantData.unitTotal); } }); }; @@ -428,9 +448,6 @@ angular.module('copayApp.controllers').controller('SendController', }; var addr = parsed.address.toString(); - -console.log('[send.js.430:parsed:]',addr,parsed.data); //TODO - if (parsed.data.merchant) return $scope.setFromPayPro(parsed.data.merchant); diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 43993fa27..2855a9812 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -1747,9 +1747,11 @@ Wallet.prototype._addOutputsToMerchantData = function(merchantData) { // If user is granted the privilege of choosing // their own amount, add it to the tx. - if (merchantData.total === 0 && options.amount) { - merchant.outs[0].amountSatStr = merchantData.total = outions.amount; + if (merchantData.total == "0" && options.amount) { + merchant.outs[0].amountSatStr = merchantData.total = options.amount; } + + merchantData.unitTotal = merchantData.total ? (+merchantData.total / this.settings.unitToSatoshi) + '' : 0; }; /** @@ -1829,11 +1831,11 @@ Wallet.prototype.parsePaymentRequest = function(options, rawData) { request_url: options.url, domain: /^(?:https?)?:\/\/([^\/:]+).*$/.exec(options.url)[1], total: total, - unitTotal: total ? (+total / w.settings.unitToSatoshi) + '' : null, expirationDate: expires ? new Date(expires * 1000) : null, }; this._addOutputsToMerchantData(merchantData, options.amount); + return merchantData; }; @@ -2178,9 +2180,21 @@ Wallet.prototype.spend = function(opts, cb) { var toAddress = opts.toAddress; var amountSat = opts.amountSat; var comment = opts.comment; - var url = opts.url; + var merchantData = opts.merchantData; + + + // PayPro? With given merchant data + if (opts.merchantData && !opts.toAddress) { + if (!merchantData.outs[0].address) + return cb(new Error('BADPAYPRO')); + + opts.toAddress = merchantData.outs[0].address; + opts.amountSat = parseInt(merchantData.outs[0].amountSatStr); + return self.spend(opts, cb); + } // PayPro? Fetch payment data and recurse + var url = opts.url; if (url && !opts.merchantData) { return self.fetchPaymentRequest({ url: url, @@ -2189,8 +2203,6 @@ Wallet.prototype.spend = function(opts, cb) { }, function(err, merchantData) { if (err) return cb(err); opts.merchantData = merchantData; - opts.toAddress = merchantData.outs[0].address; - opts.amountSat = parseInt(merchantData.outs[0].amountSatStr); return self.spend(opts, cb); }); } @@ -2208,13 +2220,13 @@ Wallet.prototype.spend = function(opts, cb) { try { txp = self._createTxProposal(toAddress, amountSat, comment, safeUnspent, opts.builderOpts); - } catch (e) { - log.error(e); - return cb(e); - } - if (opts.merchantData) { - txp.addMerchantData(opts.merchantData); + if (opts.merchantData) { + txp.addMerchantData(opts.merchantData); + } + } catch (e) { + log.warn(e); + return cb(e); } var ntxid = self.txProposals.add(txp); diff --git a/views/modals/paypro.html b/views/modals/paypro.html index 399a6ab16..5d8fdff56 100644 --- a/views/modals/paypro.html +++ b/views/modals/paypro.html @@ -1,25 +1,26 @@ ×
-

Payment Protocol Request

+

Payment Request

- Signature: - {{$root.merchant.pr.ca}} - Untrusted + Signature: {{md.domain}} + {{md.pr.ca}} + Untrusted

Merchant Message: - {{$root.merchant.pr.pd.memo || address}} +
+ {{md.pr.pd.memo || address}}

- Merchant Message: - {{amount}} {{$root.wallet.settings.unitName}} + Amount: + {{md.unitTotal}} {{$root.wallet.settings.unitName}} {{ alternative }} {{ alternativeIsoCode }} -

- Expires {{$root.merchant.expiration | amTimeAgo }} [{{$root.merchant.domain}}] +

+ Expires {{md.expiration | amTimeAgo }}

diff --git a/views/send.html b/views/send.html index 16f116f72..fefad6bd1 100644 --- a/views/send.html +++ b/views/send.html @@ -29,47 +29,52 @@ -
-
+
+
- + - + - Not valid - - + Not valid + + - -
- -
- - -
- +
-
- + +
+ + +
+ +
+
+ +
+
+
-
-
- {{_url}} -
-
+
+
-
-
-
- + +
+ + +
+
+
@@ -118,34 +123,11 @@
-
-
-

+
+
+

>> - Fetching payment -

-

From {{fetchingURL}} -

- -
-

Payment Protocol Request

-
-

- {{$root.merchant.pr.ca}} - Untrusted - {{$root.merchant.pr.pd.memo || address}} TODO ADDRESS -

- -

TODO AMOUNT - {{amount}} {{$root.wallet.settings.unitName}} - - TODO ALT - {{ alternative }} {{ alternativeIsoCode }} - -

- Expires {{$root.merchant.expiration | amTimeAgo }} [{{$root.merchant.domain}}] -

-
+ Fetching payment request
@@ -164,10 +146,10 @@