node-paytmpg 7.3.9 → 7.4.1

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.
package/README.MD CHANGED
@@ -13,7 +13,10 @@ npm install node-paytmpg multi-db-orm
13
13
  ```js
14
14
  const express = require("express");
15
15
  const { FireStoreDB } = require("multi-db-orm");
16
- const { attachBodyParser, createPaymentMiddleware } = require("node-paytmpg");
16
+ const {
17
+ attachRawBodyAndEngine,
18
+ createPaymentMiddleware,
19
+ } = require("node-paytmpg");
17
20
 
18
21
  const app = express();
19
22
  const db = new FireStoreDB(require("./creds.json"));
@@ -37,7 +40,10 @@ const config = {
37
40
  INDUSTRY_TYPE_ID: "Retail",
38
41
  };
39
42
 
40
- attachBodyParser(app, config);
43
+ // Make sure to call this before adding any other body parsers
44
+ // this preserves the original body in req.rawBody so it can be used to verify
45
+ // signatures in webhooks especially for razorpay
46
+ attachRawBodyAndEngine(app, config);
41
47
 
42
48
  const paymentRouter = createPaymentMiddleware(app, config, db);
43
49
  app.use("/" + config.path_prefix, paymentRouter);
@@ -47,17 +53,25 @@ app.listen(5544, () => {
47
53
  });
48
54
  ```
49
55
 
50
- ## `attachBodyParser(app, config)`
56
+ ## `attachRawBodyAndEngine(app, config)`
51
57
 
52
- Use this before creating the payment middleware.
58
+ > ⚠️ Caution — avoid calling this helper directly in production apps.
59
+
60
+ Use this only if your application does **not** already configure body parsing or a view engine. In most cases you should **not** call this helper; prefer one of the alternatives below.
53
61
 
54
62
  What it does:
55
63
 
56
- - Adds JSON and URL-encoded body parsing.
57
- - Captures `req.rawBody` for webhook signature verification (important for Razorpay webhooks).
58
- - Sets up handlebars view engine expected by payment pages.
64
+ - Adds JSON and URL-encoded body parsing and captures `req.rawBody` (required for some gateway webhook verifications).
65
+ - Configures Handlebars (`hbs`) view engine and a default layout used by the payment pages.
66
+ - Sets `app.set('attachRawBodyAndEngine', true)` so the middleware knows the setup is present.
67
+
68
+ When _not_ to use it:
69
+
70
+ - Do **not** call this if your app already defines body-parsing middleware or a view engine — it will override or duplicate global settings and can cause conflicts.
71
+ - Instead, either let `createPaymentMiddleware` auto-attach the required parsers/engine (it logs a warning if missing) or manually ensure `req.rawBody` and a compatible view engine are configured.
72
+ - If you must call it, call it once at app startup and do not call it from sub-apps or multiple times.
59
73
 
60
- If you skip this call, `createPaymentMiddleware` auto-attaches a default parser and logs a warning. For custom body-parser setups, make sure raw request body is still available as `req.rawBody`.
74
+ If you skip this call, `createPaymentMiddleware` auto-attaches a default parser/engine and logs a warning. For custom body-parser setups, make sure raw request body is still available as `req.rawBody`.
61
75
 
62
76
  ## How to invoke `createPaymentMiddleware`
63
77
 
@@ -676,6 +676,7 @@ class PaymentController {
676
676
  const payuInstance = this.payuInstance;
677
677
  const openMoneyInstance = this.openMoneyInstance;
678
678
  console.log("request_data ", req.originalUrl, JSON.stringify(req.body));
679
+ console.log("request_data rawBody", req.originalUrl, req.rawBody);
679
680
  console.log("request_headers ", req.originalUrl, JSON.stringify(req.headers));
680
681
  if (config.paytm_url) {
681
682
  await this.callback(req, res);
@@ -696,8 +697,6 @@ class PaymentController {
696
697
  const reqBody = req.rawBody;
697
698
  const signature = req.headers["x-razorpay-signature"];
698
699
  console.log("Razorpay webhook signature:", signature);
699
- console.log("Razorpay rawBody:", reqBody);
700
- console.log("Razorpay SECRET:", config.SECRET);
701
700
  if (signature === undefined) {
702
701
  res.status(400).send({ message: "Missing Razorpay signature" });
703
702
  return;
@@ -830,11 +829,6 @@ class PaymentController {
830
829
  // parameters can be from query or body
831
830
  // MID, MOBILE_NO, PRODUCT_NAME, EMAIL, NAME, limit, offset
832
831
  const params = { ...(req.query || {}), ...(req.body || {}) };
833
- // Basic authz guard if caller supplies MID and it mismatches current config
834
- if (params.MID && this.config.MID && params.MID !== this.config.MID) {
835
- res.status(403).send({ message: 'MID mismatch' });
836
- return;
837
- }
838
832
  // Build query map from incoming fields to db columns
839
833
  const query = {};
840
834
  const fieldMap = {
@@ -862,17 +856,16 @@ class PaymentController {
862
856
  const limit = Math.min(parseInt(params.limit, 10) || 20, 100);
863
857
  const offset = Math.max(parseInt(params.offset, 10) || 0, 0);
864
858
  try {
865
- let transactions = [];
866
859
  const all = await this.db.get(this.tableNames.TRANSACTION, query, {
867
- sort: [{ field: 'time', order: 'desc' }]
860
+ sort: [{ field: 'time', order: 'desc' }],
861
+ limit: limit,
862
+ offset: offset
868
863
  });
869
- const safeAll = Array.isArray(all) ? all : [];
870
- transactions = safeAll.slice(offset, offset + limit);
871
864
  res.send({
872
865
  limit,
873
866
  offset,
874
- count: transactions.length,
875
- transactions
867
+ count: all.length,
868
+ transactions: all
876
869
  });
877
870
  }
878
871
  catch (err) {
package/dist/index.js CHANGED
@@ -52,7 +52,7 @@ function attachRawBodyAndEngine(app, userConfig = {}) {
52
52
  function createPaymentMiddleware(app, userConfig, db, callbacks, authenticationMiddleware, tableNames) {
53
53
  //check attachRawBodyAndEngine
54
54
  if (!app.get('attachRawBodyAndEngine')) {
55
- console.warn('[node-paytmpg]: attachRawBodyAndEngine not attached. Attaching default view engine. Either call attachRawBodyAndEngine() before createPaymentMiddleware() or ensure that your Express app has a view engine attached. req.rawBody needed for some webhooks will not be available if this is not called.');
55
+ console.warn('[node-paytmpg]: attachRawBodyAndEngine not attached. Make sure to call attachRawBodyAndEngine() or make sure hbs view engine is set and req.rawBody is available.');
56
56
  attachRawBodyAndEngine(app, userConfig);
57
57
  }
58
58
  const config = (0, buildConfig_1.buildConfig)(userConfig);
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-paytmpg",
3
- "version": "7.3.9",
3
+ "version": "7.4.1",
4
4
  "description": "Payment Gateway Integration using NodeJS",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-paytmpg",
3
- "version": "7.3.9",
3
+ "version": "7.4.1",
4
4
  "description": "Payment Gateway Integration using NodeJS",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",