Midtrans / midtrans-nodejs-client

Official Midtrans Payment API Client for Node JS | https://midtrans.com
MIT License
174 stars 56 forks source link

Converting circular structure to JSON #39

Closed yogithesymbian closed 2 years ago

yogithesymbian commented 2 years ago

sudo journalctl -u node-xxx -n 100 --no-pager

(node:1671271) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
Oct 26 16:05:24 imt node-[1671271]:     --> starting at object with constructor 'ClientRequest'
Oct 26 16:05:24 imt node-[1671271]:     |     property 'socket' -> object with constructor 'TLSSocket'
Oct 26 16:05:24 imt node-[1671271]:     --- property '_httpMessage' closes the circle
Oct 26 16:05:24 imt node-[1671271]:     at JSON.stringify (<anonymous>)
Oct 26 16:05:24 imt node-[1671271]:     at stringify (/var/www/html/api/xxxx/node_modules/express/lib/response.js:1123:12)
Oct 26 16:05:24 imt node-[1671271]:     at ServerResponse.json (/var/www/html/api/xxxx/node_modules/express/lib/response.js:260:14)
Oct 26 16:05:24 imt node-[1671271]:     at ServerResponse.send (/var/www/html/api/xxxx/node_modules/express/lib/response.js:158:21)
Oct 26 16:05:24 imt node-[1671271]:     at Object.exports.err (/var/www/html/api/xxxx/src/utils/res.js:17:26)
Oct 26 16:05:24 imt node-[1671271]:     at /var/www/html/api/xxxx/src/controllers/notification.controller.js:55:26
Oct 26 16:05:24 imt node-[1671271]:     at processTicksAndRejections (internal/process/task_queues.js:93:5)
Oct 26 16:05:24 imt node-[1671271]: (Use `node --trace-warnings ...` to show where the warning was created)

https://docs.midtrans.com/en/after-payment/http-notification

        let notificationJson = req.body;
        apiClient.transaction.notification(notificationJson)

not solve dunno why, https://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json , i have seen on https://github.com/Midtrans/midtrans-nodejs-client/issues/34 its work properly right ?

      let notificationJson = stringify(req.body);
      let sss = JSON.stringify({ ...req.body });
      let sssd = req.body;

      console.log(`notificationJson : ${notificationJson}`);
      console.log(`notificationJson  : ${req.body}`);
      console.log(`notificationJson  : ${sss}`);
      console.log(`notificationJson  : ${sssd}`);

serverKey : SB-Mid-server-xxx. clientKey : SB-Mid-client-xx-xxx
Oct 26 17:32:11 imt xx[1676873]: notificationJson : [{"statusResponse":"1"},{"transaction_time":"2","no":"3"},"yogi","as"]
Oct 26 17:32:11 imt xx[1676873]: notificationJson  : [object Object]
Oct 26 17:32:11 imt xx[1676873]: notificationJson  : {"statusResponse":{"transaction_time":"yogi","no":"as"}}
Oct 26 17:32:11 imt xx[1676873]: notificationJson  : [object Object]
rizdaprasetya commented 2 years ago

Hi @yogithesymbian I don't think the issue is coming from Midtrans library. From your log the error TypeError: Converting circular structure to JSON is thrown by: node_modules/express/lib/response.js:1123:12 it is from Express library. Maybe your Express backend is unable to handle malformed HTTP response or something. Which you can check from your implementation of this line of code to know what function call produce the issue:

/src/controllers/notification.controller.js:55:26

apiClient.transaction.notification(notificationJson) will work properly if the notificationJson is a JSON object (or string) that have the correct attributes as example here: https://docs.midtrans.com/en/after-payment/http-notification?id=sample-for-various-payment-methods

From your example, the req.body value is:

[{"statusResponse":"1"},{"transaction_time":"2","no":"3"},"yogi","as"]

Which is not a valid Midtrans notification JSON.

If you want to get a valid Midtrans notification JSON to test, I suggest you setup your Sandbox account to send HTTP notification to your backend: https://docs.midtrans.com/en/after-payment/http-notification?id=configuring-http-notifications-on-map

Then perform a sandbox test payment: https://docs.midtrans.com/en/technical-reference/sandbox-test

yogithesymbian commented 2 years ago

Hi @rizdaprasetya thanks in advance i dunno why but i have test right now and its work . last week i have tried with three value for the notificationJson variable

stringify(req.body); // notificationJson value
JSON.stringify({ ...req.body }); // notificationJson value
req.body; // notificationJson value // now its work dunno why how error is came on yesterday , i didnt touch anything .