mollie / mollie-api-node

Official Mollie API client for Node
http://www.mollie.com
BSD 3-Clause "New" or "Revised" License
228 stars 62 forks source link

cannot get id in the webhook request via nodeJS #325

Closed Nafiz-Anam closed 11 months ago

Nafiz-Anam commented 12 months ago

The webhook URL is public. also, I have tested it with Postman all working fine. but when I do payment with Mollie I am not getting the id in the webhook call. although the webhook call is happening just not getting the id there. using node.js for my server. currently using test mood for testing, and building out the app.

Pimm commented 12 months ago

Do I understand correctly that when you make a request through Postman to your webhook, you can extract the ID, but when Mollie performs the webhook request, you can't?

Could share the request you're making through Postman, perhaps as a cURL command?

Nafiz-Anam commented 12 months ago

The webhook url i am using is this: https://freightly-server.onrender.com/api/v1/payment/response

from postman I am sending usual data in a JSON format.

curl --location 'https://freightly-server.onrender.com/api/v1/payment/response' \ --header 'Content-Type: application/json' \ --data '{ "id": "tr_kdkwew45" }'

// webhook receiving endpoint controller code response: async (req, res) => { try { const paymentId = req.body.id; console.log("req.body.id =>",paymentId); // getting undefined

        res.status(200).json({
            status: true,
            message: "Got the response",
        });
    } catch (error) {
        console.log(error);
        res.status(500).json({
            status: false,
            message: "Internal server error!",
        });
    }
},
Pimm commented 12 months ago

from postman I am sending usual data in a JSON format.

Mollie does not call your webhook with JSON data. See docs.mollie.com/overview/webhooks.

You should test your endpoint like so:

curl --location 'https://freightly-server.onrender.com/api/v1/payment/response' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data 'id=tr_kdkwew45'

You have not specified what framework or library you are using to parse the body of the request, but it should be able to handle this format. How does your endpoint respond when you try this?

Nafiz-Anam commented 12 months ago

I am using node.js and express.js for my server. This is my server.js code . let me know what should I change here?

const express = require("express"); const cors = require("cors"); const path = require("path"); const port = process.env.PORT || 5000;

//route import const Router = require("./routes/Router");

require("dotenv").config(); const app = express();

//middle-wares app.use(cors()); app.use(require("sanitize").middleware); app.use(express.json()); // using static files app.use('/static', express.static(path.join(__dirname, 'public'))) app.use("/api/v1", Router);

app.get("/", (req, res) => { res.send("Welcome to Freightly App server."); });

app.listen(port, () => { console.log(listening at ${port}); });

Pimm commented 12 months ago

You could change this line:

app.use("/api/v1", Router);

to this:

app.use("/api/v1", express.urlencoded(), Router);

This will decode URL-encoded bodies in your /api/v1 router ‒ including your webhook. See expressjs.com/en/api.html#express.urlencoded.