feat(ecommerce): 현금영수증 프로바이더 훅 축 + 이력 테이블 + 현금성 금액 + 배송비 과세 정책

현금영수증 발급 프로바이더를 PG 와 독립적으로 선택할 수 있도록 훅 축을 신설한다.
KG이니시스로 결제받으면서 현금영수증만 토스페이먼츠로 발급하는 구성이 가능하다.

발급/취소 원장(ecommerce_order_cash_receipts)이 국세청 신고 근거를 보관한다.
금액이 바뀌면 syncFromOrder 가 주문의 현재 상태에서 발급액을 재계산해
전체취소 → 전액 재발급한다. 부분취소 API 를 쓰지 않으므로 전액취소만 지원하는
벤더도 동일 인터페이스로 수용되고, 증가/감소 방향을 구분하지 않아 향후 반품·교환
배송비 청구가 도입되어도 수정 없이 동작한다.

배송비는 그동안 과세/면세 분류에서 아예 빠져 있었다. 배송비는 단계 3 에서 계산되고
할인은 단계 4 에서 적용되므로 상품 분류 시점(단계 2-b)에는 존재하지 않는다.
따라서 할인 후 배송비가 확정된 Summary 집계 직전에 3가지 정책 중 하나로 분류해
합산하고, 옵션별 과세액에는 배송비를 섞지 않는다.

재발급용 식별번호는 APP_KEY 기반으로 암호화 보관하고 구매확정 시 폐기한다.
이력에는 마스킹 값만 남기며 프로바이더 원응답의 민감 키도 가린다.
주민등록번호는 수집하지 않는다.

cash_receipt_type 은 Enum 캐스트하지 않는다 — 레거시 값(income_deduction)이 남아
있는 동안 Laravel 의 ::from 이 ValueError 를 던져 정규화해야 할 업그레이드 스텝
자신이 그 행을 읽지 못하게 되기 때문이다.
This commit is contained in:
HeuJung committed 2026-08-06 11:48:12 +09:00
1 parent 5b587d7119
commit af1da5fe0b
46 files changed
+3986 -16

No files matched your search

