package com.talentcard.server.business.payment;
import java.util.List;
import java.util.Map;
import com.talentcard.server.business.DatabaseRows;
import com.talentcard.server.business.payment.huifu.HuifuPayload;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* The single idempotent entry point that turns a provider-confirmed successful payment into local
* state.
*
*
A callback and a reconciliation query must produce the same business result, so both describe
* what the provider said as a {@link ProviderPaymentFact} and let this service decide. The fact can
* only settle the payment transaction its request number names, and before anything changes it is
* compared with the order's fixed collecting sub-merchant and the transaction amount. A statement
* that cannot be verified — another merchant, a missing merchant number, a missing or different
* amount — is recorded as an open reconciliation issue and the order is never marked paid. Repeated
* facts for the same payment converge on the same state: the payment transaction is settled once, and
* the order is only moved from pending to paid.
*/
@Service
public class PaymentSettlementService {
static final String PROVIDER = "HUIFU";
static final String ISSUE_MERCHANT_MISMATCH = "PAYMENT_MERCHANT_MISMATCH";
static final String ISSUE_MERCHANT_UNVERIFIED = "PAYMENT_MERCHANT_UNVERIFIED";
static final String ISSUE_AMOUNT_MISMATCH = "PAYMENT_AMOUNT_MISMATCH";
static final String ISSUE_COLLECTOR_UNKNOWN = "PAYMENT_COLLECTOR_UNKNOWN";
static final String ISSUE_ORDER_CANCELLED = "PAYMENT_ORDER_CANCELLED";
private final JdbcTemplate jdbc;
private final PaymentIssueRecorder issues;
private final com.talentcard.server.business.order.OrderVerificationService orderVerification;
public PaymentSettlementService(JdbcTemplate jdbc, PaymentIssueRecorder issues,
@org.springframework.context.annotation.Lazy com.talentcard.server.business.order.OrderVerificationService orderVerification) {
this.jdbc = jdbc;
this.issues = issues;
this.orderVerification = orderVerification;
}
@Transactional
public SettlementOutcome settle(ProviderPaymentFact fact) {
List