curl 在Stripe API中-出现错误,如缺少所需参数:amount

1l5u6lss  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(127)

Stripe API,尝试使用cURL在我的代码中进行intergate,但面临错误。我得到以下错误

{
    "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"
    }
}

字符串
代码:

$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);


我试了很多方法,它显示错误,如金额无效。
更新
我也得到了同样的错误使用 Postman


的数据
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"
}

gpnt7bae

gpnt7bae1#

您收到的错误表明您在向Stripe API的请求中为**“amount”**参数提供的值存在问题。您传递的值似乎不是有效的整数。
要修复此错误,您应该在发送请求之前确保$amount的值是一个有效的整数。您可以使用intval()函数将值显式转换为整数。以下是经过必要修改的代码更新版本:

$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);

字符串
通过在代码中使用intval($amount),$amount的值将被转换为整数,解决了您遇到的**“Invalid integer”**错误。请确保$amount的值确实是一个有效的整数,或者可以安全地转换为整数。

相关问题