package com.allinpay.demo.trx.filedownload;

import java.io.File;
import java.nio.charset.StandardCharsets;

import org.apache.commons.codec.binary.Base64;



import com.allinpay.demo.AIPGException;
import com.allinpay.demo.DemoConfig;

import com.allinpay.demo.util.DemoUtil;
import com.allinpay.demo.util.HttpUtil;
import com.allinpay.demo.xml.XmlParser;
import com.allinpay.demo.xstruct.RECONREQ;
import com.allinpay.demo.xstruct.common.AipgReq;
import com.allinpay.demo.xstruct.common.AipgRsp;
import com.allinpay.demo.xstruct.common.InfoReq;

import net.lingala.zip4j.ZipFile;


/**
 * @Description
 * @Author meixf@allinpay.com
 * @Date 2024年11月18日
 **/
public class Tranx200009 {

    public static void main(String args[]){
        
        InfoReq inforeq = DemoUtil.makeReq("200009");
        inforeq.setVERSION("10");
        RECONREQ dqr=new RECONREQ();
        dqr.setMERCHANT_ID(DemoConfig.merchantid);
        dqr.setRECON_DATE("20241117");
        
        AipgReq req = new AipgReq();
        req.setINFO(inforeq);
        req.addTrx(dqr);
        
        String xml = XmlParser.toXml(req);
        //step2 加签
        try {
        String signedXml = DemoUtil.buildSignedXml(xml);
        String url = DemoConfig.url;
        System.out.println("============================请求报文============================");
        System.out.println(signedXml);
        String respText = HttpUtil.post(signedXml, url);
        System.out.println("============================响应报文============================");
        System.out.println(respText);
        //step4 验签
            if(!DemoUtil.verifyXml(respText)){
                System.out.println("====================================================>验签失败");
                return;
            }
            System.out.println("====================================================>验签成功");
            AipgRsp aipgRsp = XmlParser.parseRsp(respText);
            DOWNURL downurl = (DOWNURL)aipgRsp.trxObj();
            if(downurl != null && "0000".equals(downurl.getRET_CODE())){
                String key = downurl.getKEY();
                String pass = AipgCipherUtil.decryptRSAData(key, new File(DemoConfig.pathpfx), DemoConfig.pfxpass);//RSA还是sm4
                System.out.println(pass);
                String reconurl = new String(Base64.decodeBase64(downurl.getURL()), StandardCharsets.UTF_8);
                System.out.println(reconurl);
                String zipFileName = "PDS" + dqr.getMERCHANT_ID() + dqr.getRECON_DATE() + inforeq.getVERSION() + ".zip";
                String zipOutDir = "D:/work/file/";
                HttpUtil.getFile(reconurl, zipOutDir + zipFileName);
                System.out.println("====================================================>开始生成文件解约");
                unzipFile(zipOutDir + zipFileName, zipOutDir, pass);
            }
        } catch (AIPGException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static boolean unzipFile(String zipFilePath, String outputDir, String password){
        try{
            ZipFile zipFile = new ZipFile(zipFilePath, password.toCharArray());
            zipFile.extractAll(outputDir);
            zipFile.close();
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
