267 lines
11 KiB
PHP
267 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Admin\faq;
|
|
use App\Models\Admin\country;
|
|
use App\Models\Admin\program;
|
|
use App\Models\Admin\ProgramCurrencyAmount;
|
|
use AmrShawky\LaravelCurrency\Facade\Currency;
|
|
use App\Models\Admin\program_accomadation;
|
|
use App\Models\Frontend\User;
|
|
use App\Models\Frontend\iam_principal_group;
|
|
use App\Models\Frontend\iam_principal_principal_group_link;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Models\Frontend\payment_transaction_master;
|
|
use Mollie\Laravel\Facades\Mollie;
|
|
use Carbon\Carbon;
|
|
use GuzzleHttp\Client;
|
|
use App\Mail\Frontend\GroupMemberEmail;
|
|
use App\Mail\Frontend\sendCancellationEmail;
|
|
use Mail;
|
|
|
|
class VolunteerCountriesController extends Controller {
|
|
|
|
public function index() {
|
|
$userIds = auth()->id();
|
|
|
|
if (session()->has('transaction_id')) {
|
|
$transaction_id = session('transaction_id');
|
|
|
|
$payment = payment_transaction_master::where('transaction_id', $transaction_id)->first();
|
|
|
|
if (!$payment) {
|
|
$payment_details = Mollie::api()->payments()->get($transaction_id);
|
|
if ($payment_details->isPaid()) {
|
|
// dd(auth()->user()->first_name);
|
|
$payment = new payment_transaction_master;
|
|
$payment->principal_xid = auth()->user()->id;
|
|
$payment->transaction_id = $payment_details->id;
|
|
$payment->amount = $payment_details->amount->value;
|
|
$payment->payment_status = 1;
|
|
$currency = '';
|
|
switch ($payment_details->amount->currency) {
|
|
case 'USD':
|
|
$currency = '1';
|
|
break;
|
|
case 'GBP':
|
|
$currency = '2';
|
|
break;
|
|
default:
|
|
$currency = '3';
|
|
break;
|
|
}
|
|
|
|
$payment->currency = $currency;
|
|
if (isset($payment_details->prog_start_date) && isset($payment_details->prog_end_date)) {
|
|
$payment->start_date = $payment_details->prog_start_date;
|
|
$payment->end_date = $payment_details->prog_end_date;
|
|
} else {
|
|
$payment->start_date = null;
|
|
$payment->end_date = null;
|
|
}
|
|
$payment->save();
|
|
if ($payment->id) {
|
|
session()->forget('transaction_id');
|
|
}
|
|
} elseif ($payment_details->isCanceled()) {
|
|
$user_data = auth()->user()->first_name;
|
|
Mail::to(auth()->user()->email)->send(new sendCancellationEmail($user_data));
|
|
return response()->json(['error' => 'Payment Failed']);
|
|
}
|
|
}
|
|
}
|
|
|
|
$countries = country::with('programs')->get()->toArray();
|
|
|
|
foreach ($countries as $key => $value) {
|
|
if (isset($value['programs']) && !empty($value['programs'])) {
|
|
$max_week = max(array_column($value['programs'], 'volunteer_weeks'));
|
|
$min_week = min(array_column($value['programs'], 'volunteer_weeks'));
|
|
$max_age = max(array_column($value['programs'], 'age_rules'));
|
|
|
|
$countries[$key]['max_min_value'] = $min_week . '-' . $max_week;
|
|
$countries[$key]['max_age'] = $max_age;
|
|
}
|
|
}
|
|
|
|
$latestFaqs = FAQ::latest()->limit(5)->get();
|
|
return view('Frontend.Pages.Volunteer.volunteer_countries')->with([
|
|
'faqs' => $latestFaqs,
|
|
'country' => $countries,
|
|
]);
|
|
}
|
|
|
|
public function clear(Request $request) {
|
|
$request->session()->forget('transaction_id'); // Replace 'key' with the actual session key you want to clear
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
public function view_country_program($id) {
|
|
$view_programs = country::with('programs')
|
|
->where('id', $id)
|
|
->get()
|
|
->toArray();
|
|
// dd($view_programs);
|
|
return view('Frontend.Pages.Volunteer.volunteer_countries_program')->with(['view_program' => $view_programs]);
|
|
}
|
|
|
|
public function view_country_program_detail($id) {
|
|
$view_programs_detail = program::with('programe_amount', 'programe_country', 'programe_acomadation')
|
|
->find($id)
|
|
->toArray();
|
|
// dd($view_programs_detail);
|
|
return view('Frontend.Pages.Volunteer.volunteer_countries_program_detail')->with(['view_programs_detail' => $view_programs_detail]);
|
|
}
|
|
|
|
public function fetch_progm_currency_amount(Request $request) {
|
|
$durationId = $request->input('duration_id');
|
|
$programId = $request->input('program_id');
|
|
$convert_to = $request->input('convert_to');
|
|
$start_date = $request->input('start_date');
|
|
$end_date = $request->input('end_date');
|
|
$total_weeks = $request->input('total_week');
|
|
|
|
$currency = ProgramCurrencyAmount::where('week', $durationId)
|
|
->where('programs_xid', $programId)
|
|
->pluck('usd_price');
|
|
|
|
$usdPrice = $currency[0];
|
|
|
|
if ($convert_to == 1) {
|
|
$convert_name = 'USD';
|
|
} else if ($convert_to == 2) {
|
|
$convert_name = 'GBP';
|
|
} else {
|
|
$convert_name = 'EUR';
|
|
}
|
|
|
|
|
|
$fromCurrency = 'GBP';
|
|
$apiKey = config('services.exchangerates.api_key');
|
|
$baseUrl = 'https://api.apilayer.com/exchangerates_data_api/latest';
|
|
|
|
$client = new Client();
|
|
// $response = $client->get("{$baseUrl}?access_key={$apiKey}");
|
|
$response = \Http::get('https://api.freecurrencyapi.com/v1/latest?apikey=fca_live_IUpn22wjq5zhcvRqGix4k6FlOZKo9SOHSAobRtMd');
|
|
$data = json_decode($response->getBody(), true);
|
|
// echo "<pre>";
|
|
// print_r($data['data']);
|
|
// die;
|
|
|
|
$conversionRate = $data['data'][$convert_name] / $data['data'][$fromCurrency];
|
|
$convertedAmount = $usdPrice * $conversionRate;
|
|
|
|
return response()->json([
|
|
'usd_price' => $usdPrice,
|
|
'selected_currency' => $convert_to,
|
|
'converted_amount' => $convertedAmount,
|
|
'start_date' => $start_date,
|
|
'end_date' => $end_date,
|
|
'week' => $durationId,
|
|
'total_week' => $total_weeks
|
|
]);
|
|
// } catch (\Exception $e) {
|
|
// return response()->json(['error' => 'Currency conversion failed']);
|
|
// }
|
|
}
|
|
|
|
public function insert_volunteer_program_detail(Request $request) {
|
|
if (Auth::check()) {
|
|
$program_age = $request->program_age;
|
|
$user_age = Auth::user()->date_of_birth;
|
|
$userDateOfBirth = Carbon::parse($user_age);
|
|
$currentDate = Carbon::now();
|
|
$userAge = $userDateOfBirth->diffInYears($currentDate);
|
|
|
|
// Check if the user's age matches the program age
|
|
if ($userAge >= $program_age) {
|
|
$userId = Auth::user()->id;
|
|
$user_fname = Auth::user()->first_name;
|
|
$user_lname = Auth::user()->last_name;
|
|
$iam_principal_group = new iam_principal_group;
|
|
$iam_principal_group->programs_xid = $request->input('program_id');
|
|
$program = Program::find($request->input('program_id'));
|
|
$iam_principal_group->week = $request->input('select_duration');
|
|
$iam_principal_group->currency = $request->input('convert_to');
|
|
$iam_principal_group->volunteer_with = $request->input('volunteer');
|
|
$iam_principal_group->volunteer_group_count = $request->input('member');
|
|
$iam_principal_group->volunteer_group_name = $request->input('group_name');
|
|
$startDate = Carbon::createFromFormat('m/d/Y', $request->input('start_date'))->format('d-m-y');
|
|
$iam_principal_group->start_date = $startDate;
|
|
$iam_principal_group->group_member_name = json_encode($request->input('group_member_name'));
|
|
$iam_principal_group->group_member_email = json_encode($request->input('group_member_email'));
|
|
$iam_principal_group->save();
|
|
|
|
$MemberNames = $request->input('group_member_name');
|
|
$MemberEmails = $request->input('group_member_email');
|
|
$groupMemberEmails = is_array($MemberEmails) ? $MemberEmails : json_decode($MemberEmails, true);
|
|
|
|
Mail::to($groupMemberEmails)->send(new GroupMemberEmail($iam_principal_group,$program,$user_fname,$user_lname));
|
|
|
|
$iam_principal_group_link = new iam_principal_principal_group_link;
|
|
$iam_principal_group_link->principal_xid = $userId;
|
|
$iam_principal_group_link->principal_group_xid = $iam_principal_group->id;
|
|
$iam_principal_group_link->save();
|
|
|
|
return response()->json(['success' => true, 'status' => 200]);
|
|
} else {
|
|
// User's age does not match the program age
|
|
return response()->json(['error' => 'Your age does not match the program requirements.'], 401);
|
|
}
|
|
} else {
|
|
// The user is not logged in or authenticated
|
|
$userId = null;
|
|
return response()->json(['error' => 'Please register to continue using our services.'], 401);
|
|
}
|
|
}
|
|
|
|
public function volunteer_program_payment(Request $request) {
|
|
$payment = Mollie::api()->payments()->create([
|
|
'amount' => [
|
|
'currency' => 'USD',
|
|
'value' => '200.00',
|
|
],
|
|
'description' => 'Test payment',
|
|
'redirectUrl' => route('view_volunteer_countries'),
|
|
]);
|
|
|
|
session(['transaction_id' => $payment->id]);
|
|
return redirect($payment->getCheckoutUrl(), 303);
|
|
}
|
|
|
|
public function paymentStatus(Request $request) {
|
|
if (session()->has('transaction_id')) {
|
|
$transaction_id = session('transaction_id');
|
|
}
|
|
|
|
$payment = Mollie::api()->payments()->get($transaction_id);
|
|
|
|
// Process the payment status
|
|
if ($payment->isPaid()) {
|
|
// Payment is successful
|
|
// Do something here
|
|
} elseif ($payment->isOpen()) {
|
|
// Payment is still open
|
|
// Do something here
|
|
} elseif ($payment->isCanceled()) {
|
|
// Payment is canceled
|
|
// Do something here
|
|
}
|
|
|
|
return view('payment.status', compact('payment'));
|
|
}
|
|
|
|
public function check_username(Request $request) {
|
|
$email = $request->input('email');
|
|
// dd($email);
|
|
$exists = User::where('email', $email)
|
|
->exists();
|
|
return response()->json(['exists' => $exists]);
|
|
}
|
|
|
|
}
|