英文:
Redirecting to payumoney in laravel using Curl
问题
我想使用curl从我的php服务器重定向到payumoney网关。
我正在使用laravel框架,不想使用html/blade方法触发付款网关。
$posted = array();
$posted['key'] = $MERCHANT_KEY;
$posted['hash'] = $hash;
$posted['txnid'] = $txnid;
$posted['firstname'] = $firstname;
$posted['email'] = $email;
$posted['phone'] = $phone;
$posted['amount'] = $amount;
$posted['productinfo'] = $productinfo;
$posted['surl'] = $surl;
$posted['furl'] = $furl;
ini_set('display_errors', 1);
$c = curl_init();
$url = "https://test.payu.in/_payment";
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $posted,
));
$response = curl_exec($c);
但是这段代码显示如下输出,因此无法完全重定向到网关(https://i.stack.imgur.com/WmDFS.png)。
英文:
I want to redirect to payumoney gateway from my php server using curl.
I am using laravel framework and i dont want to use html/blade method to trigger payment gateway.
$posted = array();
$posted['key'] = $MERCHANT_KEY;
$posted['hash'] = $hash;
$posted['txnid'] = $txnid;
$posted['firstname'] = $firstname;
$posted['email'] = $email;
$posted['phone'] = $phone;
$posted['amount'] = $amount;
$posted['productinfo'] = $productinfo;
$posted['surl'] = $surl;
$posted['furl'] = $furl;
ini_set('display_errors', 1);
$c = curl_init();
$url = "https://test.payu.in/_payment";
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $posted,
));
$response = curl_exec($c);
But this code is showing below output and thus doesn't redirect to gateway fully
(https://i.stack.imgur.com/WmDFS.png)
答案1
得分: 0
根据这个答案,您需要分析从CURL请求中获取的响应。
$response = curl_exec($c);
var_dump($response); // 检查响应内容
为了实现这一点,请设置
CURLOPT_RETURNTRANSFER => 1,
将其设置为true,以将curl_exec()的返回值作为字符串返回,而不是直接输出它。
根据响应的内容,您可以将用户重定向到PayUMoney支付网关。
if ($response === false) {
// 交易未创建,将用户导航到错误页面。
return redirect('payment-error');
}
// 交易已创建,一切正常。
return redirect('payment-page');
请注意,代码部分不进行翻译,仅返回已翻译的文本。
英文:
As per this answer, you would have to analyse the response you get from the CURL request.
$response = curl_exec($c);
var_dump($response); // Check the contents of response
In order to do that, please set the
CURLOPT_RETURNTRANSFER => 1,
> true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
Based on the response, you may redirect the user to PayUMoney gateway.
if ($response === false) {
// Transaction not created, navigate the user to the error page.
return redirect('payment-error');
}
// Transaction is created, everything is okay.
return redirect('payment-page');
答案2
得分: -1
以下是您要翻译的内容:
$posted = array(
'key' => $MERCHANT_KEY,
'hash' => $hash,
'txnid' => $txnid,
'firstname' => $firstname,
'email' => $email,
'phone' => $phone,
'amount' => $amount,
'productinfo' => $productinfo,
'surl' => $surl,
'furl' => $furl,
);
$url = "https://test.payu.in/_payment";
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($posted),
CURLOPT_HEADER => false,
));
curl_setopt($c, CURLOPT_VERBOSE, true);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($c);
curl_close($c);
// 重定向到 PayUmoney 网关
return Redirect::away($url)->withInput($request->input());
// 或者您可以使用:
// header("Location: $url");
// exit;
英文:
$posted = array(
'key' => $MERCHANT_KEY,
'hash' => $hash,
'txnid' => $txnid,
'firstname' => $firstname,
'email' => $email,
'phone' => $phone,
'amount' => $amount,
'productinfo' => $productinfo,
'surl' => $surl,
'furl' => $furl,
);
$url = "https://test.payu.in/_payment";
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($posted),
CURLOPT_HEADER => false,
));
curl_setopt($c, CURLOPT_VERBOSE, true);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($c);
curl_close($c);
Redirect to the PayUmoney gateway
return Redirect::away($url)->withInput($request->input());
Or you may use:
header("Location: $url");
exit;
答案3
得分: -1
$url = "https://test.payu.in/_payment";
$postdata = http_build_query($posted);
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HEADER => true,
));
$response = curl_exec($c);
// Close cURL session
curl_close($c);
$redirect_url = '';
$headers = explode("\r\n", $response);
foreach ($headers as $header) {
if (strpos($header, 'Location: ') === 0) {
$redirect_url = trim(substr($header, strlen('Location: ')));
break;
}
}
if (!empty($redirect_url)) {
header("Location: $redirect_url");
exit;
} else {
echo "Failed to get redirect URL";
}
英文:
<?php
$url = "https://test.payu.in/_payment";
$postdata = http_build_query($posted);
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HEADER => true,
));
$response = curl_exec($c);
// Close cURL session
curl_close($c);
$redirect_url = '';
$headers = explode("\r\n", $response);
foreach ($headers as $header) {
if (strpos($header, 'Location: ') === 0) {
$redirect_url = trim(substr($header, strlen('Location: ')));
break;
}
}
if (!empty($redirect_url)) {
header("Location: $redirect_url");
exit;
} else {
echo "Failed to get redirect URL";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论