【微信支付统一下单】JAVA与XML请求内容体的双向映射

【微信支付统一下单】JAVA与XML请求内容体的双向映射,第1张

微信支付统一下单接口文档中,请求体body和微信端返回值都是xml格式的内容。

例如:


<xml>
   <appid>wx2421b1c4370ec43bappid>
   <attach>支付测试attach>
   <body>JSAPI支付测试body>
   <mch_id>10000100mch_id>
   <detail>detail>
   <nonce_str>1add1a30ac87aa2db72f57a2375d8fecnonce_str>
   <notify_url>https://wxpay.wxutil.com/pub_v2/pay/notify.v2.phpnotify_url>
   <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6oopenid>
   <out_trade_no>1415659990out_trade_no>
   <spbill_create_ip>14.23.150.211spbill_create_ip>
   <total_fee>1total_fee>
   <trade_type>JSAPItrade_type>
   <sign>0CB01533B8C1EF103065174F50BCA001sign>
xml>


<xml>
   <return_code>return_code>
   <return_msg>return_msg>
   <appid>appid>
   <mch_id>mch_id>
   <nonce_str>nonce_str>
   <openid>openid>
   <sign>sign>
   <result_code>result_code>
   <prepay_id>prepay_id>
   <trade_type>trade_type>
xml>

面向对象的开发过程中,最好是实现持久的转化层,便于XML请求内容与JAVA类的双向转化。

此时就可以使用simpleframework。例如使用simpleframework简单的解析XML文件等案例。

要实现JAVA类与微信支付下单请求的XML类型双向转化,包括3个步骤:
1、 构造映射文件
2、编写XML工具类
3、调用工具类进行转化,实现微信支付的统一下单

一、构造映射文件 (一) 请求XML对应的JAVA类

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(
    name = "xml",
    strict = false
)
public class WxPayUnifiedorderRequest {
    @Element(
        name = "appid"
    )
    private String appid;
    @Element(
        name = "mch_id"
    )
    private String mchId;
    @Element(
        name = "nonce_str"
    )
    private String nonceStr;
    @Element(
        name = "sign"
    )
    private String sign;
    @Element(
        name = "attach",
        required = false
    )
    private String attach;
    @Element(
        name = "body",
        required = false
    )
    private String body;
    @Element(
        name = "detail",
        required = false
    )
    private String detail;
    @Element(
        name = "notify_url"
    )
    private String notifyUrl;
    @Element(
        name = "openid",
        required = false
    )
    private String openid;
    @Element(
        name = "out_trade_no"
    )
    private String outTradeNo;
    @Element(
        name = "spbill_create_ip"
    )
    private String spbillCreateIp;
    @Element(
        name = "total_fee"
    )
    private Integer totalFee;
    @Element(
        name = "trade_type"
    )
    private String tradeType;

    public WxPayUnifiedorderRequest() {
    }

    public String getAppid() {
        return this.appid;
    }

    public String getMchId() {
        return this.mchId;
    }

    public String getNonceStr() {
        return this.nonceStr;
    }

    public String getSign() {
        return this.sign;
    }

    public String getAttach() {
        return this.attach;
    }

    public String getBody() {
        return this.body;
    }

    public String getDetail() {
        return this.detail;
    }

    public String getNotifyUrl() {
        return this.notifyUrl;
    }

    public String getOpenid() {
        return this.openid;
    }

    public String getOutTradeNo() {
        return this.outTradeNo;
    }

    public String getSpbillCreateIp() {
        return this.spbillCreateIp;
    }

    public Integer getTotalFee() {
        return this.totalFee;
    }