@@ -29,6 +29,7 @@ use Modules\Sirsoft\Ecommerce\Enums\ProductTaxStatus;
use Modules\Sirsoft\Ecommerce\Enums\ShippingApiAuthType;
use Modules\Sirsoft\Ecommerce\Enums\ShippingApiHttpMethod;
use Modules\Sirsoft\Ecommerce\Enums\ShippingApiResponseType;
use Modules\Sirsoft\Ecommerce\Enums\ShippingFeeTaxPolicy;
use Modules\Sirsoft\Ecommerce\Models\Coupon;
use Modules\Sirsoft\Ecommerce\Models\CouponIssue;
use Modules\Sirsoft\Ecommerce\Models\ShippingPolicy;
@@ -984,6 +985,98 @@ class OrderCalculationService
return $classification;
}
/**
* 배송비 할인을 반영한 실질 배송비를 반환합니다.
*
* @param array $paymentCalculation 결제금액 계산 결과
* @return int 할인 후 배송비 (0 이상)
*/
protected function resolveNetShippingFee(array $paymentCalculation): int
{
$totalShipping = (int) ($paymentCalculation['total_shipping'] ?? 0);
$shippingDiscount = (int) ($paymentCalculation['shipping_discount'] ?? 0);
return max(0, $totalShipping - $shippingDiscount);
}
/**
* 단계 2-c: 배송비를 과세/면세로 분류합니다.
*
* 부가가치세법 제14조는 주된 재화에 부수되는 용역이 주된 재화의 과세/면세를 따른다고 정할 뿐,
* 과세·면세 상품이 혼합된 장바구니의 배송비 안분에 관한 명문 규정은 없다.
* 상점의 세무 판단에 따라 3가지 정책 중 선택한다.
*
* 상품 분류(classifyTaxStatus)와 달리 이 메서드는 배송비 할인이 확정된 뒤에 호출되어야 한다.
*
* @param int $netShippingFee 할인 후 배송비
* @param int $productTaxable 상품 과세금액 합계
* @param int $productTaxFree 상품 면세금액 합계
* @return array{taxable_amount: int, tax_free_amount: int} 배송비 과세/면세 분류
*/
protected function classifyShippingFeeTaxStatus(
int $netShippingFee,
int $productTaxable,
int $productTaxFree,
): array {
if ($netShippingFee <= 0) {
return ['taxable_amount' => 0, 'tax_free_amount' => 0];
}
$policy = $this->resolveShippingFeeTaxPolicy();
$productTotal = $productTaxable + $productTaxFree;
// 상품 금액이 전부 0 이면 안분 기준이 없다 — 전액 과세로 처리한다.
if ($productTotal <= 0) {
return ['taxable_amount' => $netShippingFee, 'tax_free_amount' => 0];
}
return match ($policy) {
ShippingFeeTaxPolicy::TAXABLE => [
'taxable_amount' => $netShippingFee,
'tax_free_amount' => 0,
],
ShippingFeeTaxPolicy::FOLLOW_MAIN_ITEM => $productTaxable >= $productTaxFree
? ['taxable_amount' => $netShippingFee, 'tax_free_amount' => 0]
: ['taxable_amount' => 0, 'tax_free_amount' => $netShippingFee],
ShippingFeeTaxPolicy::PROPORTIONAL => $this->apportionShippingFeeTax(
$netShippingFee,
$productTaxable,
$productTotal,
),
};
}
/**
* 배송비를 과세상품 비율만큼 안분합니다.
*
* 면세분을 반올림해 산출한 뒤 잔차를 과세 쪽에 귀속시켜 합계를 보존한다.
*
* @param int $netShippingFee 할인 후 배송비
* @param int $productTaxable 상품 과세금액 합계
* @param int $productTotal 상품 총액 (과세 + 면세)
* @return array{taxable_amount: int, tax_free_amount: int} 안분 결과
*/
protected function apportionShippingFeeTax(int $netShippingFee, int $productTaxable, int $productTotal): array
{
$taxableShipping = (int) round($netShippingFee * $productTaxable / $productTotal);
$taxableShipping = max(0, min($taxableShipping, $netShippingFee));
return [
'taxable_amount' => $taxableShipping,
'tax_free_amount' => $netShippingFee - $taxableShipping,
];
}
/**
* 배송비 과세 정책을 조회합니다.
*
* @return ShippingFeeTaxPolicy 배송비 과세 정책
*/
protected function resolveShippingFeeTaxPolicy(): ShippingFeeTaxPolicy
{
return app(EcommerceSettingsService::class)->getShippingFeeTaxPolicy();
}
/**
* 단계 3: 배송비를 계산합니다 (정책별 그룹).
*
@@ -1499,6 +1592,20 @@ class OrderCalculationService
// (전체 과세표준에 단일 세율을 적용하면 면세·영세·다른 세율 상품이 섞였을 때 어긋난다)
$totalVat = VatCalculator::sumFromClassification($taxClassification);
// 배송비 과세 분류 — 상품 분류(단계 2-b)와 달리 할인 후 최종 배송비가 확정된 이 시점에서만
// 계산할 수 있다. 옵션별 taxable_amount 는 상품분만 담으므로 배송비는 Summary 에만 합산한다.
$shippingTax = $this->classifyShippingFeeTaxStatus(
$this->resolveNetShippingFee($paymentCalculation),
$totalTaxable,
$totalTaxFree,
);
$totalTaxable += $shippingTax['taxable_amount'];
$totalTaxFree += $shippingTax['tax_free_amount'];
// 배송비 과세분에 내재된 부가세도 합산 — 배송비는 옵션별 세율이 없으므로 기본 세율을 적용한다
// (과세표준(taxableAmount)에 배송비가 포함되므로 부가세도 같은 기준을 따라야 정합)
$totalVat += VatCalculator::fromTaxableAmount($shippingTax['taxable_amount']);
$summary = new Summary(
subtotal: $paymentCalculation['subtotal'],
productCouponDiscount: $paymentCalculation['coupon_discount'],