JSON 支付协议接口
这是JSON支付协议接口的第一个版本. 如果你对规格本身有疑问, 查看文档。 。 。 .
开始
npm install json-payment-protocol
使用量
我们支持召回和承诺。 对于承诺,仅在函数名的结尾添加了Async. 注意注意注意你何时能广播付款. 在多数情况下,在从服务器收到成功通知之前广播付款会导致发送者付款失败. 发货人将承担再次支付交易费以取回资金的费用.
回电
const JsonPaymentProtocol = require('json-payment-protocol');
const paymentProtocol = new JsonPaymentProtocol();
let requestUrl = 'bitcoin:?r=https://test.bitpay.com/i/Jr629pwsXKdTCneLyZja4t';
paymentProtocol.getRawPaymentRequest(requestUrl, function (err, response) {
if (err) {
return console.log('Error retrieving payment request', err);
}
paymentProtocol.parsePaymentRequest(response.rawBody, response.headers, function (err, paymentRequest) {
if (err) {
return console.log('Error parsing payment request', err);
}
console.log('Payment request retrieved');
console.log(paymentRequest);
//TODO: Create the rawTransaction and sign it in your wallet instead of this, do NOT broadcast yet
let currency = 'BTC';
let signedRawTransaction = '02000000010c2b0d60448d5cdfebe222014407bdb408b8427f837447484911efddea700323000000006a47304402201d3ed3117f1968c3b0a078f15f8462408c745ff555b173eff3dfe0a25e063c0c02200551572ec33d45ece8e64275970bd1b1694621f0ed8fac2f7e18095f170fe3fe012102d4edb773e3bd94e1251790f5cc543cbfa76c2b0abad14898674b1c4e27176ef2ffffffff02c44e0100000000001976a914dd826377dcf2075e5065713453cfad675ba9434f88aca070002a010000001976a914e7d0344ba970301e93cd7b505c7ae1b5bcf5639288ac00000000';
paymentProtocol.sendPayment(currency, signedRawTransaction, paymentRequest.paymentUrl, function(err, response) {
if (err) {
//DO NOT BROADCAST PAYMENT
return console.log('Error sending payment to server');
}
console.log('Payment sent successfully');
//TODO: Broadcast payment to network here
});
});
});
承诺
const JsonPaymentProtocol = require('json-payment-protocol');
const paymentProtocol = new JsonPaymentProtocol();
let requestUrl = 'bitcoin:?r=https://test.bitpay.com/i/Jr629pwsXKdTCneLyZja4t';
paymentProtocol
.getRawPaymentRequestAsync(requestUrl)
.then((response) => {
return paymentProtocol.parsePaymentRequestAsync(response.rawBody, response.headers);
})
.then((paymentRequest) => {
console.log('Payment request retrieved');
console.log(paymentRequest);
//TODO: Create the rawTransaction and sign it in your wallet instead of this, do NOT broadcast yet
let currency = 'BTC';
let signedRawTransaction = '02000000010c2b0d60448d5cdfebe222014407bdb408b8427f837447484911efddea700323000000006a47304402201d3ed3117f1968c3b0a078f15f8462408c745ff555b173eff3dfe0a25e063c0c02200551572ec33d45ece8e64275970bd1b1694621f0ed8fac2f7e18095f170fe3fe012102d4edb773e3bd94e1251790f5cc543cbfa76c2b0abad14898674b1c4e27176ef2ffffffff02c44e0100000000001976a914dd826377dcf2075e5065713453cfad675ba9434f88aca070002a010000001976a914e7d0344ba970301e93cd7b505c7ae1b5bcf5639288ac00000000';
return paymentProtocol.sendPaymentAsync(currency, signedRawTransaction, paymentRequest.paymentUrl);
})
.then((response) => {
console.log('Payment sent successfully');
//TODO: Broadcast payment to network here
})
.catch((err) => {
//DO NOT BROADCAST PAYMENT
return console.log('Error processing payment request', err);
});
选项
转到 new JsonPaymentProtocol() 因此,如果您需要使用代理或设置任何其他请求。js的旗帜可以在即时请求时包含。 例如:
new JsonPaymentProtocol({
proxy: 'socks://mySocksProxy.local'
});
URI 格式
你可以提供其中之一 bitcoin:?r=https://bitpay.com/i/invoice 格式或 https://bitpay.com/i/invoice 直接说.