phplaravelcurlpayment-gatewaypayumoney

Redirecting to payumoney in laravel using Curl


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.sstatic.net/WmDFS.png)


Solution

  • <?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";
    }