yeg
2026-09-28 85bfb9351803d97a33329d507f30a2f78bf1fc75
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.talentcard.server.business.payment;
 
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.talentcard.server.business.merchant.MerchantSettlementService;
import com.talentcard.server.business.payment.huifu.HuifuSignatureService;
import com.talentcard.server.common.BusinessException;
import com.talentcard.server.common.ErrorCode;
 
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/api/payments/huifu")
public class PaymentCallbackController {
 
    private final HuifuPaymentCallbackService callbackService;
    private final HuifuSignatureService signatureService;
    private final MerchantSettlementService settlementService;
 
    public PaymentCallbackController(HuifuPaymentCallbackService callbackService, HuifuSignatureService signatureService, MerchantSettlementService settlementService) {
        this.callbackService = callbackService;
        this.signatureService = signatureService;
        this.settlementService = settlementService;
    }
 
    @PostMapping(value = "/notify", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> notify(@RequestParam Map<String, String> form) {
        return ResponseEntity.ok(callbackService.handle(form.get("resp_data"), form.get("sign")));
    }
 
    @PostMapping(value = "/merchant-business-notify", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> merchantBusinessNotify(@RequestParam Map<String, String> form) {
        String payload = form.get("resp_data"), signature = form.get("sign");
        if (!signatureService.verifyCallbackPayload(payload, signature)) throw new BusinessException(ErrorCode.FORBIDDEN, "Invalid Huifu merchant business callback signature");
        @SuppressWarnings("unchecked") Map<String, Object> data = JSON.parseObject(payload, Map.class);
        String no = data.get("req_seq_id") == null ? "" : String.valueOf(data.get("req_seq_id")).trim();
        if (!settlementService.handleBankCardCallback(data)) throw new BusinessException(ErrorCode.NOT_FOUND, "Unknown Huifu merchant business callback");
        return ResponseEntity.ok("RECV_ORD_ID_" + no);
    }
 
    @PostMapping(value = "/merchant-onboarding-notify", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> merchantOnboardingNotify(@RequestParam Map<String, String> form) {
        String payload = form.get("resp_data"), signature = form.get("sign");
        if (!signatureService.verifyCallbackPayload(payload, signature)) throw new BusinessException(ErrorCode.FORBIDDEN, "Invalid Huifu merchant onboarding callback signature");
        @SuppressWarnings("unchecked") Map<String, Object> data = JSON.parseObject(payload, Map.class);
        String no = data.get("req_seq_id") == null ? "" : String.valueOf(data.get("req_seq_id")).trim();
        if (!settlementService.handleOnboardingCallback(data)) throw new BusinessException(ErrorCode.NOT_FOUND, "Unknown Huifu merchant onboarding callback");
        return ResponseEntity.ok("RECV_ORD_ID_" + no);
    }
 
    @PostMapping(value = "/merchant-wechat-config-notify", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> merchantWechatConfigNotify(@RequestParam Map<String, String> form) {
        Map<String, Object> data = verifiedData(form, "WeChat merchant configuration");
        if (!settlementService.handleWechatConfigCallback(data)) throw new BusinessException(ErrorCode.NOT_FOUND, "Unknown Huifu WeChat merchant configuration callback");
        return ResponseEntity.ok("RECV_ORD_ID_" + acknowledgementId(data));
    }
 
    @PostMapping(value = "/merchant-business-open-notify", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> merchantBusinessOpenNotify(@RequestParam Map<String, String> form) {
        Map<String, Object> data = verifiedData(form, "merchant business opening");
        if (!settlementService.handleBusinessOpenCallback(data)) throw new BusinessException(ErrorCode.NOT_FOUND, "Unknown Huifu merchant business opening callback");
        return ResponseEntity.ok("RECV_ORD_ID_" + acknowledgementId(data));
    }
 
    @SuppressWarnings("unchecked")
    private Map<String, Object> verifiedData(Map<String, String> form, String operation) {
        String payload = form.get("resp_data"), signature = form.get("sign");
        if (!signatureService.verifyCallbackPayload(payload, signature)) throw new BusinessException(ErrorCode.FORBIDDEN, "Invalid Huifu " + operation + " callback signature");
        Map<String, Object> callback = JSON.parseObject(payload, Map.class);
        Object nested = callback.get("data");
        if (nested instanceof Map<?, ?> value) return (Map<String, Object>) value;
        if (nested instanceof String value && !value.isBlank()) return JSON.parseObject(value, Map.class);
        return callback;
    }
 
    private String acknowledgementId(Map<String, Object> data) {
        Object value = data.get("ord_id");
        if (value == null || String.valueOf(value).isBlank()) value = data.get("req_seq_id");
        return value == null ? "" : String.valueOf(value).trim();
    }
}