node-paytmpg 4.0.2 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,8 @@ name: Node.js Package
6
6
  on:
7
7
  release:
8
8
  types: [created]
9
-
9
+ push:
10
+ branches: [ master ]
10
11
  jobs:
11
12
  publish:
12
13
  runs-on: ubuntu-latest
package/README.MD CHANGED
@@ -202,6 +202,25 @@ var User=PayTMPG.User;
202
202
 
203
203
  ```
204
204
 
205
+ ### Webhooks
206
+
207
+ Webhooks can issued at at `/_pay/api/webhook` and are useful for payments captured late.
208
+
209
+ If you are using `bodyparser` , make sure to remove these from your express app since the body parsers will be already added with the library .
210
+ ```
211
+ Comment these if present in your app
212
+ app.use(bodyParser.json());
213
+ app.use(bodyParser.urlencoded({ extended: true }));
214
+ ```
215
+
216
+ #### For razorpay webhook
217
+ Make sure to use the same secret in your webhook as the merchant secret.
218
+ https://razorpay.com/docs/webhooks/
219
+
220
+ #### For paytm
221
+ Nothing extra needed
222
+ https://developer.paytm.com/docs/callback-and-webhook/?ref=callbackWebhook
223
+
205
224
 
206
225
 
207
226
  License : GPL
@@ -6,6 +6,7 @@ var IDLEN = 10;
6
6
  var nodeBase64 = require('nodejs-base64-converter');
7
7
  var RazorPay = require('razorpay');
8
8
  const PaytmChecksum = require('./checksum/PaytmChecksum.js');
9
+ const { stat } = require('fs');
9
10
 
10
11
 
11
12
  function sanitizeRequest(body) {
@@ -108,7 +109,7 @@ module.exports = function (app, callbacks) {
108
109
  params['ORDER_ID'] = req.body.ORDER_ID;
109
110
  params['CUST_ID'] = req.body.CUST_ID;
110
111
  params['TXN_AMOUNT'] = req.body.TXN_AMOUNT;
111
- params['CALLBACK_URL'] = req.body.CALLBACK_URL + "?order_id="+req.body.ORDER_ID;
112
+ params['CALLBACK_URL'] = req.body.CALLBACK_URL + "?order_id=" + req.body.ORDER_ID;
112
113
  params['EMAIL'] = req.body.EMAIL;
113
114
  params['MOBILE_NO'] = req.body.MOBILE_NO;
114
115
  params['PRODUCT_NAME'] = req.body.PRODUCT_NAME;
@@ -574,6 +575,44 @@ module.exports = function (app, callbacks) {
574
575
 
575
576
  }
576
577
 
578
+ function updateTransaction(req, res) {
579
+ var myquery = { orderId: req.body.ORDERID };
580
+
581
+ Transaction.findOne(myquery, function (err, objForUpdate) {
582
+
583
+ if (err) {
584
+ res.send({ message: "Transaction Not Found !", ORDERID: req.body.ORDERID, TXNID: req.body.TXNID })
585
+ return;
586
+ }
587
+ if (objForUpdate.status != ("INITIATED") && objForUpdate.status != ("TXN_PENDING")) {
588
+ res.send({ message: "Transaction already processed", status: objForUpdate.status, ORDERID: objForUpdate.orderId, TXNID: objForUpdate.TXNID, TXNID: req.body.TXNID })
589
+ return;
590
+ }
591
+ if (req.body.status == "paid" && !req.body.STATUS) {
592
+ req.body.STATUS = "TXN_SUCCESS"
593
+ }
594
+ objForUpdate.status = req.body.STATUS;
595
+ objForUpdate.TXNID = req.body.TXNID;
596
+ objForUpdate.extra = JSON.stringify(req.body);
597
+
598
+ var newvalues = { $set: objForUpdate };
599
+ Transaction.updateOne(myquery, newvalues, function (err, saveRes) {
600
+
601
+ if (err) {
602
+ res.send({ message: "Error Occured !", ORDERID: req.body.ORDERID, TXNID: req.body.TXNID })
603
+ }
604
+ else {
605
+
606
+ if (callbacks !== undefined)
607
+ callbacks.onFinish(req.body.ORDERID, req.body);
608
+ objForUpdate.readonly = "readonly"
609
+ objForUpdate.action = config.homepage
610
+ res.render(vp + "result.hbs", objForUpdate);
611
+ }
612
+ });
613
+
614
+ }, usingMultiDbOrm ? Transaction : undefined)
615
+ }
577
616
 
578
617
  module.callback = (req, res) => {
579
618
 
@@ -617,37 +656,7 @@ module.exports = function (app, callbacks) {
617
656
 
618
657
  if (result || isCancelled) {
619
658
 
620
- var myquery = { orderId: req.body.ORDERID };
621
- Transaction.findOne(myquery, function (err, objForUpdate) {
622
-
623
- if (err) {
624
- res.send({ message: "Transaction Not Found !", ORDERID: req.body.ORDERID, TXNID: req.body.TXNID })
625
- return;
626
- }
627
- if (req.body.status == "paid" && !req.body.STATUS) {
628
- req.body.STATUS = "TXN_SUCCESS"
629
- }
630
- objForUpdate.status = req.body.STATUS;
631
- objForUpdate.TXNID = req.body.TXNID;
632
- objForUpdate.extra = JSON.stringify(req.body);
633
-
634
- var newvalues = { $set: objForUpdate };
635
- Transaction.updateOne(myquery, newvalues, function (err, saveRes) {
636
-
637
- if (err) {
638
- res.send({ message: "Error Occured !", ORDERID: req.body.ORDERID, TXNID: req.body.TXNID })
639
- }
640
- else {
641
-
642
- if (callbacks !== undefined)
643
- callbacks.onFinish(req.body.ORDERID, req.body);
644
- objForUpdate.readonly = "readonly"
645
- objForUpdate.action = config.homepage
646
- res.render(vp + "result.hbs", objForUpdate);
647
- }
648
- });
649
-
650
- }, usingMultiDbOrm ? Transaction : undefined)
659
+ updateTransaction(req, res);
651
660
 
652
661
  }
653
662
  else {
@@ -658,6 +667,61 @@ module.exports = function (app, callbacks) {
658
667
 
659
668
  }
660
669
 
670
+ module.webhook = (req, res) => {
671
+ if (config.paytm_url) {
672
+ module.callback(req, res)
673
+ }
674
+ else if (config.razor_url) {
675
+ let events = ["payment.captured", "payment.pending", "payment.failed"]
676
+ if (req.body.event && events.indexOf(req.body.event) > -1) {
677
+ if (req.body.payload &&
678
+ req.body.payload.payment &&
679
+ req.body.payload.payment.entity) {
680
+
681
+ let entity = req.body.payload.payment.entity;
682
+ let razorpay_order_id = entity.order_id;
683
+ let razorpay_payment_id = entity.id;
684
+ let status = entity.status;
685
+ let event = req.body.event;
686
+ console.log(`Razorpay webhook payment order=${razorpay_order_id} payid=${razorpay_payment_id} status=${status}`)
687
+
688
+ let reqBody = req.rawBody, signature = req.headers["x-razorpay-signature"];
689
+
690
+ result = RazorPay.validateWebhookSignature(reqBody, req.headers['x-razorpay-signature'], config.SECRET)
691
+ req.signatureVerified = result;
692
+ if (true) {
693
+ if (event == events[0]) {
694
+ req.body.STATUS = "TXN_SUCCESS";
695
+ }
696
+ else if (event == events[1]) { //pending
697
+ req.body.STATUS = "TXN_PENDING";
698
+ }
699
+ else { // failed
700
+ req.body.STATUS = "TXN_FAILURE";
701
+ }
702
+ req.body.ORDERID = razorpay_order_id;
703
+ req.body.TXNID = razorpay_payment_id;
704
+ setTimeout(() => {
705
+ updateTransaction(req, res)
706
+ }, 3000)
707
+ }
708
+ else {
709
+ res.status(401)
710
+ res.send({ message: "Invalid Rzpay signature" })
711
+ }
712
+ }
713
+ else {
714
+ res.status(400)
715
+ res.send({ message: "Invalid Payload" })
716
+ }
717
+ }
718
+ else {
719
+ res.status(400)
720
+ res.send({ message: "Unsupported event : " + req.body.event })
721
+ }
722
+ }
723
+ }
724
+
661
725
  module.createTxn = (req, res) => {
662
726
 
663
727
 
@@ -49,16 +49,21 @@ module.exports = (app, express, callbacks) => {
49
49
 
50
50
  app.set('view engine', 'handlebars');
51
51
 
52
+ let saveRawBody = function (req, res, buf, encoding) {
53
+ req.rawBody = buf.toString();
54
+ }
52
55
  app.use(bodyParser.urlencoded({ extended: true }))
53
- app.use(bodyParser.json())
56
+ app.use(bodyParser.json({ verify: saveRawBody }))
57
+
54
58
  app.use("/" + config.path_prefix, express.static(path.join(__dirname, '../../public')));
55
59
  app.use('/' + config.path_prefix, router);
56
60
 
57
61
  router.all('/', pc.init);
58
62
  router.all('/init', pc.init);
59
63
 
60
- router.all('/home', pc.home)
64
+ // router.all('/home', pc.home)
61
65
  router.all('/callback', pc.callback)
66
+ router.all('/api/webhook', pc.webhook)
62
67
  router.all('/api/status', pc.status)
63
68
  router.all('/api/createTxn', pc.createTxn)
64
69
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-paytmpg",
3
- "version": "4.0.2",
3
+ "version": "4.1.0",
4
4
  "description": "Payment Gateway Integration using NodeJS",
5
5
  "main": "index.js",
6
6
  "scripts": {