App下載

教你在Springboot項(xiàng)目實(shí)現(xiàn)接入微信app支付的詳細(xì)方法

來源: 擼串研究生 2021-08-16 15:04:13 瀏覽數(shù) (6912)
反饋

現(xiàn)在我們支付往往都會(huì)選擇線上支付,這大大方便了我們的生活,線上支付我們一般會(huì)選擇使用支付寶或者微信來支付。下面,我就為大家分享一篇關(guān)于Springboot中接入微信app支付接口的文章,希望能夠?qū)Ω魑恍』锇橛兴鶐椭?/p>

1.前戲

1.1請(qǐng)先完成微信APP支付接入商戶服務(wù)中心

1.2詳情請(qǐng)參考微信官方文檔:https://open.weixin.qq.com/

2.application.yml文件的配置如下

#微信支付配置
tenpayconfig:
  #商戶APPID
  appId: asdfg12345
  #商戶號(hào)
  mchId: 12345678
  #商戶的key(API密匙)
  key: qwertyuiop
  #API支付請(qǐng)求地址
  payUrl: https://api.mch.weixin.qq.com/pay/unifiedorder
  #API查詢請(qǐng)求地址
  queryUrl: https://api.mch.weixin.qq.com/pay/orderquery
  #package
  packageValue: Sign=WXPay

3.配置文件對(duì)應(yīng)的TenpayConfig,若沒集成lombok請(qǐng)自行生成get/set方法

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;

/** 
 * @Description: 微信支付配置類
 * @Param: 
 * @return: 
 * @Author: zengXianKang
 * @Date: 2019/7/28 
 */
@Data
@Component
@ConfigurationProperties(prefix = "tenpayconfig")
public class TenpayConfig {
    //appId
    private String appId;
    //商戶號(hào)
    private String mchId;
    //商戶的key(API密匙)
    private String key;
    //API支付請(qǐng)求地址
    private String payUrl;
    //API查詢請(qǐng)求地址
    private String queryUrl;
    //Sign=WXPay
    private String packageValue;
}    

3.1新建一個(gè)TenPayVO

import java.math.BigDecimal;
import lombok.Data;

/**
 * @description: TenPayVO
 * @author: zengXianKang
 * @create: 2019-07-28
 **/
@Data
public class TenPayVO {
    //商戶訂單號(hào)
    private String outTradeNo;
    //業(yè)務(wù)結(jié)果
    private String resultCode;
    //簽名方式
    private String signType;
    //簽名
    private String sign;
    //交易類型
    private String tradeType;
    //交易狀態(tài)
    private String tradeState;
    //商戶號(hào)
    private String mchId;
    //付款銀行
    private String bankType;
    //支付金額
    private BigDecimal totalFee;
    //幣種
    private String feeType;
    //微信支付訂單號(hào)
    private String transactionId;
    //支付完成時(shí)間
    private String timeEnd;
}

3.2由于微信支付和回調(diào)的報(bào)文都是xml,先在maven中添加xstream的jar依賴

<!--xstream-->
<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.7</version>
</dependency>

3.3TenPayUtils工具類,直接拿去用吧,該用到的我基本都寫好了,拿走不謝

import com.github.pagehelper.util.StringUtil;
import com.huaku.ecom.common.config.TenpayConfig;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Map.Entry;

/**
 * TenPayUtils
 * @author :zengXianKang
 */
@Component
public class TenPayUtils {
    private static TenPayUtils tenPayUtils;
    @Autowired
    private TenpayConfig tenpayConfig;

    @PostConstruct
    public void init(){
        tenPayUtils = this;
        tenPayUtils.tenpayConfig = this.tenpayConfig;
    }

