View.cshtml.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Application.Abstractions.Messaging;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. using System.ComponentModel.DataAnnotations;
  5. namespace Admin.Pages.Payments.Toss.Confirm;
  6. public class ViewModel(IMediator mediator) : PageModel
  7. {
  8. [BindProperty(SupportsGet = true)]
  9. public int ID { get; set; }
  10. [BindProperty]
  11. public CancelInput Input { get; set; } = new();
  12. public sealed class CancelInput
  13. {
  14. [Display(Name = "취소 요청자")]
  15. [MaxLength(20)]
  16. public string? CancelRequester { get; set; }
  17. [Display(Name = "취소 사유")]
  18. [MaxLength(200)]
  19. public string? CancelReason { get; set; }
  20. }
  21. public GetTossConfirm.Response? Data { get; set; }
  22. public async Task<IActionResult> OnGetAsync(CancellationToken ct)
  23. {
  24. try
  25. {
  26. Data = await mediator.Send(new GetTossConfirm.Query(ID), ct);
  27. return Page();
  28. }
  29. catch (Exception e)
  30. {
  31. TempData["ErrorMessages"] = e.Message;
  32. return RedirectToPage("/Payments/Toss/Confirm/Index");
  33. }
  34. }
  35. public async Task<IActionResult> OnPostCancelAsync(CancellationToken ct)
  36. {
  37. var result = await mediator.Send(new CancelTossConfirm.Command(
  38. ID,
  39. Input.CancelRequester,
  40. Input.CancelReason
  41. ), ct);
  42. if (result.IsSuccess)
  43. {
  44. TempData["SuccessMessage"] = "토스 결제 취소가 완료되었습니다.";
  45. }
  46. else
  47. {
  48. TempData["ErrorMessages"] = result.Error?.Description ?? "취소에 실패했습니다.";
  49. }
  50. return Redirect($"/Payments/Toss/Confirm/View/{ID}{Request.QueryString}");
  51. }
  52. }