package com.allinpay.demo.trx;

import java.util.ArrayList;
import java.util.List;

import com.allinpay.demo.AIPGException;
import com.allinpay.demo.DemoConfig;
import com.allinpay.demo.util.HttpUtil;
import com.allinpay.demo.util.DemoUtil;
import com.allinpay.demo.xml.XmlParser;
import com.allinpay.demo.xstruct.common.AipgReq;
import com.allinpay.demo.xstruct.common.AipgRsp;
import com.allinpay.demo.xstruct.common.InfoReq;
import com.allinpay.demo.xstruct.common.InfoRsp;
import com.allinpay.demo.xstruct.trans.DETAIL_LEDGERS;
import com.allinpay.demo.xstruct.trans.LedgerDtl;
import com.allinpay.demo.xstruct.trans.Ledgers;
import com.allinpay.demo.xstruct.trans.breq.Body;
import com.allinpay.demo.xstruct.trans.breq.Trans_Detail;
import com.allinpay.demo.xstruct.trans.breq.Trans_Sum;
import com.allinpay.demo.xstruct.trans.brsp.Ret_Detail;

/**
 * @Description 批量代收
 * @Author meixf@allinpay.com
 * @Date 2018年5月29日
 **/
public class Tranx100001 {

	public static void main(String args[]){
	
		InfoReq inforeq = DemoUtil.makeReq("100001");
		Body reqBody = new Body();

		Trans_Sum trans_sum = new Trans_Sum();
		trans_sum.setBUSINESS_CODE("10600");//必须使用业务人员提供的业务代码，否则返回“未开通业务类型”
		trans_sum.setMERCHANT_ID(DemoConfig.merchantid) ;
		trans_sum.setTOTAL_ITEM("1") ;
		trans_sum.setTOTAL_SUM("1") ;
		trans_sum.setNOTIFYURL("http://dongerpil");
		reqBody.setTRANS_SUM(trans_sum);

		LedgerDtl dtl1 = new LedgerDtl();

		dtl1.setMERCHANT_ID("1329000017");
		dtl1.setSN("9");
		dtl1.setTYPE("1");
		DETAIL_LEDGERS DETAIL_LEDGERS=new DETAIL_LEDGERS();

		LedgerDtl LedgerDtl1=new LedgerDtl();
		LedgerDtl1.setSN("0");
		LedgerDtl1.setDETAIL_SN("1");
		LedgerDtl1.setTYPE("0");
		LedgerDtl1.setMERCHANT_ID("1329000017");
		LedgerDtl1.setAMOUNT("1");
		DETAIL_LEDGERS.addTrx(LedgerDtl1);
		LedgerDtl LedgerDtl2=new LedgerDtl();
		LedgerDtl2.setSN("1");
		LedgerDtl2.setDETAIL_SN("1");
		LedgerDtl2.setTYPE("0");
		LedgerDtl2.setMERCHANT_ID("1329000017");
		LedgerDtl2.setAMOUNT("1");
		DETAIL_LEDGERS.addTrx(LedgerDtl2);
		/* trans_sum.setDETAIL_LEDGERS(DETAIL_LEDGERS); */
		
		 Ledgers ledgers = new Ledgers(); ledgers.addTrx(dtl1);
			/* trans_sum.setLEDGERS(ledgers); */
		  
		List<Trans_Detail> transList = new ArrayList<Trans_Detail>();
		Trans_Detail trans_detail = new Trans_Detail() ;
		trans_detail.setSN("1") ;
    	trans_detail.setACCOUNT_NAME("华䶮") ;
 		trans_detail.setACCOUNT_PROP("0") ;
		trans_detail.setACCOUNT_NO("6214*******8224") ;
		/* trans_detail.setBANK_CODE("0105"); */
		trans_detail.setID("44522*****0312");
		trans_detail.setTEL("132****0872");
		/*trans_detail.setBANK_CODE("0103") ;*/
		trans_detail.setAMOUNT("1") ;
		trans_detail.setCURRENCY("CNY");

		transList.add(trans_detail) ;

	
		reqBody.setDetails(transList);

		AipgReq req = new AipgReq();
		req.setINFO(inforeq);
		req.addTrx(reqBody);
		try{
			//step1 对象转xml
			String xml = XmlParser.toXml(req);
			//step2 加签
			String signedXml = DemoUtil.buildSignedXml(xml.replaceAll("\\s+", ""));
			//step3 发往通联
			System.out.println( "*******************6666666*********");
			System.out.println(signedXml);
			String url = DemoConfig.url+"?MERCHANT_ID="+DemoConfig.merchantid+"&REQ_SN="+inforeq.getREQ_SN();
			System.out.println("============================请求报文============================");
			System.out.println(signedXml);
			String respText = HttpUtil.post(signedXml, DemoConfig.url);
			System.out.println("============================响应报文============================");
			System.out.println(respText);
			//step4 验签
			if(!DemoUtil.verifyXml(respText)){
				System.out.println("====================================================>验签失败");
				return;
			}
			System.out.println("====================================================>验签成功");
			//step5 xml转对象
			AipgRsp rsp = XmlParser.parseRsp(respText);
			InfoRsp infoRsp = rsp.getINFO();
			System.out.println(infoRsp.getRET_CODE());
			System.out.println(infoRsp.getERR_MSG());
			if("0000".equals(infoRsp.getRET_CODE())){
				com.allinpay.demo.xstruct.trans.brsp.Body rspBody = (com.allinpay.demo.xstruct.trans.brsp.Body)rsp.trxObj();
				@SuppressWarnings("unchecked")
				List<Ret_Detail> rspList = rspBody.getDetails();
				for(Ret_Detail rd : rspList){
					System.out.println(rd.getSN());
					System.out.println(rd.getRET_CODE());
					System.out.println(rd.getERR_MSG());
				}
			}
		}catch(AIPGException e){
			e.printStackTrace();
		}
	}
	
}
