package com.allinpay.demo.trx;

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.util.SM4Util;
import com.allinpay.demo.xml.XmlParser;
import com.allinpay.demo.xstruct.common.AipgReq;
import com.allinpay.demo.xstruct.common.InfoReq;
import com.allinpay.demo.xstruct.trans.TransExt;


/**
 * @Description 单笔实时代付
 * @Author meixf@allinpay.com
 * @Date 2018年5月29日
 **/
public class Tranx100014 {

	public static void main(String[] args){
/*		for(int i=0;i<100;i++) {*/
		InfoReq infoReq = DemoUtil.makeReq("100014");

		TransExt trans = new TransExt();
		trans.setBUSINESS_CODE("00600");//必须使用业务人员提供的业务代码，否则返回“未开通业务类型”
		trans.setMERCHANT_ID(DemoConfig.merchantid);
		trans.setSUBMIT_TIME(DemoUtil.getNow());
		 trans.setBANK_CODE("0102"); 
		trans.setACCOUNT_NAME("林");
		trans.setACCOUNT_NO("6214851005478555");
		trans.setACCOUNT_TYPE("00");
		trans.setACCOUNT_PROP("0");
		trans.setAMOUNT("1");
		/*trans.setCUSTOMIZEDFEE("100");*/
		trans.setNOTIFYURL("https://test.allinpaygd.com:8443/aipg/notifytest?abc=wer&qwe=asdf");	
		trans.setCURRENCY("CNY");
		trans.setTEL("13226665872");
		trans.setID_TYPE("0");
		trans.setID("");
		trans.setCUST_USERID("410421197808247033");
		trans.setSUMMARY("测试摘要");
		/*trans.setREMARK("备注");*/
		AipgReq req = new AipgReq();
		req.setINFO(infoReq);
		req.addTrx(trans);
		try{
			//step1 对象转xml
			String xml = XmlParser.toXml(req);
			
			//step2 加签
			String signedXml = DemoUtil.buildSignedXml(xml.replaceAll("\\s+", ""));
			/* String signedXml = DemoUtil.buildSignedXml(xml); */
		
			/*
			 * try { signedXml=SM4Util.encrptWithBase64(signedXml,"jex0402492RrrL44","GBK");
			 * } catch (Exception e) { // TODO Auto-generated catch block
			 * e.printStackTrace(); e.printStackTrace(); }
			 */
			//step3 发往通联
			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("====================================================>验签成功");
			//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())){
				TransRet ret = (TransRet) rsp.trxObj();
				System.out.println(ret.getRET_CODE());
				System.out.println(ret.getERR_MSG());
				System.out.println(ret.getSETTLE_DAY());

			}*/
		}catch(AIPGException e){
			e.printStackTrace();
		}
	}
	/* } */
}
