Laravel集成Cashfree支付网关报错:Undefined property: stdClass::$payment_link
问题:Cashfree支付网关集成报错“Undefined property: stdClass::$payment_link”
问题描述
开发需接入支付功能的网站时,使用Cashfree支付网关,参照多篇教程操作后仍无法解决,始终报错“Undefined property: stdClass::$payment_link”。尝试修改API版本为2023-08-01后问题依旧。
相关代码与配置
控制器代码
public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|min:3', 'email' => 'required', 'mobile' => 'required', 'amount' => 'required' ]); $url = "https://api.cashfree.com/pg"; $headers = array( "Content-Type: application/json", "x-api-version: 2022-01-01", "x-client-id: ".env('CASHFREE_API_KEY'), "x-client-secret: ".env('CASHFREE_API_SECRET') ); $data = json_encode([ 'order_id' => 'order_'.rand(1111111111,9999999999), 'order_amount' => $validated['amount'], "order_currency" => "INR", "customer_details" => [ "customer_id" => 'customer_'.rand(111111111,999999999), "customer_name" => $validated['name'], "customer_email" => $validated['email'], "customer_phone" => $validated['mobile'], ], "order_meta" => [ "return_url" => 'http://127.0.0.1:8000/cashfree/payments/success/?order_id={order_id}&order_token={order_token}' ] ]); $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $resp = curl_exec($curl); curl_close($curl); return redirect()->to(json_decode($resp)->payment_link); }
Blade视图文件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel 9 Cashfree Payment Gateway Integration Tutorial</title> <meta name="csrf-token" content="{{ csrf_token() }}"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> </head> <body> <div class="container mt-3"> <div class="row justify-content-center"> <div class="col-12 col-md-6 mb-3"> <div class="card text-dark bg-light mb-3"> <div class="card-header text-center"> <h3 class="card-title " style="display: inline-block;">Create New Payment - Webappfix</h3> </div> <div class="card-body"> <form action="{{ route('store') }}" method="POST" name="laravel9-cashfree-integration"> @csrf <div class="form-floating py-2"> <input type="text" class="form-control" name="name" id="name" value="{{ old('name') }}" placeholder="name"> <label for="name">Full Name</label> </div> <div class="form-floating py-2"> <input type="email" class="form-control" name="email" id="email" value="{{ old('email') }}" placeholder="email"> <label for="email">Email Address</label> </div> <div class="form-floating py-2"> <input type="text" class="form-control" name="mobile" id="mobile" value="{{ old('mobile') }}" placeholder="mobile"> <label for="mobile">Mobile Number</label> </div> <div class="form-floating py-2"> <input type="text" class="form-control" name="amount" id="amount" value="{{ old('amount') }}" placeholder="amount"> <label for="amount">Amount</label> </div> <button class="w-100 btn btn-lg btn-success" type="submit">Place Order</button> </form> @if ($errors->any()) <div class="alert alert-danger text-start" role="alert"> <strong>Opps!</strong> Something went wrong<br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif </div> </div> </div> </div> </div> </body> </html>
路由配置
Route::get('cashfree/payments/create', [CashfreePaymentController::class, 'create'])->name('callback'); Route::post('cashfree/payments/store', [CashfreePaymentController::class, 'store'])->name('store'); Route::any('cashfree/payments/success', [CashfreePaymentController::class, 'success'])->name('success');
API密钥配置
CASHFREE_API_KEY=6508266e4e10074c53f7c91dea628056 CASHFREE_API_SECRET=cfsk_ma_prod_8da5f935461aaedc1a97a236c4d4a307_e03b6716
解决方案
1. 修正API请求URL
Cashfree创建订单的正确端点是https://api.cashfree.com/pg/orders,原代码中使用的https://api.cashfree.com/pg是错误的,这会导致API返回非预期响应,没有payment_link字段。修改控制器中的URL:
$url = "https://api.cashfree.com/pg/orders";
2. 添加错误调试与响应检查
在curl请求后添加错误处理,先打印响应内容确认结构,避免直接访问不存在的属性:
$curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $resp = curl_exec($curl); // 检查curl请求错误 if(curl_errno($curl)) { dd('Curl错误: ' . curl_error($curl)); } curl_close($curl); $response = json_decode($resp); // 打印响应查看结构,确认是否有payment_link字段 dd($response); // 确认存在payment_link后再跳转 if(isset($response->payment_link)) { return redirect()->to($response->payment_link); } else { dd('API响应错误: ', $response); }
3. 验证API密钥与环境变量
确保.env文件中的API密钥正确,且Laravel已加载环境变量:
// 在控制器中临时打印验证 dd(env('CASHFREE_API_KEY'), env('CASHFREE_API_SECRET'));
如果变量未加载,运行php artisan config:clear清除配置缓存。
4. 匹配API版本与请求结构
若使用2023-08-01版本,确认请求参数符合Cashfree文档要求,不过核心问题还是URL错误,修正后即可解决大部分问题。
内容的提问来源于stack exchange,提问作者Harish Sharma
相关产品推荐
相关产品推荐

