Wallet.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Domain.Entities.Common.ValueObject;
  4. using Domain.Entities.Members;
  5. using Domain.Entities.Wallets.Policy;
  6. using Domain.Entities.Wallets.ValueObject;
  7. namespace Domain.Entities.Wallets;
  8. public class Wallet
  9. {
  10. [ForeignKey(nameof(MemberID))]
  11. public virtual Member Member { get; private set; } = null!;
  12. private readonly List<WalletBalance> _balances = [];
  13. public IReadOnlyCollection<WalletBalance> Balances => _balances;
  14. private readonly List<WalletTransaction> _transactions = [];
  15. public IReadOnlyCollection<WalletTransaction> Transactions => _transactions;
  16. [Key]
  17. public int ID { get; private set; }
  18. public Guid WalletKey { get; private set; } = Guid.NewGuid();
  19. public int MemberID { get; private set; }
  20. public DateTime? UpdatedAt { get; private set; }
  21. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  22. private Wallet() { }
  23. private Wallet(int memberID)
  24. {
  25. if (memberID <= 0)
  26. {
  27. throw new ArgumentOutOfRangeException(nameof(memberID));
  28. }
  29. MemberID = memberID;
  30. // 지갑 생성 시 구분별 잔액 기본 생성
  31. EnsureBalance(WalletBalanceType.PgCharged);
  32. EnsureBalance(WalletBalanceType.Deposit);
  33. EnsureBalance(WalletBalanceType.Donation);
  34. EnsureBalance(WalletBalanceType.Reward);
  35. EnsureBalance(WalletBalanceType.Airdrop);
  36. EnsureBalance(WalletBalanceType.Locked);
  37. EnsureBalance(WalletBalanceType.Adjusted);
  38. EnsureBalance(WalletBalanceType.StoreRevenue);
  39. }
  40. public static Wallet Create(int memberID) => new(memberID);
  41. public Money GetBalance(WalletBalanceType type) => GetBalanceEntity(type).Amount;
  42. public Money GetTotalAvailable()
  43. {
  44. var total = Money.KRW(0);
  45. foreach (var b in Balances.Where(x => x.Type != WalletBalanceType.Locked))
  46. total += b.Amount;
  47. return total;
  48. }
  49. /// <summary>
  50. /// 상점 결제로 사용 가능한 잔액 합계 (SpendPolicy.StoreOrderSpendOrder 기준).
  51. /// Donation/Locked/StoreRevenue 는 제외.
  52. /// </summary>
  53. public Money GetStoreOrderAvailable()
  54. {
  55. var total = Money.KRW(0);
  56. foreach (var type in Policy.SpendPolicy.StoreOrderSpendOrder)
  57. {
  58. total += GetBalance(type);
  59. }
  60. return total;
  61. }
  62. // ---- Credit ----
  63. public void CreditPgCharge(Money amount, string reason = "PG_CHARGE", string? refID = null)
  64. => Credit(WalletBalanceType.PgCharged, WalletTransactionType.Charge, amount, reason, refID);
  65. public void CreditDonationIn(Money amount, string reason, string? refID = null)
  66. => Credit(WalletBalanceType.Donation, WalletTransactionType.DonationIn, amount, reason, refID);
  67. public void CreditReward(Money amount, string reason = "REWARD", string? refID = null)
  68. => Credit(WalletBalanceType.Reward, WalletTransactionType.RewardEarned, amount, reason, refID);
  69. public void CreditStoreRevenue(Money amount, string reason = "STORE_CHANNEL_REWARD", string? refID = null)
  70. => Credit(WalletBalanceType.StoreRevenue, WalletTransactionType.OrderChannelReward, amount, reason, refID);
  71. /// <summary>
  72. /// 게임사 API 결제 보고 수수료 적립 (보류 확정 시): 채널주 StoreRevenue 입금.
  73. /// </summary>
  74. public void CreditApiCommission(Money amount, string reason = "API_COMMISSION", string? refID = null)
  75. => Credit(WalletBalanceType.StoreRevenue, WalletTransactionType.ApiCommissionEarned, amount, reason, refID);
  76. /// <summary>
  77. /// 상점 환불 환원: 원본 결제(OrderPay)가 차감한 BalanceType 그대로 복원해 출처를 보존한다.
  78. /// 호출자는 OrderPay 트랜잭션을 BalanceType 별로 집계해 type 마다 한 번씩 호출한다.
  79. /// </summary>
  80. public void CreditStoreOrderRefund(WalletBalanceType type, Money amount, string reason = "ORDER_REFUND", string? refID = null)
  81. => Credit(type, WalletTransactionType.OrderRefund, amount, reason, refID);
  82. private void Credit(
  83. WalletBalanceType balanceType,
  84. WalletTransactionType txType,
  85. Money amount,
  86. string reason,
  87. string? refID
  88. ) {
  89. EnsureMoney(amount);
  90. var balance = EnsureBalance(balanceType);
  91. balance.Increase(amount);
  92. _transactions.Add(WalletTransaction.Create(
  93. walletKey: WalletKey,
  94. balanceType: balanceType,
  95. txType: txType,
  96. amount: amount,
  97. balanceAfter: balance.Amount,
  98. reason: reason,
  99. refID: refID
  100. ));
  101. }
  102. // ---- Debit ----
  103. public void DebitDonationOut(Money amount, string reason, string? refID = null)
  104. => DebitSingle(WalletBalanceType.Donation, WalletTransactionType.DonationOut, amount, reason, refID);
  105. /// <summary>
  106. /// 상점 결제 전용 다중 잔액 차감: SpendPolicy.StoreOrderSpendOrder 순서로 분할 차감하고
  107. /// 사용된 BalanceType 별로 OrderPay 트랜잭션을 각각 기록한다 (환불 시 출처 복원용).
  108. /// 합계 부족 시 InvalidOperationException — 호출자가 GetStoreOrderAvailable 로 사전 검증할 것.
  109. /// </summary>
  110. public IReadOnlyList<(WalletBalanceType Type, Money Amount)> DebitForStoreOrder(
  111. Money total,
  112. string reason = "ORDER_PAY",
  113. string? refID = null)
  114. {
  115. EnsureMoney(total);
  116. var remaining = total;
  117. var splits = new List<(WalletBalanceType, Money)>();
  118. foreach (var type in Policy.SpendPolicy.StoreOrderSpendOrder)
  119. {
  120. if (remaining.IsZero) break;
  121. var balance = EnsureBalance(type);
  122. if (balance.Amount.IsZero) continue;
  123. var takeValue = Math.Min(balance.Amount.Value, remaining.Value);
  124. if (takeValue <= 0) continue;
  125. var take = Money.KRW(takeValue);
  126. balance.Decrease(take);
  127. remaining = remaining - take;
  128. _transactions.Add(WalletTransaction.Create(
  129. walletKey: WalletKey,
  130. balanceType: type,
  131. txType: WalletTransactionType.OrderPay,
  132. amount: take,
  133. balanceAfter: balance.Amount,
  134. reason: reason,
  135. refID: refID
  136. ));
  137. splits.Add((type, take));
  138. }
  139. if (!remaining.IsZero)
  140. {
  141. throw new InvalidOperationException("Insufficient balance for store order.");
  142. }
  143. return splits;
  144. }
  145. /// <summary>
  146. /// 환불 시 채널 보상 역분개: 채널주 StoreRevenue 잔액 차감.
  147. /// 잔액 부족 시 InvalidOperationException — 호출자가 별도 정책 처리 필요 (예: 음수 허용 모드).
  148. /// </summary>
  149. public void DebitStoreRevenueRefund(Money amount, string reason = "ORDER_REFUND_CHANNEL", string? refID = null)
  150. => DebitSingle(WalletBalanceType.StoreRevenue, WalletTransactionType.OrderRefundChannelReward, amount, reason, refID);
  151. /// <summary>
  152. /// API 결제 취소 회수 (확정 후 취소): 채널주 StoreRevenue 차감.
  153. /// 잔액 부족 시 WalletBalance.Decrease 예외 — 호출자가 가용분을 사전 계산해 부족분(shortfall)을 별도 기록할 것.
  154. /// </summary>
  155. public void DebitApiCommissionRevoke(Money amount, string reason = "API_COMMISSION_REVOKE", string? refID = null)
  156. => DebitSingle(WalletBalanceType.StoreRevenue, WalletTransactionType.ApiCommissionRevoked, amount, reason, refID);
  157. // ---- Spend (정책 순서대로 분할 차감) ----
  158. public void Spend(Money amount, string reason = "SPEND", string? refID = null)
  159. {
  160. EnsureMoney(amount);
  161. var remaining = amount;
  162. foreach (var type in SpendPolicy.DefaultSpendOrder)
  163. {
  164. if (remaining.IsZero) break;
  165. var balance = EnsureBalance(type);
  166. if (balance.Amount.IsZero) continue;
  167. var takeValue = Math.Min(balance.Amount.Value, remaining.Value);
  168. if (takeValue <= 0) continue;
  169. var take = Money.KRW(takeValue);
  170. balance.Decrease(take);
  171. remaining = remaining - take;
  172. _transactions.Add(WalletTransaction.Create(
  173. walletKey: WalletKey,
  174. balanceType: type,
  175. txType: WalletTransactionType.Spend,
  176. amount: take,
  177. balanceAfter: balance.Amount,
  178. reason: reason,
  179. refID: refID
  180. ));
  181. }
  182. if (!remaining.IsZero)
  183. {
  184. throw new InvalidOperationException("Insufficient balance for spending.");
  185. }
  186. }
  187. // ---- Lock/Unlock ----
  188. public void LockForWithdrawal(Money amount, string reason = "WITHDRAW_REQUEST", string? refID = null)
  189. {
  190. EnsureMoney(amount);
  191. var from = EnsureBalance(WalletBalanceType.Donation);
  192. var locked = EnsureBalance(WalletBalanceType.Locked);
  193. from.Decrease(amount);
  194. _transactions.Add(WalletTransaction.Create(WalletKey, WalletBalanceType.Donation, WalletTransactionType.Lock, amount, from.Amount, reason, refID));
  195. locked.Increase(amount);
  196. _transactions.Add(WalletTransaction.Create(WalletKey, WalletBalanceType.Locked, WalletTransactionType.Lock, amount, locked.Amount, reason, refID));
  197. }
  198. public void UnlockWithdrawal(Money amount, string reason = "WITHDRAW_CANCEL", string? refID = null)
  199. {
  200. EnsureMoney(amount);
  201. var locked = EnsureBalance(WalletBalanceType.Locked);
  202. var donation = EnsureBalance(WalletBalanceType.Donation);
  203. locked.Decrease(amount);
  204. _transactions.Add(WalletTransaction.Create(WalletKey, WalletBalanceType.Locked, WalletTransactionType.Unlock, amount, locked.Amount, reason, refID));
  205. donation.Increase(amount);
  206. _transactions.Add(WalletTransaction.Create(WalletKey, WalletBalanceType.Donation, WalletTransactionType.Unlock, amount, donation.Amount, reason, refID));
  207. }
  208. /// <summary>
  209. /// 상점 판매 수익 출금 잠금: StoreRevenue → Locked.
  210. /// Donation/StoreRevenue 두 잔액에서 동시 출금할 때 LockForWithdrawal + 이 메서드를 따로 호출.
  211. /// 정산서 명세는 호출자(WithdrawalRequest)에 분개 컬럼으로 분리 표시.
  212. /// </summary>
  213. public void LockStoreRevenueForWithdrawal(Money amount, string reason = "WITHDRAW_REQUEST_STORE", string? refID = null)
  214. {
  215. EnsureMoney(amount);
  216. var from = EnsureBalance(WalletBalanceType.StoreRevenue);
  217. var locked = EnsureBalance(WalletBalanceType.Locked);
  218. from.Decrease(amount);
  219. _transactions.Add(WalletTransaction.Create(WalletKey, WalletBalanceType.StoreRevenue, WalletTransactionType.WithdrawalStoreRevenue, amount, from.Amount, reason, refID));
  220. locked.Increase(amount);
  221. _transactions.Add(WalletTransaction.Create(WalletKey, WalletBalanceType.Locked, WalletTransactionType.WithdrawalStoreRevenue, amount, locked.Amount, reason, refID));
  222. }
  223. /// <summary>
  224. /// 상점 판매 수익 출금 잠금 해제: Locked → StoreRevenue.
  225. /// </summary>
  226. public void UnlockStoreRevenueWithdrawal(Money amount, string reason = "WITHDRAW_CANCEL_STORE", string? refID = null)
  227. {
  228. EnsureMoney(amount);
  229. var locked = EnsureBalance(WalletBalanceType.Locked);
  230. var storeRevenue = EnsureBalance(WalletBalanceType.StoreRevenue);
  231. locked.Decrease(amount);
  232. _transactions.Add(WalletTransaction.Create(WalletKey, WalletBalanceType.Locked, WalletTransactionType.Unlock, amount, locked.Amount, reason, refID));
  233. storeRevenue.Increase(amount);
  234. _transactions.Add(WalletTransaction.Create(WalletKey, WalletBalanceType.StoreRevenue, WalletTransactionType.Unlock, amount, storeRevenue.Amount, reason, refID));
  235. }
  236. // ---- Adjust ----
  237. public void AdjustIncrease(Money amount, string reason, string? refID = null, string? memo = null)
  238. => AdjustIncrease(WalletBalanceType.Adjusted, amount, reason, refID, memo);
  239. /// <summary>
  240. /// 관리자 조정 충전: 지정한 잔액 유형으로 입금. 트랜잭션 타입은 Adjusted 로 기록해 수동 조작 추적을 보존한다.
  241. /// Locked 는 출금 프로세스 전용이라 직접 조정 불가.
  242. /// </summary>
  243. public void AdjustIncrease(WalletBalanceType type, Money amount, string reason, string? refID = null, string? memo = null)
  244. {
  245. EnsureMoney(amount);
  246. EnsureAdjustableType(type);
  247. if (string.IsNullOrWhiteSpace(reason))
  248. {
  249. throw new ArgumentException("Adjustment reason is required.", nameof(reason));
  250. }
  251. var balance = EnsureBalance(type);
  252. balance.Increase(amount);
  253. _transactions.Add(WalletTransaction.Create(
  254. walletKey: WalletKey,
  255. balanceType: type,
  256. txType: WalletTransactionType.Adjusted,
  257. amount: amount,
  258. balanceAfter: balance.Amount,
  259. reason: $"ADJUST_IN:{reason}",
  260. refID: refID,
  261. memo: memo
  262. ));
  263. }
  264. public void AdjustDecrease(Money amount, string reason, string? refID = null, string? memo = null)
  265. => AdjustDecrease(WalletBalanceType.Adjusted, amount, reason, refID, memo);
  266. /// <summary>
  267. /// 관리자 조정 차감: 지정한 잔액 유형에서만 차감. 잔액 부족 시 WalletBalance.Decrease 가 예외 —
  268. /// 호출자가 해당 잔액을 사전 검증할 것.
  269. /// </summary>
  270. public void AdjustDecrease(WalletBalanceType type, Money amount, string reason, string? refID = null, string? memo = null)
  271. {
  272. EnsureMoney(amount);
  273. EnsureAdjustableType(type);
  274. if (string.IsNullOrWhiteSpace(reason))
  275. {
  276. throw new ArgumentException("Adjustment reason is required.", nameof(reason));
  277. }
  278. var balance = EnsureBalance(type);
  279. balance.Decrease(amount);
  280. _transactions.Add(WalletTransaction.Create(
  281. walletKey: WalletKey,
  282. balanceType: type,
  283. txType: WalletTransactionType.Adjusted,
  284. amount: amount,
  285. balanceAfter: balance.Amount,
  286. reason: $"ADJUST_OUT:{reason}",
  287. refID: refID,
  288. memo: memo
  289. ));
  290. }
  291. private static void EnsureAdjustableType(WalletBalanceType type)
  292. {
  293. if (type == WalletBalanceType.Locked)
  294. {
  295. throw new ArgumentException("Locked balance cannot be adjusted directly.", nameof(type));
  296. }
  297. }
  298. /// <summary>
  299. /// 관리자 차감 전용 다중 잔액 분할 차감: SpendPolicy.DefaultSpendOrder 순서로 분할 차감하고
  300. /// 사용된 BalanceType 별로 Adjusted 트랜잭션을 각각 기록한다.
  301. /// 합계 부족 시 InvalidOperationException — 호출자가 GetTotalAvailable 로 사전 검증할 것.
  302. /// </summary>
  303. public void AdjustDecreaseAcrossBalances(Money amount, string reason, string? refID = null, string? memo = null)
  304. {
  305. EnsureMoney(amount);
  306. if (string.IsNullOrWhiteSpace(reason))
  307. {
  308. throw new ArgumentException("Adjustment reason is required.", nameof(reason));
  309. }
  310. var remaining = amount;
  311. foreach (var type in SpendPolicy.DefaultSpendOrder)
  312. {
  313. if (remaining.IsZero) break;
  314. var balance = EnsureBalance(type);
  315. if (balance.Amount.IsZero) continue;
  316. var takeValue = Math.Min(balance.Amount.Value, remaining.Value);
  317. if (takeValue <= 0) continue;
  318. var take = Money.KRW(takeValue);
  319. balance.Decrease(take);
  320. remaining = remaining - take;
  321. _transactions.Add(WalletTransaction.Create(
  322. walletKey: WalletKey,
  323. balanceType: type,
  324. txType: WalletTransactionType.Adjusted,
  325. amount: take,
  326. balanceAfter: balance.Amount,
  327. reason: $"ADJUST_OUT:{reason}",
  328. refID: refID,
  329. memo: memo
  330. ));
  331. }
  332. if (!remaining.IsZero)
  333. {
  334. throw new InvalidOperationException("Insufficient balance for adjustment.");
  335. }
  336. }
  337. // ---- Internal ----
  338. private void DebitSingle(WalletBalanceType balanceType, WalletTransactionType txType, Money amount, string reason, string? refID)
  339. {
  340. EnsureMoney(amount);
  341. var balance = EnsureBalance(balanceType);
  342. balance.Decrease(amount);
  343. _transactions.Add(WalletTransaction.Create(
  344. walletKey: WalletKey,
  345. balanceType: balanceType,
  346. txType: txType,
  347. amount: amount,
  348. balanceAfter: balance.Amount,
  349. reason: reason,
  350. refID: refID
  351. ));
  352. }
  353. private WalletBalance GetBalanceEntity(WalletBalanceType type)
  354. {
  355. var found = _balances.SingleOrDefault(x => x.Type == type);
  356. if (found is null)
  357. {
  358. throw new InvalidOperationException($"Balance type '{type}' not initialized.");
  359. }
  360. return found;
  361. }
  362. private WalletBalance EnsureBalance(WalletBalanceType type)
  363. {
  364. var found = _balances.SingleOrDefault(x => x.Type == type);
  365. if (found != null)
  366. {
  367. return found;
  368. }
  369. var created = WalletBalance.Create(WalletKey, type);
  370. _balances.Add(created);
  371. return created;
  372. }
  373. private static void EnsureMoney(Money amount)
  374. {
  375. if (amount.IsZero || amount.Value <= 0)
  376. {
  377. throw new ArgumentException("Amount must be positive.", nameof(amount));
  378. }
  379. }
  380. }