微信下载对账单并解析

**报错是以为没引入IJPay **


	com.github.javen205
	IJPay
	1.1.6

package jeecg.ext.uche.utils.weixin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import jeecg.ext.uche.idata.entity.weixin.WxTradeDetail;
import jeecg.ext.uche.idata.entity.weixin.WxTradeSummary;
import jeecg.system.service.SystemService;

import org.springframework.util.StringUtils;

import com.jpay.ext.kit.PaymentKit;
import com.jpay.util.HttpUtils;
import com.jpay.weixin.api.WxPayApiConfig;
import com.jpay.weixin.api.WxPayApiConfig.PayModel;
import com.jpay.weixin.api.WxPayApiConfigKit;

/**
 * 微信下载对账单
 * 
 * @author longzl
 * 
 */
public class WxBillDownload {
	public final static String WX_DOWNLOAD_BILL_URL = "https://api.mch.weixin.qq.com/pay/downloadbill";
	//放啥你懂的
	public final static String appid = "";
	public final static String mch_id = "";
	public final static String appKey = "";
	public final static String partnerkey = "";

	public static Map getWxBill(String sbillDate,SystemService systemService) {
		try {
			String billDate=sbillDate.replace("-", "");

			Map parameters = new HashMap();
			// 设置必传参数
			parameters.put("appid", appid);
			parameters.put("mch_id", mch_id);
			parameters.put("nonce_str", UUID.randomUUID().toString().replace("-", ""));
			parameters.put("bill_date", billDate);// 下载对账单的日期,格式:20140603,日期不可为当天。
			// bill_type:ALL返回当日所有订单信息,默认值SUCCESS返回当日成功支付的订单。REFUND,返回当日退款订单
			parameters.put("bill_type", "SUCCESS");
			parameters.put("sign", PaymentKit.createSign(parameters, partnerkey));
			String result = HttpUtils.post("https://api.mch.weixin.qq.com/pay/downloadbill", PaymentKit.toXml(parameters));
			
			if (StringUtils.hasText(result) && !result.contains("error_code")) {
				List wxTradeDetailList = new ArrayList();
				String[] str = result.split("\n");//按行读取数据(*这个尤为重要*)
				int len = str.length;
				for (int i = 0; i < len; i++) {
					String[] tradeDetailArray = str[i].replace("`", "").split(",");
					if (i > 0 && i < (len - 2)) {
						// 明细行数据[交易时间,公众账号ID,商户号,特约商户号,设备号,微信订单号,商户订单号,用户标识,交易类型,交易状态,付款银行,货币种类,应结订单金额,代金券金额,商品名称,商户数据包,手续费,费率,订单金额,费率备注]
						WxTradeDetail entity = new WxTradeDetail();
						entity.setBillDate(billDate);
						entity.setTransDate(getArrayValue(tradeDetailArray, 0));// 交易时间
						entity.setCommonId(getArrayValue(tradeDetailArray, 1));// 公众账号ID
						entity.setBusinessNo(getArrayValue(tradeDetailArray, 2));// 商户号
						entity.setChildBusinessNo(getArrayValue(tradeDetailArray, 3));// 特约商户号
						entity.setEquipmentNo(getArrayValue(tradeDetailArray, 4));// 设备号
						entity.setWxOrderNo(getArrayValue(tradeDetailArray, 5));// 微信订单号
						entity.setBusinessOrderNo(getArrayValue(tradeDetailArray, 6));// 商户订单号
						entity.setUserIdentity(getArrayValue(tradeDetailArray, 7));// 用户标识
						entity.setTransType(getArrayValue(tradeDetailArray, 8));// 交易类型
						entity.setTransStatus(getArrayValue(tradeDetailArray, 9));// 交易状态
						entity.setPaymentBank(getArrayValue(tradeDetailArray, 10));// 付款银行
						entity.setCurrency(getArrayValue(tradeDetailArray, 11)); // 货币种类
						entity.setSettleAccounts(getArrayValue(tradeDetailArray, 12)); // 应结订单金额
						entity.setCoupon(getArrayValue(tradeDetailArray, 13)); // 代金券金额
						entity.setBusinessName(getArrayValue(tradeDetailArray, 14));// 商品名称
						entity.setBusinessData(getArrayValue(tradeDetailArray, 15));// 商户数据包
						entity.setFee(getArrayValue(tradeDetailArray, 16));// 手续费
						entity.setRate(getArrayValue(tradeDetailArray, 17));// 费率
						entity.setOrderAmount(getArrayValue(tradeDetailArray, 18));// 订单金额
						entity.setRateRemark(getArrayValue(tradeDetailArray, 19));// 费率备注
						wxTradeDetailList.add(entity);
					}

					// 汇总行数据 [总交易单数,应结订单总金额,退款总金额,充值券退款总金额,手续费总金额,订单总金额,申请退款总金额]
					if (i > (len - 2)) {
						WxTradeSummary wxTradeSummary = new WxTradeSummary();
						wxTradeSummary.setBillDate(billDate);
						wxTradeSummary.setTradeCount(getArrayValue(tradeDetailArray, 0));// 总交易单数
						wxTradeSummary.setShouldSettleAmount(getArrayValue(tradeDetailArray, 1));// 应结订单总金额
						wxTradeSummary.setRefundAmount(getArrayValue(tradeDetailArray, 2));// 退款总金额
						wxTradeSummary.setRechargeVoucherAmount(getArrayValue(tradeDetailArray, 3));// 充值券退款总金额
						wxTradeSummary.setServiceCharge(getArrayValue(tradeDetailArray, 4));// 手续费总金额
						wxTradeSummary.setTotalOrderAmount(getArrayValue(tradeDetailArray, 5));// 订单总金额
						wxTradeSummary.setApplyRefundAmount(getArrayValue(tradeDetailArray, 6));// 申请退款总金额
						save(wxTradeSummary);
					}
				}
				batchSave(wxTradeDetailList);
			} else {
				System.out.println("###########获取对账数据有误#################");
				System.out.println("返回对账数据为:" + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getArrayValue(String[] tradeDetailArray, int index) {
		try {
			return tradeDetailArray[index];
		} catch (Exception e) {
			return "";
		}
	}
}

package jeecg.ext.uche.idata.entity.weixin;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class WxTradeDetail {
	private Integer id; // 自动生成id
	private String billDate; // 查询日期
	private String transDate;// 交易时间
	private String commonId;// 公众账号ID
	private String businessNo;// 商户号
	private String childBusinessNo;// 子商户号
	private String equipmentNo;// 设备号
	private String wxOrderNo;// 微信订单号
	private String businessOrderNo;// 商户订单号
	private String userIdentity;// 用户标识
	private String transType;// 交易类型
	private String transStatus;// 交易状态
	private String paymentBank;// 付款银行
	private String currency;// 货币种类
	private String settleAccounts;// 应结订单金额
	private String coupon;// 代金券金额
	private String businessName;// 商品名称
	private String businessData;// 商户数据包
	private String fee;// 手续费
	private String rate;// 费率
	private String rateRemark;// 费率备注
	private String orderAmount;// 订单金额

	public Integer getId() {
		return id;
	}

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

	public String getBillDate() {
		return billDate;
	}

	public void setBillDate(String billDate) {
		this.billDate = billDate;
	}

	public String getTransDate() {
		return transDate;
	}

	public void setTransDate(String transDate) {
		this.transDate = transDate;
	}

	public String getCommonId() {
		return commonId;
	}

	public void setCommonId(String commonId) {
		this.commonId = commonId;
	}

	public String getBusinessNo() {
		return businessNo;
	}

	public void setBusinessNo(String businessNo) {
		this.businessNo = businessNo;
	}

	public String getChildBusinessNo() {
		return childBusinessNo;
	}

	public void setChildBusinessNo(String childBusinessNo) {
		this.childBusinessNo = childBusinessNo;
	}

	public String getEquipmentNo() {
		return equipmentNo;
	}

	public void setEquipmentNo(String equipmentNo) {
		this.equipmentNo = equipmentNo;
	}

	public String getWxOrderNo() {
		return wxOrderNo;
	}

	public void setWxOrderNo(String wxOrderNo) {
		this.wxOrderNo = wxOrderNo;
	}

	public String getBusinessOrderNo() {
		return businessOrderNo;
	}

	public void setBusinessOrderNo(String businessOrderNo) {
		this.businessOrderNo = businessOrderNo;
	}

	public String getUserIdentity() {
		return userIdentity;
	}

	public void setUserIdentity(String userIdentity) {
		this.userIdentity = userIdentity;
	}

	public String getTransType() {
		return transType;
	}

	public void setTransType(String transType) {
		this.transType = transType;
	}

	public String getTransStatus() {
		return transStatus;
	}

	public void setTransStatus(String transStatus) {
		this.transStatus = transStatus;
	}

	public String getPaymentBank() {
		return paymentBank;
	}

	public void setPaymentBank(String paymentBank) {
		this.paymentBank = paymentBank;
	}

	public String getCurrency() {
		return currency;
	}

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

	public String getSettleAccounts() {
		return settleAccounts;
	}

	public void setSettleAccounts(String settleAccounts) {
		this.settleAccounts = settleAccounts;
	}

	public String getCoupon() {
		return coupon;
	}

	public void setCoupon(String coupon) {
		this.coupon = coupon;
	}

	public String getBusinessName() {
		return businessName;
	}

	public void setBusinessName(String businessName) {
		this.businessName = businessName;
	}

	public String getBusinessData() {
		return businessData;
	}

	public void setBusinessData(String businessData) {
		this.businessData = businessData;
	}

	public String getFee() {
		return fee;
	}

	public void setFee(String fee) {
		this.fee = fee;
	}

	public String getRate() {
		return rate;
	}

	public void setRate(String rate) {
		this.rate = rate;
	}

	public String getRateRemark() {
		return rateRemark;
	}

	public void setRateRemark(String rateRemark) {
		this.rateRemark = rateRemark;
	}

	public String getOrderAmount() {
		return orderAmount;
	}

	public void setOrderAmount(String orderAmount) {
		this.orderAmount = orderAmount;
	}

	

	@Override
	public String toString() {
		return "PtWxTradeDetail [id=" + id + ", transDate=" + transDate + ", commonId=" + commonId + ", businessNo=" + businessNo + ", childBusinessNo=" + childBusinessNo + ", equipmentNo=" + equipmentNo + ", wxOrderNo=" + wxOrderNo + ", businessOrderNo=" + businessOrderNo + ", userIdentity=" + userIdentity + ", transType=" + transType + ", transStatus=" + transStatus + ", paymentBank=" + paymentBank + ", currency=" + currency + ", settleAccounts=" + settleAccounts + ", coupon=" + coupon + ", businessName=" + businessName + ", businessData=" + businessData + ", fee=" + fee + ", rate=" + rate + ", rateRemark=" + rateRemark + ", orderAmount=" + orderAmount + "]";
	}
}

package jeecg.ext.uche.idata.entity.weixin;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


public class WxTradeSummary {
	private Integer id; // 自动生成id
	private String billDate; // 查询日期
	private String tradeCount;// 总交易单数
	private String shouldSettleAmount;// 应结订单总金额
	private String refundAmount;// 退款总金额
	private String rechargeVoucherAmount;// 充值券退款总金额
	private String ServiceCharge;// 手续费总金额
	private String totalOrderAmount;// 订单总金额
	private String applyRefundAmount;// 申请退款总金额

	public Integer getId() {
		return id;
	}

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

	public String getBillDate() {
		return billDate;
	}

	public void setBillDate(String billDate) {
		this.billDate = billDate;
	}

	public String getTradeCount() {
		return tradeCount;
	}

	public void setTradeCount(String tradeCount) {
		this.tradeCount = tradeCount;
	}

	public String getShouldSettleAmount() {
		return shouldSettleAmount;
	}

	public void setShouldSettleAmount(String shouldSettleAmount) {
		this.shouldSettleAmount = shouldSettleAmount;
	}

	public String getRefundAmount() {
		return refundAmount;
	}

	public void setRefundAmount(String refundAmount) {
		this.refundAmount = refundAmount;
	}

	public String getRechargeVoucherAmount() {
		return rechargeVoucherAmount;
	}

	public void setRechargeVoucherAmount(String rechargeVoucherAmount) {
		this.rechargeVoucherAmount = rechargeVoucherAmount;
	}

	public String getServiceCharge() {
		return ServiceCharge;
	}

	public void setServiceCharge(String serviceCharge) {
		ServiceCharge = serviceCharge;
	}

	public String getTotalOrderAmount() {
		return totalOrderAmount;
	}

	public void setTotalOrderAmount(String totalOrderAmount) {
		this.totalOrderAmount = totalOrderAmount;
	}

	public String getApplyRefundAmount() {
		return applyRefundAmount;
	}

	public void setApplyRefundAmount(String applyRefundAmount) {
		this.applyRefundAmount = applyRefundAmount;
	}

	@Override
	public String toString() {
		return "WxTradeSummary [id=" + id + ", tradeCount=" + tradeCount + ", shouldSettleAmount=" + shouldSettleAmount + ", refundAmount=" + refundAmount + ", rechargeVoucherAmount=" + rechargeVoucherAmount + ", ServiceCharge=" + ServiceCharge + ", totalOrderAmount=" + totalOrderAmount + ", applyRefundAmount=" + applyRefundAmount + "]";
	}

}

你可能感兴趣的:(微信下载对账单并解析)