| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System.ComponentModel.DataAnnotations;
- using System.ComponentModel.DataAnnotations.Schema;
- using Domain.Entities.Members;
- namespace Domain.Entities.Payments.Toss;
- /// <summary>
- /// 토스페이먼츠 결제 승인 요청 + 응답 통합
- /// confirm API 호출 시 요청 데이터를 먼저 저장하고, 응답 수신 시 업데이트
- /// </summary>
- public class TossConfirm
- {
- [ForeignKey(nameof(MemberID))]
- public virtual Member? Member { get; private set; }
- [ForeignKey(nameof(PaymentOrderID))]
- public virtual PaymentOrder? PaymentOrder { get; private set; }
- [Key]
- public int ID { get; private set; }
- public int MemberID { get; private set; }
- public int PaymentOrderID { get; private set; }
- /// <summary>토스 결제 키 (unique — 취소·조회 API 호출에 필요)</summary>
- public string PaymentKey { get; private set; } = default!;
- /// <summary>가맹점 주문번호</summary>
- public string OrderID { get; private set; } = default!;
- /// <summary>승인 요청 금액 (서버 저장 주문 금액)</summary>
- public int Amount { get; private set; }
- // ── 응답 필드 (승인 완료/실패 시 업데이트) ──────────────────────
- /// <summary>응답 코드 (SUCCESS = 성공, 그 외 = Toss 에러 코드/EXCEPTION)</summary>
- public string? ResponseCode { get; private set; }
- /// <summary>응답 메시지 (실패 사유)</summary>
- public string? ResponseMessage { get; private set; }
- /// <summary>Toss Payment.status (DONE / WAITING_FOR_DEPOSIT / CANCELED / ABORTED / EXPIRED ...)</summary>
- public string? Status { get; private set; }
- /// <summary>결제수단 (Toss method 문자열 — 카드/가상계좌/간편결제 등)</summary>
- public string? Method { get; private set; }
- /// <summary>최종 승인 금액 (Toss totalAmount)</summary>
- public int? TotalAmount { get; private set; }
- /// <summary>승인 일시 (UTC)</summary>
- public DateTime? ApprovedAt { get; private set; }
- /// <summary>영수증 URL (Toss receipt.url)</summary>
- public string? ReceiptUrl { get; private set; }
- /// <summary>Toss Payment 객체 원문 JSON — 대사/CS 용</summary>
- public string? RawResponse { get; private set; }
- public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
- public DateTime? UpdatedAt { get; private set; }
- private TossConfirm() { }
- /// <summary>승인 요청 시 생성 (응답 전)</summary>
- public static TossConfirm CreateRequest(
- int memberID,
- int paymentOrderID,
- string paymentKey,
- string orderID,
- int amount
- ) {
- return new TossConfirm
- {
- MemberID = memberID,
- PaymentOrderID = paymentOrderID,
- PaymentKey = paymentKey,
- OrderID = orderID,
- Amount = amount
- };
- }
- /// <summary>승인 성공 응답 수신 시 업데이트</summary>
- public void SetResponse(
- string status,
- string? method = null,
- int? totalAmount = null,
- DateTime? approvedAt = null,
- string? receiptUrl = null,
- string? rawResponse = null
- ) {
- ResponseCode = "SUCCESS";
- ResponseMessage = null;
- Status = status;
- Method = method;
- TotalAmount = totalAmount;
- ApprovedAt = approvedAt;
- ReceiptUrl = receiptUrl;
- RawResponse = rawResponse;
- UpdatedAt = DateTime.UtcNow;
- }
- /// <summary>승인 실패/예외 시 업데이트</summary>
- public void SetError(string code, string message)
- {
- ResponseCode = code;
- ResponseMessage = message.Length > 500 ? message[..500] : message;
- UpdatedAt = DateTime.UtcNow;
- }
- /// <summary>
- /// 재승인(대사 재확인) 시 기존 요청 행을 재사용한다. PaymentKey/OrderID unique 인덱스 충돌을 피하려
- /// 새 행을 만들지 않고, 결제 키를 갱신하고 이전 응답을 초기화해 "요청 후 응답 미수신" 상태로 되돌린다. (Phase 1.5 승계)
- /// </summary>
- public void PrepareRetry(string paymentKey)
- {
- PaymentKey = paymentKey;
- ResponseCode = null;
- ResponseMessage = null;
- UpdatedAt = DateTime.UtcNow;
- }
- public bool IsSuccess => ResponseCode == "SUCCESS";
- }
|