paypro: maintain received payment requests.

This commit is contained in:
Christopher Jeffrey 2014-07-31 12:23:06 -07:00 committed by Manuel Araoz
parent b9d5219426
commit af12b56678
2 changed files with 36 additions and 9 deletions

View File

@ -94,10 +94,14 @@ angular.module('copayApp.controllers').controller('SendController',
&& copay.HDPath.parseBitcoinURI(address);
if (uri && uri.merchant) {
w.createPaymentTx({
uri: uri.merchant,
memo: commentText
}, done);
var data = w.paymentRequests[uri.merchant];
if (data) {
} else {
w.createPaymentTx({
uri: uri.merchant,
memo: commentText
}, done);
}
} else {
w.createTx(address, amount, commentText, done);
}

View File

@ -150,6 +150,8 @@ function Wallet(opts) {
this.addressBook = opts.addressBook || {};
this.publicKey = this.privateKey.publicHex;
this.paymentRequests = opts.paymentRequests || {};
//network nonces are 8 byte buffers, representing a big endian number
//one nonce for oneself, and then one nonce for each copayer
this.network.setHexNonce(opts.networkNonce);
@ -899,12 +901,30 @@ Wallet.prototype.createPaymentTx = function(options, cb) {
};
Wallet.prototype.fetchPaymentTx = function(options, cb) {
var self = this;
options = options || {};
if (typeof options === 'string') {
options = { uri: options };
}
options.fetch = true;
return this.createPaymentTx(options, cb);
return this.createPaymentTx(options, function(err, merchantData, options, pr) {
var id = self.hashMerchantData(merchantData);
self.paymentRequests[id] = {
id: id,
merchantData: merchantData,
options: options,
pr: pr
};
return cb(null, merchantData);
});
};
Wallet.hashMerchantData =
Wallet.prototype.hashMerchantData = function(merchantData) {
return merchantData.request_url
+ ':' + merchantData.pr.payment_url
+ ':' + merchantData.total
+ ':' + merchantData.pr.pd.merchant_data;
};
Wallet.prototype.receivePaymentRequest = function(options, pr, cb) {
@ -989,11 +1009,12 @@ Wallet.prototype.receivePaymentRequest = function(options, pr, cb) {
total: bignum('0', 10).toString(10)
};
if (options.fetch) {
return cb(null, merchantData);
}
return this.getUnspent(function(err, unspent) {
if (options.fetch) {
self.createPaymentTxSync(options, merchantData, unspent);
return cb(null, merchantData, options, pr);
}
var ntxid = self.createPaymentTxSync(options, merchantData, unspent);
if (ntxid) {
self.sendIndexes();
@ -1195,6 +1216,8 @@ Wallet.prototype.createPaymentTxSync = function(options, merchantData, unspent)
merchantData.total = merchantData.total.toString(10);
if (options.fetch) return;
this.log('');
this.log('Created transaction:');
this.log(b.tx.getStandardizedObject());