import com.alibaba.fastjson.JSONObject;
import com.fop.sdk.DefaultFopClient;
import com.fop.sdk.EncryptType;
import com.fop.sdk.FopClient;
import com.fop.sdk.SignType;
import com.fop.sdk.common.request.FopCommonRequest;
import com.fop.sdk.common.response.FopCommonResponse;
import com.fop.sdk.internal.util.SequenceUtils;

public class Demo {
    /**
     *@Description  下单支付条形码接口Demo
     *@params
     *@author linqiang@gdrcu.com
     *@date  2021/4/20
     */
    public static void main(String[] args)throws Exception {

        // write your code here
        String serverUrl = "https://open_test.gdrcu.com:9080/api/pay/preOrder";
        // 应用ID,官网查询
        String appId = "86***795";
        // 个人私钥:对上送的报文进行加签
        String privateKey = "*****************************************";
       // 平台公钥:对返回的报文进行验签
        String fopPublicKey = "############################";
        // 初始化客户端参数
        boolean isBodyEncrypt = true;
        String cstSeqNo = SequenceUtils.getTrackNo(appId);
        //统一支付专属编号，可在官网查询
        String cstId = "88***00";

        FopClient fopClient = new DefaultFopClient(appId, privateKey, fopPublicKey, SignType.RSA2, isBodyEncrypt, EncryptType.AES, cstId,cstSeqNo, "json", "UTF-8");

        fopClient.setServerUrl(serverUrl);

        // apiCode和apiVersion可在官网查询
        String apiCode="pay.preOrder";
        String apiVersion="0.0.2";

        FopCommonRequest request = new FopCommonRequest(apiCode, apiVersion);
        JSONObject bizContent = new JSONObject();

        //外部商户接入平台的订单号
        bizContent.put("outTradeNo", "M2021032****97600005");
        //支付方式
        bizContent.put("payWay","WX_BCTP");
        //商户ID
        bizContent.put("mercId", "800*********449");
        //订单金额测试0.01元
        Double payOrderAmount = new Double(0.01);
        bizContent.put("payOrderAmount",payOrderAmount);
        //商品名称
        bizContent.put("mercProName","腾讯形象店- image-QQ公仔");
        //支付类型
        bizContent.put("payType","1");
        //订单实付金额
        Double payOrdRcvAmt = new Double(0.01);
        bizContent.put("payOrdRcvAmt",payOrdRcvAmt);
        //终端IP，此IP固定
        bizContent.put("termialIp","123.12.12.123");
        //条码支付，在微信支付码中，详见官网接口说明
        bizContent.put("qrCodeInfo","13463*******8858");
        //中文名
        bizContent.put("mercChaName","测试1");
        //产品详情
        bizContent.put("mercProDec","测试2");

        request.setBizContent(bizContent.toJSONString());
        //调用 API并生成Response对象用于接收返回的报文，
        FopCommonResponse response =fopClient.execute(request);
        //可打印出来查看具体内容
        System.out.println("response:[" + response.getBody() + "]");
    }
}