0

Stripe API, Trying to intergate in my code using cURL, But face the error. I am getting the following error

{
    "error": {
        "code": "parameter_invalid_integer",
        "doc_url": "https://stripe.com/docs/error-codes/parameter-invalid-integer",
        "message": "Invalid integer: <integer>",
        "param": "amount",
        "request_log_url": "https://dashboard.stripe.com/test/logs/req_r1VgKFZTeHcKu1?t=1686081517",
        "type": "invalid_request_error"
    }
}  

Code:

$token = "XXXXX";
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded",                                                                               
"Accept: application/json","Authorization: Bearer $token"));

$data = array(
    "amount"=> (int)$amount,
    "payment_method"=>$paymentId,
    "confirm"=> "true",
    "confirmation_method"=>"automatic",
    "currency"=> "dollor",
    "customer"=> $paymentMethod->customer                
);
$jsonPayload = json_encode($data);
curl_setopt($curl, CURLOPT_URL, "https://api.stripe.com/v1/payment_intents");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonPayload);           
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);            
$output = curl_exec($curl);           
curl_close($curl);

I tried many ways, it shows error like amount is Invalid Integer.


UPDATE

I'm also getting the same error using Postman

enter image description here

Output of var_dump($data)

array(6) { 
    ["amount"]=> int(5000)
    ["payment_method"]=> string(27) "pm_1NGHfRSHUnJwHMxFxRyvs03T"
    ["confirm"]=> string(4) "true"
    ["confirmation_method"]=> string(9) "automatic" 
    ["currency"]=> string(3) "USD"
    ["customer"]=> string(18) "cus_O22SRWfvFRfVvE"
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
Mukhila Asokan
  • 641
  • 3
  • 11
  • 29

1 Answers1

-2

The error you received indicates that there is an issue with the value you provided for the "amount" parameter in your request to the Stripe API. It seems that the value you passed is not a valid integer.

To fix this error, you should ensure that the value of $amount is a valid integer before sending the request. You can use the intval() function to convert the value to an integer explicitly. Here's an updated version of your code with the necessary modification:

$token = "XXXXX";
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/x-www-form-urlencoded",
    "Accept: application/json",
    "Authorization: Bearer $token"
));

$data = array(
    "amount" => intval($amount),
    "payment_method" => $paymentId,
    "confirm" => "true",
    "confirmation_method" => "automatic",
    "currency" => "dollor",
    "customer" => $paymentMethod->customer
);
$jsonPayload = json_encode($data);
curl_setopt($curl, CURLOPT_URL, "https://api.stripe.com/v1/payment_intents");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonPayload);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);

By using intval($amount) in the code, the value of $amount will be cast to an integer, resolving the "Invalid integer" error you encountered. Make sure that the value of $amount is indeed a valid integer or can be safely converted to one.

Aldan
  • 674
  • 9
  • 23
  • There is no real difference in casting the value with `(int)` or using the function `intval` - See [here](https://stackoverflow.com/questions/1912599/is-there-any-particular-difference-between-intval-and-casting-to-int-int-x) – DarkBee Jun 07 '23 at 05:42