Fix Paypro URI on address input and Payment intent.

This commit is contained in:
Gustavo Maximiliano Cortez 2014-12-01 19:01:10 -03:00
parent 68f21f3c4d
commit d8edca66fb
2 changed files with 83 additions and 39 deletions

View File

@ -43,7 +43,6 @@ angular.module('copayApp.controllers').controller('SendController',
$scope.updateTxs = _.throttle(function() {
console.log('[send.js.44:updateTxs:]'); //TODO
var w = $rootScope.wallet;
if (!w) return;
@ -104,6 +103,18 @@ console.log('[send.js.44:updateTxs:]'); //TODO
enumerable: true,
configurable: true
});
Object.defineProperty($scope,
"address", {
get: function() {
return this._address;
},
set: function(newValue) {
this._address = newValue;
_onChanged();
},
enumerable: true,
configurable: true
});
$scope.init = function() {
@ -122,22 +133,6 @@ console.log('[send.js.44:updateTxs:]'); //TODO
return w && _.keys(w.addressBook).length > 0;
};
if ($rootScope.pendingPayment) {
var pp = $rootScope.pendingPayment;
var amount = pp.data.amount * 100000000 * satToUnit;
var alternativeAmountPayPro = rateService.toFiat((amount + $scope.defaultFee) * w.settings.unitToSatoshi, $scope.alternativeIsoCode);
if (pp.data.merchant) {
$scope.address = 'bitcoin:' + pp.address.data + '?amount=' + amount + '&r=' + pp.data.r;
}
else {
$scope.address = pp.address + '';
$scope.amount = amount;
$scope.alternative = alternativeAmountPayPro;
}
$scope.alternativeAmountPayPro = $filter('noFractionNumber')(alternativeAmountPayPro, 2);
$scope.commentText = pp.data.message;
}
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
@ -201,6 +196,7 @@ console.log('[send.js.44:updateTxs:]'); //TODO
$scope.address = $scope.amount = $scope.commentText = null;
form.address.$pristine = form.amount.$pristine = true;
$rootScope.pendingPayment = null;
$scope.isPayUri = null;
if (err) return $scope._showError(err);
$scope.notifyStatus(status);
@ -451,6 +447,7 @@ console.log('[send.js.44:updateTxs:]'); //TODO
if (!$scope.sendForm || !$scope.sendForm.address) {
delete $rootScope.merchant;
$rootScope.merchantError = false;
$scope.isPayUri = false;
if (callback) callback();
return;
}
@ -483,15 +480,36 @@ console.log('[send.js.44:updateTxs:]'); //TODO
$scope.cancelSend = function(form) {
delete $rootScope.merchant;
$rootScope.merchantError = false;
$scope.isPayUri = false;
form.address.$setViewValue('');
form.address.$render();
form.amount.$setViewValue('');
form.amount.$render();
form.comment.$setViewValue('');
form.comment.$render();
form.$setPristine();
};
};
$scope.onChanged = function() {
var value = $scope.address || '';
var _onChanged = function(pp) {
var value;
if (pp) {
$scope.isPayUri = true;
var amount = (pp.data && pp.data.amount) ? pp.data.amount * 100000000 * satToUnit : 0;
$scope.commentText = pp.data.message;
if (pp.data.merchant) {
value = 'bitcoin:' + pp.address.data + '?amount=' + amount + '&r=' + pp.data.r;
}
else {
value = pp.address + '';
$timeout(function() {
$scope.amount = amount;
}, 1000);
$scope.address = value;
}
}
value = value || $scope.address || '';
var uri;
$scope.error = $scope.success = null;
@ -501,14 +519,28 @@ console.log('[send.js.44:updateTxs:]'); //TODO
}
if (value.indexOf('bitcoin:') === 0) {
uri = new bitcore.BIP21(value).data;
uri = new bitcore.BIP21(value);
} else if (/^https?:\/\//.test(value)) {
uri = {
merchant: value
data : {
merchant: value
}
};
}
if (!uri || !uri.merchant) {
if (!uri || !uri.data.merchant) {
if (uri && uri.address) {
var amount = (uri.data && uri.data.amount) ? uri.data.amount * 100000000 * satToUnit : 0;
var address = uri.address.data;
if (amount && address) {
$scope.isPayUri = true;
}
$timeout(function() {
$scope.amount = amount;
}, 1000);
$scope.commentText = uri.data.message;
$scope.address = address;
}
return;
}
@ -518,7 +550,7 @@ console.log('[send.js.44:updateTxs:]'); //TODO
}
};
$scope.fetchingURL = uri.merchant;
$scope.fetchingURL = uri.data.merchant;
$scope.loading = true;
apply();
@ -535,7 +567,7 @@ console.log('[send.js.44:updateTxs:]'); //TODO
// Payment Protocol URI (BIP-72)
$scope.wallet.fetchPaymentRequest({
url: uri.merchant
url: uri.data.merchant
}, function(err, merchantData) {
if (!timeout) return;
clearTimeout(timeout);
@ -562,6 +594,7 @@ console.log('[send.js.44:updateTxs:]'); //TODO
var unregister = $scope.$watch('address', function() {
if ($scope.sendForm.address.$viewValue !== lastAddr) {
delete $rootScope.merchantError;
$scope.isPayUri = false;
$scope.sendForm.amount.$setViewValue('');
$scope.sendForm.amount.$render();
unregister();
@ -608,4 +641,11 @@ console.log('[send.js.44:updateTxs:]'); //TODO
apply();
});
};
if ($rootScope.pendingPayment) {
var value;
var pp = $rootScope.pendingPayment;
_onChanged(pp);
}
});

