package com.talentcard.server.business.payment;
import com.talentcard.server.business.DatabaseRows;
import com.talentcard.server.business.payment.huifu.HuifuPayload;
import com.talentcard.server.business.payment.huifu.HuifuPayload.TransStatus;
import com.talentcard.server.business.payment.PaymentSettlementService.ProviderPaymentFact;
import com.talentcard.server.business.payment.PaymentSettlementService.SettlementOutcome;
import com.talentcard.server.business.payment.huifu.HuifuMiniProgramPaymentGateway;
import com.talentcard.server.business.payment.huifu.HuifuPaymentGatewayException;
import com.talentcard.server.business.payment.huifu.HuifuPaymentQueryCommand;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
/**
* Second confirmation path for payments whose callback did not arrive or whose outcome was unknown.
*
*
A query result is validated exactly like a callback: it is described as a
* {@link ProviderPaymentFact} and settled by {@link PaymentSettlementService}, so both paths produce
* the same local result. A query that cannot confirm the payment leaves the transaction pending and
* records an open issue instead of guessing.
*/
@Service
public class PaymentReconciliationService {
private static final String PROVIDER = "HUIFU";
private static final DateTimeFormatter REQUEST_DATE = DateTimeFormatter.ofPattern("yyyyMMdd");
private final JdbcTemplate jdbc;
private final HuifuMiniProgramPaymentGateway paymentGateway;
private final HuifuRefundService refundService;
private final PaymentSettlementService settlementService;
private final PaymentIssueRecorder issues;
public PaymentReconciliationService(JdbcTemplate jdbc, HuifuMiniProgramPaymentGateway paymentGateway, HuifuRefundService refundService,
PaymentSettlementService settlementService, PaymentIssueRecorder issues) {
this.jdbc = jdbc;
this.paymentGateway = paymentGateway;
this.refundService = refundService;
this.settlementService = settlementService;
this.issues = issues;
}
public Map reconcile(int pendingAfterMinutes) {
int paymentChecked = reconcilePayments(pendingAfterMinutes);
int refundChecked = reconcileRefunds(pendingAfterMinutes);
return Map.of("paymentChecked", paymentChecked, "refundChecked", refundChecked);
}
private int reconcilePayments(int pendingAfterMinutes) {
List