RcbPaymentController.java
4.32 KB
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
87
88
89
90
91
92
93
94
package com.diligrp.cashier.boss.controller;
import com.diligrp.cashier.boss.exception.BossServiceException;
import com.diligrp.cashier.pipeline.core.RcbOnlinePipeline;
import com.diligrp.cashier.pipeline.domain.OnlinePaymentResponse;
import com.diligrp.cashier.pipeline.service.IPaymentPipelineManager;
import com.diligrp.cashier.pipeline.type.OutPaymentType;
import com.diligrp.cashier.pipeline.type.PaymentState;
import com.diligrp.cashier.pipeline.util.RcbSignatureUtils;
import com.diligrp.cashier.pipeline.util.RcbStateUtils;
import com.diligrp.cashier.shared.ErrorCode;
import com.diligrp.cashier.shared.util.JsonUtils;
import com.diligrp.cashier.shared.util.ObjectUtils;
import com.diligrp.cashier.trade.model.OnlinePayment;
import com.diligrp.cashier.trade.service.ICashierPaymentService;
import com.diligrp.cashier.trade.service.ITradeAssistantService;
import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;
@RestController
@RequestMapping(value = "/rcb")
public class RcbPaymentController {
private static final Logger LOG = LoggerFactory.getLogger(RcbPaymentController.class);
@Resource
private ITradeAssistantService tradeAssistantService;
@Resource
private ICashierPaymentService cashierPaymentService;
@Resource
private IPaymentPipelineManager paymentPipelineManager;
/**
* 支付结果通知
*/
@RequestMapping(value = "/payment/{paymentId}/notify.do")
public ResponseEntity<?> paymentNotify(HttpServletRequest request, @PathVariable("paymentId") String paymentId) {
String payload = request.getParameter("order");
LOG.info("Receiving rcb payment pipeline result: {}\n{}", paymentId, payload);
try {
LocalDateTime when = LocalDateTime.now();
Map<String, String> params = JsonUtils.fromJsonString(payload, new TypeReference<>(){});
String sign = params.remove("sign");
String source = RcbSignatureUtils.map2String(params);
OnlinePayment payment = tradeAssistantService.findByPaymentId(paymentId);
RcbOnlinePipeline pipeline = paymentPipelineManager.findPipelineById(payment.getPipelineId(), RcbOnlinePipeline.class);
RcbOnlinePipeline.RcbParams config = pipeline.params();
if (!RcbSignatureUtils.verify(source, config.getKey(), sign)) {
LOG.error("Rcb pipeline data sign verify failed");
throw new BossServiceException(ErrorCode.UNAUTHORIZED_ACCESS_ERROR, "Data sign verify failed");
}
// String paymentId = params.get("outTradeNo");
PaymentState paymentState = RcbStateUtils.paymentState(params.get("orderStatus"));
String outTradeNo = params.get("cposOrderId");
String paidTime = params.get("paidTime");
if (ObjectUtils.isNotEmpty(paidTime)) {
long timestamp = Long.parseLong(paidTime); // ⽀付完成时间戳
Instant instant = Instant.ofEpochMilli(timestamp);
when = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}
OutPaymentType outPayType = RcbStateUtils.outPayType(params.get("tradeChannel"));
String payerId = params.get("payUserInfo");
String errorDesc = params.get("errorDesc");
// String outOrderId = params.get("chnOrderId"); // 第三方支付通道的订单号
OnlinePaymentResponse paymentResponse = new OnlinePaymentResponse(paymentId, outTradeNo, outPayType,
payerId, when, paymentState, errorDesc);
cashierPaymentService.notifyPaymentResponse(paymentResponse);
return ResponseEntity.ok("SUCCESS");
} catch (Exception ex) {
LOG.error("Process rcb payment result exception", ex);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("FAILED");
}
}
}