    /**
     * @Description: 微信支付簽名
     *
     * @Param: [paramsMap, charSetName]
     * @return: java.lang.String
     * @Author: zengXianKang
     * @Date: 2019/7/28 
     */
    public String createSign(SortedMap<String, Object> paramsMap, String charSetName) throws UnsupportedEncodingException,
            NoSuchAlgorithmException {
        StringBuffer buffer = new StringBuffer();
        //參數(shù)按照ACCSII排序(升序)
        Set set = paramsMap.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            if(!key.equals("sign") && StringUtil.isNotEmpty(value)){
                buffer.append(key + "=" + value + "&");
            }
        }
        buffer.append("key=" + tenPayUtils.tenpayConfig.getKey());
        String sign = MDUtils.MD5EncodeForHex(buffer.toString(), charSetName).toUpperCase();
        return sign;
    }
    /**
     * @Description: 組裝微信支付請(qǐng)求報(bào)文
     *
     * @Param: [paramsMap]
     * @return: java.lang.String
     * @Author: zengXianKang
     * @Date: 2019/7/28
     */
    public static String tenPayXmlInfo(SortedMap<String, Object> paramsMap){
        StringBuffer buffer = new StringBuffer();
        if(paramsMap != null){
            buffer.append("<xml>");
            for(Map.Entry<String, Object> entry : paramsMap.entrySet()){
                buffer.append("<").append(entry.getKey()).append("><![CDATA[").append(entry.getValue()).append("]]></").append(entry.getKey()).append(">");
            }
            buffer.append("</xml>");
        }
        return buffer.toString();
    }

    /**
     * @Description: 請(qǐng)求調(diào)用URL
     *
     * @Param: [requestUrl, requestMethod, output]
     * @return: java.lang.String
     * @Author: zengXianKang
     * @Date: 2019/7/28
     */
    public static String httpsRequest(String requestUrl, String requestMethod, String output) throws Exception {
        URL url = new URL(requestUrl);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod(requestMethod);
        if(StringUtil.isNotEmpty(output)){
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(output.getBytes("UTF-8"));
            outputStream.close();
        }
        InputStream inputStream = connection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String str = null;
        StringBuffer buffer = new StringBuffer();
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        connection.disconnect();
        return buffer.toString();
    }
    /**
     * @Description: 解析xml
     *
     * @Param: [xml, rootName, rowName]
     * @return: java.lang.Object
     * @Author: zengXianKang
     * @Date: 2019/7/28
     */
    public static Object readXml(String xml, String rootName, String rowName){
        XStream xStream = new XStream(new DomDriver());
        xStream.alias(rootName, Map.class);
        xStream.registerConverter(new TenPayUtils.MapEntryConverter(rowName));
        Object object = xStream.fromXML(xml);
        return object;
    }
    /** 
     * @Description: 內(nèi)部類,readXml專用
     *
     * @Param: 
     * @return: 
     * @Author: zengXianKang
     * @Date: 2019/7/28
     */
    public static class MapEntryConverter implements Converter {
        private String rowName;

        public MapEntryConverter(String rowName) {
            this.rowName = rowName;
        }

        public boolean canConvert(Class clazz) {
            return Map.class.isAssignableFrom(clazz) || LinkedHashMap.class.isAssignableFrom(clazz);
        }

        public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
            this._marshal(value, writer, context);
        }

        private void _marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
            Iterator i$;
            Object object;
            if (value instanceof Map) {
                Map map = (Map) value;

                for (i$ = map.entrySet().iterator(); i$.hasNext(); writer.endNode()) {
                    object = i$.next();
                    Entry entry = (Entry) object;
                    Object _key = entry.getKey();
                    Object _value = entry.getValue();
                    writer.startNode(entry.getKey().toString());
                    if (_value instanceof Map) {
                        this._marshal(_value, writer, context);
                    } else if (_value instanceof List) {
                        this._marshal(_value, writer, context);
                    } else {
                        writer.setValue(entry.getValue().toString());
                    }
                }
            } else if (value instanceof List) {
                List list = (List) value;

                for (i$ = list.iterator(); i$.hasNext(); writer.endNode()) {
                    object = i$.next();
                    writer.startNode(this.rowName);
                    if (!(object instanceof Map) && !(object instanceof List)) {
                        writer.setValue(object.toString());
                    } else {
                        this._marshal(object, writer, context);
                    }
                }
            }

        }

        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
            return this._unmarshal(reader, context);
        }

        public Object _unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
            Map map = new HashMap();
            List list = new ArrayList();

            boolean isList;
            for (isList = false; reader.hasMoreChildren(); reader.moveUp()) {
                reader.moveDown();
                String nodeName = reader.getNodeName();
                if (reader.hasMoreChildren()) {
                    if (isList) {
                        list.add(this._unmarshal(reader, context));
                    } else if (map.containsKey(nodeName)) {
                        isList = true;
                        list.add(map.remove(nodeName));
                        list.add(this._unmarshal(reader, context));
                    } else if (this.rowName.equals(nodeName)) {
                        isList = true;
                        list.add(this._unmarshal(reader, context));
                    } else {
                        map.put(nodeName, this._unmarshal(reader, context));
                    }
                } else {
                    String value = reader.getValue();
                    if (isList) {
                        list.add(value);
                    } else if (map.containsKey(nodeName)) {
                        isList = true;
                        list.add(map.remove(nodeName));
                        list.add(value);
                    } else if (this.rowName.equals(nodeName)) {
                        isList = true;
                        list.add(value);
                    } else {
                        map.put(nodeName, value);
                    }
                }
            }
            return isList ? list : map;
        }
    }
}

