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

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


class PaymentController 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['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_SSL_VERIFYPEER, false);
// for check the error use this one in each curl_setopt function
        if(curl_exec($ch) === false)
        {
            echo 'Curl error: ' . curl_error($ch);
        }
        else
        {
            //echo "No Error";
        }
        $result = curl_exec($ch);

        return new Response($result);
    }

}