Файловый менеджер - Редактировать - /home/clickysoft/public_html/somni.clickysoft.net/app/Repositories/PaymentRepository.php
Назад
<?php namespace App\Repositories; use App\Interfaces\PaymentRepositoryInterface; use App\Models\User; use App\Models\RentPayment; use App\Models\Unit; use App\Models\Wallet; use App\Models\PropertyUnitTenant; use App\Models\Transaction; use App\Models\TransactionDetail; use Illuminate\Support\Facades\DB; use Stripe\Stripe; use Stripe\SetupIntent; use App\Events\RentPaymentEvent; use App\Events\SomniAnnualFeeEvent; use App\Events\OtherPaymentEvent; use App\Events\BankRentPaymentEvent; class PaymentRepository implements PaymentRepositoryInterface { public function getLastRentPaymentRecord($unit_id,$user_id){ $rentPayment = RentPayment::where('user_id', $user_id)->where('unit_id',$unit_id)->latest()->first(); return $rentPayment; } public function getBreakdownOfPayment($unit_id,$user_id, $type,$payment_through){ $breakdown = []; $unit = Unit::where('id', $unit_id)->first(); $first_time = $this->checkForFirstTime($unit,$user_id); if($first_time){ $rents = RentPayment::where('user_id', $user_id)->where('unit_id',$unit->id)->where('status','unpaid')->whereIn('payment_type',[ 'monthly_rent' , 'security_deposit' ,'annual_somni_fee','somni_first_transaction_fee'])->get(); foreach($rents as $rent){ $breakdown[$rent->payment_type] = $rent->amount_due; // if($rent->payment_type == 'security_deposit'){ // $breakdown['security_deposit'] = $rent->amount_due; // } // if($rent->payment_type == 'annual_somni_fee'){ // $breakdown['annual_somni_fee'] = $rent->amount_due; // } } $monthlyRent = $unit->monthly_rent ?? 0; $daysInMonth = now()->daysInMonth; $property_unit_tenant = PropertyUnitTenant::where('unit_id', $unit_id)->where('tenant_user_id', $user_id)->first(); // Calculate daily rent $dailyRent = $monthlyRent / $daysInMonth; $moveInDate = \Carbon\Carbon::parse($property_unit_tenant->from_date); $units_due_date = \Carbon\Carbon::parse($unit->rent_due_date); $unit_due_date_day = $units_due_date->day; $currentDate = now(); $desiredDate = $currentDate->copy()->day($unit_due_date_day); // Format the date as needed $formattedDate = $desiredDate->toDateString(); $DueformattedDate = \Carbon\Carbon::parse($formattedDate); // dd($moveInDate->startOfDay()->lte($DueformattedDate->startOfDay())); if($DueformattedDate->isCurrentMonth() && $moveInDate->startOfDay()->gte($DueformattedDate->startOfDay())){ if(($DueformattedDate->isCurrentMonth() && $moveInDate->isCurrentMonth()) && ($moveInDate->startOfDay()->eq($DueformattedDate->startOfDay()))){ $remainingDays = 0; }else{ if($moveInDate->startOfDay()->gt($DueformattedDate->startOfDay())){ $nextMonthRentDueDate = $DueformattedDate->copy()->addMonth(); $remainingDays = $moveInDate->diffInDays($nextMonthRentDueDate); }else{ $remainingDays = $units_due_date->diffInDays($moveInDate); } // dd($remainingDays); } }else{ // dd('outer else'); $remainingDays = $moveInDate->diffInDays($units_due_date); } // dd($units_due_date); $currentMonthRent = $remainingDays * $dailyRent; $breakdown['current_month_rent'] = $currentMonthRent; $breakdown['daily_rent'] = $dailyRent; $breakdown['total_days'] = $remainingDays; $breakdown['total_rent'] = $currentMonthRent + $breakdown['security_deposit'] + $breakdown['annual_somni_fee']; }else{ if($type == 'full_balance'){ $rents = RentPayment::where('user_id', $user_id)->where('unit_id',$unit->id)->where('status','unpaid')->whereIn('payment_type',[ 'monthly_rent' , 'security_deposit' ,'annual_somni_fee'])->get(); foreach($rents as $rent){ $breakdown[$rent->payment_type] = $rent->amount_due; } $breakdown['current_month_rent'] = $unit->monthly_rent; $amount = RentPayment::where('user_id',$user_id)->where('unit_id',$unit_id)->where('status','unpaid')->sum('amount_due'); $breakdown['total_rent'] = $amount; }else if($type == 'current_balance'){ $current_month = now()->month; $rent = RentPayment::where('user_id',$user_id)->where('unit_id',$unit_id)->where('status','unpaid')->where('month',$current_month)->first(); if($rent){ $breakdown['current_month_rent'] = $rent->amount_due; }else{ $breakdown['current_month_rent'] = $unit->monthly_rent; } $breakdown['total_rent'] = $breakdown['current_month_rent']; }else{ //Other Payment $breakdown['current_month_rent'] = 'N/A'; $breakdown['daily_rent'] = 'N/A'; $breakdown['total_days'] = 'N/A'; // $breakdown['total_rent'] = 'N/A'; } } if($payment_through == 'card'){ $subtracted_amount = \App\Helpers\Helper::getPercentageAmount(config('constants.payment.card_percentage'), $breakdown['total_rent'] ?? 0); $breakdown['transaction_fee'] = $subtracted_amount; $breakdown['total_rent'] += $subtracted_amount; }else{ $breakdown['transaction_fee'] = config('constants.payment.transaction_through_bank_fee'); $breakdown['total_rent'] += $breakdown['transaction_fee']; } // dd($breakdown); return $breakdown; } public function getCurrentMonthCalculatedRentByDays($unit_id,$user_id,$payment_through = null){ $unit = Unit::where('id', $unit_id)->first(); $monthlyRent = $unit->monthly_rent ?? 0; $daysInMonth = now()->daysInMonth; $first_time = $this->checkForFirstTime($unit,$user_id); if($first_time){ //First Time Calculations $property_unit_tenant = PropertyUnitTenant::where('unit_id', $unit_id)->where('tenant_user_id', $user_id)->first(); // Calculate daily rent $dailyRent = $monthlyRent / $daysInMonth; // // Determine move-in date (assuming $moveInDate is a tenant's move-in date) $moveInDate = \Carbon\Carbon::parse($property_unit_tenant->from_date); $units_due_date = \Carbon\Carbon::parse($unit->rent_due_date); $unit_due_date_day = $units_due_date->day; // $currentDate = now(); // $desiredDate = $currentDate->copy()->day($unit_due_date_day); // // Format the date as needed // $formattedDate = $desiredDate->toDateString(); // $remainingDays = $moveInDate->diffInDays($units_due_date); // // Calculate rent for the current month // $currentMonthRent = ($remainingDays + 1) * $dailyRent; $currentDate = now(); $desiredDate = $currentDate->copy()->day($unit_due_date_day); // Format the date as needed $formattedDate = $desiredDate->toDateString(); $DueformattedDate = \Carbon\Carbon::parse($formattedDate); // dd($moveInDate->startOfDay()->lte($DueformattedDate->startOfDay())); if($DueformattedDate->isCurrentMonth() && $moveInDate->startOfDay()->gte($DueformattedDate->startOfDay())){ if(($DueformattedDate->isCurrentMonth() && $moveInDate->isCurrentMonth()) && ($moveInDate->startOfDay()->eq($DueformattedDate->startOfDay()))){ $remainingDays = 0; }else{ // dd($units_due_date); if($moveInDate->startOfDay()->gt($DueformattedDate->startOfDay())){ $nextMonthRentDueDate = $DueformattedDate->copy()->addMonth(); $remainingDays = $moveInDate->diffInDays($nextMonthRentDueDate); }else{ $remainingDays = $units_due_date->diffInDays($moveInDate); } // dd('here ',$remainingDays); } }else{ // dd('outer else'); $remainingDays = $moveInDate->diffInDays($units_due_date); } // dd($remainingDays); $currentMonthRent = $remainingDays * $dailyRent; $r['currentMonthRent'] = $currentMonthRent + config('constants.payment.tenant_anual_fee') + $unit->security_deposit; if($payment_through != null){ if($payment_through == 'card'){ $amount_of_percentage = 0; $subtracted_amount = \App\Helpers\Helper::getPercentageAmount(config('constants.payment.card_percentage'), $r['currentMonthRent']); $r['currentMonthRent'] += $subtracted_amount; }else{ $r['currentMonthRent'] += config('constants.payment.transaction_through_bank_fee'); } } $r['totalRent'] = $r['currentMonthRent']; return $r; }else{ $total = 0; $amount = RentPayment::where('user_id',$user_id)->where('unit_id',$unit_id)->where('status','unpaid')->sum('amount_due'); $rent_next = RentPayment::where('user_id',$user_id)->where('unit_id',$unit_id)->where('status','unpaid')->latest()->first(); $r['currentMonthRent'] = ($rent_next) ? $unit->monthly_rent : 0; $r['totalRent'] = $amount ?? 0; return $r; } } public function checkForTransaction($user_id){ $transaction = Transaction::where('payer_id',$user_id)->count(); if($transaction <= 0){ return true; }else{ return false; } } public function captureTransaction($payment_type,$payment_amount = null, $payment_method_id, $unit_id, $user,$other_payment_note = null,$payment_through,$bank_intent = null, $setup_intent_id = null){ try { // dd($payment_amount); if($payment_through == 'bank'){ $secret = config('constants.stripe.secret_key'); \Stripe\Stripe::setApiKey($secret); } DB::beginTransaction(); $transaction_ids = []; $unit = Unit::where('id',$unit_id)->first(); $property = $unit->property; $property_owner = $property->user; $check = $this->checkForFirstTime($unit,$user->id); if($check && $payment_type != 'other_balance'){ $monthly_rent = RentPayment::where('user_id', $user->id)->where('unit_id',$unit->id)->where('status','unpaid')->where('payment_type','monthly_rent')->first(); $breakdown = $this->getBreakdownOfPayment($unit_id,$user->id, null,$payment_through); if($payment_method_id){ $user->updateDefaultPaymentMethod($payment_method_id); } $transaction_new['payer_id'] = $user->id; $transaction_new['payee_id'] = $property_owner->id; $transaction_new['amount'] = $payment_amount; $transaction_new['status'] = ($payment_through == "bank") ? 'pending' : 'paid'; $main_transaction = Transaction::create($transaction_new); $date = $monthly_rent->month.', '.$monthly_rent->year; // if($payload['code'] == 200){ $transaction['transaction_id'] = $main_transaction->id; $transaction['unit_id'] = $unit->id; $transaction['amount'] = $breakdown['current_month_rent']; $transaction['note'] = 'Monthly Rent For '.$date; $transaction['payload'] = ''; $transaction['rent_payment_id'] = $monthly_rent->id; $transaction['payment_month'] = $monthly_rent->month; $transaction['payment_year'] = $monthly_rent->year; $transaction['payment_type'] = 'monthly_rent'; $transaction['created_at'] = now(); $trans = TransactionDetail::create($transaction); if($trans){ array_push($transaction_ids,$trans->id); //Update the rent_payments record if($payment_through != "bank"){ RentPayment::where('id',$monthly_rent->id)->where('payment_type','monthly_rent')->update(['status' => 'paid', 'amount_paid' => $breakdown['current_month_rent'] ]); } } // $amount_to_wallet = 0; // $subtracted_amount = \App\Helpers\Helper::getPercentageAmount(config('constants.payment.card_percentage'), $monthly_rent->amount_due); // $amount_to_wallet = $monthly_rent->amount_due - $subtracted_amount; if($payment_through != "bank"){ Wallet::where('user_id',$property_owner->id)->increment('balance', (double)$breakdown['current_month_rent']); } $security_deposit = RentPayment::where('user_id', $user->id)->where('unit_id',$unit->id)->where('status','unpaid')->where('payment_type','security_deposit')->first(); $transaction_security_deposit['transaction_id'] = $main_transaction->id; $transaction_security_deposit['unit_id'] = $unit->id; $transaction_security_deposit['amount'] = $security_deposit->amount_due; $transaction_security_deposit['note'] = 'Unit Security Deposit'; $transaction_security_deposit['payload'] = ''; $transaction_security_deposit['rent_payment_id'] = $security_deposit->id; $transaction_security_deposit['payment_month'] = $security_deposit->month; $transaction_security_deposit['payment_year'] = $security_deposit->year; $transaction_security_deposit['payment_type'] = 'security_deposit'; $transaction_security_deposit['created_at'] = now(); $trans = TransactionDetail::create($transaction_security_deposit); if($trans){ //Update the rent_payments record array_push($transaction_ids,$trans->id); if($payment_through != "bank"){ RentPayment::where('id',$security_deposit->id)->where('payment_type','security_deposit')->update(['status' => 'paid', 'amount_paid' => $security_deposit->amount_due ]); } } if($payment_through != "bank"){ Wallet::where('user_id',$property_owner->id)->increment('balance', (double)$security_deposit->amount_due); } $annual_somni_fee = RentPayment::where('user_id', $user->id)->where('unit_id',$unit->id)->where('status','unpaid')->where('payment_type','annual_somni_fee')->first(); $transaction_annual_somni_fee['transaction_id'] = $main_transaction->id; $transaction_annual_somni_fee['unit_id'] = $unit->id; $transaction_annual_somni_fee['amount'] = $annual_somni_fee->amount_due; $transaction_annual_somni_fee['note'] = 'Annual Somni Fee'; $transaction_annual_somni_fee['payload'] = ''; $transaction_annual_somni_fee['rent_payment_id'] = $annual_somni_fee->id; $transaction_annual_somni_fee['payment_month'] = $annual_somni_fee->month; $transaction_annual_somni_fee['payment_year'] = $annual_somni_fee->year; $transaction_annual_somni_fee['payment_type'] = 'annual_somni_fee'; $transaction_annual_somni_fee['created_at'] = now(); $trans = TransactionDetail::create($transaction_annual_somni_fee); if($trans){ array_push($transaction_ids,$trans->id); //Update the rent_payments record if($payment_through != "bank"){ RentPayment::where('id',$annual_somni_fee->id)->where('payment_type','annual_somni_fee')->update(['status' => 'paid', 'amount_paid' => $annual_somni_fee->amount_due ]); } } if($payment_through != "bank"){ Wallet::where('user_id', 1)->increment('balance', (double)$payment_amount); // SOMNI/ADMIN Wallet } $transaction_charges = $this->makeTransactionCharges($user,$property_owner,$payment_amount,$payment_through,$main_transaction->id); //Create Next Months RentPayment Record. // $last_rent = RentPayment::where('user_id', $user->id)->where('unit_id',$unit->id)->where('status','paid')->latest()->first(); // if($last_rent){ // $new_rent = $this->createNextRentPayment($monthly_rent,$unit,$user); // } if($payment_through != "bank"){ $payload = $this->stripePayment($payment_type,$payment_amount,$payment_method_id, $user,null,$monthly_rent,$property_owner); TransactionDetail::whereIn('id', $transaction_ids)->update(['payload' => $payload['message']]); $wallet_amount = (double)$security_deposit->amount_due + (double)$breakdown['current_month_rent']; event(new RentPaymentEvent($property_owner,$user,$unit,$date,$wallet_amount)); event(new SomniAnnualFeeEvent($user,$annual_somni_fee->amount_due)); if($payload['code'] != 200){ DB::rollback(); return $payload; } }else{ $bank_intent_decode = json_decode($bank_intent); $cust = \Stripe\SetupIntent::retrieve($setup_intent_id); $payment_method = \Stripe\PaymentMethod::retrieve($bank_intent_decode->payment_method); // dd($cust->customer); $payment_method->attach(['customer' => $cust->customer]); $intent = \Stripe\PaymentIntent::create([ 'payment_method_types' => ['us_bank_account'], 'payment_method' => $bank_intent_decode->payment_method, 'customer' => $cust->customer, 'confirm' => true, 'amount' => (number_format($payment_amount,2)) * 100, 'currency' => 'usd', ]); TransactionDetail::whereIn('id', $transaction_ids)->update(['payload' => json_encode($intent)]); \Log::info('This is Bank Intent that is returned', [$intent]); event(new BankRentPaymentEvent($user,$unit,$payment_amount)); } }else{ switch ($payment_type) { case 'full_balance': // $complete_payment_amount = 0; $rents = RentPayment::where('user_id', $user->id)->where('unit_id',$unit->id)->where('status','unpaid')->get(); $transaction_new['payer_id'] = $user->id; $transaction_new['payee_id'] = $property_owner->id; $transaction_new['amount'] = $payment_amount; $transaction_new['status'] = ($payment_through == "bank") ? 'pending' : 'paid'; $main_transaction = Transaction::create($transaction_new); foreach($rents as $rent){ $date = $rent->month.', '.$rent->year; $transaction['transaction_id'] = $main_transaction->id; $transaction['unit_id'] = $unit->id; $transaction['amount'] = $rent->amount_due; $transaction['note'] = 'Monthly Rent For '.$date; $transaction['payload'] = ''; $transaction['rent_payment_id'] = $rent->id; $transaction['payment_month'] = $rent->month; $transaction['payment_year'] = $rent->year; $transaction['payment_type'] = $rent->payment_type; $transaction['created_at'] = now(); $trans = TransactionDetail::create($transaction); if($trans){ array_push($transaction_ids,$trans->id); //Update the rent_payments record if($payment_through != "bank"){ RentPayment::where('id',$rent->id)->where('payment_type',$rent->payment_type)->update(['status' => 'paid', 'amount_paid' => $rent->amount_due ]); } } if($rent->payment_type != 'security_deposit' || $rent->payment_type != 'annual_somni_fee'){ if($payment_through != "bank"){ Wallet::where('user_id', $property_owner->id)->increment('balance', $rent->amount_due); // SOMNI/ADMIN Wallet event(new RentPaymentEvent($property_owner,$user,$unit,$date,$rent->amount_due)); } } // $complete_payment_amount += $rent->amount_due; } $last_rent = RentPayment::where('user_id', $user->id)->where('unit_id',$unit->id)->where('status','paid')->latest()->first(); // if($last_rent){ // $new_rent = $this->createNextRentPayment($last_rent,$unit,$user); // } $transaction_charges = $this->makeTransactionCharges($user,$property_owner,$payment_amount,$payment_through,$main_transaction->id); if($payment_through != "bank"){ $payload = $this->stripePayment($rent->payment_type,$payment_amount ,$payment_method_id, $user,null,$rent,$property_owner); if($payload['code'] != 200){ DB::rollback(); return $payload; } TransactionDetail::whereIn('id', $transaction_ids)->update(['payload' => $payload['message']]); }else{ $bank_intent_decode = json_decode($bank_intent); $cust = \Stripe\SetupIntent::retrieve($setup_intent_id); $payment_method = \Stripe\PaymentMethod::retrieve($bank_intent_decode->payment_method); // dd($cust->customer); $payment_method->attach(['customer' => $cust->customer]); $intent = \Stripe\PaymentIntent::create([ 'payment_method_types' => ['us_bank_account'], 'payment_method' => $bank_intent_decode->payment_method, 'customer' => $cust->customer, 'confirm' => true, 'amount' => (number_format($payment_amount,2)) * 100, 'currency' => 'usd', ]); event(new BankRentPaymentEvent($user,$unit,$payment_amount)); } break; case 'current_balance': $current_month = now()->month; $current_month_year = now()->year; $current_month_rent = RentPayment::where('user_id', $user->id)->where('unit_id',$unit->id)->where('status','unpaid')->where('month',$current_month)->where('year',$current_month_year)->first(); if(!$current_month_rent){ DB::rollback(); return [ 'code' => 404, 'message' => 'Error, Not found' ]; } $date = $current_month_rent->month.', '.$current_month_rent->year; $transaction_new['payer_id'] = $user->id; $transaction_new['payee_id'] = $property_owner->id; $transaction_new['amount'] = $current_month_rent->amount_due; $transaction_new['status'] = ($payment_through == "bank") ? 'pending' : 'paid'; $main_transaction = Transaction::create($transaction_new); $transaction['transaction_id'] = $main_transaction->id; $transaction['unit_id'] = $unit->id; $transaction['amount'] = $current_month_rent->amount_due; $transaction['note'] = 'Monthly Rent For '.$date; $transaction['payload'] = ''; $transaction['rent_payment_id'] = $current_month_rent->id; $transaction['payment_month'] = $current_month_rent->month; $transaction['payment_year'] = $current_month_rent->year; $transaction['payment_type'] = $current_month_rent->payment_type; $transaction['created_at'] = now(); $trans = TransactionDetail::create($transaction); if($trans){ //Update the rent_payments record if($payment_through != "bank"){ RentPayment::where('id',$current_month_rent->id)->where('payment_type',$current_month_rent->payment_type)->update(['status' => 'paid', 'amount_paid' => $current_month_rent->amount_due ]); } } if($current_month_rent->payment_type != 'security_deposit' || $current_month_rent->payment_type != 'annual_somni_fee'){ if($payment_through != "bank"){ Wallet::where('user_id', $property_owner->id)->increment('balance', $current_month_rent->amount_due); // SOMNI/ADMIN Wallet event(new RentPaymentEvent($property_owner,$user,$unit,$date,$current_month_rent->amount_due)); } } //Create Next Months RentPayment Record. $last_rent = RentPayment::where('user_id', $user->id)->where('unit_id',$unit->id)->where('status','paid')->latest()->first(); if($last_rent){ // $new_rent = $this->createNextRentPayment($last_rent,$unit,$user); } $transaction_charges = $this->makeTransactionCharges($user,$property_owner,$payment_amount,$payment_through,$main_transaction->id); if($payment_through != "bank"){ $current_month_rent->amount_due += config('constants.payment.card_percentage'); $payload = $this->stripePayment($current_month_rent->payment_type,$current_month_rent->amount_due ,$payment_method_id, $user,null,$current_month_rent,$property_owner); if($payload['code'] != 200){ DB::rollback(); return $payload; } TransactionDetail::where('id', $trans->id)->update(['payload' => $payload['message']]); }else{ $bank_intent_decode = json_decode($bank_intent); $cust = \Stripe\SetupIntent::retrieve($setup_intent_id); $payment_method = \Stripe\PaymentMethod::retrieve($bank_intent_decode->payment_method); // dd($cust->customer); $payment_method->attach(['customer' => $cust->customer]); $intent = \Stripe\PaymentIntent::create([ 'payment_method_types' => ['us_bank_account'], 'payment_method' => $bank_intent_decode->payment_method, 'customer' => $cust->customer, 'confirm' => true, 'amount' => (number_format($payment_amount,2)) * 100, 'currency' => 'usd', ]); event(new BankRentPaymentEvent($user,$unit,$payment_amount)); } break; case 'other_balance': $transaction_new['payer_id'] = $user->id; $transaction_new['payee_id'] = $property_owner->id; $transaction_new['amount'] = $payment_amount; $transaction_new['status'] = ($payment_through == "bank") ? 'pending' : 'paid'; $main_transaction = Transaction::create($transaction_new); $transaction_other_payment['transaction_id'] = $main_transaction->id; $transaction_other_payment['unit_id'] = $unit->id; $transaction_other_payment['amount'] = $payment_amount; $transaction_other_payment['note'] = $other_payment_note; $transaction_other_payment['payload'] = ''; $transaction_other_payment['rent_payment_id'] = null; $transaction_other_payment['payment_month'] = now()->month; $transaction_other_payment['payment_year'] = now()->year; $transaction_other_payment['payment_type'] = 'other_payment'; $transaction_other_payment['created_at'] = now(); $trans = TransactionDetail::create($transaction_other_payment); $transaction_charges = $this->makeTransactionCharges($user,$property_owner,$payment_amount,$payment_through,$main_transaction->id); if($payment_through != "bank"){ event(new OtherPaymentEvent($property_owner,$user,$payment_amount)); // dd($trans); Wallet::where('user_id',$property_owner->id)->increment('balance', (double)$payment_amount); // dd($other_payment_note); $payload = $this->stripePayment($payment_type,$payment_amount,$payment_method_id, $user,$other_payment_note); if($payload['code'] != 200){ DB::rollback(); return $payload; } }else{ $bank_intent_decode = json_decode($bank_intent); $cust = \Stripe\SetupIntent::retrieve($setup_intent_id); $paymentMethodId = $bank_intent_decode->payment_method; if (!$cust->payment_method) { // If not attached, attach it to the Customer $paymentMethod = \Stripe\PaymentMethod::retrieve($paymentMethodId); $paymentMethod->attach(['customer' => $cust->customer]); } $intent = \Stripe\PaymentIntent::create([ 'payment_method_types' => ['us_bank_account'], 'payment_method' => $bank_intent_decode->payment_method, 'customer' => $cust->customer, 'confirm' => true, 'amount' => (number_format($payment_amount,2)) * 100, 'currency' => 'usd', ]); event(new BankRentPaymentEvent($user,$unit,$payment_amount)); } break; } } DB::commit(); return [ 'code' => 200, 'message' => ($payment_through == "bank") ? 'Payment Processed' : 'Payment Done', 'amount' => $payment_amount, 'transaction_id' => $main_transaction->id, 'payment_through' => $payment_through, ]; } catch (CardException $e) { // Handle Stripe card exception DB::rollback(); return [ 'code' => 500, 'message' => $e ]; // echo "Card error: " . $e->getMessage(); } catch (ValidationException $e) { // Handle validation exception DB::rollback(); return [ 'code' => 500, 'message' => $e ]; // echo "Validation error: " . $e->getMessage(); } catch (\Exception $e) { // Handle other exceptions DB::rollback(); return [ 'code' => 500, 'message' => $e ]; // echo "An error occurred: " . $e->getMessage(); } } public function checkForFirstTime($unit,$user_id){ //Criteria for first time check is that the user should have unpiad monthly_rent, security_deposit & annual_somni_fee in rent_payments Table $rent_payments = RentPayment::where('user_id', $user_id)->where('unit_id',$unit->id)->where('status','unpaid')->whereIn('payment_type',[ 'monthly_rent' , 'security_deposit' ,'annual_somni_fee'])->get(); $current_month = now()->month; if(count($rent_payments) > 0){ $next_month_number = ($rent_payments[0]->month == 12) ? 1 : $rent_payments[0]->month + 1; }else{ $last_rent = RentPayment::where('user_id', $user_id)->where('unit_id',$unit->id)->where('status','paid')->where('payment_type','monthly_rent')->first(); $next_month_number = $last_rent->month; $next_month_number = ($next_month_number == 12) ? 1 : $next_month_number + 1; } $next_month = RentPayment::where('user_id', $user_id)->where('unit_id',$unit->id)->where('status','unpaid')->where('month' , $next_month_number )->whereIn('payment_type',[ 'monthly_rent'])->get(); if($rent_payments->count() > 0){//First Timer foreach($rent_payments as $payment ){ $due_date = \Carbon\Carbon::parse($unit->rent_due_date); if( ($current_month == $payment->month && $due_date->greaterThan(\Carbon\Carbon::parse($payment->created_at))) || $next_month->isEmpty()){ return true; } } // return true; } return false; } public function stripePayment($payment_type = null,$payment_amount,$payment_method_id = null, $user,$other_payment_note = null, $rent_object = null,$property_owner = null){ try { if($other_payment_note && $payment_type == 'other_balance'){ $payload = $user->charge((int)($payment_amount * 100), $payment_method_id,[ 'description' => $other_payment_note, ]); }else{ $payload = $user->charge((int)($payment_amount * 100), $payment_method_id,[ 'description' => 'Monthly Rent For '.$rent_object->month.', '.$rent_object->year, 'receipt_email' => $property_owner->email, ]); } return [ 'code' => 200, 'message' => json_encode($payload) ]; } catch (CardException $e) { // Handle Stripe card exception return [ 'code' => 500, 'message' => $e ]; } catch (ValidationException $e) { // Handle validation exception return [ 'code' => 500, 'message' => $e ]; } catch (\Exception $e) { // Handle other exceptions return [ 'code' => 500, 'message' => $e ]; } } public function createNextRentPayment($last_rent,$unit,$user){ $currentMonth = \Carbon\Carbon::now()->month; $currentYear = \Carbon\Carbon::now()->year; // Get the month and year of the last rent record $lastRentMonth = $last_rent->month; $lastRentYear = $last_rent->year; // Determine the next month and year $nextMonth = ($lastRentMonth % 12) + 1; // Wrap around to 1 if the last rent month is 12 $nextYear = ($lastRentMonth == 12) ? $lastRentYear + 1 : $lastRentYear; // Check if the last rent record is from the current or a previous month // if ($lastRentYear < $currentYear || ($lastRentYear == $currentYear && $lastRentMonth < $currentMonth)) { // Create a new RentPayment record for the next month // $newRentPayment = new RentPayment(); $new_rent_record['unit_id'] = $unit->id; $new_rent_record['user_id'] = $user->id; $new_rent_record['month'] = $nextMonth; $new_rent_record['year'] = $nextYear; $new_rent_record['amount_due'] = (double)$unit->monthly_rent; $new_rent_record['amount_paid'] = 0; $new_rent_record['status'] = 'unpaid'; // $new_rent_record['full_balance'] = 0; $new_rent_record['payment_type'] = 'monthly_rent'; $new_rent = RentPayment::create($new_rent_record); // dd($new_rent_record); return ($new_rent) ? $new_rent : false; // }else{ // $newRentPayment = new RentPayment(); // $newRentPayment->unit_id = $unit->id; // $newRentPayment->user_id = $user->id; // $newRentPayment->month = $nextMonth; // $newRentPayment->year = $nextYear; // $newRentPayment->amount_due = $unit->monthly_rent; // $newRentPayment->amount_paid = 0; // $newRentPayment->status = 'unpaid'; // $newRentPayment->full_balance = 0; // $newRentPayment->created_at = now(); // $newRentPayment->save(); // return $newRentPayment; // } // return false; } public function makeTransactionCharges($user,$property_owner,$payment_amount,$payment_through,$transaction_id = null){ if($payment_through == 'card'){ $transaction_charges['transaction_id'] = $transaction_id; $transaction_charges['amount'] = number_format( \App\Helpers\Helper::getPercentageAmount(config('constants.payment.card_percentage'),$payment_amount) ,2); $transaction_charges['note'] = 'Transaction Charges For Payment Through Card'; $transaction_charges['payload'] = ''; $transaction_charges['rent_payment_id'] = null; $transaction_charges['payment_month'] = now()->month; $transaction_charges['payment_year'] = now()->year; $transaction_charges['payment_type'] = 'card_transaction_charges'; $transaction_charges['created_at'] = now(); $trans = TransactionDetail::create($transaction_charges); }else{ $transaction_charges['transaction_id'] = $transaction_id; $transaction_charges['amount'] = number_format( config('constants.payment.transaction_through_bank_fee') ,2); $transaction_charges['note'] = 'Transaction Charges For Payment Through Bank'; $transaction_charges['payload'] = ''; $transaction_charges['rent_payment_id'] = null; $transaction_charges['payment_month'] = now()->month; $transaction_charges['payment_year'] = now()->year; $transaction_charges['payment_type'] = 'bank_transaction_charges'; $transaction_charges['created_at'] = now(); $trans = TransactionDetail::create($transaction_charges); } return $trans; } public function extraUnitPayment($user,$payment_method_id,$payment_amount,$transaction_charge_amount){ try { DB::beginTransaction(); $payload = $user->charge((int)($payment_amount * 100), $payment_method_id,[ 'description' => 'Extra Unit Charges', ]); $transaction_new['payer_id'] = $user->id; $transaction_new['payee_id'] = null; $transaction_new['amount'] = $payment_amount; $transaction_new['status'] = 'paid'; $main_transaction = Transaction::create($transaction_new); $transaction_charges['transaction_id'] = $main_transaction->id; $transaction_charges['amount'] = $transaction_charge_amount; $transaction_charges['note'] = 'Transaction Charges For Payment Through Card'; $transaction_charges['payload'] = ''; $transaction_charges['rent_payment_id'] = null; $transaction_charges['payment_month'] = now()->month; $transaction_charges['payment_year'] = now()->year; $transaction_charges['payment_type'] = 'card_transaction_charges'; $transaction_charges['created_at'] = now(); $trans = TransactionDetail::create($transaction_charges); $transaction_charges_extra['transaction_id'] = $main_transaction->id; $transaction_charges_extra['amount'] = $payment_amount - $transaction_charge_amount; $transaction_charges_extra['note'] = 'Extra Unit Charges'; $transaction_charges_extra['payload'] = ''; $transaction_charges_extra['rent_payment_id'] = null; $transaction_charges_extra['payment_month'] = now()->month; $transaction_charges_extra['payment_year'] = now()->year; $transaction_charges_extra['payment_type'] = 'extra_unit_charges'; $transaction_charges_extra['created_at'] = now(); $trans = TransactionDetail::create($transaction_charges_extra); DB::commit(); return [ 'code' => 200, 'message' => json_encode($payload), 'transaction_id' => $main_transaction->id ]; } catch (CardException $e) { // Handle Stripe card exception return [ 'code' => 500, 'message' => $e ]; } catch (ValidationException $e) { // Handle validation exception return [ 'code' => 500, 'message' => $e ]; } catch (\Exception $e) { // Handle other exceptions return [ 'code' => 500, 'message' => $e ]; } } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.29 | Генерация страницы: 0.01 |
proxy
|
phpinfo
|
Настройка