fix(performance): 카페24 환불 — 네이버페이만·전체취소 배송비 포함, 0원 결제 주문은 0원

9/4·9/16·9/25 대조 결과:
- 9/4 환불 122,100 = 환불 목록 98,700 + 네이버페이 취소 19,900 + 그 주문 배송비
  3,500 (주문 전체취소) — 우리는 배송비를 빠뜨려 −3,500 (9/14 −7,000 도 같은 유형)
- 9/16 환불 201,100 = 환불 목록 그대로 — 우리가 더 뺀 9,900 은 톡체크아웃
  (KAKAOPAY) 환불 기록 없는 취소. 카페24 는 네이버페이만 센다 (9/21 도 같은 유형)
- 9/25 결제: 실결제·적립금·네이버포인트는 카페24 와 같은데 합계만 +9,900 —
  네 값이 모두 0인 주문(가상계좌·휴대폰 9,900)을 상품식으로 넣었기 때문.
  카페24 는 0원 (9/11 +24,900 도 같은 유형)

- unrefunded_claims: 네이버페이(NCHECKOUT) 주문만, 주문 전체가 취소·반품되면
  배송비(− 배송비 할인·쿠폰)를 마지막 클레임 품목에 한 번 더함
- _paid_total: 상품식 대체 제거
- 대조 표에 품목별 배송비 포함액 표시, 테스트 갱신

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-29 23:03:50 +09:00
parent 2f86445077
commit 643ff25f27
6 changed files with 69 additions and 35 deletions
@@ -407,11 +407,12 @@ def test_not_implemented_and_mock():
# ════════════════════════════════════════════════════════════
def _c24_order(oid, market="self", paid="T", price=50_000, ship=3_000, coupon=5_000, member=1_000, items=2, **kw):
amt = {"order_price_amount": f"{price}.00", "shipping_fee": f"{ship}.00", "coupon_discount_price": f"{coupon}.00",
"membership_discount_amount": f"{member}.00", "points_spent_amount": "0.00", "payment_amount": "0.00"}
"membership_discount_amount": f"{member}.00", "points_spent_amount": "0.00",
"payment_amount": f"{price + ship - coupon - member}.00"}
return {"order_id": oid, "market_id": market, "paid": paid, "order_date": "2026-09-28T10:00:00+09:00",
"payment_date": "2026-09-29T11:21:35+09:00", "buyer_name": "홍길동", "receiver_address": "서울시",
"bank_account_no": "1234567890", "initial_order_amount": amt,
"actual_order_amount": {**amt, "order_price_amount": "0.00"}, # 취소 후 금액은 쓰지 않는다
"actual_order_amount": {**amt, "order_price_amount": "0.00", "payment_amount": "0.00"}, # 취소 후 금액은 쓰지 않는다
"items": [{"order_item_code": f"{oid}-{i:02d}", "product_name": f"상품{i}", "quantity": 1}
for i in range(1, items + 1)], **kw}
@@ -483,8 +484,10 @@ def test_cafe24_normalize_matches_daily_report():
assert by[("CANCEL", "c24:C20260929-01")].event_date == date(2026, 9, 29)
for e in events:
assert "buyer_name" not in str(e.raw) and "1234567890" not in str(e.raw) # 개인정보 미저장
assert order_total({"initial_order_amount": {"order_price_amount": "1578300.00", "shipping_fee": "45500.00",
"coupon_discount_price": "57000.00"}}) == 1_566_800 # 9/29 화면값
assert order_total({"naver_point": "151500.00", # 9/23 화면값
"initial_order_amount": {"order_price_amount": "1860800.00", "shipping_fee": "35000.00",
"coupon_discount_price": "21000.00", "payment_amount": "1713128.00",
"points_spent_amount": "10110.00"}}) == 1_874_738
assert target_code("self") == "cafe24" and target_code("SK11ST") == "elevenst"
assert target_code("unknown", "coupang") == "coupang" and target_code("unknown") == "cafe24"
assert target_code("NCHECKOUT", "NCHECKOUT") == "cafe24" and target_code("KAKAOPAY") == "cafe24"
@@ -513,7 +516,7 @@ def test_cafe24_normalize_matches_daily_report():
# 결제일 기준: 그날 결제된 카페24 열 주문(A,D,E,F,G)의 환불만 — X(결제 없음)·M(스마트스토어)·미완료 제외
bp = rec["refund_basis"]["by_pay_date"]
assert bp["count"] == 1 and bp["actual_refund_amount"] == 12_610
assert bp["amount_diff"] == 5 * 50_000 # 결제 당시 − 현재(가짜 현재 상품금액 0)
assert bp["amount_diff"] == 5 * 47_000 # 결제 당시 − 현재(가짜 현재 상품금액 0)
red = {x["order_id"]: x for x in rec["reduced_orders"]}
assert set(red) == {"A", "D", "E", "F", "G"} and red["A"]["refunded"] == 12_610 and red["D"]["refunded"] == 0
assert rec["reduced_orders"][-1]["order_id"] == "A" # 환불 기록으로 설명 안 되는 금액이 큰 순
@@ -586,8 +589,14 @@ def test_cafe24_refund_total_includes_points_and_naverpay_cancels():
coupon_discount_price="1000.00")]},
{**_c24_order("NP3", market="NCHECKOUT"), "items": [item("NP3-01", "C40", 5_000, day="2026-09-27 01:00:00")]},
{**_c24_order("NP4", market="NCHECKOUT"), "items": [item("NP4-01", "C00", 5_000)]}, # 취소신청만 → 제외
# 전체취소 두 품목 — 배송비(3,000)는 마지막 품목(NP5-02)에 한 번만. 앞 품목은 전날 취소(기간 밖)
{**_c24_order("NP5", market="NCHECKOUT"), "items": [item("NP5-01", "C40", 7_000, day="2026-09-25 10:00:00"),
item("NP5-02", "C40", 8_000)]},
{**_c24_order("KP1", market="KAKAOPAY"), "items": [item("KP1-01", "C40", 9_900)]}, # 톡체크아웃 → 제외
{**_c24_order("S1"), "items": [item("S1-01", "R40", 9_900)]}, # 자체몰 → 제외
],
"return_complete_date": [{**_c24_order("NP1", market="NCHECKOUT"), "items": [item("NP1-01", "C40", 55_800)]}],
"return_complete_date": [{**_c24_order("NP1", market="NCHECKOUT"),
"items": [item("NP1-01", "C40", 55_800), item("NP1-02", "N40", 9_000)]}],
})
orig, _tz.today_kst = _tz.today_kst, (lambda: date(2026, 9, 29))
try:
@@ -599,14 +608,16 @@ def test_cafe24_refund_total_includes_points_and_naverpay_cancels():
assert by["c24:C20260926-01"].amount == -(63_720 + 1_080) # 적립금 환불 포함
assert by["c24:NP1-01-CL"].amount == -55_800 and by["c24:NP1-01-CL"].event_type == "CANCEL"
assert by["c24:NP1-01-CL"].event_date == date(2026, 9, 26) and by["c24:NP1-01-CL"].marketplace_code == "cafe24"
assert by["c24:NP2-01-CL"].amount == -(40_000 - 1_000) # 판매가×수량 − 품목 쿠폰
assert set(by) == {"c24:C20260926-01", "c24:NP1-01-CL", "c24:NP2-01-CL"} # 다른 날·신청·환불 기록 있는 주문 제외
assert by["c24:NP2-01-CL"].amount == -(40_000 - 1_000 + 3_000) # 판매가×수량 − 품목 쿠폰 + 전체취소 배송비
assert by["c24:NP5-02-CL"].amount == -(8_000 + 3_000) # 배송비는 마지막 품목에 한 번
# 다른 날·신청·환불 기록 있는 주문·톡체크아웃·자체몰 제외
assert set(by) == {"c24:C20260926-01", "c24:NP1-01-CL", "c24:NP2-01-CL", "c24:NP5-02-CL"}
rs = rec["refunds"]["self"]
assert rs["no_record_count"] == 2 and rs["no_record"] == 55_800 + 39_000
assert rs["total"] == 63_720 + 1_080 + 55_800 + 39_000
assert rs["no_record_count"] == 3 and rs["no_record"] == 55_800 + 42_000 + 11_000
assert rs["total"] == 63_720 + 1_080 + 55_800 + 42_000 + 11_000
cand = {c["key"]: c["amount"] for c in rec["refund_candidates"]}
assert cand["applied"] == rs["total"]
assert {c["item_code"] for c in rec["no_record_claims"]} == {"NP1-01", "NP2-01"}
assert {c["item_code"] for c in rec["no_record_claims"]} == {"NP1-01", "NP2-01", "NP5-02"}
def test_cafe24_scope_and_replace_range():
@@ -662,7 +673,9 @@ def test_cafe24_paid_total_matches_daily_report():
assert ev.amount == 46_938 and ev.discount_amount == 1_000 + 5_000
o["actual_order_amount"] = {**o["initial_order_amount"], "payment_amount": "20000.00"}
assert order_total_now(o) == 20_000 + 1_000 + 3_000
assert order_total(_c24_order("Z")) == 47_000 # 결제 필드가 모두 0이면 상품식
z = _c24_order("Z")
z["initial_order_amount"]["payment_amount"] = "0.00"
assert order_total(z) == 0 # 결제 필드가 모두 0이면 카페24도 0원
# 대조 진단: 원본 금액 필드 합계 + 이상 주문(개인정보 숫자 필드는 제외)
import app.timezone as _tz
from app.modules.performance.adapters.base import AdapterContext