springboot 集成 微信支付APIV3

1.准备工作

  <dependency>
    <groupId>com.github.wechatpay-apiv3</groupId>
    <artifactId>wechatpay-apache-httpclient</artifactId>
    <version>0.2.1</version>
  </dependency>
  <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
  </dependency>

2.统一下单以及退款单

2.1统一下单

private Map<String, String> wxPay(String openId, BigDecimal consume, Orders orders, String notifyUrl) throws IOException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        //WxPayConfig.certPath 为证书存放路径,绝对路径。eg:/root/xx/xx/xx.pem
        PrivateKey privateKey = WxUtils.create().getPrivateKey(WxPayConfig.certPath);
        CloseableHttpClient client = WxUtils.create().client();
        String param = ModelOrder.WxCreateOrder.create()
                .setAmount(new ModelOrder.WxCreateOrder.Amount(consume.intValue(), "CNY"))
                .setMchid(WxPayConfig.mchId)
                .setDescription(orders.getContent())
                .setNotify_url(WxPayConfig.notifyUrl + notifyUrl)
                .setPayer(new ModelOrder.WxCreateOrder.Payer(openId))
                .setOut_trade_no(orders.getCode())
                .setAppid(WxPayConfig.appId).build();
        logger.debug(param);
        Map<String, String> headers = new HashMap<>(3);
        headers.put("Accept", "application/json");
        headers.put("Content-Type", "application/json");
       //参考另一篇文章中的HttpUtils
        String ret = HttpUtils.create().headers(headers).client(client).post(WxUtils.CREATE_ORDER, param);
        ModelOrder.WxCreateOrderResult result = new Gson().fromJson(ret, new TypeToken<ModelOrder.WxCreateOrderResult>() {
        }.getType());
        String nonceStr = WxUtils.create().nonceStr(32).toUpperCase();
        String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
        String packAge = "prepay_id=" + result.getPrepay_id();
        String signType = "RSA";
        String message = WxPayConfig.appId + "\n" + timeStamp + "\n" + nonceStr + "\n" + packAge + "\n";
        logger.debug(message);
        String sign = WxUtils.create().paySign(message, privateKey);
        Map<String, String> data = new HashMap<>(6);
        data.put("appId", WxPayConfig.appId);
        data.put("timeStamp", timeStamp);
        data.put("nonceStr", nonceStr);
        data.put("package", packAge);
        data.put("signType", signType);
        data.put("paySign", sign);
        return data;
    }

2.2退款单

	生成退款单方法。参数可自行设置
	CloseableHttpClient client = WxUtils.create().client();
	String param = ModelOrder.WxRefundOrder.create().setOut_trade_no(order.getCode())
    .setOut_refund_no(orderRefund.getCode()).setNotify_url(WxPayConfig.notifyUrl + "refund")
    .setAmount(
    	new ModelOrder.WxRefundOrder.Amount(order.getPayConsume().multiply(BigDecimal.valueOf(100)).intValue(), 		 order.getPayConsume().multiply(BigDecimal.valueOf(100)).intValue(), "CNY")).build();
            logger.debug(param);

            Map<String, String> headers = new HashMap<>(3);
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json");
            String ret = HttpUtils.create().headers(headers).client(client).post(WxUtils.REFUND, param);
            if (StringUtils.isEmpty(ret)) {
                return JsonResult.fail("退款失败");
            }
            Refund result = new Gson().fromJson(ret, new TypeToken<Refund>() {
            }.getType());
            if (result == null) {
                return JsonResult.fail("退款失败");
            }
            String msg;
            int state;
            switch (result.getStatus()) {
                case "PROCESSING":
                    msg = "退款处理中";
                    state = 0;
                    break;
                case "SUCCESS":
                    msg = "退款成功";
                    state = 1;
                    break;
                case "ABNORMAL":
                    msg = "退款异常";
                    state = 2;
                    break;
                case "CLOSED":
                    msg = "退款关闭";
                    state = 3;
                    break;
                default:
                    msg = "退款成功";
                    state = 1;
            }
Refund.java
public class Refund {
    private String refund_id;
    private String out_refund_no;
    private String transaction_id;
    private String out_trade_no;
    private String channel;
    private String user_received_account;
    private String success_time;
    private String create_time;
    private String status;
    private String funds_account;
    private Amount amount;
    private List<PromotionDetail> promotion_detail;

    public String getRefund_id() {
        return refund_id;
    }

    public void setRefund_id(String refund_id) {
        this.refund_id = refund_id;
    }

