package com.icloud.server.payutils.notify;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.internal.util.AlipaySignature;
import com.icloud.server.payutils.ali.util.AlipayNotify;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class t {
    public static void main(String[] args) {
        String req="{\"gmt_create\":\"2022-08-01 16:19:51\",\"charset\":\"utf-8\",\"seller_email\":\"674571001@qq.com\",\"subject\":\"0.03\",\"sign\":\"sDHQ+stbBghX/lIcOglNE02vahsfzAhCmU8w6NcuzaczEeLd9OoORU87FFMPTCB8K0x4MnvULjQothvlKGM2pOLIV82QCvmyzi7AqGEENbQcTD8ur7ZkcZ+3wbENZA89kTwvzsVSyLUb38KQfKaQ+zbAO6lKfw6nBGa5wjfuzQ7/6ksPfdltOQzBqNPJVo2jYl9PmPup/T8Nhi5gWkuAASZaRVDIHZ+2oamUkVQLl+nM87rhDkr+n3ta8ACMSth1W6QX453hPiyXpxTxwyeMDo7HguMMyp8kU9ZQ8MtEckR5jHNfKt2JmJEmgjUOOUocejhSD+GfBL73EuaYooJe1w==\",\"body\":\"0.03的商品\",\"buyer_id\":\"2088302198022740\",\"invoice_amount\":\"0.03\",\"notify_id\":\"2022080100222161952022741444701295\",\"fund_bill_list\":\"[{\\\"amount\\\":\\\"0.03\\\",\\\"fundChannel\\\":\\\"ALIPAYACCOUNT\\\"}]\",\"notify_type\":\"trade_status_sync\",\"trade_status\":\"TRADE_SUCCESS\",\"receipt_amount\":\"0.03\",\"buyer_pay_amount\":\"0.03\",\"app_id\":\"2019111869281079\",\"sign_type\":\"RSA2\",\"seller_id\":\"2088631859906049\",\"gmt_payment\":\"2022-08-01 16:19:51\",\"notify_time\":\"2022-08-01 16:19:52\",\"version\":\"1.0\",\"out_trade_no\":\"1659341970123\",\"total_amount\":\"0.03\",\"trade_no\":\"2022080122001422741417433494\",\"auth_app_id\":\"2019111869281079\",\"buyer_logon_id\":\"yiy***@163.com\",\"point_amount\":\"0.00\"}";
        String req2="{\"gmt_create\":\"2022-08-01 16:37:26\",\"charset\":\"utf-8\",\"seller_email\":\"674571001@qq.com\",\"subject\":\"0.03\",\"sign\":\"gUS+CAob4mWgW3KYYqYz3n6p5QFmwq5U91rrqjhFOfJgshl8hZgH7wAJR/0RsGFms+Fv+edFEgANytHgfyAk/X2rXBenLaFC7ipIYHAt0JqTcPlLKkA+WGeFoQdzy4Yae6HeGM4IJWx718QGBESwB+eKXzEi6XtbNmECT9/QHTe8eOj/r3zgNdjUio28o8ZI4NMeE6KVzphdPVD/ucvRuL9PMXMMlNH7IauW4bgYZAQGCQHsFNov5F7PkxGu6RKP/SiiFnKuvmC2qEYyX2lUtc62yzFwM7+4/kc3+ULgQ7u18QcQSNdsERccKF14RAPIdhFmx4hBpxsIS9F4yh5wIA==\",\"body\":\"0.03的商品\",\"buyer_id\":\"2088302198022740\",\"invoice_amount\":\"0.03\",\"notify_id\":\"2022080100222163727022741444961403\",\"fund_bill_list\":\"[{\"amount\":\\\"0.03\\\",\\\"fundChannel\\\":\\\"ALIPAYACCOUNT\\\"}]\",\"notify_type\":\"trade_status_sync\",\"trade_status\":\"TRADE_SUCCESS\",\"receipt_amount\":\"0.03\",\"buyer_pay_amount\":\"0.03\",\"app_id\":\"2019111869281079\",\"sign_type\":\"RSA2\",\"seller_id\":\"2088631859906049\",\"gmt_payment\":\"2022-08-01 16:37:27\",\"notify_time\":\"2022-08-01 16:37:27\",\"version\":\"1.0\",\"out_trade_no\":\"1659343031780\",\"total_amount\":\"0.03\",\"trade_no\":\"2022080122001422741417255631\",\"auth_app_id\":\"2019111869281079\",\"buyer_logon_id\":\"yiy***@163.com\",\"point_amount\":\"0.00\"}";
//        req="{\"gmt_create\":\"2022-08-01 17:12:28\",\"charset\":\"utf-8\",\"seller_email\":\"674571001@qq.com\",\"subject\":\"0.03\",\"sign\":\"bk7sJ4HP5L7OTvBdj+GT0hVsTr9nyD9H9u3jTJ3/ZTW7BWU2XdNDOmYQkJSXXbwEdetSYkbag5rngsjysOJeVDYqbkdx1kpPPfD1PSM0yIfpX6/CYBE+pfhmTD2IPTpDsDRaQQWoxOXrnABePVOjkuIwjQ0tP9w+Qbx2mNowzjblCKZHT+OAUlLJz+9oePh2YZFKa77maD4NlZXKx09+101wBtGfD3tdS/grGg8sYNUuuP0RD6yR4Is3idi4D1N+ZVGCGc3jFJKXaWvqZmscT6whWiD/NFLOKy8E9RJ7IJsSYCvhLSCaG/4i2eosPJsC7oh1scdpW6w940l/u2oW/Q==\",\"body\":\"0.03的商品\",\"buyer_id\":\"2088302198022740\",\"invoice_amount\":\"0.03\",\"notify_id\":\"2022080100222171229022741444902347\",\"fund_bill_list\":\"[{\"amount\":\"0.03\",\"fundChannel\":\"ALIPAYACCOUNT\"}]\",\"notify_type\":\"trade_status_sync\",\"trade_status\":\"TRADE_SUCCESS\",\"receipt_amount\":\"0.03\",\"buyer_pay_amount\":\"0.03\",\"app_id\":\"2019111869281079\",\"sign_type\":\"RSA2\",\"seller_id\":\"2088631859906049\",\"gmt_payment\":\"2022-08-01 17:12:28\",\"notify_time\":\"2022-08-01 17:12:29\",\"version\":\"1.0\",\"out_trade_no\":\"1659345129037\",\"total_amount\":\"0.03\",\"trade_no\":\"2022080122001422741416761958\",\"auth_app_id\":\"2019111869281079\",\"buyer_logon_id\":\"yiy***@163.com\",\"point_amount\":\"0.00\"}";
        String p_k="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgk0caRPeLrONJf8IcCU5VCpFXww15MmDAiLto0c6QwMBfeF5oK47EzhzNuFCuXAlKSWSKffxdfBRLUWt2HRART+aINTfqUl6Bo0gTSiBBitYKDwKFLttN+Q4Oer6r1ozitCq9W/iaiTHhrDaLeCEyi2NSDKIc0YX4LG/yasfgwgM+lUjHWFZQ2zcsm6hkX97Q2eSYLLyx1Z1FWf4FsmJBcqZtZ0Z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FkPNQ==";
//        pk2="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
       pk2="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgk0caRPeLrONJf8IcCU5VCpFXww15MmDAiLto0c6QwMBfeF5oK47EzhzNuFCuXAlKSWSKffxdfBRLUWt2HRART+aINTfqUl6Bo0gTSiBBitYKDwKFLttN+Q4Oer6r1ozitCq9W/iaiTHhrDaLeCEyi2NSDKIc0YX4LG/yasfgwgM+lUjHWFZQ2zcsm6hkX97Q2eSYLLyx1Z1FWf4FsmJBcqZtZ0ZbCJAYAEZkZ4rVht9LKJLkqqoEX8FtGZaXlE9YbqBNi0JC12k/17bcE2HCbTd0QUIeKEY39loElLXNp6unpxvQUG+0hGDSl2U9Jf6X2ArtLyhoGWSdWQDtR3qiQIDAQAB";
       req="{\"gmt_create\":\"2022-08-02 14:07:47\",\"charset\":\"utf-8\",\"seller_email\":\"674571001@qq.com\",\"subject\":\"依诺维绅REBEL书房双人布艺沙发床库特 小户型客厅阳台可折\",\"sign\":\"s2wgg1aiTnkIZRmRKsCGQwUIZWOYa6SYwSH7f1W5J+71W7QSe3BxuL9QE35IyAAbBPGOCwfqy2Nh+akoQgqoAlqoDlZqhc7mx4Am33W6qtWT84KMpTzjTPBLyTU82rhEDvUZyUITwEy3OfuvOzdGsbOWzk1rE5O55BZ3AxYuSkJ3SSIWPObZYcpyYmzK1TTNSmpqEju1bz5Ljo9+mz7+4h9LjRfPnqjhDQU54D4giUpV3XrM78SNgf+RpfAIFkWPGKhiO0PJ9GArfWxtyd86sIZ/HWf5tdQpTkzOpg5mE4o9/x9J2+tHLZcU5FEh8Bte5RdUTAYTua7/MKVqInXR3A==\",\"body\":\"依诺维绅REBEL书房双人布艺沙发床库特 小户型客厅阳台可折叠两用\",\"buyer_id\":\"2088302198022740\",\"invoice_amount\":\"0.01\",\"notify_id\":\"2022080200222140748022741446355810\",\"fund_bill_list\":\"[{\\\"amount\\\":\\\"0.01\\\",\\\"fundChannel\\\":\\\"ALIPAYACCOUNT\\\"}]\",\"notify_type\":\"trade_status_sync\",\"trade_status\":\"TRADE_SUCCESS\",\"receipt_amount\":\"0.01\",\"buyer_pay_amount\":\"0.01\",\"app_id\":\"2019111869281079\",\"sign_type\":\"RSA2\",\"seller_id\":\"2088631859906049\",\"gmt_payment\":\"2022-08-02 14:07:47\",\"notify_time\":\"2022-08-02 14:07:48\",\"version\":\"1.0\",\"out_trade_no\":\"1659420446122\",\"total_amount\":\"0.01\",\"trade_no\":\"2022080222001422741418678355\",\"auth_app_id\":\"2019111869281079\",\"buyer_logon_id\":\"yiy***@163.com\",\"point_amount\":\"0.00\"}";
        req="{\"gmt_create\":\"2022-08-02 17:08:06\",\"charset\":\"utf-8\",\"seller_email\":\"674571001@qq.com\",\"subject\":\"商品包0801\",\"sign\":\"J/5GJWzNuMIdjOYWvJm5UN+aJCpGmHvz4A5eMnjPnGPXsb95RsPT4oun8qigyp4NC2ZkCWOavLnh9+lvBvhHQqKQda0m9CHpTH3hhyQzFoKy6obW/77XxgVLfjK0dh8Gd4rm9UknDZvCJ2tW5FwjHEQJ/dobWM2L1PHl+6jaxqjrZiNunvTqi2omUi8PPxvUKVXOibWZkp2oTm+c/sIy+aIhLEegQukXFTQuLADxCID9z8zaV4Zdp0Csf6gqhon/l9QwvTRffrflMArzIxhXn57wWjvsdLMgPjsFXswz1R2eybLiwXgmCGMNIhIomInE9RGhzHOavNPUjxsil0Tlqg==\",\"body\":\"0802\",\"buyer_id\":\"2088002555764930\",\"invoice_amount\":\"0.03\",\"notify_id\":\"2022080200222170807064931439669674\",\"fund_bill_list\":\"[{\\\"amount\\\":\\\"0.03\\\",\\\"fundChannel\\\":\\\"ALIPAYACCOUNT\\\"}]\",\"notify_type\":\"trade_status_sync\",\"trade_status\":\"TRADE_SUCCESS\",\"receipt_amount\":\"0.03\",\"buyer_pay_amount\":\"0.03\",\"app_id\":\"2019111869281079\",\"sign_type\":\"RSA2\",\"seller_id\":\"2088631859906049\",\"gmt_payment\":\"2022-08-02 17:08:06\",\"notify_time\":\"2022-08-02 17:22:18\",\"version\":\"1.0\",\"out_trade_no\":\"1659431276066\",\"total_amount\":\"0.03\",\"trade_no\":\"2022080222001464931432853185\",\"auth_app_id\":\"2019111869281079\",\"buyer_logon_id\":\"578***@qq.com\",\"point_amount\":\"0.00\"}";
        JSONObject jsonObject = JSONObject.parseObject(req);
        Map<String, Object> innerMap = jsonObject.getInnerMap();
        HashMap<String, String> hm = new HashMap<>();

        Set<String> strings = innerMap.keySet();
        for(String key:strings){
            String s = innerMap.get(key).toString();
            hm.put(key,s);
        }

        boolean verify_result = false;
//        verify_result = AlipayNotify.verify(hm,hm.get("sign"));
//            new AlipayConfig().getCharset();
        try {
            verify_result = AlipaySignature.rsaCheckV1(hm,pk2, "utf-8","RSA2");
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        System.out.println(verify_result);
    }

    public Boolean flag(HttpServletRequest request) throws AlipayApiException {
        //获取支付宝POST过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
//切记alipaypublickey是支付宝的公钥，请去open.alipay.com对应应用下查看。
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
        boolean flag = AlipaySignature.rsaCheckV1(params, "", "utf-8","RSA2");
        return flag;
    }


    public static Map<String,Object> JsonToMap(JSONObject resJson){
        Map<String, Object> innerMap = resJson.getInnerMap();
        JSONObject user = resJson.getJSONObject("user");
        Map<String, Object> userMap = new HashMap<>();

        for (Map.Entry<String, Object> entry : user.entrySet()) {
            userMap.put(entry.getKey(), entry.getValue());
        }
        System.out.println("map对象:" + userMap.toString());


        return userMap;

    }
}
