Receiving and acknowledging payment
Once the customer verifies the payment request, they post a payment transaction to the merchant server (toward payment_url, which points to the payment/ route). Therefore, in this route, we extract the payment details (payment message) from the client's transaction as follows:
var rawBodyParser = bodyParser.raw({type: PaymentProtocol.PAYMENT_CONTENT_TYPE});
app.post("/payment", rawBodyParser, function(req, res) {
var body = PaymentProtocol.Payment.decode(req.body);
var payment = new PaymentProtocol().makePayment(body);
var refund_to = payment.get('refund_to'); //output where a refund should be sent.
var memo = payment.get('memo');
var Rawtransaction = payment.get('transactions')[0].toBuffer();/*One or more valid, signed Bitcoin transactions that fully pay the PaymentRequest*/
var TransactionToBrodcast = new bitcore_lib.Transaction(Rawtransaction).toString('hex');
/* potentially broadcast the transaction as we did in the first chapter using chain.so/api/ */
});
Depending on your design, you can choose which side will broadcast the payment to the bitcoin network, whether the server or the customer's wallet. Still, in the second section (BitcoinJ), we will instead request the customer's approval and then let the merchant forward the transaction to the network using a web API (or by using bitcore-p2p at https://bitcoinj.github.io/payment-protocol).
The final action in the payment process is sending to the client a payment acknowledgment message with a receipt ID. It's possible you can listen on the bitcoin network for whether the transaction took place before sending such a message:
var ack = new PaymentProtocol().makePaymentACK();
ack.set('payment', payment.message);
ack.set('memo', 'Payment processed,Thank you ;) \n invoice ID :'+req.query.id);
//store invoice details in database
var rawack = ack.serialize();
res.set({
'Content-Type': PaymentProtocol.PAYMENT_ACK_CONTENT_TYPE,
'Content-Length': rawack.length,
});
res.send(rawack);