public IActionResult UpdateCoursePayment([FromBody] StudentCourseUpdatePaymentDTO updatePaymentDTO) { try { if (ModelState.IsValid) { var result = repository.UpdateCoursePayment(updatePaymentDTO); if (result == 1) { return(Ok(new { Message = "Payment successful!" })); } else if (result == 3) { return(NotFound(new { Message = "Error: Course not found!" })); } else if (result == 4) { return(Conflict(new { Message = "Payment for this course has already been completed." })); } else if (result == 5) { return(Conflict(new { Message = "Unable to process request. You have either cancelled the course or request has been rejeted by mentor." })); } return(BadRequest(new { Message = "An error occurred while processing payment. Please try again later." })); } return(BadRequest(new { Message = "Invalid action. Try again." })); } catch (Exception e) { return(BadRequest(new { Message = "Internal server error.", error = e.InnerException })); } }
public int UpdateCoursePayment(StudentCourseUpdatePaymentDTO updatePaymentDTO) { try { var course = (from studentCourse in context.StudentCourses join student in context.MODUsers on studentCourse.User equals student where student.Email == updatePaymentDTO.Email && studentCourse.CourseId == updatePaymentDTO.CourseId select studentCourse).Include(sc => sc.User).SingleOrDefault(); if (course != null) { if (course.PaymentStatus) { return(4); } else if (course.Status == 6 || course.Status == 7) { return(5); // cancelled or rejected } course.PaymentStatus = true; course.PaymentId = updatePaymentDTO.PaymentId; course.Status = 3; // payment transaction var payment = new Payment { PaymentId = course.PaymentId, User = course.User, Amount = context.MentorSkills.Where(s => s.SkillId == course.SkillId).FirstOrDefault().TotalFee, TransactionType = true }; context.Payments.Add(payment); context.StudentCourses.Update(course); var result = context.SaveChanges(); if (result > 0) { return(1); // success } return(2); // error updating payment details } return(3); // course not found or payment already completed } catch (Exception e) { throw e; } }