@RequestMapping(value = '/yunpay/app/aliPayAppPayV2Notify', method = RequestMethod.POST) public void aliPayAppPayV2Notify(HttpServletRequest req, HttpServletResponse resp) { LOGGER.info('支付宝app(钱包)支付V2.0->交易结果通知->开始'); long start = System.currentTimeMillis(); dealWhitAliPayAppV2Req(req, resp, 'aliPayAppPayV2Notify', ALIPAY_APPPAY_V2_KEY); long end = System.currentTimeMillis(); LOGGER.info('支付宝app(钱包)支付V2.0->交易结果通知->结束,耗时:{}', end - start); }

private void dealWhitAliPayAppV2Req(HttpServletRequest req,
                                    HttpServletResponse resp, String transCode, String redisKey) {
    /* 编码格式 */
    String charset = req.getCharacterEncoding();
    if (StringUtil.isEmpty(charset)) {
        charset = CHARSET;
    }

// String merNo = ''; JSONObject paramMap = getParams(req); // 使用JSONObject接收参数 if (null == paramMap) { LOGGER.error('支付宝手机app(钱包)支付V2->钱包支付结果异步通知->请求报文为空'); return; } LOGGER.info('支付宝手机app(钱包)支付V2->钱包支付结果异步通知->请求数据:{}', paramMap);

    //读取惠付发往通道订单号
    String merNo = paramMap.getString(AliPayV2CommonConstans.APP_ID);
    JSONObject secureFld = null;
    try {
        //截取通道商户号//读取安全域

// merNo = serialNo.substring(0, 16); //共38位 通道商户号为16位 Map<String, String> secureFldMap = redisInstance.getObj(HashMap.class, redisKey); String secureFldStr = secureFldMap.get(merNo); secureFld = JSONObject.parseObject(secureFldStr); } catch (Exception e) { LOGGER.error('支付宝手机app(钱包)支付V2->钱包支付结果异步通知->安全域读取失败或解析失败:{}', e); return; } if (secureFld == null) { LOGGER.error('支付宝手机app(钱包)支付V2->钱包支付结果异步通知->安全域读取失败或解析失败'); return; } try { LOGGER.info('请求客户端地址:{}', HttpUtils.getReqIPAddr(req)); // //除去sign、sign_type两个参数 // paramMap.remove(AliPayV2CommonConstans.SIGN); // paramMap.remove(AliPayV2CommonConstans.SIGN_TYPE); //验签 调用支付宝SDK boolean signVerify = false; if (alipayV2Flag) { signVerify = true; } else { signVerify = AlipaySignature.rsaCheckV1(paramMap, secureFld.getString(AliPayV2CommonConstans.ALIPAY_PUBLIC_KEY), charset, AliPayV2CommonConstans.SIGN_TYPE_RSA2); } if (signVerify) { //验签成功后对比交易请求的金额与异步返回是否一致 String total_amount = secureFld.getString(AliPayV2CommonConstans.TOTAL_AMOUNT);//支付金额 // JSONArray array = JSONArray.parseArray(paramMap.get('fund_bill_list')); String returnAmout = paramMap.getString(AliPayV2CommonConstans.TOTAL_AMOUNT);//返回金额 if (!StringUtil.equals(total_amount, returnAmout)){ LOGGER.error('支付宝APP支付V2->APP支付结果异步通知->支付与返回的交易金额不一致'); return; } IYunPayGateWayService notifyService = yunPayGateWayServiceFactory .getServiceByServiceName(transCode); notifyService.execute(paramMap); // 这里需要修改notifyService.execute()方法,接受JSONObject参数 writeToResp(resp, AliPayV2CommonConstans.SUCCESS); } else { LOGGER.error('异步通知异常:{}', '验签失败'); throw new SanxiaPayBizException( EnumRespMsg.VERIFY_FAILED.getRespCode(), EnumRespMsg.VERIFY_FAILED.getRespMsg()); } } catch (Exception e) { LOGGER.error('异步通知异常:{}', e); } }

private void writeToResp(HttpServletResponse resp, String response) {
    resp.setContentType('application/json');
    resp.setCharacterEncoding('UTF-8');
    try (PrintWriter writer = resp.getWriter()) {
        writer.write(response);
    } catch (IOException e) {
        LOGGER.error('Error writing response: {}', e);
    }
}
支付宝APP支付V2.0交易结果异步通知

原文地址: https://www.cveoy.top/t/topic/pTSs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录