    public String getOut_refund_no() {
        return out_refund_no;
    }

    public void setOut_refund_no(String out_refund_no) {
        this.out_refund_no = out_refund_no;
    }

    public String getTransaction_id() {
        return transaction_id;
    }

    public void setTransaction_id(String transaction_id) {
        this.transaction_id = transaction_id;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getUser_received_account() {
        return user_received_account;
    }

    public void setUser_received_account(String user_received_account) {
        this.user_received_account = user_received_account;
    }

    public String getSuccess_time() {
        return success_time;
    }

    public void setSuccess_time(String success_time) {
        this.success_time = success_time;
    }

    public String getCreate_time() {
        return create_time;
    }

    public void setCreate_time(String create_time) {
        this.create_time = create_time;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getFunds_account() {
        return funds_account;
    }

    public void setFunds_account(String funds_account) {
        this.funds_account = funds_account;
    }

    public Amount getAmount() {
        return amount;
    }

    public void setAmount(Amount amount) {
        this.amount = amount;
    }

    public List<PromotionDetail> getPromotion_detail() {
        return promotion_detail;
    }

    public void setPromotion_detail(List<PromotionDetail> promotion_detail) {
        this.promotion_detail = promotion_detail;
    }

    public static class Amount {
        private int total;
        private int refund;
        private int payer_total;
        private int payer_refund;
        private int settlement_refund;
        private int settlement_total;
        private int discount_refund;
        private String currency;

        public int getTotal() {
            return total;
        }

        public void setTotal(int total) {
            this.total = total;
        }

        public int getRefund() {
            return refund;
        }

        public void setRefund(int refund) {
            this.refund = refund;
        }

        public int getPayer_total() {
            return payer_total;
        }

        public void setPayer_total(int payer_total) {
            this.payer_total = payer_total;
        }

        public int getPayer_refund() {
            return payer_refund;
        }

        public void setPayer_refund(int payer_refund) {
            this.payer_refund = payer_refund;
        }

        public int getSettlement_refund() {
            return settlement_refund;
        }

        public void setSettlement_refund(int settlement_refund) {
            this.settlement_refund = settlement_refund;
        }

        public int getSettlement_total() {
            return settlement_total;
        }

        public void setSettlement_total(int settlement_total) {
            this.settlement_total = settlement_total;
        }

        public int getDiscount_refund() {
            return discount_refund;
        }

        public void setDiscount_refund(int discount_refund) {
            this.discount_refund = discount_refund;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }
    }

    public static class PromotionDetail {
        private String promotion_id;
        private String scope;
        private String type;
        private int amount;
        private int refund_amount;
        private List<GoodsDetail> goods_detail;

        public String getPromotion_id() {
            return promotion_id;
        }

        public void setPromotion_id(String promotion_id) {
            this.promotion_id = promotion_id;
        }

        public String getScope() {
            return scope;
        }

        public void setScope(String scope) {
            this.scope = scope;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }

        public int getRefund_amount() {
            return refund_amount;
        }

        public void setRefund_amount(int refund_amount) {
            this.refund_amount = refund_amount;
        }

        public List<GoodsDetail> getGoods_detail() {
            return goods_detail;
        }

        public void setGoods_detail(List<GoodsDetail> goods_detail) {
            this.goods_detail = goods_detail;
        }

        public static class GoodsDetail {
            private String merchant_goods_id;
            private String wechatpay_goods_id;
            private String goods_name;
            private int unit_price;
            private int refund_amount;
            private int refund_quantity;

            public String getMerchant_goods_id() {
                return merchant_goods_id;
            }

            public void setMerchant_goods_id(String merchant_goods_id) {
                this.merchant_goods_id = merchant_goods_id;
            }

            public String getWechatpay_goods_id() {
                return wechatpay_goods_id;
            }

            public void setWechatpay_goods_id(String wechatpay_goods_id) {
                this.wechatpay_goods_id = wechatpay_goods_id;
            }

            public String getGoods_name() {
                return goods_name;
            }

            public void setGoods_name(String goods_name) {
                this.goods_name = goods_name;
            }

            public int getUnit_price() {
                return unit_price;
            }

            public void setUnit_price(int unit_price) {
                this.unit_price = unit_price;
            }

            public int getRefund_amount() {
                return refund_amount;
            }

            public void setRefund_amount(int refund_amount) {
                this.refund_amount = refund_amount;
            }

            public int getRefund_quantity() {
                return refund_quantity;
            }

            public void setRefund_quantity(int refund_quantity) {
                this.refund_quantity = refund_quantity;
            }
        }
    }
}

ModelOrder.java
public class ModelOrder {
    /**
     * 创建微信支付订单类
     */
    public static class WxCreateOrder {
        private String time_expire;
        private Amount amount;
        private String mchid;
        private String description;
        private String notify_url;
        private Payer payer;
        private String out_trade_no;
        private String appid;

        public String build() {
            return new Gson().toJson(this);
        }

        public static WxCreateOrder create() {
            return new WxCreateOrder();
        }

        private WxCreateOrder() {
        }

        public String getTime_expire() {
            return time_expire;
        }

        public WxCreateOrder setTime_expire(String time_expire) {
            this.time_expire = time_expire;
            return this;
        }

        public Amount getAmount() {
            return amount;
        }

        public WxCreateOrder setAmount(Amount amount) {
            this.amount = amount;
            return this;
        }

        public String getMchid() {
            return mchid;
        }

        public WxCreateOrder setMchid(String mchid) {
            this.mchid = mchid;
            return this;
        }

        public String getDescription() {
            return description;
        }

        public WxCreateOrder setDescription(String description) {
            this.description = description;
            return this;
        }

        public String getNotify_url() {
            return notify_url;
        }

        public WxCreateOrder setNotify_url(String notify_url) {
            this.notify_url = notify_url;
            return this;
        }

        public Payer getPayer() {
            return payer;
        }

        public WxCreateOrder setPayer(Payer payer) {
            this.payer = payer;
            return this;
        }

        public String getOut_trade_no() {
            return out_trade_no;
        }

        public WxCreateOrder setOut_trade_no(String out_trade_no) {
            this.out_trade_no = out_trade_no;
            return this;
        }

        public String getAppid() {
            return appid;
        }

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

        public static class Amount {
            private int total;
            private String currency = "CNY";

            public Amount(int total, String currency) {
                this.total = total;
                this.currency = currency;
            }

            public int getTotal() {
                return total;
            }

            public void setTotal(int total) {
                this.total = total;
            }

            public String getCurrency() {
                return currency;
            }

            public void setCurrency(String currency) {
                this.currency = currency;
            }
        }

        public static class Payer {
            private String openid;

            public Payer(String openid) {
                this.openid = openid;
            }

            public String getOpenid() {
                return openid;
            }

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

    /**
     * 创建微信订单 获取预付款ID
     */
    public static class WxCreateOrderResult {
        private String prepay_id;

        public String getPrepay_id() {
            return prepay_id;
        }

        public void setPrepay_id(String prepay_id) {
            this.prepay_id = prepay_id;
        }
    }

    public static class WxMakeOrder {
        private long userid;
        private BigDecimal consume;
        private String title;
        private String content;
        private String payCode;

        public long getUserid() {
            return userid;
        }

        public void setUserid(long userid) {
            this.userid = userid;
        }

        public BigDecimal getConsume() {
            return consume;
        }

        public void setConsume(BigDecimal consume) {
            this.consume = consume;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getPayCode() {
            return payCode;
        }

        public void setPayCode(String payCode) {
            this.payCode = payCode;
        }
    }

    /**
     * 退款订单
     */
    public static class WxRefundOrder {
        private String out_trade_no;
        private String out_refund_no;
        private String reason;
        private String notify_url;
        private Amount amount;

        public String build() {
            return new Gson().toJson(this);
        }

        public static WxRefundOrder create() {
            return new WxRefundOrder();
        }

        public String getOut_trade_no() {
            return out_trade_no;
        }

        public WxRefundOrder setOut_trade_no(String out_trade_no) {
            this.out_trade_no = out_trade_no;
            return this;
        }

        public String getOut_refund_no() {
            return out_refund_no;
        }

        public WxRefundOrder setOut_refund_no(String out_refund_no) {
            this.out_refund_no = out_refund_no;
            return this;
        }

        public String getReason() {
            return reason;
        }

        public WxRefundOrder setReason(String reason) {
            this.reason = reason;
            return this;
        }

        public String getNotify_url() {
            return notify_url;
        }

        public WxRefundOrder setNotify_url(String notify_url) {
            this.notify_url = notify_url;
            return this;
        }

        public Amount getAmount() {
            return amount;
        }

        public WxRefundOrder setAmount(Amount amount) {
            this.amount = amount;
            return this;
        }

        public static class Amount {
            private int refund;
            private int total;
            private String currency = "CNY";

            public Amount(int refund, int total, String currency) {
                this.refund = refund;
                this.total = total;
                this.currency = currency;
            }

            public int getRefund() {
                return refund;
            }

            public void setRefund(int refund) {
                this.refund = refund;
            }

            public int getTotal() {
                return total;
            }

            public void setTotal(int total) {
                this.total = total;
            }

            public String getCurrency() {
                return currency;
            }

            public void setCurrency(String currency) {
                this.currency = currency;
            }
        }


    }

    /**
     * 微信支付
     */
    public static class WxPay {
        private String orderCode;
        private int payType;
        private long couponId = 0L;
        private BigDecimal price = BigDecimal.valueOf(0);

        public String getOrderCode() {
            return orderCode;
        }

        public void setOrderCode(String orderCode) {
            this.orderCode = orderCode;
        }

        public int getPayType() {
            return payType;
        }

        public void setPayType(int payType) {
            this.payType = payType;
        }

        public long getCouponId() {
            return couponId;
        }

        public void setCouponId(long couponId) {
            this.couponId = couponId;
        }

        public BigDecimal getPrice() {
            return price;
        }

        public void setPrice(BigDecimal price) {
            this.price = price;
        }
    }
}

3.微信支付相关处理类 WxUtils.java

public class WxUtils {
    //生成微信支付nonce使用的字符
    private static final String BASE_CHAR = "qwertyuiopasdfghjklzxcvbnm1234567890";
    //以下static final 均为微信支付所需url,自行查看文档
    public static final String CODE_SESSION = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
    public static final String SEND_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
    public static final String ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    public static final String CREATE_CODE = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN";
    public static final String CREATE_ORDER = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
    public static final String REFUND = "https://api.mch.weixin.qq.com/v3/refund/domestic/refunds";
    public static final String QUERY_ORDER = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{order}?mchid={mchid}";
    private WxUtils() {
    }

    public static WxUtils create() {
        return new WxUtils();
    }

    public String paySign(String message, PrivateKey key) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
        Signature sign = Signature.getInstance("SHA256withRSA");
        sign.initSign(key);
        sign.update(message.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(sign.sign());
    }

    public String nonceStr(int len) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        int length = BASE_CHAR.length();
        for (int i = 0; i < len; i++) {
            int number = random.nextInt(length);
            sb.append(BASE_CHAR.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取私钥。
     *
     * @param filename 私钥文件路径  (required)
     * @return 私钥对象
     */
    public PrivateKey getPrivateKey(String filename) throws IOException {
        String content = new String(Files.readAllBytes(Paths.get(filename)), StandardCharsets.UTF_8);
        try {
            String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "")
                    .replace("-----END PRIVATE KEY-----", "")
                    .replaceAll("\\s+", "");
            KeyFactory kf = KeyFactory.getInstance("RSA");
            return kf.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("当前Java环境不支持RSA", e);
        } catch (InvalidKeySpecException e) {
            throw new RuntimeException("无效的密钥格式");
        }
    }

    public CloseableHttpClient client() throws IOException {
        //WxPayConfig.mchId 商户ID,WxPayConfig.v3Secret apiv3 secret
        //WxPayConfig.mchSerial 商户序列号
        PrivateKey privateKey = getPrivateKey(WxPayConfig.certPath);
        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(WxPayConfig.mchId, new PrivateKeySigner(WxPayConfig.mchSerial, privateKey)),
                WxPayConfig.v3Secret.getBytes(StandardCharsets.UTF_8));
        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                .withMerchant(WxPayConfig.mchId, WxPayConfig.mchSerial, privateKey)
                .withValidator(new WechatPay2Validator(verifier));
        return builder.build();
    }
}

4.支付回调

/**
 * 余额充值支付回调
 */
public NotifyResult invest(@RequestBody NotifyBody body) throws IOException, GeneralSecurityException {
        logger.info("余额充值支付回调:" + body.toString());
        if (StringUtils.equals("TRANSACTION.SUCCESS", body.getEvent_type())) {
            NotifyBody.Resource resource = body.getResource();
            String ret = new AesUtil(WxPayConfig.v3Secret.getBytes(StandardCharsets.UTF_8)).decryptToString(resource.getAssociated_data().getBytes(StandardCharsets.UTF_8), resource.getNonce().getBytes(StandardCharsets.UTF_8), resource.getCiphertext());
            NotifyResource result = new Gson().fromJson(ret, NotifyResource.class);
            if (StringUtils.equals(result.getTrade_state(), "SUCCESS")) {
                //支付回调 数据解析成功后,相关处理
            }
            logger.debug(ret);
        }
        return NotifyResult.create().success();
    }
/**
 * 退款回调
 */
@PostMapping("/refund")
public NotifyResult refund(@RequestBody NotifyBody body) throws IOException, GeneralSecurityException {
    logger.info("退款回调:" + body.toString());
    if (StringUtils.equals("REFUND.SUCCESS", body.getEvent_type())) {
        NotifyBody.Resource resource = body.getResource();
        String ret = new AesUtil(WxPayConfig.v3Secret.getBytes(StandardCharsets.UTF_8)).decryptToString(resource.getAssociated_data().getBytes(StandardCharsets.UTF_8), resource.getNonce().getBytes(StandardCharsets.UTF_8), resource.getCiphertext());
        RefundResource result = new Gson().fromJson(ret, RefundResource.class);
        if (StringUtils.equals(result.getRefund_status(), "SUCCESS")) {
            //退款回调 数据解析成功后,相关处理
        }
        logger.debug(ret);
    }
    return NotifyResult.create().success();
}

5.接收回调参数类以及解析结果类 NotifyBody.java,RefundResource.java

public class NotifyBody {
    private String id;
    private String create_time;
    private String event_type;
    private String resource_type;
    private String summary;
    private Resource resource;

    public static class Resource {
        private String algorithm;
        private String ciphertext;
        private String associated_data;
        private String original_type;
        private String nonce;

        @Override
        public String toString() {
            return "Resource{" +
                    "algorithm='" + algorithm + '\'' +
                    ", ciphertext='" + ciphertext + '\'' +
                    ", associated_data='" + associated_data + '\'' +
                    ", original_type='" + original_type + '\'' +
                    ", nonce='" + nonce + '\'' +
                    '}';
        }

        public String getAlgorithm() {
            return algorithm;
        }

        public void setAlgorithm(String algorithm) {
            this.algorithm = algorithm;
        }

        public String getCiphertext() {
            return ciphertext;
        }

        public void setCiphertext(String ciphertext) {
            this.ciphertext = ciphertext;
        }

        public String getAssociated_data() {
            return associated_data;
        }

        public void setAssociated_data(String associated_data) {
            this.associated_data = associated_data;
        }

        public String getOriginal_type() {
            return original_type;
        }

        public void setOriginal_type(String original_type) {
            this.original_type = original_type;
        }

        public String getNonce() {
            return nonce;
        }

        public void setNonce(String nonce) {
            this.nonce = nonce;
        }
    }

    @Override
    public String toString() {
        return "NotifyBody{" +
                "id='" + id + '\'' +
                ", create_time='" + create_time + '\'' +
                ", event_type='" + event_type + '\'' +
                ", resource_type='" + resource_type + '\'' +
                ", summary='" + summary + '\'' +
                ", resource=" + resource +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCreate_time() {
        return create_time;
    }

    public void setCreate_time(String create_time) {
        this.create_time = create_time;
    }

    public String getEvent_type() {
        return event_type;
    }

    public void setEvent_type(String event_type) {
        this.event_type = event_type;
    }

    public String getResource_type() {
        return resource_type;
    }

    public void setResource_type(String resource_type) {
        this.resource_type = resource_type;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }
}
RefundResource.java
public class RefundResource {
    private String mchid;
    private String transaction_id;
    /**
     * 商户订单号
     */
    private String out_trade_no;
    /**
     * 微信支付退款号
     */
    private String refund_id;
    /**
     * 商户退款单号
     */
    private String out_refund_no;
    private String refund_status;
    private String success_time;
    /**
     * 退款入账账户
     */
    private String user_received_account;
    private Amount amount;
    public static class Payer{
        private String openid;

        public String getOpenid() {
            return openid;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }
    }
    public static class Amount{
        private int total;
        private int payer_total;
        private String currency;
        private String payer_currency;

        public int getTotal() {
            return total;
        }

        public void setTotal(int total) {
            this.total = total;
        }

        public int getPayer_total() {
            return payer_total;
        }

        public void setPayer_total(int payer_total) {
            this.payer_total = payer_total;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }

        public String getPayer_currency() {
            return payer_currency;
        }

        public void setPayer_currency(String payer_currency) {
            this.payer_currency = payer_currency;
        }
    }

    public String getMchid() {
        return mchid;
    }

    public void setMchid(String mchid) {
        this.mchid = mchid;
    }

    public String getTransaction_id() {
        return transaction_id;
    }

    public void setTransaction_id(String transaction_id) {
        this.transaction_id = transaction_id;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getRefund_id() {
        return refund_id;
    }

    public void setRefund_id(String refund_id) {
        this.refund_id = refund_id;
    }

    public String getOut_refund_no() {
        return out_refund_no;
    }

    public void setOut_refund_no(String out_refund_no) {
        this.out_refund_no = out_refund_no;
    }

    public String getRefund_status() {
        return refund_status;
    }

    public void setRefund_status(String refund_status) {
        this.refund_status = refund_status;
    }

    public String getSuccess_time() {
        return success_time;
    }

    public void setSuccess_time(String success_time) {
        this.success_time = success_time;
    }

    public String getUser_received_account() {
        return user_received_account;
    }

    public void setUser_received_account(String user_received_account) {
        this.user_received_account = user_received_account;
    }

    public Amount getAmount() {
        return amount;
    }

    public void setAmount(Amount amount) {
        this.amount = amount;
    }
}

6.向微信返回结果类NotifyResult.java

public class NotifyResult {
    private String code;
    private String message;

    public NotifyResult() {
    }
    public static NotifyResult create(){
        return new NotifyResult();
    }
    public NotifyResult success(){
        this.code = "SUCCESS";
        this.message = "成功";
        return this;
    }
    public NotifyResult fail(){
        this.code = "FAIL";
        this.message = "失败";
        return this;
    }
    public NotifyResult fail(String message){
        this.code = "FAIL";
        this.message = message;
        return this;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

7.至此 微信支付集成完毕

8.文中缺少的一些类文件如下:

/**
 * @author deilsky
 * @description 微信支付处理结果 解析结果类
 */
public class NotifyResource {
    private String mchid;
    private String appid;
    /**
     * 原始订单号
     */
    private String out_trade_no;
    /**
     * 交易ID
     */
    private String transaction_id;
    private String trade_type;
    private String trade_state;
    private String trade_state_desc;
    private String bank_type;
    private String attach;
    private String success_time;
    private Payer payer;
    private Amount amount;
    public static class Payer{
        private String openid;

        public String getOpenid() {
            return openid;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }
    }
    public static class Amount{
        private int total;
        private int payer_total;
        private String currency;
        private String payer_currency;

        public int getTotal() {
            return total;
        }

        public void setTotal(int total) {
            this.total = total;
        }

        public int getPayer_total() {
            return payer_total;
        }

        public void setPayer_total(int payer_total) {
            this.payer_total = payer_total;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }

        public String getPayer_currency() {
            return payer_currency;
        }

        public void setPayer_currency(String payer_currency) {
            this.payer_currency = payer_currency;
        }
    }

    public String getMchid() {
        return mchid;
    }

    public void setMchid(String mchid) {
        this.mchid = mchid;
    }

    public String getAppid() {
        return appid;
    }

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

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getTransaction_id() {
        return transaction_id;
    }

    public void setTransaction_id(String transaction_id) {
        this.transaction_id = transaction_id;
    }

    public String getTrade_type() {
        return trade_type;
    }

    public void setTrade_type(String trade_type) {
        this.trade_type = trade_type;
    }

    public String getTrade_state() {
        return trade_state;
    }

    public void setTrade_state(String trade_state) {
        this.trade_state = trade_state;
    }

    public String getTrade_state_desc() {
        return trade_state_desc;
    }

    public void setTrade_state_desc(String trade_state_desc) {
        this.trade_state_desc = trade_state_desc;
    }

    public String getBank_type() {
        return bank_type;
    }

    public void setBank_type(String bank_type) {
        this.bank_type = bank_type;
    }

    public String getAttach() {
        return attach;
    }

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

    public String getSuccess_time() {
        return success_time;
    }

    public void setSuccess_time(String success_time) {
        this.success_time = success_time;
    }

    public Payer getPayer() {
        return payer;
    }

    public void setPayer(Payer payer) {
        this.payer = payer;
    }

    public Amount getAmount() {
        return amount;
    }

    public void setAmount(Amount amount) {
        this.amount = amount;
    }
}

/**
 * @author deilsky
 * @description 微信支付配置
 */
public class WxPayConfig {
    public static String appId;
    public static String secret;
    public static String mchId;
    public static String mchSerial;
    public static String v3Secret;
    public static String notifyUrl;
    public static String certPath;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【归心】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值