Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

php - express parsing all request to json

I am started to implement some API to my marketplace. I am using Shoppy.gg for get some cryptopayments. And i have got a problem about the verify their callbacks from my back end.

In their documentation explain to how validate shoppy with php:

$signature = hash_hmac('sha512', file_get_contents('php://input'), 'secret');
$is_valid = hash_equals($signature, $_SERVER['HTTP_X_SHOPPY_SIGNATURE']);

And i am trying to do it with nodejs express module

  const signature =  req.header('x-shoppy-signature')
  const verifySignature = sha512(JSON.stringify(req), shoppyWebhookSecret)
  if(signature !== verifySignature) return res.json({status: "error", message: "payment-hash-wrong"})

But in my code, ivgot an exception about Converting circular structure to JSON. Am i understand this documentation wrong ? Shouldnt i create sha512 hash from request by using my secret key ?

Edit: Also i thought maybe it can be gave url encoded string in php and i do it with javascript as coded following:

var verifySignature = sha512(new URLSearchParams(req.body).toString(), shoppyWebhookSecret)

but doesnt works too.

The documentation: https://shoppy.dev/#/webhooks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JSON.stringify(req) is not the equivalent to file_get_contents('php://input'). req is a complex object that cannnot be represented as json. That is why you are getting the error Converting circular structure to JSON.

You need to find the equivalent to file_get_contents('php://input') in node.js. This should be the full request-body.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...