Laravel集成Cashfree支付网关时版本参数报错求助
[2024-03-26 17:19:44] local.DEBUG: Input data for payment initiation:
{"orderId":"inv004","orderAmount":"0963462492","customerName":"Manuel Portillo","customerEmail":"teodoro.almaraz8@rezult.org","customerPhone":"7365435505","returnUrl":"https://payment.remwhacktechnologies.com/payment/callback","version":"2021-05-21"}
[2024-03-26 17:19:45] local.ERROR: Error initiating payment: 400 -
{"message":"version value should be 2021-05-21 or 2022-01-01 or 2022-09-01 or 2023-08-01","code":"request_failed","type":"invalid_request_error"}
我在Laravel应用中集成Cashfree支付网关时,每次发起支付都会触发上述400错误。我已经尝试了错误提示里列出的所有版本值,但问题依然存在。以下是我用于支付初始化的控制器代码:
public function initiatePayment(Request $request) { try { // Perform validation on $request->input() if necessary $clientId = 'TEST10152050eee78259b07eef2a86fc05025101'; // Your Cashfree client ID $clientSecret = 'cfsk_ma_test_303cb4db3e6ad084b222959b7014db1a_0161753d'; // Your Cashfree client secret $orderId = 'inv004'; // Generate a unique order ID $orderAmount = $request->input('amount'); $customerName = $request->input('name'); $customerEmail = $request->input('email'); $customerPhone = $request->input('phone'); $returnUrl = 'https://payment.remwhacktechnologies.com/payment/callback'; // Ensure the return URL is correct $version = '2021-05-21'; // Set the correct version parameter // Log input data for debugging Log::debug('Input data for payment initiation:', [ 'orderId' => $orderId, 'orderAmount' => $orderAmount, 'customerName' => $customerName, 'customerEmail' => $customerEmail, 'customerPhone' => $customerPhone, 'returnUrl' => $returnUrl, 'version' => $version, ]); $response = Http::withHeaders([ 'X-Client-Id' => $clientId, 'X-Client-Secret' => $clientSecret, 'Content-Type' => 'application/json', 'Accept' => 'application/json', ])->post('https://sandbox.cashfree.com/pg/orders', [ 'orderId' => $orderId, 'orderAmount' => $orderAmount, 'customerName' => $customerName, 'customerEmail' => $customerEmail, 'customerPhone' => $customerPhone, 'returnUrl' => $returnUrl, 'version' => $version, // Include the version parameter ]); if ($response->successful()) { // Redirect user to payment gateway return redirect($response['paymentLink']); } else { // Log error response Log::error('Error initiating payment: '.$response->status().' - '.$response->body()); // Handle unsuccessful response return response()->json(['error' => 'Error initiating payment']); } } catch (RequestException $e) { // Log exception Log::error('Error initiating payment: '.$e->getCode().' - '.$e->getMessage()); // Handle request exception return response()->json(['error' => 'Error initiating payment']); } }
排查方向及解决方案
修正订单金额格式
从日志可见orderAmount是带前导零的字符串"0963462492",Cashfree要求金额为数字类型(整数或浮点数)。修改代码将金额转为数字:$orderAmount = (float)$request->input('amount'); // 若金额以分为单位,可改为(int)$request->input('amount')调整版本参数传递位置
Cashfree部分API版本要求version参数放在请求头而非请求体中,尝试修改请求代码:$response = Http::withHeaders([ 'X-Client-Id' => $clientId, 'X-Client-Secret' => $clientSecret, 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'x-api-version' => $version, // 移至请求头 ])->post('https://sandbox.cashfree.com/pg/orders', [ 'orderId' => $orderId, 'orderAmount' => $orderAmount, 'customerName' => $customerName, 'customerEmail' => $customerEmail, 'customerPhone' => $customerPhone, 'returnUrl' => $returnUrl, // 删除请求体中的version参数 ]);验证请求JSON格式
临时添加日志输出编码后的JSON,确认无格式异常:$payload = [ 'orderId' => $orderId, 'orderAmount' => $orderAmount, 'customerName' => $customerName, 'customerEmail' => $customerEmail, 'customerPhone' => $customerPhone, 'returnUrl' => $returnUrl, 'version' => $version, ]; Log::debug('Encoded request JSON:', ['json' => json_encode($payload)]);确认沙箱环境配置
检查clientId、clientSecret是否为沙箱环境有效凭证,确保未混用生产环境密钥,同时验证沙箱接口地址https://sandbox.cashfree.com/pg/orders正确。
内容的提问来源于stack exchange,提问作者Harish Sharma

