Skip to content

Commit

Permalink
Merge pull request #462 from chjj/paypro_example
Browse files Browse the repository at this point in the history
paypro: a more complete example of how to use server outputs.
  • Loading branch information
Ryan X. Charles committed Jul 28, 2014
2 parents 1292a06 + 3582ff3 commit 434ba6f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 41 deletions.
45 changes: 39 additions & 6 deletions examples/PayPro/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function sendPayment(msg, callback) {
var pay = new PayPro();
pay = pay.makePayment();
pay.set('merchant_data', merchant_data);
pay.set('transactions', [createTX()]);
pay.set('transactions', [createTX(outputs)]);
pay.set('refund_to', refund_outputs);

msg = msg || 'Hi server, I would like to give you some money.';
Expand Down Expand Up @@ -273,6 +273,19 @@ function sendPayment(msg, callback) {
var memo = ack.get('memo');
print('Our payment was acknowledged!');
print('Message from Merchant: %s', memo);
payment = PayPro.Payment.decode(payment);
var pay = new PayPro();
payment = pay.makePayment(payment);
print(payment);
var tx = payment.message.transactions[0];
if (tx.buffer) {
tx.buffer = tx.buffer.slice(tx.offset, tx.limit);
var ptx = new bitcore.Transaction();
ptx.parse(tx.buffer);
tx = ptx;
}
var txid = tx.getHash().toString('hex');
print('First payment txid: %s', txid);
return callback();
});
});
Expand Down Expand Up @@ -324,7 +337,7 @@ function parseQS(query) {
return out;
}

function createTX() {
function createTX(outputs) {
// Addresses
var addrs = [
'mzTQ66VKcybz9BD1LAqEwMFp9NrBGS82sY',
Expand Down Expand Up @@ -361,10 +374,13 @@ function createTX() {
];

// define transaction output
var outs = [{
address: addrs[2],
amount: 0.00003000
}];
var outs = [];
outputs.forEach(function(output) {
outs.push({
address: addrs[0], // dummy address
amount: 0 // dummy value
});
});

// set change address
var opts = {
Expand All @@ -379,6 +395,23 @@ function createTX() {
.sign(keys)
.build();

outputs.forEach(function(output, i) {
var value = output.get('amount');
var script = output.get('script');
var v = new Buffer(8);
v[0] = (value.low >> 0) & 0xff;
v[1] = (value.low >> 8) & 0xff;
v[2] = (value.low >> 16) & 0xff;
v[3] = (value.low >> 24) & 0xff;
v[4] = (value.high >> 0) & 0xff;
v[5] = (value.high >> 8) & 0xff;
v[6] = (value.high >> 16) & 0xff;
v[7] = (value.high >> 24) & 0xff;
var s = script.buffer.slice(script.offset, script.limit);
tx.outs[i].v = v;
tx.outs[i].s = s;
});

print('');
print('Customer created transaction:');
print(tx.getStandardizedObject());
Expand Down
71 changes: 36 additions & 35 deletions examples/PayPro/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,41 +123,42 @@ app.post('/-/request', function(req, res, next) {

var outputs = [];

var po = new PayPro();
po = po.makeOutput();
// number of satoshis to be paid
po.set('amount', 0);
// a TxOut script where the payment should be sent. similar to OP_CHECKSIG
po.set('script', new Buffer([
118, // OP_DUP
169, // OP_HASH160
76, // OP_PUSHDATA1
20, // number of bytes
0xcf,
0xbe,
0x41,
0xf4,
0xa5,
0x18,
0xed,
0xc2,
0x5a,
0xf7,
0x1b,
0xaf,
0xc7,
0x2f,
0xb6,
0x1b,
0xfc,
0xfc,
0x4f,
0xcd,
136, // OP_EQUALVERIFY
172 // OP_CHECKSIG
]));

outputs.push(po.message);
[2000, 1000].forEach(function(value) {
var po = new PayPro();
po = po.makeOutput();
// number of satoshis to be paid
po.set('amount', value);
// a TxOut script where the payment should be sent. similar to OP_CHECKSIG
po.set('script', new Buffer([
118, // OP_DUP
169, // OP_HASH160
76, // OP_PUSHDATA1
20, // number of bytes
0xcf,
0xbe,
0x41,
0xf4,
0xa5,
0x18,
0xed,
0xc2,
0x5a,
0xf7,
0x1b,
0xaf,
0xc7,
0x2f,
0xb6,
0x1b,
0xfc,
0xfc,
0x4f,
0xcd,
136, // OP_EQUALVERIFY
172 // OP_CHECKSIG
]));
outputs.push(po.message);
});

/**
* Payment Details
Expand Down

0 comments on commit 434ba6f

Please sign in to comment.