1.接受回调处理的接口
@RestController
@Slf4j
public class BaseNotifyController extends BaseController {
@Autowired
NotifyFactory notifyFactory;
/**
* 接受支付宝统一回调处理
*
* @param req
* @return
*/
@PostMapping(value = "/alipay/notify/baseCallBack")
ResponseResult<String> baseCallBack(@RequestBody AliPayNotifyReq req) {
log.info("【接受支付宝统一回调处理】请求参数:{}", JSONObject.toJSONString(req));
ResponseResult<String> re = new ResponseResult<>();
try {
String data = req.getNotifyData();
JSONObject content = JSONObject.parseObject(data);
String msg_method = content.getString("msg_method");
NotifyHandleService valueHandle = notifyFactory.getHandle(msg_method);
re = valueHandle.notifyHandle(req);
} catch (Exception e) {
log.error("【接受支付宝统一回调处理】error:{}", e);
return ResponseResult.build(-1,"支付宝统一回调处理异常",null);
}
log.info("【接受支付宝统一回调处理】返回结果:{}", JSONObject.toJSONString(re));
return re;
}
}
2.定义工厂方法
@Component
public class NotifyFactory {
static Map<String, String> operationMap = new HashMap<>();
static {
operationMap.put(NotifyMsgMethodEnum.ZMGO_SIGN_NOTIFY.getCode(), ZmGoSignNotifyServiceImpl.class.getName());
operationMap.put(NotifyMsgMethodEnum.ZMGO_AGREEMENT_CHANGED.getCode(), ZmGoAgreementChangedNotifyServiceImpl.class.getName());
operationMap.put(NotifyMsgMethodEnum.ZMGO_SETTLE_NOTIFY.getCode(), ZmGoSettleSignNotifyServiceImpl.class.getName());
}
public NotifyHandleService getHandle(String msg_method) throws Exception {
String className = operationMap.get(msg_method);
if (className == null) {
throw new Exception("msg_method:" + msg_method + " not fund");
}
Class cs = Class.forName(className);
NotifyHandleService handle = (NotifyHandleService) ApplicationContextHelper.getBean(cs);
return handle;
}
3.每个业务提供实现类
@Slf4j
@Service
public class ZmGoSignNotifyServiceImpl extends NotifyBaseImpl implements NotifyHandleService {
@Override
@Transactional
public ResponseResult<String> notifyHandle(AliPayNotifyReq req) {
//处理具体的业务
return ResponseResult.buildSuccessResponse();
}
}
4.NotifyBaseImpl 提供通用的验签和其他处理
@Service
@Slf4j
public class NotifyBaseImpl {
@Resource
private AliPayConfiguration aliPayConfig;
/**
* 校验签名
*
* @para notifyData
* @return
* @throws Exception
*/
public boolean alipaySignCheck(Map<String, String> notifyData) throws Exception {
log.info("【notifyData】:{}", JSONObject.toJSONString(notifyData));
boolean check = AlipaySignature.rsaCheckV1(notifyData, aliPayConfig.getPublicKey(), AliPayConfiguration.UTF8, "RSA2");
log.info("【验签结果】:{}",check);
return check;
}
/**
* 获取bizContent对象
*/
JSONObject getBizContent(AliPayNotifyReq req) throws Exception {
if (req == null) {
throw new Exception("AliPayNotifyReq not empty");
}
String notify = req.getNotifyData();
JSONObject notifyJson = JSONObject.parseObject(notify);
if (notifyJson == null) {
throw new Exception("notifyData not empty");
}
String bizContent = notifyJson.getString("biz_content");
if (bizContent == null) {
throw new Exception("bizContent not empty");
}
JSONObject bizContentJson = JSONObject.parseObject(bizContent);
return bizContentJson;
}