Payment method not enabled in Mollie website profile when using dynamic price in Node.js app

203 Views Asked by At

I'm working on a Node.js application that integrates with the Mollie payment gateway. I'm facing an issue where I receive the error "The payment method is not enabled in your website profile" when trying to create a payment with a dynamic price using the Mollie API. However, when I switch to using a static price, everything works fine. In my application, users can input a price value, and I pass this value to the Mollie API when creating a payment. Here's an example of the code:

// Relevant code snippet
// ...

app.post('/create-payment', async (req, res) => {
  try {
    const price = req.body.price; // Dynamic price value from user input
    const mollieClient = await createMollieClient({ apiKey: 'My_API' });

    const payment = await mollieClient.payments.create({
      amount: {
        currency: 'EUR',
        value: price,
      },
      // Other payment details
      // ...
    });

    res.json({ url: payment.getCheckoutUrl() });
  } catch (error) {
    console.error('Error creating Mollie payment:', error);
    res.status(500).send('Error creating payment');
  }
});

// ...

When I test the application with a static price value, the payment creation is successful. However, when I switch to using a dynamic price value obtained from user input, I receive the "The payment method is not enabled in your website profile" error.

I have verified that the Mollie website profile has all the necessary payment methods enabled, and the API key has the required permissions. Additionally, I have confirmed that the dynamic price value is correctly received and passed to the Mollie API.

I'm unsure why the error occurs only with dynamic prices and not with static prices. Is there anything specific I need to consider when using dynamic values for the payment amount in Mollie?

1

There are 1 best solutions below

0
Андрей Калиниченко On

You need to edit your price in correct format:

yourTotalPrice.toFixed(2).toString()

This is my node js example for mollie with mongoDB flow

const {createMollieClient} = require("@mollie/api-client");
var express = require('express');
require('dotenv').config();
var {MongoClient} = require('mongodb');
var app = express();
var cors = require('cors');
app.use(cors())
app.use(express.json())



const mollieClient = createMollieClient({apiKey: process.env.MOLLIE_API});
const client = new MongoClient(process.env.MONGO_API);
let payments

async function connect() {
    try {
        await client.connect()
        console.log('connected to mongo db')
        payments = client.db().collection('payments')

    } catch (error) {
        console.log(error)
    }
}

app.listen(3012, function () {
    console.log('API started')
    connect()
});


const getPaymentById = async (paymentId) => {
    return await payments.findOne({paymentId: paymentId})
}

const insertPayment = async (id, amount, metadata, mollieId) => {
    await payments.insertOne({paymentId: id, status: null, amount, metadata, mollieId})
}

const getNewPaymentId = async () => {
    const collectionLength = await payments.count();
    return collectionLength + 1;
}

const findAndUpdatePayment = async (paymentId, status) => {
    const filter = {paymentId: paymentId};
    const update = {status: status};

    await payments.findOneAndUpdate(filter, {"$set": update}, (err => {
        if (err) {
            console.log('ERR',err)
        }
        console.log('Updated')
    }))
}


app.post('/payment', async function (req, res) {
    const data = req.body
    console.log(data.data)
    const paymentId = await getNewPaymentId()
    mollieClient.payments
        .create({
            amount: data.data.amount,
            locale: "fr_FR",
            metadata: data.metadata,
            method: ["creditcard", "paypal", "ideal", "directdebit"],
            description: "My first API payment",
            redirectUrl: `http://localhost:8000/checkout?id=${paymentId}`,
        })
        .then(async payment => {
            await insertPayment(paymentId, data.amount, data.metadata, payment.id)
            res.send(payment.getCheckoutUrl())
        })
        .catch(error => {
            console.log("error.title => ", error.title);
            console.log(error);
            res.write(error.title);
        });
});

app.post('/webhook', async function (req, res) {
    const paymentId = Number(req.body.data.paymentId)
    const paymentData = await getPaymentById(paymentId)

    mollieClient.payments
        .get(paymentData.mollieId)
        .then(async payment => {
            await findAndUpdatePayment(paymentId, payment.status)

            res.send(payment.status)
        })
        .catch(error => {
            console.error(error);
            res.end(); //end the response
        });
});