4.請(qǐng)求微信支付

在很多電商的項(xiàng)目中都有類似保存訂單,充值等一系列微信支付場(chǎng)景;我們需要發(fā)起支付返回給前端支付sdk;該篇博文為微信支付的博文,支付寶支付的博文我也已編寫,若有興趣的話可以去我的博文中看看,希望能幫到你。

4.1發(fā)起支付請(qǐng)求

    /** 
     * @Description: 支付請(qǐng)求
     * @Param: [payType, outTradeNo, totalAmount]
     * @return: java.util.Map<java.lang.String,java.lang.String>
     * @Author: zengXianKang
     * @Date: 2019/7/28 
     */
    @Override
    public Map<String, String> payRequest(String payType, String outTradeNo, BigDecimal totalAmount) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        switch (PayTypeEnum.valueOf(payType)) {
            case TENPAY://財(cái)付通
                SortedMap<String, Object> paramsMap = new TreeMap<String, Object>();
                //公眾賬號(hào)ID
                paramsMap.put("appid", tenpayConfig.getAppId());
                //商戶號(hào)
                paramsMap.put("mch_id", tenpayConfig.getMchId());
                //隨機(jī)字符串
                paramsMap.put("nonce_str", Convert.getUUID());
                //描述
                paramsMap.put("body", "名繪優(yōu)家訂單支付");
                //商戶訂單號(hào)(支付編號(hào))
                paramsMap.put("out_trade_no", outTradeNo);
                //支付金額,金額單位為 分
                double price = totalAmount.doubleValue();
                int totalFee = (int) (price * 100);
                paramsMap.put("total_fee", String.valueOf(totalFee));
                //回調(diào)地址
                paramsMap.put("notify_url", ConstantInfo.TENPAY_ORDER_CALLBACK);
                //交易類型
                paramsMap.put("trade_type", "APP");
                //用戶端ip
                String spbillCreateIp = "";
                InetAddress inetAddress = InetAddress.getLocalHost();
                if (inetAddress != null) {
                    spbillCreateIp = inetAddress.getHostAddress();
                }
                paramsMap.put("spbill_create_ip", spbillCreateIp);
                TenPayUtils tenPayUtils = new TenPayUtils();
                //sign簽名
                String sign = tenPayUtils.createSign(paramsMap, "UTF-8");
                paramsMap.put("sign", sign);
                //請(qǐng)求報(bào)文
                String requestXml = TenPayUtils.tenPayXmlInfo(paramsMap);
                //logger.info("微信支付請(qǐng)求報(bào)文: " + requestXml);

                //發(fā)送微信支付post請(qǐng)求
                String tenPayPost = TenPayUtils.httpsRequest(tenpayConfig.getPayUrl(), "POST", requestXml);
                //獲取返回
                Map<String, String> tenPayMap = (Map<String, String>) TenPayUtils.readXml(tenPayPost, "xml", "");
                //微信返回狀態(tài)碼
                if (!tenPayMap.get("return_code").equals("SUCCESS")) {
                    logger.error("微信支付請(qǐng)求連接失敗: " + tenPayMap.get("return_msg"));
                    throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
                }
                //業(yè)務(wù)結(jié)果
                if (!tenPayMap.get("result_code").equals("SUCCESS")) {
                    logger.error("err_code: " + tenPayMap.get("err_code"), "err_code_des: " + tenPayMap.get("err_code_des"));
                    throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
                }
                //APPID
                map.put("appid", tenPayMap.get("appid"));
                //商戶號(hào)
                map.put("partnerid", tenPayMap.get("mch_id"));
                //預(yù)支付交易會(huì)話ID
                map.put("prepayid", tenPayMap.get("prepay_id"));
                //擴(kuò)展字段
                map.put("package", tenpayConfig.getPackageValue());
                //隨機(jī)字符串
                map.put("noncestr", tenPayMap.get("nonce_str"));
                //時(shí)間戳
                map.put("timestamp", String.valueOf(new Date().getTime()).substring(0, 10));
                SortedMap<String, Object> signMap = new TreeMap<>(map);
                String newSign = tenPayUtils.createSign(signMap, "UTF-8");
                //簽名
                map.put("sign", newSign);
                break;
            default:
                break;
        }
        return map;
    }

