<?php
/**
 * Created by PhpStorm.
 * User: varun_2
 * Date: 23-01-2016
 * Time: 14:15
 */

namespace App\Http\Controllers;
use App\Http\Controllers\Action\ccavenueValidator;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;


class PaymentCcavenueController extends Controller
{

    public function getRSA( Request $request )
    {
        $url = "https://secure.ccavenue.com/transaction/getRSAKey";
        $fields = array(
            'access_code'=>$_ENV['CCAVENUE_ACCESS_CODE'],
            'order_id'=>$request->input('order_id')
        );

        $cert_path = $_ENV['CCAVENUE_CACERT_PATH'] ;

        if (!File::exists($cert_path))
            return new Response("Cert file not exits");

        $postvars='';
        $sep='';
        foreach($fields as $key=>$value)
        {
            $postvars.= $sep.urlencode($key).'='.urlencode($value);
            $sep='&';
        }

        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POST,count($fields));
        curl_setopt($ch, CURLOPT_CAINFO, $_ENV['CACERT_PATH']);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
//      curl_setopt($ch, CURLOPT_HEADER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE );

        $response = curl_exec($ch);
        $header = substr($response, 0, $header_size);
        $body = substr($response, $header_size);

        echo $response ;
    }
}