All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zopen.alipay.pay.action.AtoAliPayAction Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.zopen.alipay.pay.action;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.zcj.util.UtilDate;
import com.zcj.util.UtilRandom;
import com.zcj.web.dto.ServiceResult;
import com.zopen.alipay.pay.dto.AlipayNotifyDto;
import com.zopen.alipay.pay.service.AliPayInterface;
import com.zopen.ato.properties.AlipayProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("/ato/alipay/pay")
public class AtoAliPayAction {

    private static final Logger logger = LoggerFactory.getLogger(AtoAliPayAction.class);

    @Autowired
    private AlipayProperties alipayProperties;

    @Autowired(required = false)
    private AliPayInterface aliPayInterface;

    // 支付成功的异步调用地址
    @ResponseBody
    @RequestMapping("/callback")
    public String callback(HttpServletRequest request, AlipayNotifyDto alipayNotifyDto) {

        String logId = UtilRandom.getRandomChar(4);
        logger.info("[{}]支付宝异步回调:{}", logId, alipayNotifyDto);

        // 验证签名
        boolean flag = false;
        try {
            flag = AlipaySignature.rsaCheckV1(initParams(request), alipayProperties.getAlipayPublicKey(), "UTF-8", "RSA2");
        } catch (AlipayApiException e) {
            logger.warn("[" + logId + "]支付宝回调的签名验证出错:" + e.getMessage(), e);
            return "failure";
        }
        if (!flag) {
            logger.warn("[{}]支付宝回调的签名验证失败", logId);
            return "failure";
        }

        // 验证 app_id 是否为该商户本身
        if (alipayNotifyDto == null) {
            logger.error("[{}]支付宝回调的参数获取失败", logId);
            return "failure";
        } else if (!alipayProperties.getAppId().equals(alipayNotifyDto.getApp_id())) {
            logger.warn("[{}]支付宝回调处理失败:appId错误,本appId[{}]、收到的appId[{}]", logId, alipayProperties.getAppId(), alipayNotifyDto.getApp_id());
            return "failure";
        }

        // 处理回调
        ServiceResult sr = aliPayInterface.payCallback(alipayNotifyDto.getOut_trade_no(), alipayNotifyDto.getTrade_no(),
                alipayNotifyDto.getTotal_amount(), alipayNotifyDto.getRefund_fee(), alipayNotifyDto.getSeller_id(), alipayNotifyDto.getSeller_email(),
                alipayNotifyDto.getTrade_status(), UtilDate.format(alipayNotifyDto.getGmt_payment()), UtilDate.format(alipayNotifyDto.getGmt_refund()));
        if (!sr.success()) {
            logger.warn("[{}]支付宝回调处理失败:{}", logId, sr.getMsg());
            return "failure";
        }

        return "success";
    }

    // 支付成功的同步调用地址(由于同步返回的不可靠性,支付结果必须以异步通知或查询接口返回为准,不能依赖同步跳转)
    @RequestMapping("/callback-syn")
    public String callbackSyn(HttpServletRequest request, AlipayNotifyDto alipayNotifyDto) {
        String logId = UtilRandom.getRandomChar(4);
        logger.info("[{}]支付宝同步回调:{}", logId, alipayNotifyDto);
        return alipayProperties.getPaySuccessUrl();
    }

    // 获取支付宝 POST 过来反馈信息
    private Map initParams(HttpServletRequest request) {
        Map params = new HashMap<>(32);
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            /// 乱码解决,这段代码在出现乱码时使用。
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        return params;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy