支付宝对账

简介: 支付宝对账实现

1.对账文件存放目录


privateStringaliFileSavePath=System.getProperty("user.dir")+"/aliFileSave/bill";
privateStringaliFileUploadPath=System.getProperty("user.dir")+"/aliFileSave/upload";


2.支付宝对账文件实体类


publicclassPtAliTreadDetailEntity {
privateStringposTime;//交易时间privateStringbusinessOrderNo;//商户订单号privateStringtransType;//操作类型privateStringbusinessName;//商品名称privateStringcreateDate;//创建时间privateStringendDate;//完成时间privateStringhouseNumber;//门店编号privateStringhouseName;//门店名称privateStringoperName;//操作员privateStringequipmentNo;//终端号privateStringhisAccount;//对方账户privateStringtotalAmount;//订单金额(元)privateStringtrueTotalAmount;//商家实收(元)privateStringredBao;//支付宝红包(元)privateStringcollegeBao;//集分宝(元)privateStringaLiOnSale;//支付宝优惠(元)privateStringbusinessOnSale;//商家优惠(元)privateStringquanOnSale;//券核销金额(元)privateStringquanName;//券名称privateStringbusinessRedBao;//商家红包消费金额(元)privateStringcardFee;//卡消费金额(元)privateStringbackFeeBatch;//退款批次号/请求号privateStringserveFee;//服务费(元)privateStringgiveMoney;//分润(元)privateStringremark;//备注publicStringgetPosTime() {
returnposTime;
    }
publicvoidsetPosTime(StringposTime) {
this.posTime=posTime;
    }
publicStringgetBusinessOrderNo() {
returnbusinessOrderNo;
    }
publicvoidsetBusinessOrderNo(StringbusinessOrderNo) {
this.businessOrderNo=businessOrderNo;
    }
publicStringgetTransType() {
returntransType;
    }
publicvoidsetTransType(StringtransType) {
this.transType=transType;
    }
publicStringgetBusinessName() {
returnbusinessName;
    }
publicvoidsetBusinessName(StringbusinessName) {
this.businessName=businessName;
    }
publicStringgetCreateDate() {
returncreateDate;
    }
publicvoidsetCreateDate(StringcreateDate) {
this.createDate=createDate;
    }
publicStringgetEndDate() {
returnendDate;
    }
publicvoidsetEndDate(StringendDate) {
this.endDate=endDate;
    }
publicStringgetHouseNumber() {
returnhouseNumber;
    }
publicvoidsetHouseNumber(StringhouseNumber) {
this.houseNumber=houseNumber;
    }
publicStringgetHouseName() {
returnhouseName;
    }
publicvoidsetHouseName(StringhouseName) {
this.houseName=houseName;
    }
publicStringgetOperName() {
returnoperName;
    }
publicvoidsetOperName(StringoperName) {
this.operName=operName;
    }
publicStringgetEquipmentNo() {
returnequipmentNo;
    }
publicvoidsetEquipmentNo(StringequipmentNo) {
this.equipmentNo=equipmentNo;
    }
publicStringgetHisAccount() {
returnhisAccount;
    }
publicvoidsetHisAccount(StringhisAccount) {
this.hisAccount=hisAccount;
    }
publicStringgetTotalAmount() {
returntotalAmount;
    }
publicvoidsetTotalAmount(StringtotalAmount) {
this.totalAmount=totalAmount;
    }
publicStringgetTrueTotalAmount() {
returntrueTotalAmount;
    }
publicvoidsetTrueTotalAmount(StringtrueTotalAmount) {
this.trueTotalAmount=trueTotalAmount;
    }
publicStringgetRedBao() {
returnredBao;
    }
publicvoidsetRedBao(StringredBao) {
this.redBao=redBao;
    }
publicStringgetCollegeBao() {
returncollegeBao;
    }
publicvoidsetCollegeBao(StringcollegeBao) {
this.collegeBao=collegeBao;
    }
