customerService = new CustomerService; } public function all() { return PaymentCode::with(['user','planPrice.plan','addons'])->orderBy('id')->get(); } public function create($request) { $data = $request->except('_token'); $total = 0; $paymentCode = PaymentCode::create($data); if($data['addons'] != null){ $paymentCode->addons()->createMany($data['addons']); } return true; } public function update($request) { $data = $request->except('_token'); $paymentCode = PaymentCode::find($data['id']); if($paymentCode){ $paymentCode->update($data); foreach ($data['addons'] as $key => $addon) { $paymentCode->addons()->updateOrCreate( [ 'id' => $addon['id'] ?? null, ], $addon ); } $addOnsId = collect($data['addons'])->pluck('id')->toArray(); $paymentCode->addons()->whereNotIn('id', $addOnsId)->delete(); } return true; } public function delete($id) { $paymentCode = PaymentCode::find($id); if ($paymentCode) { $paymentCode->delete(); return true; } return false; } public function validate($code) { $paymentCode = PaymentCode::with('planPrice.plan','addons')->where('code',$code)->where('type','renewal')->first(); if ($paymentCode) { if($paymentCode->user_id != null) { return response()->json([ 'error' => 'Payment Code has been used already!' ], 500); } return response()->json([ 'message' => 'Payment Code Valid', 'subscription' => $paymentCode ], 200); } return response()->json([ 'error' => 'Payment Code Not Found!' ], 500); } public function apply($code,$id = null) { $user_id = $id != null ? $id : auth()->user()->id; $paymentCode = PaymentCode::with('planPrice.plan')->where('code',$code)->where('type','renewal')->first(); if ($paymentCode) { if($paymentCode->user_id != null) { return response()->json([ 'error' => 'Payment Code has been used already!' ], 500); } $customer_subscription = new CustomerSubscription(); $customer_subscription->customer_id = $user_id; $customer_subscription->txn_id = $paymentCode->code; $customer_subscription->subscription_plan_price_id = $paymentCode->planPrice->id; $customer_subscription->payment_code_id = $paymentCode->id; $customer_subscription->payment_method = 'prepaid'; $customer_subscription->payment_mode = 'All'; $customer_subscription->status = 'active'; $customer_subscription->payment_status = 'succeeded'; $customer_subscription->is_paid = true; $customer_subscription->approved = true; $customer_subscription->next_payment_at = null; $customer_subscription->expired_at = Carbon::now()->addMonths($paymentCode->planPrice->access_expire_duration); $customer_subscription->last_payment_date = Carbon::now()->format('Y-m-d'); $customer_subscription->save(); $paymentCode->user_id = $user_id; $paymentCode->save(); $user_credits = UserCredit::firstOrCreate(['user_id' => $user_id]); $before_credits = $this->customerService->credits($user_credits->user_id); $user_credits->increment('subscription_credits',$paymentCode->planPrice->total_credits); $after_credits = $this->customerService->credits($user_credits->user_id); $user_credits_history = new UserCreditHistory; $user_credits_history->user_id = $user_id; $user_credits_history->subscription_credits_from = $before_credits['total_available_credits']['subscription']; $user_credits_history->subscription_credits_to = $after_credits['total_available_credits']['subscription']; $user_credits_history->alacarte_credits_from = $before_credits['total_available_credits']['alacarte'];; $user_credits_history->alacarte_credits_to = $after_credits['total_available_credits']['alacarte']; $user_credits_history->details = 'Added Total Plan Credits of '.$paymentCode->planPrice->plan->title.' (Prepaid)'; $user_credits_history->save(); return response()->json([ 'message' => __('Prepaid Code successfully applied.') ], 200); } return response()->json([ 'error' => 'Payment Code Not Found!' ], 404); } }