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

com.zopen.wechat.pay.action.AtoWechatPayAction Maven / Gradle / Ivy

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

import com.zcj.util.UtilDate;
import com.zcj.util.UtilString;
import com.zcj.util.UtilXml;
import com.zcj.web.dto.ServiceResult;
import com.zopen.wechat.pay.dto.PayCallbackRequest;
import com.zopen.wechat.pay.dto.PayCallbackResponse;
import com.zopen.wechat.pay.service.WechatPayInterface;
import com.zopen.wechat.pay.util.SignUtil;
import com.zopen.ato.properties.WechatPayProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ato/wechat/pay")
public class AtoWechatPayAction {

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

    @Autowired
    private WechatPayProperties wechatPayProperties;

    @Autowired(required = false)
    private WechatPayInterface wechatPayInterface;

    // 支付完成的回调
    @RequestMapping("/callback")
    public String callback(@RequestBody(required = false) String str) {
        String rid = UtilString.getSoleCode();
        logger.info("<<支付回调内容[" + rid + "]:\n" + str);
        PayCallbackResponse response = payCallback(str);
        String result = UtilXml.convertToXml(response);
        logger.info(">>返回回调[" + rid + "]:\n" + result);
        return result;
    }

    /**
     * 支付完成的回调
     *
     * @param body 回调内容
     * @return com.zopen.wechat.pay.dto.PayCallbackResponse
     */
    private PayCallbackResponse payCallback(String body) {

        // 验证是否接收到内容
        if (UtilString.isBlank(body)) {
            return PayCallbackResponse.initError("body为空");
        }
        PayCallbackRequest request = UtilXml.convertXmlStrToObject(PayCallbackRequest.class, body);
        if (request == null) {
            return PayCallbackResponse.initError("body格式错误");
        }

        // 验证返回的结果
        if (!request.success()) {
            logger.error("支付结果回调未正常处理:回调内容{}", request.errorInfo());
            return PayCallbackResponse.initSuccess();
        }

        // 验证签名
        String sign = request.getSign();
        String sign2 = SignUtil.getSign(request, wechatPayProperties.getAppKey());
        if (!sign.equals(sign2)) {
            return PayCallbackResponse.initError("sign错误");
        }

        // 处理支付结果回调
        if (wechatPayInterface == null) {
            logger.error("支付结果回调未正确处理:WechatPayInterface 未初始化");
            return PayCallbackResponse.initError("内部问题,未正常处理");
        }
        ServiceResult sr = wechatPayInterface.payCallback(request.getTotal_fee(), request.getOut_trade_no(),
                request.getTransaction_id(), UtilDate.format(request.getTime_end()));
        if (!sr.success()) {
            logger.error("支付结果回调未正确处理:" + sr.getMsg());
            return PayCallbackResponse.initError("内部问题,未正常处理");
        }

        return PayCallbackResponse.initSuccess();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy