<?php

namespace App\Http\Controllers\Lamis;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\MembersRequest;
use App\Http\Requests\ContinueRequest;
use App\Http\Requests\PaidRequest;
use App\Http\Requests\RegistRequest;
use App\Http\Requests\loginRequest;
use App\Idea;
use App\PackageDescriptions;
use App\Field;
use App\Package;
use App\Payment;
use App\Project;
use App\Member;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    public function payddd($token){
       
      
####### Initiate Payment ######
$token =  "Tfwjij9tbcHVD95LUQfsOtbfcEEkw1hkDGvUbWPs9CscSxZOttanv3olA6U6f84tBCXX93GpEqkaP_wfxEyNawiqZRb3Bmflyt5Iq5wUoMfWgyHwrAe1jcpvJP6xRq3FOeH5y9yXuiDaAILALa0hrgJH5Jom4wukj6msz20F96Dg7qBFoxO6tB62SRCnvBHe3R-cKTlyLxFBd23iU9czobEAnbgNXRy0PmqWNohXWaqjtLZKiYY-Z2ncleraDSG5uHJsC5hJBmeIoVaV4fh5Ks5zVEnumLmUKKQQt8EssDxXOPk4r3r1x8Q7tvpswBaDyvafevRSltSCa9w7eg6zxBcb8sAGWgfH4PDvw7gfusqowCRnjf7OD45iOegk2iYSrSeDGDZMpgtIAzYVpQDXb_xTmg95eTKOrfS9Ovk69O7YU-wuH4cfdbuDPTQEIxlariyyq_T8caf1Qpd_XKuOaasKTcAPEVUPiAzMtkrts1QnIdTy1DYZqJpRKJ8xtAr5GG60IwQh2U_-u7EryEGYxU_CUkZkmTauw2WhZka4M0TiB3abGUJGnhDDOZQW2p0cltVROqZmUz5qGG_LVGleHU3-DgA46TtK8lph_F9PdKre5xqXe6c5IYVTk4e7yXd6irMNx4D4g1LxuD8HL4sYQkegF2xHbbN8sFy4VSLErkb9770-0af9LT29kzkva5fERMV90w"; #token value to be placed here;

/*
$basURL = "https://apitest.myfatoorah.com";

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "$basURL/v2/ExecutePayment",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"PaymentMethodId\":\"2\",\"CustomerName\": \"Ahmed\",\"DisplayCurrencyIso\": \"SAR\", \"MobileCountryCode\":\"+965\",\"CustomerMobile\": \"92249038\",\"CustomerEmail\": \"aramadan@myfatoorah.com\",\"InvoiceValue\": 100,\"CallBackUrl\": \"https://google.com\",\"ErrorUrl\": \"https://google.com\",\"Language\": \"en\",\"CustomerReference\" :\"ref 1\",\"CustomerCivilId\":12345678,\"UserDefinedField\": \"Custom field\",\"ExpireDate\": \"\",\"CustomerAddress\" :{\"Block\":\"\",\"Street\":\"\",\"HouseBuildingNo\":\"\",\"Address\":\"\",\"AddressInstructions\":\"\"},\"InvoiceItems\": [{\"ItemName\": \"Product 01\",\"Quantity\": 1,\"UnitPrice\": 100}]}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);


 $json  = json_decode((string)$response, true);
 //echo "json  json: $json '<br />'";
print_r($json);
dd();
$payment_url = $json["Data"]["PaymentURL"];

    # after getting the payment url call it as a post API and pass card info to it
    # if you saved the card info before you can pass the token for the api
    
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "$payment_url",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"paymentType\": \"card\",\"card\": {\"Number\":\"5123450000000008\",\"expiryMonth\":\"05\",\"expiryYear\":\"21\",\"securityCode\":\"100\"},\"saveToken\": false}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);
*/

/*

$apiKey = 'Tfwjij9tbcHVD95LUQfsOtbfcEEkw1hkDGvUbWPs9CscSxZOttanv3olA6U6f84tBCXX93GpEqkaP_wfxEyNawiqZRb3Bmflyt5Iq5wUoMfWgyHwrAe1jcpvJP6xRq3FOeH5y9yXuiDaAILALa0hrgJH5Jom4wukj6msz20F96Dg7qBFoxO6tB62SRCnvBHe3R-cKTlyLxFBd23iU9czobEAnbgNXRy0PmqWNohXWaqjtLZKiYY-Z2ncleraDSG5uHJsC5hJBmeIoVaV4fh5Ks5zVEnumLmUKKQQt8EssDxXOPk4r3r1x8Q7tvpswBaDyvafevRSltSCa9w7eg6zxBcb8sAGWgfH4PDvw7gfusqowCRnjf7OD45iOegk2iYSrSeDGDZMpgtIAzYVpQDXb_xTmg95eTKOrfS9Ovk69O7YU-wuH4cfdbuDPTQEIxlariyyq_T8caf1Qpd_XKuOaasKTcAPEVUPiAzMtkrts1QnIdTy1DYZqJpRKJ8xtAr5GG60IwQh2U_-u7EryEGYxU_CUkZkmTauw2WhZka4M0TiB3abGUJGnhDDOZQW2p0cltVROqZmUz5qGG_LVGleHU3-DgA46TtK8lph_F9PdKre5xqXe6c5IYVTk4e7yXd6irMNx4D4g1LxuD8HL4sYQkegF2xHbbN8sFy4VSLErkb9770-0af9LT29kzkva5fERMV90w';
$apiURL = 'https://apitest.myfatoorah.com';

//Live
//$apiKey = ''; //Live token value to be placed here: https://myfatoorah.readme.io/docs/live-token
//$apiURL = 'https://api.myfatoorah.com';

$paymentId = 284759;
//####### Prepare Data ######
$url    = "$apiURL/v2/getPaymentStatus";
$data   = array(
    'KeyType' => 'paymentId',
    'Key'     => "$paymentId" //the callback paymentID
);
$fields = json_encode($data);


//####### Call API ######
$curl = curl_init($url);

curl_setopt_array($curl, array(
    CURLOPT_CUSTOMREQUEST  => 'POST',
    CURLOPT_POSTFIELDS     => $fields,
    CURLOPT_HTTPHEADER     => array("Authorization: Bearer $apiKey", 'Content-Type: application/json'),
    CURLOPT_RETURNTRANSFER => true,
));

$res = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo "$res '<br />'";
}
*/








/*

$basURL = "https://apitest.myfatoorah.com";

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "$basURL/v2/ExecutePayment",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"PaymentMethodId\":\"2\",\"CustomerName\": \"Ahmed\",\"DisplayCurrencyIso\": \"SAR\", \"MobileCountryCode\":\"+965\",\"CustomerMobile\": \"92249038\",\"CustomerEmail\": \"aramadan@myfatoorah.com\",\"InvoiceValue\": 100,\"CallBackUrl\": \"https://google.com\",\"ErrorUrl\": \"https://google.com\",\"Language\": \"en\",\"CustomerReference\" :\"ref 1\",\"CustomerCivilId\":12345678,\"UserDefinedField\": \"Custom field\",\"ExpireDate\": \"\",\"CustomerAddress\" :{\"Block\":\"\",\"Street\":\"\",\"HouseBuildingNo\":\"\",\"Address\":\"\",\"AddressInstructions\":\"\"},\"InvoiceItems\": [{\"ItemName\": \"Product 01\",\"Quantity\": 1,\"UnitPrice\": 100}]}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);


 $json  = json_decode((string)$response, true);
 //echo "json  json: $json '<br />'";
print_r($json);
$payment_url = $json["Data"]["PaymentURL"];

    # after getting the payment url call it as a post API and pass card info to it
    # if you saved the card info before you can pass the token for the api
    
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "$payment_url",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"paymentType\": \"card\",\"card\": {\"Number\":\"5123450000000008\",\"expiryMonth\":\"05\",\"expiryYear\":\"21\",\"securityCode\":\"100\"},\"saveToken\": false}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$err = curl_error($curl);
060628508120093862
curl_close($curl);
*/






/*
$basURL = "https://apitest.myfatoorah.com";
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "$basURL/v2/InitiatePayment",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"InvoiceAmount\": 100,\"CurrencyIso\": \"SAR\"}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

$data = json_decode($response,true);
$datas = $data['Data']['PaymentMethods'][4];

####### Execute Payment ######
$cost= 500;
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "$basURL/v2/ExecutePayment",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"PaymentMethodId\":\"5\",\"CustomerName\": \"Ahmed\",\"DisplayCurrencyIso\": \"SAR\", \"MobileCountryCode\":\"+965\",\"CustomerMobile\": \"92249038\",\"CustomerEmail\": \"aramadan@myfatoorah.com\",\"InvoiceValue\": ".$cost.",\"CallBackUrl\": \"https://google.com\",\"ErrorUrl\": \"https://google.com\",\"Language\": \"en\",\"CustomerReference\" :\"ref 1\",\"CustomerCivilId\":12345678,\"UserDefinedField\": \"Custom field\",\"ExpireDate\": \"\",\"CustomerAddress\" :{\"Block\":\"\",\"Street\":\"\",\"HouseBuildingNo\":\"\",\"Address\":\"\",\"AddressInstructions\":\"\"},\"InvoiceItems\": [{\"ItemName\": \"Product 01\",\"Quantity\": 1,\"UnitPrice\": ".$cost."}]}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

print_r(json_decode($response,true));
dd();  
*/
$apiKey = 'Tfwjij9tbcHVD95LUQfsOtbfcEEkw1hkDGvUbWPs9CscSxZOttanv3olA6U6f84tBCXX93GpEqkaP_wfxEyNawiqZRb3Bmflyt5Iq5wUoMfWgyHwrAe1jcpvJP6xRq3FOeH5y9yXuiDaAILALa0hrgJH5Jom4wukj6msz20F96Dg7qBFoxO6tB62SRCnvBHe3R-cKTlyLxFBd23iU9czobEAnbgNXRy0PmqWNohXWaqjtLZKiYY-Z2ncleraDSG5uHJsC5hJBmeIoVaV4fh5Ks5zVEnumLmUKKQQt8EssDxXOPk4r3r1x8Q7tvpswBaDyvafevRSltSCa9w7eg6zxBcb8sAGWgfH4PDvw7gfusqowCRnjf7OD45iOegk2iYSrSeDGDZMpgtIAzYVpQDXb_xTmg95eTKOrfS9Ovk69O7YU-wuH4cfdbuDPTQEIxlariyyq_T8caf1Qpd_XKuOaasKTcAPEVUPiAzMtkrts1QnIdTy1DYZqJpRKJ8xtAr5GG60IwQh2U_-u7EryEGYxU_CUkZkmTauw2WhZka4M0TiB3abGUJGnhDDOZQW2p0cltVROqZmUz5qGG_LVGleHU3-DgA46TtK8lph_F9PdKre5xqXe6c5IYVTk4e7yXd6irMNx4D4g1LxuD8HL4sYQkegF2xHbbN8sFy4VSLErkb9770-0af9LT29kzkva5fERMV90w';
$apiURL = 'https://apitest.myfatoorah.com';

//Live
//$apiKey = ''; //Live token value to be placed here: https://myfatoorah.readme.io/docs/live-token
//$apiURL = 'https://api.myfatoorah.com';

$paymentId = '060628518320099763';
//####### Prepare Data ######
$url    = "$apiURL/v2/getPaymentStatus";
$data   = array(
    'KeyType' => 'paymentId',
    'Key'     => "$paymentId" //the callback paymentID
);
$fields = json_encode($data);


//####### Call API ######
$curl = curl_init($url);

curl_setopt_array($curl, array(
    CURLOPT_CUSTOMREQUEST  => 'POST',
    CURLOPT_POSTFIELDS     => $fields,
    CURLOPT_HTTPHEADER     => array("Authorization: Bearer $apiKey", 'Content-Type: application/json'),
    CURLOPT_RETURNTRANSFER => true,
));

$res = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
print_r($res);
    }
    public function regist()
    {
    	$ideas  = Idea::where('language_id',currentLanguage()->id)->get();
    	$fields = Field::where('language_id',currentLanguage()->id)->get();
    	$packages = Package::cursor();
    	
$token =  "Tfwjij9tbcHVD95LUQfsOtbfcEEkw1hkDGvUbWPs9CscSxZOttanv3olA6U6f84tBCXX93GpEqkaP_wfxEyNawiqZRb3Bmflyt5Iq5wUoMfWgyHwrAe1jcpvJP6xRq3FOeH5y9yXuiDaAILALa0hrgJH5Jom4wukj6msz20F96Dg7qBFoxO6tB62SRCnvBHe3R-cKTlyLxFBd23iU9czobEAnbgNXRy0PmqWNohXWaqjtLZKiYY-Z2ncleraDSG5uHJsC5hJBmeIoVaV4fh5Ks5zVEnumLmUKKQQt8EssDxXOPk4r3r1x8Q7tvpswBaDyvafevRSltSCa9w7eg6zxBcb8sAGWgfH4PDvw7gfusqowCRnjf7OD45iOegk2iYSrSeDGDZMpgtIAzYVpQDXb_xTmg95eTKOrfS9Ovk69O7YU-wuH4cfdbuDPTQEIxlariyyq_T8caf1Qpd_XKuOaasKTcAPEVUPiAzMtkrts1QnIdTy1DYZqJpRKJ8xtAr5GG60IwQh2U_-u7EryEGYxU_CUkZkmTauw2WhZka4M0TiB3abGUJGnhDDOZQW2p0cltVROqZmUz5qGG_LVGleHU3-DgA46TtK8lph_F9PdKre5xqXe6c5IYVTk4e7yXd6irMNx4D4g1LxuD8HL4sYQkegF2xHbbN8sFy4VSLErkb9770-0af9LT29kzkva5fERMV90w"; #token value to be placed here;
$basURL = "https://apitest.myfatoorah.com";
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "$basURL/v2/InitiatePayment",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"InvoiceAmount\": 100,\"CurrencyIso\": \"SAR\"}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$data = json_decode($response,true)['Data']['PaymentMethods'];

$ways = collect();
foreach($data as $row){
    if($row['PaymentMethodId'] == '2' ||$row['PaymentMethodId'] == '6'){
        $ways->push(['PaymentMethodId'=>$row['PaymentMethodId'],'PaymentMethodAr'=>$row['PaymentMethodAr'],'PaymentMethodEn'=>$row['PaymentMethodAr']]);
    }
}

    	return view('lamis.regist',['ideas'=>$ideas,'fields'=>$fields]);
    }
    
    
    
    
    public function pay()
    {
    	$ideas  = Idea::where('language_id',currentLanguage()->id)->get();
    	$fields = Field::where('language_id',currentLanguage()->id)->get();
    	$packages = Package::cursor();
$token =  "Tfwjij9tbcHVD95LUQfsOtbfcEEkw1hkDGvUbWPs9CscSxZOttanv3olA6U6f84tBCXX93GpEqkaP_wfxEyNawiqZRb3Bmflyt5Iq5wUoMfWgyHwrAe1jcpvJP6xRq3FOeH5y9yXuiDaAILALa0hrgJH5Jom4wukj6msz20F96Dg7qBFoxO6tB62SRCnvBHe3R-cKTlyLxFBd23iU9czobEAnbgNXRy0PmqWNohXWaqjtLZKiYY-Z2ncleraDSG5uHJsC5hJBmeIoVaV4fh5Ks5zVEnumLmUKKQQt8EssDxXOPk4r3r1x8Q7tvpswBaDyvafevRSltSCa9w7eg6zxBcb8sAGWgfH4PDvw7gfusqowCRnjf7OD45iOegk2iYSrSeDGDZMpgtIAzYVpQDXb_xTmg95eTKOrfS9Ovk69O7YU-wuH4cfdbuDPTQEIxlariyyq_T8caf1Qpd_XKuOaasKTcAPEVUPiAzMtkrts1QnIdTy1DYZqJpRKJ8xtAr5GG60IwQh2U_-u7EryEGYxU_CUkZkmTauw2WhZka4M0TiB3abGUJGnhDDOZQW2p0cltVROqZmUz5qGG_LVGleHU3-DgA46TtK8lph_F9PdKre5xqXe6c5IYVTk4e7yXd6irMNx4D4g1LxuD8HL4sYQkegF2xHbbN8sFy4VSLErkb9770-0af9LT29kzkva5fERMV90w"; #token value to be placed here;
$basURL = "https://apitest.myfatoorah.com";
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "$basURL/v2/InitiatePayment",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"InvoiceAmount\": 000,\"CurrencyIso\": \"SAR\"}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$data = json_decode($response,true)['Data']['PaymentMethods'];

$ways = collect();
foreach($data as $row){
    if($row['PaymentMethodId'] == '2' ||$row['PaymentMethodId'] == '6'){
        $ways->push(['PaymentMethodId'=>$row['PaymentMethodId'],'PaymentMethodAr'=>$row['PaymentMethodAr'],'PaymentMethodEn'=>$row['PaymentMethodAr']]);
    }
}

    	return view('lamis.paid',['ideas'=>$ideas,'fields'=>$fields,'packages'=>$packages,'ways'=>$ways]);
    }
    
    
    
    public function registting(MembersRequest $request)
    {
        $password = $request->password;
    	 $request->merge(['password' => bcrypt($request->password)]);
        $member= Member::create($request->all());
        
         if (!$request->hasFile('img')) {
        $member->img='user.png';
        $member->save();
}
else{
     $img=$request->file('img');
        $name=mt_rand(1000000, 9999999).time().'img-'.$img->getClientOriginalExtension();
        $img->move(public_path('/assets/lamis/images/member/'),$name);
        $member->img=$name;
        $member->save();
}
        if (Auth::guard('member')->attempt(['email' =>request('email') ,'password' => request('password') ]  )) {
            return redirect(getCurrentLocale().'/member');
        }
    }
    public function paids(MembersRequest $request){
        $project = Project::create([
            'member_id'=>$member->id,
            'package_id'=>$request->package,
            ]);
            $member->project_id = $project->id;
            $member->save();
            $username= $request->username;
            $email = $request->email;
        $token =  "Tfwjij9tbcHVD95LUQfsOtbfcEEkw1hkDGvUbWPs9CscSxZOttanv3olA6U6f84tBCXX93GpEqkaP_wfxEyNawiqZRb3Bmflyt5Iq5wUoMfWgyHwrAe1jcpvJP6xRq3FOeH5y9yXuiDaAILALa0hrgJH5Jom4wukj6msz20F96Dg7qBFoxO6tB62SRCnvBHe3R-cKTlyLxFBd23iU9czobEAnbgNXRy0PmqWNohXWaqjtLZKiYY-Z2ncleraDSG5uHJsC5hJBmeIoVaV4fh5Ks5zVEnumLmUKKQQt8EssDxXOPk4r3r1x8Q7tvpswBaDyvafevRSltSCa9w7eg6zxBcb8sAGWgfH4PDvw7gfusqowCRnjf7OD45iOegk2iYSrSeDGDZMpgtIAzYVpQDXb_xTmg95eTKOrfS9Ovk69O7YU-wuH4cfdbuDPTQEIxlariyyq_T8caf1Qpd_XKuOaasKTcAPEVUPiAzMtkrts1QnIdTy1DYZqJpRKJ8xtAr5GG60IwQh2U_-u7EryEGYxU_CUkZkmTauw2WhZka4M0TiB3abGUJGnhDDOZQW2p0cltVROqZmUz5qGG_LVGleHU3-DgA46TtK8lph_F9PdKre5xqXe6c5IYVTk4e7yXd6irMNx4D4g1LxuD8HL4sYQkegF2xHbbN8sFy4VSLErkb9770-0af9LT29kzkva5fERMV90w"; #token value to be placed here;
$package = Package::find($request->package);
$cost= $package->cost;
$phone = $request->phone;
$month = $request->month;
$year = $request->year;
$code = $request->code;
$credit = $request->credit_number;
$pay = $request->pay_type;
$packagedesc = PackageDescriptions::where('package_id',$request->package)->where('language_id',currentLanguage()['id'])->first();
$package_name = $packagedesc->name;
$basURL = "https://apitest.myfatoorah.com";




$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "$basURL/v2/ExecutePayment",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"PaymentMethodId\":\"".$pay."\",\"CustomerName\": \"".$username."\",\"DisplayCurrencyIso\": \"SAR\", \"MobileCountryCode\":\"+965\",\"CustomerMobile\": \"".$phone."\",\"CustomerEmail\": \"".$email."\",\"InvoiceValue\": ".$cost.",\"CallBackUrl\": \"https://google.com\",\"ErrorUrl\": \"https://google.com\",\"Language\": \"en\",\"CustomerReference\" :\"ref 1\",\"CustomerCivilId\":12345678,\"UserDefinedField\": \"Custom field\",\"ExpireDate\": \"\",\"CustomerAddress\" :{\"Block\":\"\",\"Street\":\"\",\"HouseBuildingNo\":\"\",\"Address\":\"\",\"AddressInstructions\":\"\"},\"InvoiceItems\": [{\"ItemName\": \"".$package_name."\",\"Quantity\": 1,\"UnitPrice\": $cost}]}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
    redirect(getCurrentLocale().'/');
}
if (Auth::guard('member')->attempt(['email' =>request('email') ,'password' => request('password') ]  )) {
 $json  = json_decode((string)$response, true);
 
 //echo "json  json: $json '<br />'";

$payment_url = $json["Data"]["PaymentURL"];

    # after getting the payment url call it as a post API and pass card info to it
    # if you saved the card info before you can pass the token for the api
    
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "$payment_url",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"paymentType\": \"card\",\"card\": {\"Number\":\"".$credit."\",\"expiryMonth\":\"".$month."\",\"expiryYear\":\"".$year."\",\"securityCode\":\"".$code."\"},\"saveToken\": false}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$data = json_decode((string)$response, true);
$paymentid = $data['Data']['PaymentId'];
Payment::create(['member_id'=>$member->id,'paymentid'=>$paymentid]);
$err = curl_error($curl);
if ($err) {
    redirect(getCurrentLocale().'/');
}
curl_close($curl);


             return redirect()->action('Lamis\AuthController@Second_auth')->with('message',__('site.paying'));
}
        
    }
    public function paid(){
       $payment_id = request()->paymentId;
        
        $user = \Auth::guard('member')->user();
        
        $member = Member::find($user->id);

       Payment::create([
           'member_id'=>$user->id,
           'paymentid'=>$payment_id
           ]);
          return redirect()->action('Lamis\AuthController@Second_auth');

    }
    
    public function paying(PaidRequest $request){
        $user = \Auth::guard('member')->user();
        
        $username= $user->username;
        $phone = $user->phone;
        $pay = $request->pay_type;

            $email = $user->email;
         $token =  "Tfwjij9tbcHVD95LUQfsOtbfcEEkw1hkDGvUbWPs9CscSxZOttanv3olA6U6f84tBCXX93GpEqkaP_wfxEyNawiqZRb3Bmflyt5Iq5wUoMfWgyHwrAe1jcpvJP6xRq3FOeH5y9yXuiDaAILALa0hrgJH5Jom4wukj6msz20F96Dg7qBFoxO6tB62SRCnvBHe3R-cKTlyLxFBd23iU9czobEAnbgNXRy0PmqWNohXWaqjtLZKiYY-Z2ncleraDSG5uHJsC5hJBmeIoVaV4fh5Ks5zVEnumLmUKKQQt8EssDxXOPk4r3r1x8Q7tvpswBaDyvafevRSltSCa9w7eg6zxBcb8sAGWgfH4PDvw7gfusqowCRnjf7OD45iOegk2iYSrSeDGDZMpgtIAzYVpQDXb_xTmg95eTKOrfS9Ovk69O7YU-wuH4cfdbuDPTQEIxlariyyq_T8caf1Qpd_XKuOaasKTcAPEVUPiAzMtkrts1QnIdTy1DYZqJpRKJ8xtAr5GG60IwQh2U_-u7EryEGYxU_CUkZkmTauw2WhZka4M0TiB3abGUJGnhDDOZQW2p0cltVROqZmUz5qGG_LVGleHU3-DgA46TtK8lph_F9PdKre5xqXe6c5IYVTk4e7yXd6irMNx4D4g1LxuD8HL4sYQkegF2xHbbN8sFy4VSLErkb9770-0af9LT29kzkva5fERMV90w"; #token value to be placed here;
$package = Package::find($request->package);
$cost= $package->cost;
$packagedesc = PackageDescriptions::where('package_id',$request->package)->where('language_id',currentLanguage()['id'])->first();
$package_name = $packagedesc->name;
$basURL = "https://apitest.myfatoorah.com";
$callback = 'http://omar.alyomhost.net/lamis/public/paid';

$month = $request->month;
$year = $request->year;
$code = $request->code;
$credit = $request->credit_number;

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "$basURL/v2/ExecutePayment",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"PaymentMethodId\":\"".$pay."\",\"CustomerName\": \"".$username."\",\"DisplayCurrencyIso\": \"SAR\", \"MobileCountryCode\":\"+965\",\"CustomerMobile\": \"".$phone."\",\"CustomerEmail\": \"".$email."\",\"InvoiceValue\": ".$cost.",\"CallBackUrl\": \"https://google.com\",\"ErrorUrl\": \"https://google.com\",\"Language\": \"en\",\"CustomerReference\" :\"ref 1\",\"CustomerCivilId\":12345678,\"UserDefinedField\": \"Custom field\",\"ExpireDate\": \"\",\"CustomerAddress\" :{\"Block\":\"\",\"Street\":\"\",\"HouseBuildingNo\":\"\",\"Address\":\"\",\"AddressInstructions\":\"\"},\"InvoiceItems\": [{\"ItemName\": \"".$package_name."\",\"Quantity\": 1,\"UnitPrice\": $cost}]}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
    redirect(getCurrentLocale().'/');
}

 $json  = json_decode((string)$response, true);
 
 //echo "json  json: $json '<br />'";

