paypro: messy work to get xhr to payment server working.

This commit is contained in:
Christopher Jeffrey 2014-07-30 16:53:50 -07:00 committed by Manuel Araoz
parent a7f176890f
commit 7c39915dd0
3 changed files with 119 additions and 1 deletions

View File

@ -1,8 +1,9 @@
'use strict'; 'use strict';
angular.module('copayApp.directives') angular.module('copayApp.directives')
//.directive('validAddress', ['$rootScope',
//function($rootScope) {
.directive('validAddress', [ .directive('validAddress', [
function() { function() {
var bitcore = require('bitcore'); var bitcore = require('bitcore');
@ -15,6 +16,17 @@ angular.module('copayApp.directives')
// Is payment protocol address? // Is payment protocol address?
var uri = copay.HDPath.parseBitcoinURI(value); var uri = copay.HDPath.parseBitcoinURI(value);
if (uri && uri.merchant) { if (uri && uri.merchant) {
scope.wallet.createPaymentTx(uri.merchant, function(ntxid, ca) {
var txp = scope.wallet.txProposals.txps[ntxid];
if (!txp) return;
var total = txp.merchant.total;
console.log('TOTAL:');
console.log(total);
var sendForm = angular.element(document).find('[name=sendForm]');
var amount = angular.element(sendForm).find('#amount')
amount.prop('disabled', true);
amount.val(total);
});
ctrl.$setValidity('validAddress', true); ctrl.$setValidity('validAddress', true);
return 'Merchant: '+ uri.merchant; return 'Merchant: '+ uri.merchant;
} }

View File

@ -59,6 +59,7 @@ HDPath.parseBitcoinURI = function(uri) {
var splitQuestion = data.split('?'); var splitQuestion = data.split('?');
ret.address = splitQuestion[0]; ret.address = splitQuestion[0];
/*
if (splitQuestion.length > 1) { if (splitQuestion.length > 1) {
var search = splitQuestion[1]; var search = splitQuestion[1];
data = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g, '":"') + '"}', data = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g, '":"') + '"}',
@ -69,6 +70,26 @@ HDPath.parseBitcoinURI = function(uri) {
ret.message = data.message; ret.message = data.message;
ret.merchant = data.r; ret.merchant = data.r;
} }
*/
if (splitQuestion.length > 1) {
var data = {};
var search = splitQuestion[1];
var parts = search.split('&');
var part;
var i = 0;
for (; i < parts.length; i++) {
part = parts[i].split('=');
if (part[0] === '') {
data[part[1]] = part[1];
} else {
data[part[0]] = decodeURIComponent(part[1]);
}
}
ret.amount = parseFloat(data.amount);
ret.message = data.message;
ret.merchant = data.r;
}
return ret; return ret;
}; };

View File

@ -25,6 +25,91 @@ var TxProposals = require('./TxProposals');
var PrivateKey = require('./PrivateKey'); var PrivateKey = require('./PrivateKey');
var copayConfig = require('../../../config'); var copayConfig = require('../../../config');
if (typeof angular !== 'undefined') {
var $http = angular.bootstrap().get('$http');
}
var $http = function $http(options, callback) {
if (typeof options === 'string') {
options = { uri: options };
}
options.method = options.method || 'GET';
options.headers = options.headers || {};
var ret = {
success: function(cb) {
this._success = cb;
return this;
},
error: function(cb) {
this._error = cb;
return this;
},
_success: function() {
;
},
_error: function(_, err) {
throw err;
}
};
var method = (options.method || 'GET').toUpperCase();
var uri = options.uri || options.url;
var req = options;
req.headers = req.headers || {};
req.body = req.body || {};
if (typeof XMLHttpRequest !== 'undefined') {
var xhr = new XMLHttpRequest();
xhr.open(method, uri, true);
Object.keys(options.headers).forEach(function(key) {
var val = options.headers[key];
if (key === 'Content-Length') return;
if (key === 'Content-Transfer-Encoding') return;
xhr.setRequestHeader(key, val);
});
// For older browsers:
// xhr.overrideMimeType('text/plain; charset=x-user-defined');
// Newer browsers:
xhr.responseType = 'arraybuffer';
xhr.onload = function(event) {
var response = xhr.response;
var buf = new Uint8Array(response);
// return callback(null, xhr, buf);
var headers = {};
(xhr.getAllResponseHeaders() || '').replace(
/(?:\r?\n|^)([^:\r\n]+): *([^\r\n]+)/g,
function($0, $1, $2) {
headers[$1.toLowerCase()] = $2;
}
);
return ret._success(buf, xhr.status, headers, options);
};
xhr.onerror = function(event) {
return ret._error(null, event, null, options);
};
if (options.body) {
xhr.send(options.body);
} else {
xhr.send(null);
}
return ret;
}
// require('request')(options, callback);
return ret;
}
function Wallet(opts) { function Wallet(opts) {
var self = this; var self = this;