package com.izouma.nineth;

import com.izouma.nineth.utils.MD5Util;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther YaoJun Qi
 * @Date 2021/03/05 13:43
 * @description
 */

public class GenerateUrl {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        String createTime = sdf.format(calendar.getTime());
        calendar.add(Calendar.HOUR, 1);
        String endTime = sdf.format(calendar.getTime());

        String version = "10";
        String mer_no = "6888806043057";
        //key1
        String mer_key = "fvLh4EEoJPJEkqg+6s3B8sTRVREAyVAxS34+5JGiJOmIFnIrnfD1k/KiUYo5Nfb7tBx0jj9HOI0=";
        UUID uuid = UUID.randomUUID();
        String mer_order_no = uuid.toString().replaceAll("-", "");
        String notify_url = "https://www.baidu.com";
        String order_amt = "1";
        String return_url = "http://sandcash/return";
        String goods_name = "test";
        //"userId":"用户在商户下唯一标识 1-10位","userName":"证件姓名","idCard":"18位身份证号码"
        String pay_extra = "{\"userId\":\"1\",\"userName\":\"熊竹\",\"idCard\":\"321002199408304614\"}";
        //md5key
        String key = "KvKQYXhwvkoQRAiQN2IzVSaecqYFP8emDHJrLettbShZsd8k/V2OU/myAV6My/qeotZ8K0WG8ifDzgelCexY7517gmF7SqFUcyuKgSC+HFNmn14C4iy9JRy0NvsSJRjgMzZWAhrwNL5ixuz+UKFTiw==";


        Map<String, String> map = new LinkedHashMap<>();
        map.put("accsplit_flag", "NO");
        map.put("create_ip", "127_0_0_1");
        map.put("create_time", createTime);
//        if(!(gh_static_url==null||"".equals(gh_static_url)))map.put("gh_static_url",gh_static_url);;
        map.put("mer_key", mer_key);
        map.put("mer_no", mer_no);
        map.put("mer_order_no", mer_order_no);
        map.put("notify_url", notify_url);
        map.put("order_amt", order_amt);
        map.put("pay_extra", pay_extra);
        map.put("return_url", return_url);
        map.put("sign_type", "MD5");
        map.put("store_id", "000000");
        map.put("version", version);
        map.put("key", key);


//        map.put("expire_time",endTime);
//        map.put("goods_name",goods_name);
//        map.put("product_code","02010006");
//        map.put("clear_cycle","0");

        String signature = "";

        for (String s : map.keySet()) {
            if (!(map.get(s) == null || map.get(s).equals(""))) {
                signature += s + "=";
                signature += map.get(s) + "&";
            }
        }
        signature = signature.substring(0, signature.length() - 1);
        String sign = MD5Util.encode(signature).toUpperCase();


        //url
        String url = "https://sandcash.mixienet.com.cn/pay/h5/quicktopup?" +
                "version=" + version +
                "&mer_no=" + mer_no +
                "&mer_key=" + URLEncoder.encode(mer_key, StandardCharsets.UTF_8) +
                "&mer_order_no=" + mer_order_no +
                "&create_time=" + createTime +
                "&expire_time=" + endTime +  //endTime
                "&order_amt=1" +
                "&notify_url=" + URLEncoder.encode(notify_url, StandardCharsets.UTF_8) +
                "&return_url=" + URLEncoder.encode(return_url, StandardCharsets.UTF_8) +
                "&create_ip=127_0_0_1" +
                "&goods_name=" + URLEncoder.encode(goods_name, StandardCharsets.UTF_8) +
                "&store_id=000000" +
                "&product_code=06030003" +
                "&clear_cycle=3" +
                "&pay_extra=" + URLEncoder.encode(pay_extra, StandardCharsets.UTF_8) +
                "&meta_option=%5B%7B%22s%22%3A%22Android%22,%22n%22%3A%22wxDemo%22,%22id%22%3A%22com.pay.paytypetest%22,%22sc%22%3A%22com.pay.paytypetest%22%7D%5D" +
                "&accsplit_flag=NO" +
                "&jump_scheme=" +
//                "&gh_static_url="+gh_static_url+""+
                "&sign_type=MD5" +
                "&key=" + URLEncoder.encode(key, StandardCharsets.UTF_8) +
                "&sign=" + sign;

        System.out.println("url\n\n" + url);

    }
}