$payment_url = $json["Data"]["PaymentURL"];

    # after getting the payment url call it as a post API and pass card info to it
    # if you saved the card info before you can pass the token for the api
    
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "$payment_url",
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"paymentType\": \"card\",\"card\": {\"Number\":\"".$credit."\",\"expiryMonth\":\"".$month."\",\"expiryYear\":\"".$year."\",\"securityCode\":\"".$code."\"},\"saveToken\": false}",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token","Content-Type: application/json"),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$data = json_decode((string)$response, true);
$paymentid = $data['Data']['PaymentId'];
Payment::create(['member_id'=>$user->id,'paymentid'=>$paymentid]);
$err = curl_error($curl);

if ($err) {
    redirect(getCurrentLocale().'/');
}

curl_close($curl);


return redirect(getCurrentLocale().'/')->with('message',__('site.paying'));
    }
    
    
    public function continueauth(ContinueRequest $request){
        $user = \Auth::guard('member')->user();
        
        
        if($request->has('full_name'))
        {
            $user->full_name = $request->full_name;
        }
        
        if($request->has('job'))
        {
            $user->job = $request->job;
        }
        if($request->has('city'))
        {
            $user->city = $request->city;
        }
        if($request->has('gender'))
        {
            $user->gender = $request->gender;
        }
         if ($request->hasFile('img')) {
        $img=$request->file('img');
        $name=mt_rand(1000000, 9999999).time().'img-'.$img->getClientOriginalExtension();
        $img->move(public_path('/assets/lamis/images/member/'),$name);
        $user->img=$name;
}
        $user->save();
        return redirect()->action('Lamis\ProfileController@index');
    }
    public function Second_auth()
    {
        return view('lamis.continue_regist');
    }
    public function login()
    {
    	return view('lamis.login');
    }
    public function check(loginRequest $request)
    {
    	if (Auth::guard('member')->attempt(['email' =>request('email') ,'password' => request('password') ]  )) {
            return redirect()->action('Lamis\ProfileController@index');

        }
        else{

            return back()->with('error','تاكد من البيانات');
        }

    }
    public function logout()
    {
    	Auth::guard('member')->logout();
    	 return redirect()->action('Lamis\HomeController@index');
    }
}
