Reputation: 11
In local environment it works well, but in production with even sandbox keys this error occurs. I am using PHP server SDK for creation of judopay payment using card.
Error: JudoPay ApiException (status code 400, error code 1, category 2) Sorry, we're unable to process your request. Please check your details and try again. Fields errors: field "Amount" (code 6): Sorry, but the amount submitted does not match the decimal place requirements for the currency submitted. Please amend the format to meet the currency requirements.
Here is the code of making payment:
$total_amount = $_SESSION['checkout_subtotal'];
$total_amount += $delivery;
// judopay payment creation
$payment = $judopay->getModel('Payment');
// Generate unique references
$consumerReference = generateGUID(); // Generate GUID for customer reference
$paymentReference = generatePaymentReference('pay-'); // Generate payment reference
$fixed_amount = (float) number_format($total_amount, 2, '.', '');
$payment->setAttributeValues(
array(
'judoId' => $judoId,
'yourConsumerReference' => $consumerReference,
'yourPaymentReference' => $paymentReference,
'amount' => $fixed_amount,
'currency' => 'GBP',
'cardNumber' => $cardNumber,
'expiryDate' => $expiryDate,
'cv2' => $cv2,
)
);
// judopay payment request creation and exceptions handling
try {
$response = $payment->create();
if ($response['result'] === 'Success') {
echo json_encode(['success' => true, 'response' => $response]);
} else {
echo json_encode(['success' => false, 'response' => $response, 'error' => 'Payment failed: There were some problems while processing your payment.']);
}
} catch (\Judopay\Exception\ApiException $apiException) {
echo json_encode([
'success' => false,
'error' => $apiException->getSummary()
]);
} catch (\Judopay\Exception\ValidationError $validationErrors) {
// Required attributes are missing from the request
echo json_encode([
'success' => false,
'error' => $validationErrors->getSummary()
]);
} catch (Exception $e) {
// Returning JSON response with success as false and error message
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}
I make sure the type of amount is float and toFixed(2) (7.98) GBP
But it didn't work.
Upvotes: 1
Views: 99
Reputation: 41
The error you're encountering indicates that the amount field does not meet the decimal place requirements for the specified currency. In PHP, it's crucial to ensure that the amount is formatted correctly as a string with exactly two decimal places before sending it to the Judopay API.
Here's how you can adjust your code:
$total_amount = $_SESSION['checkout_subtotal'];
$total_amount += $delivery;
// Format the amount to a string with two decimal places
$formatted_amount = number_format((float)$total_amount, 2, '.', '');
// Judopay payment creation
$payment = $judopay->getModel('Payment');
// Generate unique references
$consumerReference = generateGUID(); // Generate GUID for customer reference
$paymentReference = generatePaymentReference('pay-'); // Generate payment reference
$payment->setAttributeValues([
'judoId' => $judoId,
'yourConsumerReference' => $consumerReference,
'yourPaymentReference' => $paymentReference,
'amount' => $formatted_amount, // Use the formatted amount
'currency' => 'GBP',
'cardNumber' => $cardNumber,
'expiryDate' => $expiryDate,
'cv2' => $cv2,
]);
// Judopay payment request creation and exceptions handling
try {
$response = $payment->create();
if ($response['result'] === 'Success') {
echo json_encode(['success' => true, 'response' => $response]);
} else {
echo json_encode(['success' => false, 'response' => $response, 'error' => 'Payment failed: There were some problems while processing your payment.']);
}
} catch (\Judopay\Exception\ApiException $apiException) {
echo json_encode([
'success' => false,
'error' => $apiException->getSummary()
]);
} catch (\Judopay\Exception\ValidationError $validationErrors) {
// Required attributes are missing from the request
echo json_encode([
'success' => false,
'error' => $validationErrors->getSummary()
]);
} catch (Exception $e) {
// Returning JSON response with success as false and error message
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}
Key Points:
Formatting the Amount: The number_format function converts the total amount to a float and then formats it as a string with exactly two decimal places. This ensures that the amount adheres to the currency's decimal place requirements.
Data Type: Ensure that the amount is passed as a string to the Judopay API, as some APIs expect monetary values in string format to avoid floating-point precision issues.
By formatting the amount correctly, you should resolve the "field 'Amount' (code 6)" error and ensure compatibility with Judopay's API requirements.
Upvotes: 0