-1

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)

3 Answers3

0

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.

CURLOPT_RETURNTRANSFER

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');
linktoahref
  • 7,812
  • 3
  • 29
  • 51
-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);

Redirect to the PayUmoney gateway

return Redirect::away($url)->withInput($request->input());

Or you may use:

header("Location: $url");
exit;
Wakil Ahmed
  • 1,053
  • 1
  • 8
  • 16
-1
<?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";
}
Hasan Raza
  • 424
  • 3
  • 9
  • What was wrong with the original code? What did you change? Explain this answer please. – miken32 Jul 18 '23 at 17:37
  • CURLOPT_RETURNTRANSFER is 0 which needs to be 1 where as 0 mean the curl_exec() funct will not return the rsponse of curl request – Hasan Raza Jul 19 '23 at 06:12
  • curl_exec($c) execute curl request using the function to trigger redirection to pay gateway – Hasan Raza Jul 19 '23 at 06:14
  • @HasanRaza payment page is loaded fine but after payment success and failure url are not working with your solution – anoopKumarSharma Jul 19 '23 at 06:17
  • [edit] your answer to provide a clear explanation of how it answers the question. @anoopKumarSharma if this doesn’t solve the problem why did you mark it accepted? – miken32 Jul 19 '23 at 13:08