| | |
| | | package com.talentcard.server.business.payment; |
| | | |
| | | import java.util.Map; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.talentcard.server.business.merchant.HuifuMerchantWithdrawalService; |
| | | 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; |
| | |
| | | public class PaymentCallbackController { |
| | | |
| | | private final HuifuPaymentCallbackService callbackService; |
| | | private final HuifuMerchantWithdrawalService withdrawalService; |
| | | private final HuifuSignatureService signatureService; |
| | | |
| | | public PaymentCallbackController(HuifuPaymentCallbackService callbackService) { |
| | | public PaymentCallbackController(HuifuPaymentCallbackService callbackService, HuifuMerchantWithdrawalService withdrawalService, HuifuSignatureService signatureService) { |
| | | this.callbackService = callbackService; |
| | | this.withdrawalService = withdrawalService; |
| | | this.signatureService = signatureService; |
| | | } |
| | | |
| | | @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 = "/withdrawal-notify", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.TEXT_PLAIN_VALUE) |
| | | public ResponseEntity<String> withdrawalNotify(@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 withdrawal 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 (no.isBlank() || !withdrawalService.exists(no)) throw new BusinessException(ErrorCode.NOT_FOUND, "Unknown Huifu withdrawal callback"); |
| | | withdrawalService.handleCallback(no, data); |
| | | return ResponseEntity.ok("RECV_ORD_ID_" + no); |
| | | } |
| | | } |