    public String getTradeType() {
        return this.tradeType;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public void setAttach(String attach) {
        this.attach = attach;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.outTradeNo = outTradeNo;
    }

    public void setSpbillCreateIp(String spbillCreateIp) {
        this.spbillCreateIp = spbillCreateIp;
    }

    public void setTotalFee(Integer totalFee) {
        this.totalFee = totalFee;
    }

    public void setTradeType(String tradeType) {
        this.tradeType = tradeType;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof WxPayUnifiedorderRequest)) {
            return false;
        } else {
            WxPayUnifiedorderRequest other = (WxPayUnifiedorderRequest)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label167: {
                    Object this$appid = this.getAppid();
                    Object other$appid = other.getAppid();
                    if (this$appid == null) {
                        if (other$appid == null) {
                            break label167;
                        }
                    } else if (this$appid.equals(other$appid)) {
                        break label167;
                    }

                    return false;
                }

                Object this$mchId = this.getMchId();
                Object other$mchId = other.getMchId();
                if (this$mchId == null) {
                    if (other$mchId != null) {
                        return false;
                    }
                } else if (!this$mchId.equals(other$mchId)) {
                    return false;
                }

                label153: {
                    Object this$nonceStr = this.getNonceStr();
                    Object other$nonceStr = other.getNonceStr();
                    if (this$nonceStr == null) {
                        if (other$nonceStr == null) {
                            break label153;
                        }
                    } else if (this$nonceStr.equals(other$nonceStr)) {
                        break label153;
                    }

                    return false;
                }

                Object this$sign = this.getSign();
                Object other$sign = other.getSign();
                if (this$sign == null) {
                    if (other$sign != null) {
                        return false;
                    }
                } else if (!this$sign.equals(other$sign)) {
                    return false;
                }

                label139: {
                    Object this$attach = this.getAttach();
                    Object other$attach = other.getAttach();
                    if (this$attach == null) {
                        if (other$attach == null) {
                            break label139;
                        }
                    } else if (this$attach.equals(other$attach)) {
                        break label139;
                    }

                    return false;
                }

                Object this$body = this.getBody();
                Object other$body = other.getBody();
                if (this$body == null) {
                    if (other$body != null) {
                        return false;
                    }
                } else if (!this$body.equals(other$body)) {
                    return false;
                }

                label125: {
                    Object this$detail = this.getDetail();
                    Object other$detail = other.getDetail();
                    if (this$detail == null) {
                        if (other$detail == null) {
                            break label125;
                        }
                    } else if (this$detail.equals(other$detail)) {
                        break label125;
                    }

                    return false;
                }

                label118: {
                    Object this$notifyUrl = this.getNotifyUrl();
                    Object other$notifyUrl = other.getNotifyUrl();
                    if (this$notifyUrl == null) {
                        if (other$notifyUrl == null) {
                            break label118;
                        }
                    } else if (this$notifyUrl.equals(other$notifyUrl)) {
                        break label118;
                    }

                    return false;
                }

                Object this$openid = this.getOpenid();
                Object other$openid = other.getOpenid();
                if (this$openid == null) {
                    if (other$openid != null) {
                        return false;
                    }
                } else if (!this$openid.equals(other$openid)) {
                    return false;
                }

                label104: {
                    Object this$outTradeNo = this.getOutTradeNo();
                    Object other$outTradeNo = other.getOutTradeNo();
                    if (this$outTradeNo == null) {
                        if (other$outTradeNo == null) {
                            break label104;
                        }
                    } else if (this$outTradeNo.equals(other$outTradeNo)) {
                        break label104;
                    }

                    return false;
                }

                label97: {
                    Object this$spbillCreateIp = this.getSpbillCreateIp();
                    Object other$spbillCreateIp = other.getSpbillCreateIp();
                    if (this$spbillCreateIp == null) {
                        if (other$spbillCreateIp == null) {
                            break label97;
                        }
                    } else if (this$spbillCreateIp.equals(other$spbillCreateIp)) {
                        break label97;
                    }

                    return false;
                }

                Object this$totalFee = this.getTotalFee();
                Object other$totalFee = other.getTotalFee();
                if (this$totalFee == null) {
                    if (other$totalFee != null) {
                        return false;
                    }
                } else if (!this$totalFee.equals(other$totalFee)) {
                    return false;
                }

                Object this$tradeType = this.getTradeType();
                Object other$tradeType = other.getTradeType();
                if (this$tradeType == null) {
                    if (other$tradeType != null) {
                        return false;
                    }
                } else if (!this$tradeType.equals(other$tradeType)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof WxPayUnifiedorderRequest;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $appid = this.getAppid();
        int result = result * 59 + ($appid == null ? 43 : $appid.hashCode());
        Object $mchId = this.getMchId();
        result = result * 59 + ($mchId == null ? 43 : $mchId.hashCode());
        Object $nonceStr = this.getNonceStr();
        result = result * 59 + ($nonceStr == null ? 43 : $nonceStr.hashCode());
        Object $sign = this.getSign();
        result = result * 59 + ($sign == null ? 43 : $sign.hashCode());
        Object $attach = this.getAttach();
        result = result * 59 + ($attach == null ? 43 : $attach.hashCode());
        Object $body = this.getBody();
        result = result * 59 + ($body == null ? 43 : $body.hashCode());
        Object $detail = this.getDetail();
        result = result * 59 + ($detail == null ? 43 : $detail.hashCode());
        Object $notifyUrl = this.getNotifyUrl();
        result = result * 59 + ($notifyUrl == null ? 43 : $notifyUrl.hashCode());
        Object $openid = this.getOpenid();
        result = result * 59 + ($openid == null ? 43 : $openid.hashCode());
        Object $outTradeNo = this.getOutTradeNo();
        result = result * 59 + ($outTradeNo == null ? 43 : $outTradeNo.hashCode());
        Object $spbillCreateIp = this.getSpbillCreateIp();
        result = result * 59 + ($spbillCreateIp == null ? 43 : $spbillCreateIp.hashCode());
        Object $totalFee = this.getTotalFee();
        result = result * 59 + ($totalFee == null ? 43 : $totalFee.hashCode());
        Object $tradeType = this.getTradeType();
        result = result * 59 + ($tradeType == null ? 43 : $tradeType.hashCode());
        return result;
    }

    public String toString() {
        return "WxPayUnifiedorderRequest(appid=" + this.getAppid() + ", mchId=" + this.getMchId() + ", nonceStr=" + this.getNonceStr() + ", sign=" + this.getSign() + ", attach=" + this.getAttach() + ", body=" + this.getBody() + ", detail=" + this.getDetail() + ", notifyUrl=" + this.getNotifyUrl() + ", openid=" + this.getOpenid() + ", outTradeNo=" + this.getOutTradeNo() + ", spbillCreateIp=" + this.getSpbillCreateIp() + ", totalFee=" + this.getTotalFee() + ", tradeType=" + this.getTradeType() + ")";
    }
}

(二)返回值XML的映射JAVA类
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.lly835.bestpay.model.wxpay.response;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(
    name = "xml",
    strict = false
)
public class WxPaySyncResponse {
    @Element(
        name = "return_code"
    )
    private String returnCode;
    @Element(
        name = "return_msg",
        required = false
    )
    private String returnMsg;
    @Element(
        name = "appid",
        required = false
    )
    private String appid;
    @Element(
        name = "mch_id",
        required = false
    )
    private String mchId;
    @Element(
        name = "device_info",
        required = false
    )
    private String deviceInfo;
    @Element(
        name = "nonce_str",
        required = false
    )
    private String nonceStr;
    @Element(
        name = "sign",
        required = false
    )
    private String sign;
    @Element(
        name = "result_code",
        required = false
    )
    private String resultCode;
    @Element(
        name = "err_code",
        required = false
    )
    private String errCode;
    @Element(
        name = "err_code_des",
        required = false
    )
    private String errCodeDes;
    @Element(
        name = "trade_type",
        required = false
    )
    private String tradeType;
    @Element(
        name = "prepay_id",
        required = false
    )
    private String prepayId;
    @Element(
        name = "code_url",
        required = false
    )
    private String codeUrl;
    @Element(
        name = "mweb_url",
        required = false
    )
    private String mwebUrl;