4.1.1ConstantInfo中的內(nèi)容為

/**
 * 常量
 */
public class ConstantInfo {

    //訂單支付財(cái)付通回調(diào)地址
    public static String TENPAY_ORDER_CALLBACK = "http://mall.gzmhyj.com:8085/huakuEComBuyer/pay/tenPayOrderCallBack";
}

4.2訂單微信支付回調(diào)

該接口為為微信異步回調(diào)提供的接口

    /** 
     * @Description: 訂單微信支付回調(diào)
     *
     * @Param: [request]
     * @return: java.util.Map<java.lang.String,java.lang.String>
     * @Author: zengXianKang
     * @Date: 2019/7/28
     */
    @RequestMapping(value = "/tenPayOrderCallBack", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> tenPayOrderCallBack(HttpServletRequest request){
        Map<String, String> map = new HashMap<String, String>();
        try {
            TenPayVO tenPayVO = payService.tenPayCallBack(request);
            payService.tenPayOrderCallBack(tenPayVO);
            map.put("return_code", "SUCCESS");
            map.put("return_msg", "OK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

4.2.1payService的tenPayCallBack,用于解析回調(diào)的信息,拼接TenPayVO

    /**
     * @Description: 財(cái)付通回調(diào)
     * @Param: [request]
     * @return: TenPayVO
     * @Author: zengXianKang
     * @Date: 2019/7/28
     */
    @Override
    public TenPayVO tenPayCallBack(HttpServletRequest request) throws Exception {
        InputStream inputStream = request.getInputStream();
        StringBuffer resXml = new StringBuffer();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        String str;
        while ((str = bufferedReader.readLine()) != null) {
            resXml.append(str);
        }
        bufferedReader.close();
        inputStream.close();
        //logger.info("微信回調(diào)報(bào)文: " + resXml);
        TenPayVO tenPayVO = this.tenPayCallBackInfo(resXml.toString(), "xml", "");
        return tenPayVO;
    }

4.2.2tenPayCallBackInfo,用于解析微信支付回調(diào)返回結(jié)果,拼接TenPayVO

    /**
     * @Description: 微信支付回調(diào)返回結(jié)果
     * @Param: [xml, rootName, rowName]
     * @return: com.huaku.ecom.system.model.vo.TenPayVO
     * @Author: zengXianKang
     * @Date: 2019/7/28
     */
    private TenPayVO tenPayCallBackInfo(String xml, String rootName, String rowName) throws Exception {
        Map<String, Object> resHashMap = (Map<String, Object>) TenPayUtils.readXml(xml, "xml", "");
        SortedMap<String, Object> resMap = new TreeMap<String, Object>(resHashMap);
        //微信返回狀態(tài)碼
        if (!resMap.get("return_code").equals("SUCCESS")) {
            logger.error("微信支付回調(diào)連接失敗: " + resMap.get("return_msg"));
            throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
        }
        //業(yè)務(wù)結(jié)果
        if (!resMap.get("result_code").equals("SUCCESS")) {
            logger.error("err_code: " + resMap.get("err_code"), "err_code_des: " + resMap.get("err_code_des"));
            throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
        }
        TenPayUtils tenPayUtils = new TenPayUtils();
        //校驗(yàn)簽名
        String sign = tenPayUtils.createSign(resMap, "UTF-8");
        if (!sign.equals(resMap.get("sign"))) {
            logger.error("微信支付回調(diào)簽名不正確");
            throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
        }
        TenPayVO tenPayVO = new TenPayVO();
        //商戶訂單號(hào)
        tenPayVO.setOutTradeNo((String) resMap.get("out_trade_no"));
        //業(yè)務(wù)結(jié)果
        tenPayVO.setResultCode((String) resMap.get("result_code"));
        //簽名方式
        tenPayVO.setSignType("ASCII");
        //簽名
        tenPayVO.setSign((String) resMap.get("sign"));
        //交易類型
        tenPayVO.setTradeType("APP");
        //交易狀態(tài)
        tenPayVO.setTradeState((String) resMap.get("trade_state"));
        //商戶號(hào)
        tenPayVO.setMchId((String) resMap.get("mch_id"));
        //付款銀行
        tenPayVO.setBankType((String) resMap.get("bank_type"));
        //交易金額
        BigDecimal totalFee = new BigDecimal((String) resMap.get("total_fee"));
        totalFee = totalFee.divide(new BigDecimal(100));
        tenPayVO.setTotalFee(totalFee);
        //幣種
        if (resMap.containsKey("fee_type")) {
            tenPayVO.setFeeType((String) resMap.get("fee_type"));
        }
        //微信支付訂單號(hào)
        tenPayVO.setTransactionId((String) resMap.get("transaction_id"));
        //支付完成時(shí)間
        tenPayVO.setTimeEnd((String) resMap.get("time_end"));
        return tenPayVO;
    }

4.2.3payService的tenPayOrderCallBack,用于自己的回調(diào)完成的業(yè)務(wù)邏輯,如修改訂單狀態(tài),存微信支付交易表等操作

    /**
     * @Description: 微信支付訂單回調(diào)
     * @Param: [tenPayVO]
     * @return: void
     * @Author: zengXianKang
     * @Date: 2019/7/28
     */
    @Override
    public void tenPayOrderCallBack(TenPayVO tenPayVO) throws Exception {
        if (tenPayVO != null && tenPayVO.getResultCode().equals("SUCCESS") && tenPayVO.getTradeState().equals("SUCCESS")) {
            //根據(jù)交易編號(hào)加鎖,處理高并發(fā)
            synchronized (tenPayVO.getOutTradeNo()) {
                TOrder order = orderMapper.getOneOrderByPayNo(tenPayVO.getOutTradeNo());
                if (order.getOrderStatus().equals(OrderStatusEnum.PENDING_PAYMENT.toString())) {
                    //訂單需支付金額總和
                    BigDecimal payNumSum = this.getPayNumSumByPayNo(tenPayVO.getOutTradeNo());
                    String orderStatus = "";
                    //以防萬(wàn)一,再次校驗(yàn)金額
                    if (payNumSum.compareTo(tenPayVO.getTotalFee()) != 0) {
                        logger.error("***訂單號(hào): " + tenPayVO.getOutTradeNo() + "***微信支付支付金額與訂單需支付金額總和不一致***微信支付金額為:" + tenPayVO.getTotalFee() + " ***訂單需支付金額總為:" + payNumSum + "***日期:" + new Date());
                        //金額異常,訂單狀態(tài)為支付金額異常
                        orderStatus = OrderStatusEnum.ABNORMAL_PAYMENT_AMOUNT.toString();
                    } else {
                        //金額正常,訂單狀態(tài)為已付款(待發(fā)貨)
                        orderStatus = OrderStatusEnum.WAIT_FOR_DELIVERY.toString();
                    }
                    //修改訂單狀態(tài)
                    int orderFlag = orderMapper.updatePayOrderStatusByPayNo(orderStatus, tenPayVO.getOutTradeNo());
                    //微信支付交易記錄表
                    TTenpayTradeLog tenpayTradeLog = new TTenpayTradeLog();
                    tenpayTradeLog.setTradeLogId(Convert.createUniqueId(idWorker));
                    //簽名方式
                    tenpayTradeLog.setSignType(tenPayVO.getSignType());
                    //交易方式
                    tenpayTradeLog.setTradeMode(tenPayVO.getTradeType());
                    //交易狀態(tài)
                    tenpayTradeLog.setTradeStatus(tenPayVO.getResultCode());
                    //商戶號(hào)
                    tenpayTradeLog.setPartner(tenPayVO.getMchId());
                    //銀行類型
                    tenpayTradeLog.setBankType(tenPayVO.getBankType());
                    //交易金額
                    tenpayTradeLog.setTotalFee(tenPayVO.getTotalFee());
                    //幣種
                    tenpayTradeLog.setFeeType(tenPayVO.getFeeType());
                    //微信支付訂單號(hào)
                    tenpayTradeLog.setTransactionId(tenPayVO.getTransactionId());
                    //商戶訂單號(hào)
                    tenpayTradeLog.setOutTradeNo(tenPayVO.getOutTradeNo());
                    //支付完成時(shí)間
                    tenpayTradeLog.setTimeEnd(tenPayVO.getTimeEnd());

                    int payFlag = tenpayTradeLogMapper.insertSelective(tenpayTradeLog);

                    //若有一個(gè)操作出錯(cuò),拋錯(cuò)回滾
                    if (!(orderFlag > 0 && payFlag == 1)) {
                        logger.error("微信支付訂單回調(diào)失敗");
                        throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
                    }
                } else {
                    logger.info("該訂單已支付處理,交易編號(hào)為: " + tenPayVO.getOutTradeNo());
                    throw new RRException(AppWSConstant.RS_MSG_ORDER_PAY_ERROR);
                }
            }
        }
    }

4.3定時(shí)任務(wù)主動(dòng)查詢微信支付回調(diào),一般微信發(fā)起的異步回調(diào)都是無序不定時(shí)的,所以一般保險(xiǎn)起見都會(huì)寫一個(gè)自己的定時(shí)任務(wù)主動(dòng)查詢微信支付回調(diào)

    /**
     * 定時(shí)任務(wù):每十五分鐘觸發(fā)一次主動(dòng)調(diào)用訂單支付回調(diào)
     */
    @Scheduled(cron = "0 */15 * * * ?")
    public void initiativeOrderPayCallBack(){
        //主動(dòng)調(diào)用訂單支付回調(diào)
        try {
            payService.initiativeOrderPayCallBack();
        } catch (Exception e) {
            logger.error("timer initiativeOrderPayCallBack Error.", e);
            e.printStackTrace();
        }
    }

4.3.1payService的initiativeOrderPayCallBack,用于主動(dòng)查詢微信支付回調(diào)與回調(diào)業(yè)務(wù)邏輯處理

    /**
     * 主動(dòng)調(diào)用訂單支付回調(diào)
     *
     * @throws Exception
     */
    @Override
    public void initiativeOrderPayCallBack() throws Exception {
        //查詢訂單狀態(tài)為orderStatus的支付編號(hào)
        List<Map<String, String>> payNoList = orderMapper.getPayNoByStatus(OrderStatusEnum.PENDING_PAYMENT.toString());
        for (Map<String, String> map : payNoList) {
            try {
                switch (PayTypeEnum.valueOf(map.get("payType"))) {
                    case TENPAY://財(cái)付通
                        TenPayVO tenPayVO = this.tenPayQueryCallBack(map.get("payNo"));
                        //訂單回調(diào)處理
                        this.tenPayOrderCallBack(tenPayVO);
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
                e.printStackTrace();
            }
        }
    }

4.3.2payService的tenPayQueryCallBack,用于主動(dòng)查詢微信支付回調(diào),拼接TenPayVO

    /**
     * @Description: 微信支付主動(dòng)查詢回調(diào)
     * @Param: [payNo]
     * @return: com.huaku.ecom.system.model.vo.TenPayVO
     * @Author: zengXianKang
     * @Date: 2019/5/30
     */
    @Override
    public TenPayVO tenPayQueryCallBack(String payNo) throws Exception {
        SortedMap<String, Object> paramsMap = new TreeMap<String, Object>();
        //應(yīng)用APPID
        paramsMap.put("appid", tenpayConfig.getAppId());
        //商戶號(hào)
        paramsMap.put("mch_id", tenpayConfig.getMchId());
        //商戶訂單號(hào)
        paramsMap.put("out_trade_no", payNo);
        //隨機(jī)字符串
        paramsMap.put("nonce_str", Convert.getUUID());
        TenPayUtils tenPayUtils = new TenPayUtils();
        //簽名
        String sign = tenPayUtils.createSign(paramsMap, "UTF-8");
        paramsMap.put("sign", sign);
        //請(qǐng)求報(bào)文
        String requestXml = TenPayUtils.tenPayXmlInfo(paramsMap);
        //發(fā)送微信查詢post請(qǐng)求
        String tenQueryPost = TenPayUtils.httpsRequest(tenpayConfig.getQueryUrl(), "POST", requestXml);

        TenPayVO tenPayVO = this.tenPayCallBackInfo(tenQueryPost, "xml", "");
        return tenPayVO;
    }

到此這篇關(guān)于springboot接入微信app支付的方法的文章就介紹到這了,更多相關(guān)springboot App微信支付內(nèi)容,請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持我們!


0 人點(diǎn)贊