View File

@ -39,7 +39,7 @@
tooltip="{{'Enter a valid Bitcoin address. Payment Protocol URLs are also supported'|translate}}"
tooltip-trigger="mouseenter"
tooltip-placement="right"></i>
<small translate ng-hide="!sendForm.address.$pristine || address">required</small>
<small translate ng-hide="!sendForm.address.$pristine && address">required</small>
</label>
<span translate class="has-error right size-12" ng-show="sendForm.address.$invalid && address">
<span class="icon-input"><i class="fi-x"></i></span>
@ -49,8 +49,8 @@
</div>
<div class="input">
<input type="text" id="address" name="address" ng-disabled="loading || !!$root.merchant"
placeholder="{{'Bitcoin address'|translate}}" ng-model="address" ng-change="onChanged()" valid-address required>
<input type="text" id="address" name="address" ng-disabled="loading || !!$root.merchant || isPayUri"
placeholder="{{'Bitcoin address'|translate}}" ng-model="address" valid-address required>
<i class="fi-address-book"></i>
<div ng-hide="showScanner || disableScanner">
<a class="postfix button black" ng-click="openScanner()"><i class="fi-camera size-24"></i></a>
@ -71,9 +71,10 @@
<div class="large-6 medium-6 columns">
<div class="row collapse">
<label for="amount" class="small-7 columns m5b"><span translate>Amount</span>
<small translate ng-hide="!sendForm.amount.$pristine">required</small>
<small translate ng-hide="!sendForm.amount.$pristine && amount">required</small>
</label>
<span translate class="has-error right size-12" ng-show="(sendForm.amount.$invalid || notValidAmount) && !sendForm.amount.$pristine">
<span translate class="has-error right size-12" ng-show="sendForm.amount.$invalid &&
!sendForm.amount.$pristine && (notValidAmount || amount)">
<span class="icon-input"><i class="fi-x"></i></span>
Not valid
</span>
@ -84,7 +85,7 @@
<div class="small-9 columns">
<div class="input">
<input type="number" id="amount"
ng-disabled="loading || ($root.merchant && +$root.merchant.total > 0)"
ng-disabled="loading || ($root.merchant && +$root.merchant.total > 0) || isPayUri"
name="amount" placeholder="{{'Amount'|translate}}" ng-model="amount"
min="0.00000001" max="10000000000" valid-amount required
autocomplete="off">
@ -106,8 +107,9 @@
<label for="alternative"><span translate>Amount in</span> {{ alternativeName }} </label>
<div class="small-9 columns">
<div class="input">
<input type="number" id="alternative_amount"
ng-disabled="loading || !isRateAvailable || ($root.merchant && +$root.merchant.total > 0)"
<input type="number" id="alternative"
ng-disabled="loading || !isRateAvailable || ($root.merchant && +$root.merchant.total > 0) ||
isPayUri"
name="alternative" placeholder="{{'Amount'|translate}}" ng-model="alternative" requiredautocomplete="off">
<i class="icon-usd"></i>
</div>
@ -128,28 +130,30 @@
<p> From {{fetchingURL}}
</div>
<div class="large-12 columns" ng-show="!!$root.merchant">
<h3>This is a payment protocol transaction</h3>
<div class="large-12 columns" ng-show="!!$root.merchant || isPayUri">
<h3 ng-show="!isPayUri">This is a payment protocol transaction</h3>
<div class="send-note">
<p>
<b>{{$root.merchant.pr.pd.memo}}</b>
<span ng-show="isPayUri">Payment to: </span>
<b>{{$root.merchant.pr.pd.memo || address}}</b>
</p>
<p>
<i>{{amount + defaultFee}} {{$root.wallet.settings.unitName}}</i>
<span class="text-gray" ng-if="isRateAvailable">
{{ alternativeAmountPayPro }} {{ alternativeIsoCode }}
{{ alternative }} {{ alternativeIsoCode }}
</span>
<span class="text-gray" >
(<span translate>Including fee of</span>
{{defaultFee}}
{{$root.wallet.settings.unitName}})
</span>
<p>
<p ng-show="!!$root.merchant">
Expires {{$root.merchant.expiration | amTimeAgo }}
[{{$root.merchant.domain}}]
<span ng-show="!!$root.merchant.pr.ca"><i class="fi-lock"></i> {{$root.merchant.pr.ca}}</span>
<span ng-show="!$root.merchant.pr.ca" style="color:red;weight:bold;"><i class="fi-unlock"></i> Untrusted</span>
</p>
</div>
</div>
</div>
@ -170,7 +174,7 @@
<div class="row">
<div class="large-6 medium-6 small-6 columns text-left">
<a ng-click="cancelSend(sendForm)" class="button warning m0" ng-show="!!$root.merchant">
<a ng-click="cancelSend(sendForm)" class="button warning m0" ng-show="!!$root.merchant || isPayUri">
Cancel
</a>
</div>