我正在开发一个与 Mollie 支付网关集成的 Node.js 应用程序。我遇到一个问题,在尝试使用 Mollie API 创建动态价格付款时,收到错误“您的网站配置文件中未启用付款方式”。但是,当我切换到使用静态价格时,一切正常。 在我的应用程序中,用户可以输入价格值,我在创建付款时将此值传递给 Mollie API。这是代码示例:
// 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');
}
});
// ...
当我使用静态价格值测试应用程序时,付款创建成功。但是,当我切换到使用从用户输入获取的动态价格值时,我收到“您的网站配置文件中未启用付款方式”错误。
我已验证 Mollie 网站配置文件已启用所有必要的付款方式,并且 API 密钥具有所需的权限。此外,我已确认动态价格值已正确接收并传递至 Mollie API。
我不确定为什么错误只发生在动态价格而不是静态价格上。在 Mollie 中使用付款金额的动态值时,我需要考虑什么具体事项吗?
您需要以正确的格式设置您的价格 TotalPrice.toFixed(2).toString() 这是我的带有 mongoDB 流程的 mollie 的 Node js 示例
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
});
});