publicStringgetaLiOnSale() {
returnaLiOnSale;
    }
publicvoidsetaLiOnSale(StringaLiOnSale) {
this.aLiOnSale=aLiOnSale;
    }
publicStringgetBusinessOnSale() {
returnbusinessOnSale;
    }
publicvoidsetBusinessOnSale(StringbusinessOnSale) {
this.businessOnSale=businessOnSale;
    }
publicStringgetQuanOnSale() {
returnquanOnSale;
    }
publicvoidsetQuanOnSale(StringquanOnSale) {
this.quanOnSale=quanOnSale;
    }
publicStringgetQuanName() {
returnquanName;
    }
publicvoidsetQuanName(StringquanName) {
this.quanName=quanName;
    }
publicStringgetBusinessRedBao() {
returnbusinessRedBao;
    }
publicvoidsetBusinessRedBao(StringbusinessRedBao) {
this.businessRedBao=businessRedBao;
    }
publicStringgetCardFee() {
returncardFee;
    }
publicvoidsetCardFee(StringcardFee) {
this.cardFee=cardFee;
    }
publicStringgetBackFeeBatch() {
returnbackFeeBatch;
    }
publicvoidsetBackFeeBatch(StringbackFeeBatch) {
this.backFeeBatch=backFeeBatch;
    }
publicStringgetServeFee() {
returnserveFee;
    }
publicvoidsetServeFee(StringserveFee) {
this.serveFee=serveFee;
    }
publicStringgetGiveMoney() {
returngiveMoney;
    }
publicvoidsetGiveMoney(StringgiveMoney) {
this.giveMoney=giveMoney;
    }
publicStringgetRemark() {
returnremark;
    }
publicvoidsetRemark(Stringremark) {
this.remark=remark;
    }
}


3.支付宝对账方法


/*** 阿里支付宝对账入库* @throws Exception*/@OverridepublicvoidqueryAliCheckMenu() throwsException {
//获取昨天日期Calendarcal=Calendar.getInstance();
cal.add(Calendar.DATE, -1);
Stringyesterday="2019-06-11"; //new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());AlipayClientalipayClient=newDefaultAlipayClient(Constants.AliBillCheck, Constants.WXZN_ALIAPP_ID, Constants.WXZN_APP_PRIVATE_KEY, "json", "utf-8", Constants.WXZN_ALIPAY_PUBLIC_KEY, "RSA2");//获得初始化的AlipayClientAlipayDataDataserviceBillDownloadurlQueryRequestrequest=newAlipayDataDataserviceBillDownloadurlQueryRequest();//创建API对应的request类JSONObjectjsonObject=newJSONObject();
jsonObject.put("bill_type","trade");
jsonObject.put("bill_date",yesterday);
request.setBizContent(jsonObject.toJSONString());
AlipayDataDataserviceBillDownloadurlQueryResponseresponse=alipayClient.execute(request);//通过alipayClient调用API,获得对应的response类//根据response中的结果继续业务逻辑处理if (response.getBillDownloadUrl()==null){
System.out.println("账单不存在");
return;
        }
//将接口返回的对账单下载地址传入urlStrStringurlStr=response.getBillDownloadUrl();
//指定希望保存的文件路径StringfilePath=aliFileSavePath+yesterday+".zip";
Filef=newFile(filePath.substring(0, filePath.lastIndexOf('/')));
if (!f.exists()) {
f.mkdirs();
        }
URLurl=null;
HttpURLConnectionhttpUrlConnection=null;
InputStreamfis=null;
FileOutputStreamfos=null;
try {
url=newURL(urlStr);
httpUrlConnection= (HttpURLConnection) url.openConnection();
httpUrlConnection.setConnectTimeout(5*1000);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setRequestProperty("Charsert", "UTF-8");
httpUrlConnection.connect();
fis=httpUrlConnection.getInputStream();
byte[] temp=newbyte[1024];
intb;
fos=newFileOutputStream(newFile(filePath));
while ((b=fis.read(temp)) !=-1) {
fos.write(temp, 0, b);
fos.flush();
            }
//Thread.sleep(5*1000);Filefile=newFile(filePath);
if (file.exists()){
StringoutPath=ZipUtil.unZipFiles(file,aliFileSavePath);
zip(outPath);
System.out.println(outPath);
            }else {
System.out.println("文件不存在不作处理");
            }
        } catch (MalformedURLExceptione) {
e.printStackTrace();
        } catch (IOExceptione) {
e.printStackTrace();
        } finally {
try {
if(fis!=null) fis.close();
if(fos!=null) fos.close();
if(httpUrlConnection!=null) httpUrlConnection.disconnect();
            } catch (IOExceptione) {
e.printStackTrace();
            }
        }
    }


4.其他方法


/***   xmlxd字符串转map对象* @param xml* @return*/publicstaticMap<Object,Object>xmlToMap(Stringxml){
if (xml.contains("xml")){
XMLSerializerxmlSerializer=newXMLSerializer();
JSONjson=xmlSerializer.read(xml);
Map<Object, Object>map= (Map<Object, Object>) JSONObject.parse(String.valueOf(json));
returnmap;
        }else {
Map<Object, Object>map=newHashMap<>();
map.put("return_code","1");
returnmap;
        }
//正常只会返回数据包含xml说明出错需要解析数据    }


5.解压文件入库


/*** 解压支付宝对账文件入库*/publicvoidzip(Stringfilepath){
Filefile=newFile(filepath);
//解决中文乱码InputStreamReaderisr=null;
BufferedReaderbuff=null;
try {
isr=newInputStreamReader(newFileInputStream(file),"GBK");
buff=newBufferedReader(isr);
Stringline=null;
//数据容器List<PtAliTreadDetailEntity>alilist=newArrayList<PtAliTreadDetailEntity>();
PtAliTreadDetailEntityali=null;
inti=0;
while ((line=buff.readLine())!=null) {
//去除`Stringreplace=line.replace("`", "").replace("\t", "");
String[] split=replace.split(",");
//去除后两行,第一行也是不需要存到数据库的,在后续操作中去掉第一条数据i=i+1;
if (i<=5){
continue;
                }
if(split.length==25) {
//符合要求ali=newPtAliTreadDetailEntity();
ali.setPosTime(split[0]);
ali.setBusinessOrderNo(split[1]);
ali.setTransType(split[2]);
ali.setBusinessName(split[3]);
ali.setCreateDate(split[4]);
ali.setEndDate(split[5]);
ali.setHouseNumber(split[6]);
ali.setHouseName(split[7]);
ali.setOperName(split[8]);
ali.setEquipmentNo(split[9]);
ali.setHisAccount(split[10]);
ali.setTotalAmount(split[11]);
ali.setTrueTotalAmount(split[12]);
ali.setRedBao(split[13]);
ali.setCollegeBao(split[14]);
ali.setaLiOnSale(split[15]);
ali.setBusinessOnSale(split[16]);
ali.setQuanOnSale(split[17]);
ali.setQuanName(split[18]);
ali.setBusinessRedBao(split[19]);
ali.setCardFee(split[20]);
ali.setBackFeeBatch(split[21]);
ali.setServeFee(split[22]);
ali.setGiveMoney(split[23]);
ali.setRemark(split[24]);
//添加alilist.add(ali);
                }
            }
System.out.println(alilist);
for (PtAliTreadDetailEntityentity : alilist) {
entity.setTotalAmount(AmountUtils.changeY2F(entity.getTotalAmount()));
mapper.insertCheckAliRecord(entity);
            }
        } catch (UnsupportedEncodingExceptione) {
e.printStackTrace();
        } catch (FileNotFoundExceptione) {
e.printStackTrace();
        } catch (IOExceptione) {
e.printStackTrace();
        }finally {
try {
if(isr!=null) {
isr.close();
                }
            } catch (IOExceptione) {
e.printStackTrace();
            }
try {
if(buff!=null) {
buff.close();
                }
            } catch (IOExceptione) {
e.printStackTrace();
            }
        }
    }


6.其他方法


/*** 解压文件到指定目录* 解压后的文件名,和之前一致* @param zipFile   待解压的zip文件* @param descDir   指定目录* @return 文件路径*/publicstaticStringunZipFiles(FilezipFile, StringdescDir) throwsIOException {
ZipFilezip=newZipFile(zipFile, Charset.forName("GBK"));//解决中文文件夹乱码Stringname=zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.'));
//File pathFile = new File(descDir+name);FilepathFile=newFile(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
        }
StringoutPath="";
for (Enumeration<?extendsZipEntry>entries=zip.entries(); entries.hasMoreElements();) {
ZipEntryentry= (ZipEntry) entries.nextElement();
StringzipEntryName=entry.getName();
if (zipEntryName.contains("汇总")){
continue;
            }
InputStreamin=zip.getInputStream(entry);
//String outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/");outPath= (descDir+"/"+zipEntryName).replaceAll("\\*", "/");
// 判断路径是否存在,不存在则创建文件路径Filefile=newFile(outPath.substring(0, outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
            }
// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压if (newFile(outPath).isDirectory()) {
continue;
            }
