0

I already read this post and confirmed that I am sending a whole number 100 but for some reason I keep getting the error

I am receiving the charge amount from a POST like

$charge_amount = $_POST['charge_amount'];

then in my API I am sending it as

$request_body = array (
    "customer_id" => $customer->getId(),
    "customer_card_id" => $card->getId(),
    "amount_money" => array (
        "amount" => $charge_amount,
        "currency" => 'USD'
    ),
    "idempotency_key" => uniqid(),
);

but that didn't work. So I decided to change the value statically like so

$request_body = array (
    "customer_id" => $customer->getId(),
    "customer_card_id" => $card->getId(),
    "amount_money" => array (
        "amount" => 100,
        "currency" => 'USD'
    ),
    "idempotency_key" => uniqid(),
);

and that worked.

Why is the POST not working? I confirmed that the value of 100 is coming in through the post with echo $charge_amount. I even tried changing to "amount" => "$charge_amount", but that didn't work as well.

The error I am getting at the end of all this is

[HTTP/1.1 400 Bad Request] {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"EXPECTED_INTEGER","detail":"Expected an integer value.","field":"amount_money.amount"}]}
Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81

2 Answers2

2

You can check the data type of the variable in question using var_dump, if it isn't INT, cast it as such by either type juggling:

$my_string = (int) '100';

or using the function intval

$my_string = intval('100');
Script47
  • 14,230
  • 4
  • 45
  • 66
1

$_POST['charge_amount'] is going to be the string "100".

Cast to an integer by doing $charge_amount = (int)$_POST['charge_amount']; or $charge_amount = intval($_POST['charge_amount']); (the two are functionally identical, in this case)

ceejayoz
  • 176,543
  • 40
  • 303
  • 368