Merge pull request #493 from chjj/fix_paypro_example
Fix Payment Protocol Example
This commit is contained in:
commit
abc4259f76
|
@ -1,14 +1,67 @@
|
||||||
# Running the Payment Protocol Demo
|
# Running the Payment Protocol Demo
|
||||||
This is an example of Bitcore's Payment Protocol implementation, including a
|
|
||||||
mocked server (`server.js`) and client (`customer.js`).
|
|
||||||
|
|
||||||
1. Start the server: `node server.js`
|
## Node
|
||||||
2. Start the customer: `node customer.js`
|
|
||||||
|
|
||||||
At this point, you should see an acknowledgement from your local server:
|
The node payment protocol demonstration will run automatically via:
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
$ node examples/PayPro
|
||||||
|
```
|
||||||
|
|
||||||
|
You will see the server and customer logs output in the terminal.
|
||||||
|
|
||||||
|
## Browser
|
||||||
|
|
||||||
|
To run our payment protocol demonstration in the browser, you may run:
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
$ node examples/PayPro/server.js -b -p 8080
|
||||||
|
```
|
||||||
|
|
||||||
|
This will start the payment protocol demonstration server in browser mode,
|
||||||
|
which serves outputs in the payment protocol request (don't worry, it doesn't
|
||||||
|
ask for *too* many testnet coins).
|
||||||
|
|
||||||
|
Once the server is started, you can visit it in your browser:
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
$ chromium https://localhost:8080/
|
||||||
|
```
|
||||||
|
|
||||||
|
You will see a simple checkout page to buy some imaginary products. Once you
|
||||||
|
press checkout, you will see all the server and client logs in the browser as
|
||||||
|
well as the terminal.
|
||||||
|
|
||||||
|
If you're connected to enough peers, your transaction will be broadcast
|
||||||
|
throughout the bitcoin testnet network and hopefully ACKed by your peers.
|
||||||
|
|
||||||
|
## Logs
|
||||||
|
|
||||||
|
Your logs may ultimately look something like this:
|
||||||
|
|
||||||
```
|
```
|
||||||
Customer: Our payment was acknowledged!
|
Customer: Our payment was acknowledged!
|
||||||
Customer: Message from Merchant: Thank you for your payment!
|
Customer: Message from Merchant: Thank you for your payment!
|
||||||
Customer: Payment sent successfully.
|
Customer: Payment sent successfully.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Changing the server address contained in outputs
|
||||||
|
|
||||||
|
If you want to alter the address or public key the testnet coins get sent to by
|
||||||
|
the payment server, you can pass in the `--pubkey` or `--address` options.
|
||||||
|
`address` has to be a testnet address, whereas `pubkey` is a hex encoded public
|
||||||
|
key. The `--privkey` option is also available in the standard bitcoind privkey
|
||||||
|
format.
|
||||||
|
|
||||||
|
## Other Options
|
||||||
|
|
||||||
|
If you you're not connected to enough peers to broadcast your transaction (by
|
||||||
|
default, this example only connects to the core seed peers), you can enable
|
||||||
|
peer discovery in bitcore by passing the `--discovery` (`-d`) argument onto the
|
||||||
|
server command line.
|
||||||
|
|
||||||
|
If you don't want to actually broadcast your transaction and want to keep your
|
||||||
|
testnet coins, you can pass `--no-tx` on the server command line.
|
||||||
|
|
||||||
|
If you don't want the tests to run automatically and simply host the payment
|
||||||
|
server, simply pass `--browser` (`-b`) as mentioned above.
|
||||||
|
|
|
@ -153,7 +153,7 @@ function sendPayment(msg, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return request({
|
return request({
|
||||||
method: 'POST',
|
method: 'GET',
|
||||||
uri: 'https://localhost:' + port + '/-/request',
|
uri: 'https://localhost:' + port + '/-/request',
|
||||||
headers: {
|
headers: {
|
||||||
'Accept': PayPro.PAYMENT_REQUEST_CONTENT_TYPE
|
'Accept': PayPro.PAYMENT_REQUEST_CONTENT_TYPE
|
||||||
|
|
|
@ -15,6 +15,7 @@ var fs = require('fs');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var qs = require('querystring');
|
var qs = require('querystring');
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
// Disable strictSSL
|
// Disable strictSSL
|
||||||
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
|
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
|
||||||
|
@ -35,10 +36,6 @@ var TransactionBuilder = bitcore.TransactionBuilder;
|
||||||
* Variables
|
* Variables
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var isNode = !argv.b && !argv.browser;
|
|
||||||
|
|
||||||
var app = express();
|
|
||||||
|
|
||||||
var x509 = {
|
var x509 = {
|
||||||
priv: fs.readFileSync(__dirname + '/../../test/data/x509.key'),
|
priv: fs.readFileSync(__dirname + '/../../test/data/x509.key'),
|
||||||
pub: fs.readFileSync(__dirname + '/../../test/data/x509.pub'),
|
pub: fs.readFileSync(__dirname + '/../../test/data/x509.pub'),
|
||||||
|
@ -51,6 +48,14 @@ var server = https.createServer({
|
||||||
cert: fs.readFileSync(__dirname + '/../../test/data/x509.crt')
|
cert: fs.readFileSync(__dirname + '/../../test/data/x509.crt')
|
||||||
});
|
});
|
||||||
|
|
||||||
|
server.setOptions = function(options) {
|
||||||
|
argv = options;
|
||||||
|
};
|
||||||
|
|
||||||
|
var isNode = !argv.b && !argv.browser;
|
||||||
|
|
||||||
|
var app = express();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ignore Cache Headers
|
* Ignore Cache Headers
|
||||||
* Allow CORS
|
* Allow CORS
|
||||||
|
@ -71,12 +76,18 @@ app.use(function(req, res, next) {
|
||||||
};
|
};
|
||||||
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
|
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
|
||||||
if (req.method === 'OPTIONS') {
|
res.setHeader('Access-Control-Allow-Headers', [
|
||||||
res.setHeader('Access-Control-Allow-Methods', 'GET,POST');
|
'Host',
|
||||||
res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
|
'Connection',
|
||||||
return res.send(200);
|
'Content-Length',
|
||||||
}
|
'Accept',
|
||||||
|
'Origin',
|
||||||
|
'User-Agent',
|
||||||
|
'Content-Type',
|
||||||
|
'Accept-Encoding',
|
||||||
|
'Accept-Language'
|
||||||
|
].join(','));
|
||||||
|
|
||||||
res.setHeader('Accept', PayPro.PAYMENT_CONTENT_TYPE);
|
res.setHeader('Accept', PayPro.PAYMENT_CONTENT_TYPE);
|
||||||
|
|
||||||
|
@ -132,9 +143,32 @@ app.get('/-/request', function(req, res, next) {
|
||||||
[2000, 1000, 10000].forEach(function(value) {
|
[2000, 1000, 10000].forEach(function(value) {
|
||||||
var po = new PayPro();
|
var po = new PayPro();
|
||||||
po = po.makeOutput();
|
po = po.makeOutput();
|
||||||
|
|
||||||
// number of satoshis to be paid
|
// number of satoshis to be paid
|
||||||
po.set('amount', value);
|
po.set('amount', value);
|
||||||
|
|
||||||
// a TxOut script where the payment should be sent. similar to OP_CHECKSIG
|
// a TxOut script where the payment should be sent. similar to OP_CHECKSIG
|
||||||
|
|
||||||
|
// Instead of creating it ourselves:
|
||||||
|
// if (!argv.pubkey && !argv.privkey && !argv.address) {
|
||||||
|
// //argv.pubkey = '3730febcba04bad0cd476cfb820f9c37d7466fd9';
|
||||||
|
// argv.pubkey = 'd96f46d7379c0f82fb6c47cdd0ba04babcfe3037'
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (argv.pubkey || argv.privkey || argv.address) {
|
||||||
|
var pubKey;
|
||||||
|
if (argv.pubkey) {
|
||||||
|
pubKey = new Buffer(argv.pubkey, 'hex');
|
||||||
|
} else if (argv.privkey) {
|
||||||
|
pubKey = bitcore.Key.recoverPubKey(new Buffer(argv.privkey)).toCompressedPubKey();
|
||||||
|
} else if (argv.address) {
|
||||||
|
pubKey = bitcore.Base58Check.decode(new Buffer(argv.address));
|
||||||
|
}
|
||||||
|
var address = bitcore.Address.fromPubKey(pubKey, 'testnet');
|
||||||
|
var scriptPubKey = address.getScriptPubKey();
|
||||||
|
assert.equal(scriptPubKey.isPubkeyHash(), true);
|
||||||
|
po.set('script', scriptPubKey.getBuffer());
|
||||||
|
} else {
|
||||||
po.set('script', new Buffer([
|
po.set('script', new Buffer([
|
||||||
118, // OP_DUP
|
118, // OP_DUP
|
||||||
169, // OP_HASH160
|
169, // OP_HASH160
|
||||||
|
@ -163,6 +197,8 @@ app.get('/-/request', function(req, res, next) {
|
||||||
136, // OP_EQUALVERIFY
|
136, // OP_EQUALVERIFY
|
||||||
172 // OP_CHECKSIG
|
172 // OP_CHECKSIG
|
||||||
]));
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
outputs.push(po.message);
|
outputs.push(po.message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -282,7 +318,10 @@ app.post('/-/pay', function(req, res, next) {
|
||||||
});
|
});
|
||||||
|
|
||||||
print('Broadcasting transaction...');
|
print('Broadcasting transaction...');
|
||||||
|
|
||||||
|
if (!argv['no-tx']) {
|
||||||
conn.sendTx(tx);
|
conn.sendTx(tx);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
print('No BTC network connection. Retrying...');
|
print('No BTC network connection. Retrying...');
|
||||||
|
@ -305,7 +344,7 @@ var peerman = new bitcore.PeerManager({
|
||||||
network: 'testnet'
|
network: 'testnet'
|
||||||
});
|
});
|
||||||
|
|
||||||
peerman.peerDiscovery = false;
|
peerman.peerDiscovery = argv.d || argv.discovery || false;
|
||||||
|
|
||||||
peerman.addPeer(new bitcore.Peer('testnet-seed.alexykot.me', 18333));
|
peerman.addPeer(new bitcore.Peer('testnet-seed.alexykot.me', 18333));
|
||||||
peerman.addPeer(new bitcore.Peer('testnet-seed.bitcoin.petertodd.org', 18333));
|
peerman.addPeer(new bitcore.Peer('testnet-seed.bitcoin.petertodd.org', 18333));
|
||||||
|
|
Loading…
Reference in New Issue