    public WxPaySyncResponse() {
    }

    public String getReturnCode() {
        return this.returnCode;
    }

    public String getReturnMsg() {
        return this.returnMsg;
    }

    public String getAppid() {
        return this.appid;
    }

    public String getMchId() {
        return this.mchId;
    }

    public String getDeviceInfo() {
        return this.deviceInfo;
    }

    public String getNonceStr() {
        return this.nonceStr;
    }

    public String getSign() {
        return this.sign;
    }

    public String getResultCode() {
        return this.resultCode;
    }

    public String getErrCode() {
        return this.errCode;
    }

    public String getErrCodeDes() {
        return this.errCodeDes;
    }

    public String getTradeType() {
        return this.tradeType;
    }

    public String getPrepayId() {
        return this.prepayId;
    }

    public String getCodeUrl() {
        return this.codeUrl;
    }

    public String getMwebUrl() {
        return this.mwebUrl;
    }

    public void setReturnCode(String returnCode) {
        this.returnCode = returnCode;
    }

    public void setReturnMsg(String returnMsg) {
        this.returnMsg = returnMsg;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public void setDeviceInfo(String deviceInfo) {
        this.deviceInfo = deviceInfo;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public void setErrCode(String errCode) {
        this.errCode = errCode;
    }

    public void setErrCodeDes(String errCodeDes) {
        this.errCodeDes = errCodeDes;
    }

    public void setTradeType(String tradeType) {
        this.tradeType = tradeType;
    }

    public void setPrepayId(String prepayId) {
        this.prepayId = prepayId;
    }

    public void setCodeUrl(String codeUrl) {
        this.codeUrl = codeUrl;
    }

    public void setMwebUrl(String mwebUrl) {
        this.mwebUrl = mwebUrl;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof WxPaySyncResponse)) {
            return false;
        } else {
            WxPaySyncResponse other = (WxPaySyncResponse)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$returnCode = this.getReturnCode();
                Object other$returnCode = other.getReturnCode();
                if (this$returnCode == null) {
                    if (other$returnCode != null) {
                        return false;
                    }
                } else if (!this$returnCode.equals(other$returnCode)) {
                    return false;
                }

                Object this$returnMsg = this.getReturnMsg();
                Object other$returnMsg = other.getReturnMsg();
                if (this$returnMsg == null) {
                    if (other$returnMsg != null) {
                        return false;
                    }
                } else if (!this$returnMsg.equals(other$returnMsg)) {
                    return false;
                }

                Object this$appid = this.getAppid();
                Object other$appid = other.getAppid();
                if (this$appid == null) {
                    if (other$appid != null) {
                        return false;
                    }
                } else if (!this$appid.equals(other$appid)) {
                    return false;
                }

                label158: {
                    Object this$mchId = this.getMchId();
                    Object other$mchId = other.getMchId();
                    if (this$mchId == null) {
                        if (other$mchId == null) {
                            break label158;
                        }
                    } else if (this$mchId.equals(other$mchId)) {
                        break label158;
                    }

                    return false;
                }

                label151: {
                    Object this$deviceInfo = this.getDeviceInfo();
                    Object other$deviceInfo = other.getDeviceInfo();
                    if (this$deviceInfo == null) {
                        if (other$deviceInfo == null) {
                            break label151;
                        }
                    } else if (this$deviceInfo.equals(other$deviceInfo)) {
                        break label151;
                    }

                    return false;
                }

                Object this$nonceStr = this.getNonceStr();
                Object other$nonceStr = other.getNonceStr();
                if (this$nonceStr == null) {
                    if (other$nonceStr != null) {
                        return false;
                    }
                } else if (!this$nonceStr.equals(other$nonceStr)) {
                    return false;
                }

                label137: {
                    Object this$sign = this.getSign();
                    Object other$sign = other.getSign();
                    if (this$sign == null) {
                        if (other$sign == null) {
                            break label137;
                        }
                    } else if (this$sign.equals(other$sign)) {
                        break label137;
                    }

                    return false;
                }

                label130: {
                    Object this$resultCode = this.getResultCode();
                    Object other$resultCode = other.getResultCode();
                    if (this$resultCode == null) {
                        if (other$resultCode == null) {
                            break label130;
                        }
                    } else if (this$resultCode.equals(other$resultCode)) {
                        break label130;
                    }

                    return false;
                }

                Object this$errCode = this.getErrCode();
                Object other$errCode = other.getErrCode();
                if (this$errCode == null) {
                    if (other$errCode != null) {
                        return false;
                    }
                } else if (!this$errCode.equals(other$errCode)) {
                    return false;
                }

                Object this$errCodeDes = this.getErrCodeDes();
                Object other$errCodeDes = other.getErrCodeDes();
                if (this$errCodeDes == null) {
                    if (other$errCodeDes != null) {
                        return false;
                    }
                } else if (!this$errCodeDes.equals(other$errCodeDes)) {
                    return false;
                }

                label109: {
                    Object this$tradeType = this.getTradeType();
                    Object other$tradeType = other.getTradeType();
                    if (this$tradeType == null) {
                        if (other$tradeType == null) {
                            break label109;
                        }
                    } else if (this$tradeType.equals(other$tradeType)) {
                        break label109;
                    }

                    return false;
                }

                label102: {
                    Object this$prepayId = this.getPrepayId();
                    Object other$prepayId = other.getPrepayId();
                    if (this$prepayId == null) {
                        if (other$prepayId == null) {
                            break label102;
                        }
                    } else if (this$prepayId.equals(other$prepayId)) {
                        break label102;
                    }

                    return false;
                }

                Object this$codeUrl = this.getCodeUrl();
                Object other$codeUrl = other.getCodeUrl();
                if (this$codeUrl == null) {
                    if (other$codeUrl != null) {
                        return false;
                    }
                } else if (!this$codeUrl.equals(other$codeUrl)) {
                    return false;
                }

                Object this$mwebUrl = this.getMwebUrl();
                Object other$mwebUrl = other.getMwebUrl();
                if (this$mwebUrl == null) {
                    if (other$mwebUrl != null) {
                        return false;
                    }
                } else if (!this$mwebUrl.equals(other$mwebUrl)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof WxPaySyncResponse;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $returnCode = this.getReturnCode();
        int result = result * 59 + ($returnCode == null ? 43 : $returnCode.hashCode());
        Object $returnMsg = this.getReturnMsg();
        result = result * 59 + ($returnMsg == null ? 43 : $returnMsg.hashCode());
        Object $appid = this.getAppid();
        result = result * 59 + ($appid == null ? 43 : $appid.hashCode());
        Object $mchId = this.getMchId();
        result = result * 59 + ($mchId == null ? 43 : $mchId.hashCode());
        Object $deviceInfo = this.getDeviceInfo();
        result = result * 59 + ($deviceInfo == null ? 43 : $deviceInfo.hashCode());
        Object $nonceStr = this.getNonceStr();
        result = result * 59 + ($nonceStr == null ? 43 : $nonceStr.hashCode());
        Object $sign = this.getSign();
        result = result * 59 + ($sign == null ? 43 : $sign.hashCode());
        Object $resultCode = this.getResultCode();
        result = result * 59 + ($resultCode == null ? 43 : $resultCode.hashCode());
        Object $errCode = this.getErrCode();
        result = result * 59 + ($errCode == null ? 43 : $errCode.hashCode());
        Object $errCodeDes = this.getErrCodeDes();
        result = result * 59 + ($errCodeDes == null ? 43 : $errCodeDes.hashCode());
        Object $tradeType = this.getTradeType();
        result = result * 59 + ($tradeType == null ? 43 : $tradeType.hashCode());
        Object $prepayId = this.getPrepayId();
        result = result * 59 + ($prepayId == null ? 43 : $prepayId.hashCode());
        Object $codeUrl = this.getCodeUrl();
        result = result * 59 + ($codeUrl == null ? 43 : $codeUrl.hashCode());
        Object $mwebUrl = this.getMwebUrl();
        result = result * 59 + ($mwebUrl == null ? 43 : $mwebUrl.hashCode());
        return result;
    }

    public String toString() {
        return "WxPaySyncResponse(returnCode=" + this.getReturnCode() + ", returnMsg=" + this.getReturnMsg() + ", appid=" + this.getAppid() + ", mchId=" + this.getMchId() + ", deviceInfo=" + this.getDeviceInfo() + ", nonceStr=" + this.getNonceStr() + ", sign=" + this.getSign() + ", resultCode=" + this.getResultCode() + ", errCode=" + this.getErrCode() + ", errCodeDes=" + this.getErrCodeDes() + ", tradeType=" + this.getTradeType() + ", prepayId=" + this.getPrepayId() + ", codeUrl=" + this.getCodeUrl() + ", mwebUrl=" + this.getMwebUrl() + ")";
    }
}

二、编写XML工具类

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlUtil {
    public XmlUtil() {
    }

   /**
   * 将xml转化为java类
   **/
    public static Object toObject(String xml, Class objClass) {
        Persister serializer = new Persister();

        try {
            return serializer.read(objClass, xml);
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }

   /**
   * 将java类转化为xml
   **/
    public static String toString(Object obj) {
        Serializer serializer = new Persister();
        StringWriter output = new StringWriter();

        try {
            serializer.write(obj, output);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return output.toString();
    }

    public static Map<String, String> toMap(String strXML) {
        try {
            Map<String, String> data = new HashMap();
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
            Document doc = documentBuilder.parse(stream);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getDocumentElement().getChildNodes();

            for(int idx = 0; idx < nodeList.getLength(); ++idx) {
                Node node = nodeList.item(idx);
                if (node.getNodeType() == 1) {
                    Element element = (Element)node;
                    data.put(element.getNodeName(), element.getTextContent());
                }
            }

            try {
                stream.close();
            } catch (Exception var10) {
            }

            return data;
        } catch (Exception var11) {
            var11.printStackTrace();
            return null;
        }
    }
}
三、统一下单接口实现
public class WxPayServiceImpl {
    private static final Logger log = LoggerFactory.getLogger(WxPayServiceImpl.class);
    private WxPayConfig wxPayConfig;
    private Retrofit retrofit;

    public WxPayServiceImpl() {
        this.retrofit = (new Builder()).baseUrl("https://api.mch.weixin.qq.com/").addConverterFactory(SimpleXmlConverterFactory.create()).client((new okhttp3.OkHttpClient.Builder()).addInterceptor((new HttpLoggingInterceptor()).setLevel(Level.BODY)).build()).build();
    }

    public void setWxPayConfig(WxPayConfig wxPayConfig) {
        this.wxPayConfig = wxPayConfig;
    }

    public PayResponse pay(PayRequest request) {
        WxPayUnifiedorderRequest wxRequest = new WxPayUnifiedorderRequest();
        wxRequest.setOutTradeNo(request.getOrderId());
        wxRequest.setTotalFee(MoneyUtil.Yuan2Fen(request.getOrderAmount()));
        wxRequest.setBody(request.getOrderName());
        wxRequest.setOpenid(request.getOpenid());
        wxRequest.setTradeType(request.getPayTypeEnum().getCode());
        wxRequest.setAppid(this.wxPayConfig.getAppId());
        wxRequest.setMchId(this.wxPayConfig.getMchId());
        wxRequest.setNotifyUrl(this.wxPayConfig.getNotifyUrl());
        wxRequest.setNonceStr(RandomUtil.getRandomStr());
        wxRequest.setSpbillCreateIp(StringUtils.isEmpty(request.getSpbillCreateIp()) ? "8.8.8.8" : request.getSpbillCreateIp());
        wxRequest.setAttach(request.getAttach());
        wxRequest.setSign(WxPaySignature.sign(MapUtil.buildMap(wxRequest), this.wxPayConfig.getMchKey()));
        // XmlUtil.toString(wxRequest)),实现java object--> xml string
        RequestBody body = RequestBody.create(MediaType.parse("application/xml; charset=utf-8"), XmlUtil.toString(wxRequest));
        Call<WxPaySyncResponse> call = ((WxPayApi)this.retrofit.create(WxPayApi.class)).unifiedorder(body);
        Response retrofitResponse = null;

        try {
            retrofitResponse = call.execute();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        assert retrofitResponse != null;

        if (!retrofitResponse.isSuccessful()) {
            throw new RuntimeException("【微信统一支付】发起支付, 网络异常");
        } else {
            //xml string --> java body
            WxPaySyncResponse response = (WxPaySyncResponse)retrofitResponse.body();

            assert response != null;

            if (!response.getReturnCode().equals("SUCCESS")) {
                throw new RuntimeException("【微信统一支付】发起支付, returnCode != SUCCESS, returnMsg = " + response.getReturnMsg());
            } else if (!response.getResultCode().equals("SUCCESS")) {
                throw new RuntimeException("【微信统一支付】发起支付, resultCode != SUCCESS, err_code = " + response.getErrCode() + " err_code_des=" + response.getErrCodeDes());
            } else {
                return this.buildPayResponse(response);
            }
        }
    }

扩展阅读:
1、hashCode() 重写的要求:https://blog.csdn.net/qq_42777577/article/details/84756170
2、hashCode()重写时的神奇数:https://blog.csdn.net/weixin_43871678/article/details/116654390
3、Retrofit的使用:https://blog.csdn.net/qq_31469369/article/details/109677247

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/789892.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存