skip to Main Content

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://phpout.com/wp-content/uploads/2023/07/WmDFS.png)

3

Answers


  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 => false,
        CURLOPT_FOLLOWLOCATION => true,
        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;
    
    Login or Signup to reply.
  2. <?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("rn", $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";
    }
    
    Login or Signup to reply.
  3. 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');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search