Bu kod benden önce yazılmıştı. Ben validate kısımlarını özel fonksiyonlar oluşturarak epey kısalttım. Daha ne kadar kısaltabilirim neleri değiştirmem gerekir. şunu kullanmasan da olur diyebileceğiniz yerler var mı kodu temize çekmeye çalışıyorum iyice.
public function startSubscription(SubscriptionStartRequest $request)
{
$user = User::with('info')->where('id', Auth::id())->first();
$iyzicoCardUserKey= UserCard::where('iyzico_card_token',$request->input('card_token'))->where('user_id',Auth::id())->pluck('iyzico_card_user_key')->first();
$userPackageSubscription = UserPackage::where('car_id', $request->input('car_id'))->whereNotNull('next_payment_date')->get();
$car = Car::where('user_id', Auth::id())->where('id', $request->input('car_id'))->first();
$userPackage = UserPackage::where('car_id', $car->id)->first();
$userQuantity= UserPackage::where('car_id',$car->id)->whereNull('next_payment_date')->get();
$package = Package::where('id', $request->input('package_id'))->first();
$discountCode = DiscountCode::where('code', $request->input('discount_code'))->first();
$errorSubscription = $this->validateSubscription($userQuantity,$car,$package);
if ($errorSubscription) {
return new ErrorJsonResource($errorSubscription);
}
$errorCarSubscription = $this->validateCarSubscription($userPackage,$userPackageSubscription);
if ($errorCarSubscription) {
return new ErrorJsonResource($errorCarSubscription);
}
$errorIyzico = $this->validateIyzicoControl($iyzicoCardUserKey);
if ($errorIyzico) {
return new ErrorJsonResource($errorIyzico);
}
$errorUserInformation = $this->validateUserInformation($user);
if ($errorUserInformation) {
return new ErrorJsonResource($errorUserInformation);
}
$price = $this->calculatePrice($package, $discountCode);
if ($price instanceof ErrorJsonResource) {
return $price;
}
if ($package->package_type == 'quantity') {
$dataRequest = [
'package_name' => $package->title,
'price' => $price,
'user_card_token'=>$iyzicoCardUserKey,
'card_token' => $request->input('card_token'),
'name' => $user->name,
'phone' => $user->info->phone,
'email' => $user->email,
'city' => $user->info->city,
'country' => $user->info->country,
'address' => $user->info->address,
'zipcode' => $user->info->zipcode,
'period' => $package->subscription_type,
];
$pay = new IyzipayService(new Options());
$payment = $pay->buyingWashingPackage($dataRequest);
if (isset($payment->original['message']) && $payment->original['message']) {
return new ErrorJsonResource($payment->original['message']);
}
$data = UserPackage::create([
'user_id' => $user->id,
'package_id' => $package->id,
'car_id' => $car->id,
]);
// payment log
PaymentLog::create([
'user_id' => $user->id,
'type' => 'quantity',
'price' => $package->price,
'payment_id' => $payment['payment_id']
]);
return new SuccessJsonResource($data);
}
else {
$dataRequest = [
'package_name' => $package->title,
'price' => $price,
'user_card_token'=>$iyzicoCardUserKey,
'card_token' => $request->input('card_token'),
'name' => $user->name,
'phone' => $user->info->phone,
'email' => $user->email,
'city' => $user->info->city,
'country' => $user->info->country,
'address' => $user->info->address,
'zipcode' => $user->info->zipcode,
'period' => $package->subscription_type,
];
$pay = new IyzipayService(new Options());
$subscription = $pay->buyingWashingPackage($dataRequest);
if (isset($subscription->original['message']) && $subscription->original['message']) {
return new ErrorJsonResource($subscription->original['message']);
}
$isSubs = UserPackage::with('user')->whereNotNull('last_payment_date');
$userSubscription = $isSubs->where('car_id',$car->id)->where('next_payment_date', '<', Carbon::today()->endOfDay())->first();
if (!is_null($userSubscription)) {
$userSubscription->delete();
}
Auth::user()->update(['subscription_is_active'=>true]);
$subscriptionType = $package->subscription_type;
$startDate = Carbon::now();
$endDate = Carbon::now();
switch ($subscriptionType) {
case 'weekly':
$endDate->addWeek();
break;
case 'monthly':
$endDate->addMonth();
break;
case '6monthly':
$endDate->addMonths(6);
break;
case 'yearly':
$endDate->addYear();
break;
default:
$endDate->addMonths(6);
break;
}
$data = UserPackage::create([
'user_id' => $user->id,
'package_id' => $package->id,
'car_id' => $car->id,
'subscription_start_date' => $startDate,
'last_payment_date' => $endDate,
'next_payment_date' => $endDate->addDay(),
]);
// payment log
PaymentLog::create([
'user_id' => $user->id,
'type' => 'subscription',
'price' => $package->price,
'payment_id' => $subscription['payment_id']
]);
// payment log
return new SuccessJsonResource($data);
}
}