// 输出文件路径信息//            System.out.println(outPath);FileOutputStreamout=newFileOutputStream(outPath);
byte[] buf1=newbyte[1024];
intlen;
while ((len=in.read(buf1)) >0) {
out.write(buf1, 0, len);
            }
in.close();
out.close();
        }
System.out.println("******************解压完毕********************:"+outPath);
returnoutPath;
    }



相关文章
|
7月前
企业支付宝转账
企业支付宝转账
98 0
企业支付宝转账
|
前端开发 安全 数据安全/隐私保护
支付宝支付流程解读
支付宝支付流程解读
|
5月前
|
前端开发
支付系统45------支付宝支付对账
支付系统45------支付宝支付对账
|
7月前
|
消息中间件 Java 测试技术
支付宝支付
支付宝支付
183 1
支付宝提现方案
说明   首先使用支付宝接口实现提现功能,需要使用到【单笔转账到支付宝账户接口】   大家需要转变一个思路,单笔转账接口可以转账给个人支付宝账户,对于用户而言收到商家转账就是提现成功。所以我们可以针对这个接口来说,转账就是提现。
1355 12
|
安全 前端开发
聊聊对账
聊聊对账
616 0
聊聊对账
|
开发者
支付之去银行 | 学习笔记
快速学习支付之去银行。
150 0
支付之去银行 | 学习笔记
手机网站支付之对账篇
场景介绍 商户接入了手机网站支付后,想通过接口下载指定日期(当天除外)的业务明细账单文件,并结合自身业务系统,从而实现对账功能,为此,我们提供了查询对账单下载地址接口作为辅助,本接口支持appid所对应PID下所有的交易记录对账,下面小编就为您讲解简单的接入调用流程: 接口调用流程    1.商户系统调用查询对账单下载地址接口alipay.data.dataservice.bill.downloadurl.query,传入指定日期,获得该日期账单文件的下载地址。
911 12
如何对账
官方文档 说明:一:目前支付宝对外的对账方式有三种1、sftp账单模式:该模式需要联系对应的业务经理(BD)申请sftp账户和账单模板配置。如有业务账单需求只能申请配置sftp账单配置。 2、通过在支付宝后台下载账单的方式来对账:新企业版账单下载:商户财务人员登录新企业版【qy.alipay.com】,可通过账单下载页面,下载登录账户的已完成结算的对账单,并使用该数据完成财务核算。
2420 12
下一篇
DataWorks