feat(performance): 카페24 주문 매출 수집 연동 + 과거 매출 가져오기
카페24 (Admin API GET /admin/orders, 문서 2026-09-01 기준) - 결제(SALE): date_type=pay_date, embed=items — 결제된 주문의 품목 결제금액(할인 반영·배송비 제외), 결제일 - 취소(CANCEL): cancel_complete_date, 품목 C40~C43 만 음수(입금전취소 C47~C49 제외) - 반품(RETURN): return_complete_date, 품목 R40~R43 음수, 부분 반품은 claim_quantity 비율 - 자체몰 주문만(market_id=self) — 카페24로 연동된 다른 마켓 주문은 이중 집계 방지로 제외 - 60일 단위 분할(한 호출 3개월 미만), limit 1000 페이지 넘김, 개인정보 미저장(raw 는 금액·상태만) - 기존 app/integrations/cafe24 토큰·클라이언트 재사용, 연결 테스트는 주문 권한까지 확인 - DEFAULT_SCOPES 에 mall.read_order 추가(읽기만) — 카페24 상품관리 → 시스템에서 재연결 필요 과거 매출 가져오기(설정, 관리자): 기간 지정 수집(최대 366일), 일반 사용자는 403 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -378,6 +378,91 @@ def test_not_implemented_and_mock():
|
||||
assert event_row(a[0])["sales_date"] == a[0].payment_date
|
||||
|
||||
|
||||
# ════════════════════════════════════════════════════════════
|
||||
# 카페24 어댑터(가짜 API 응답)
|
||||
# ════════════════════════════════════════════════════════════
|
||||
def _c24_item(code, status="N40", price="30000.00", qty=1, pay="29000.00", **kw):
|
||||
return {"order_item_code": code, "order_status": status, "product_name": "냄비", "option_value": "",
|
||||
"product_price": price, "option_price": "0.00", "quantity": qty, "payment_amount": pay,
|
||||
"coupon_discount_price": "1000.00", "claim_quantity": 0, **kw}
|
||||
|
||||
|
||||
def _c24_order(oid, items, market="self", paid="T", **kw):
|
||||
return {"order_id": oid, "market_id": market, "paid": paid, "order_date": "2026-09-27T10:00:00+09:00",
|
||||
"payment_date": "2026-09-28T11:21:35+09:00", "buyer_name": "홍길동", "receiver_address": "서울시",
|
||||
"bank_account_no": "1234567890", "items": items, **kw}
|
||||
|
||||
|
||||
class _FakeCafe24(__import__("app.modules.performance.adapters.cafe24", fromlist=["Cafe24Adapter"]).Cafe24Adapter):
|
||||
def __init__(self, pages):
|
||||
self.pages = pages # date_type → [응답 orders 목록(페이지별)]
|
||||
self.calls = []
|
||||
|
||||
def authenticate(self, ctx):
|
||||
return None
|
||||
|
||||
def _get(self, ctx, params):
|
||||
self.calls.append(params)
|
||||
pages = self.pages.get(params["date_type"], [])
|
||||
idx = params.get("offset", 0) // 1000
|
||||
return {"orders": pages[idx] if idx < len(pages) else []}
|
||||
|
||||
|
||||
def test_cafe24_normalize():
|
||||
from app.modules.performance.adapters.cafe24 import Cafe24Adapter, item_amount
|
||||
|
||||
paid = _c24_order("20260928-01", [_c24_item("20260928-01-01"), _c24_item("20260928-01-02", pay="10000.00")])
|
||||
market = _c24_order("20260928-02", [_c24_item("20260928-02-01")], market="shopn") # 스마트스토어 연동 주문
|
||||
unpaid = _c24_order("20260928-03", [_c24_item("20260928-03-01", status="N00")], paid="F")
|
||||
canceled = _c24_order("20260928-01", [
|
||||
_c24_item("20260928-01-01", status="C40", cancel_date="2026-09-29 09:00:00"),
|
||||
_c24_item("20260928-01-02", status="N40")])
|
||||
precancel = _c24_order("20260928-04", [_c24_item("20260928-04-01", status="C48", cancel_date="2026-09-29 09:00:00")])
|
||||
returned = _c24_order("20260928-05", [_c24_item("20260928-05-01", status="R40", qty=3, pay="90000.00",
|
||||
claim_quantity=1, return_confirmed_date="2026-09-30 10:00:00")])
|
||||
fake = _FakeCafe24({"pay_date": [[paid, market, unpaid]],
|
||||
"cancel_complete_date": [[canceled, precancel]],
|
||||
"return_complete_date": [[returned]]})
|
||||
ctx = __import__("app.modules.performance.adapters.base", fromlist=["AdapterContext"]).AdapterContext(
|
||||
code="cafe24", credentials=None)
|
||||
fetched, events = fake.collect(ctx, date(2026, 9, 23), date(2026, 9, 30))
|
||||
by = {(e.event_type, e.external_line_id): e for e in events}
|
||||
assert fetched == 6
|
||||
assert by[("SALE", "20260928-01-01")].amount == 29_000 and by[("SALE", "20260928-01-02")].amount == 10_000
|
||||
assert by[("SALE", "20260928-01-01")].event_date == date(2026, 9, 28) # 결제일
|
||||
assert ("SALE", "20260928-02-01") not in by and ("SALE", "20260928-03-01") not in by # 마켓 주문·미결제 제외
|
||||
c = by[("CANCEL", "20260928-01-01")]
|
||||
assert c.amount == -29_000 and c.event_date == date(2026, 9, 29) and c.external_event_id == "20260928-01-01-C"
|
||||
assert ("CANCEL", "20260928-01-02") not in by and ("CANCEL", "20260928-04-01") not in by # 미취소·입금전취소 제외
|
||||
r = by[("RETURN", "20260928-05-01")]
|
||||
assert r.amount == -30_000 and r.quantity == -1 and r.event_date == date(2026, 9, 30) # 3개 중 1개 반품
|
||||
for e in events:
|
||||
assert "buyer_name" not in e.raw and "receiver_address" not in e.raw and "bank_account_no" not in e.raw
|
||||
# 금액: payment_amount 없으면 (판매가+옵션)×수량 − 할인
|
||||
assert item_amount({"product_price": "10000.00", "option_price": "500.00", "quantity": 2,
|
||||
"coupon_discount_price": "1000.00"}) == 20_000
|
||||
# 60일 단위 분할 + 페이지 넘김(1000건이면 다음 페이지)
|
||||
page1 = [_c24_order(f"x-{i}", []) for i in range(1000)]
|
||||
fake2 = _FakeCafe24({"pay_date": [page1, [paid]]})
|
||||
got = list(fake2.fetch_orders(ctx, date(2026, 7, 1), date(2026, 9, 30)))
|
||||
pay_calls = [c for c in fake2.calls if c["date_type"] == "pay_date"]
|
||||
assert len(got) == 2002 # 60일 구간 2개 × (1000건 페이지 + 다음 페이지 1건)
|
||||
assert pay_calls[0]["start_date"] == "2026-07-01" and pay_calls[0]["end_date"] == "2026-08-29"
|
||||
assert pay_calls[1]["offset"] == 1000 and all(c["limit"] == 1000 and c["embed"] == "items" for c in pay_calls)
|
||||
assert any(c["start_date"] == "2026-08-30" for c in pay_calls)
|
||||
# 주문 권한 없는 토큰 → 인증 실패(재연결 안내)
|
||||
real = Cafe24Adapter()
|
||||
real.shared_status = lambda ctx: {"connected": True, "scopes": "mall.read_product,mall.write_product"}
|
||||
try:
|
||||
real.authenticate(ctx)
|
||||
except AuthFailed as exc:
|
||||
assert "mall.read_order" in exc.message
|
||||
else:
|
||||
raise AssertionError("주문 권한 없으면 인증 실패여야 합니다")
|
||||
real.shared_status = lambda ctx: {"connected": True, "scopes": "mall.read_product,mall.read_order"}
|
||||
real.authenticate(ctx)
|
||||
|
||||
|
||||
def test_registry_and_event_validation():
|
||||
codes = [a.code for a in all_adapters()]
|
||||
assert len(codes) == len(set(codes)) == 10
|
||||
|
||||
Reference in New Issue
Block a user