I got this error when passing amount of 100 in number to create a payment. Any idea why please ?Thanks!
JsonError: amountMoney → amount: Expected bigint. Received 100.
…/backend/node_modules/square/api/resources/payments/client/Client.js:41:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
errors: [ { path: [Array], message: ‘Expected bigint. Received 100.’ } ]
}
and this is the config I used:
...
try {
const response = await squareClient.payments.create({
idempotencyKey: randomUUID(),
sourceId: appointmentId,
name: appointmentId,
amountMoney: {
currency: "CAD",
amount: 100,
},
});
console.log('result:::', response)
return response;
} catch (error) {
console.log(error);
}
...
I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:
Additional Documentation
Make your First API Call
Cash App Payments
Troubleshoot the Payments API
If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.
I tried to use a workaround I saw in this conversation Passing a variable as BigInt to create a new order - #3 by josh-square
BigInt.prototype.toJSON = function() {
return this.toString();
};
...
try {
const response = await squareClient.payments.create({
idempotencyKey: randomUUID(),
sourceId: appointmentId,
name: appointmentId,
amountMoney: {
currency: "CAD",
amount: BigInt(3000),
},
});
console.log('result:::', response)
return response;
} catch (error) {
console.log(error);
}
...
but still got another error as below
statusCode: 400,
body: { errors: [ [Object] ] },
errors: [
{
code: ‘EXPECTED_INTEGER’,
detail: ‘Expected an integer value (line 1, character 140)’,
field: ‘amount_money.amount’,
category: ‘INVALID_REQUEST_ERROR’
}
]
}
My version “square”: “^43.0.1”,
Appreciate if someone can help. Thanks!
The error message you’re encountering indicates that the amount field in the amountMoney object is expected to be a BigInt, but you are providing it as a regular number (100). In the Square Node.js SDK, monetary amounts should be specified as BigInt to ensure precision, especially for large numbers.
To resolve this issue, you need to convert the amount to a BigInt. You can do this by appending n to the number, like so: amount: 100n. This will ensure that the amount is treated as a BigInt, which should resolve the error.
This answer was reviewed by @Bryan-Square.