package com.jeequan.jeepay.thirdparty.channel.ryxpay.model;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * TODO
 *
 * @author crystal
 * @date 2024/2/27 11:52
 */
@Data
public class ReqMethod {

    /**
     * 进件统一域名
     */
    private final static String INCOME_DOMAIN = "https://rjp.ruiyinxin.com/nms";

    /**
     * 交易统一域名
     */
    private final static String TRADE_DOMAIN = "https://qr.ruiyinxin.com";


    @Getter
    @AllArgsConstructor
    public enum Method{

        MERCHANT_INCOME("/ims/merch/simpleApply",INCOME_DOMAIN,"商户入驻"),

        MERCHANT_INCOME_RESULT("/ims/merch/queryMerchResult",INCOME_DOMAIN,"进件结果"),

        MERCHANT_QUERY("/ims/merch/getById",INCOME_DOMAIN,"获取商户信息"),

        MERCHANT_UPDATE_SETTLE_CARD("/ims/merch/updateSettleCard",INCOME_DOMAIN,"修改结算卡"),

        MERCHANT_UPDATE_MERC_NAME("/ims/merch/updateMerName",INCOME_DOMAIN,"修改商户简称和经营名称"),

        MERCHANT_CARD_EXPIRE_APPLY("/ims/cardExpire/submitApply",INCOME_DOMAIN,"证件到期补充资料申请"),

        MERCHANT_UPDATE_RATE("/ims/merch/updateRate",INCOME_DOMAIN,"修改费率"),

        MERCHANT_TODAY_SETTLE_QUERY("/ims/tranSt/tranStTodayList",INCOME_DOMAIN,"当日结算查询"),

        MERCHANT_SETTLE_QUERY("/ims/tranSt/tranStList",INCOME_DOMAIN,"历史结算查询"),

        TRADE_ORDER("/ydzf/ydzf-smzf",TRADE_DOMAIN,"交易相关接口"),

        TRADE_REFUND("/ydzf/admin/ydzf-smzf001",TRADE_DOMAIN,"退款相关接口"),

        OTHER_WX_CONFIG("/ydzf/wechat/gateway",TRADE_DOMAIN,"微信支付参数配置")

        ;
        private final String method;

        private final String domain;

        private final String desc;

    }


    /**
     * 接口服务码
     */
    @Getter
    @AllArgsConstructor
    public enum ServiceCode{

        SMZF002("SMZF002",Method.TRADE_ORDER,"扫码支付"),

        SMZF003("SMZF003",Method.TRADE_ORDER,"条码支付"),

        SMZF004("SMZF004",Method.TRADE_REFUND,"申请退款"),

        SMZF005("SMZF005",Method.TRADE_REFUND,"撤销交易"),

        SMZF006("SMZF006",Method.TRADE_ORDER,"交易查询"),

        SMZF008("SMZF008",Method.TRADE_ORDER,"交易异步通知"),

        SMZF010("SMZF010",Method.TRADE_ORDER,"公众号支付"),

        SMZF014("SMZF014",Method.TRADE_ORDER,"微信APP支付"),
        SMZF015("SMZF014",Method.TRADE_ORDER,"订单关闭"),

        SMZF016("SMZF016",Method.TRADE_ORDER,"服务窗支付"),

        SMZF017("SMZF017",Method.TRADE_ORDER,"银联二维码JS支付"),

        SMZF018("SMZF018",Method.TRADE_ORDER,"微信小程序支付"),

        SMZF019("SMZF019",Method.TRADE_ORDER,"银联二维码JS支付获取用户userId"),

        JSAPI_PATH("JSAPI_PATH",Method.OTHER_WX_CONFIG,"微信支付授权目录配置"),

        SUB_APPID("SUB_APPID",Method.OTHER_WX_CONFIG,"微信绑定子商户APPID配置"),

        SUBSCRIBE_APPID("SUBSCRIBE_APPID",Method.OTHER_WX_CONFIG,"微信推荐关注APPID配置"),

        CONFIG_QUERY("CONFIG_QUERY",Method.OTHER_WX_CONFIG,"微信公众号支付商户参数查询"),

        ;
        private final String value;

        private final Method method;

        private final String desc;

    }

    /**
     * 获取请求接口地址
     * @param method
     * @return
     */
    public static String getUrl(Method method){
        return method.getDomain() + method.getMethod();
    }


    public static void main(String[] args) {
        String aa = "{\"signData\":\"MsCu6T33QiOCVk/9Eo14+MDgRA7cozwNLH6AO4f3D9BcIJp3a//MG2KKgAcPcXOuDuP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5ORL8JfzbG3oGM+8moKHmRvT3FomELIraZ02XG0BMGPJ3UgQz9arOeJgF7/rxjhqLR0KV5xRfw+M40sNekUbg91ifhYeju3YC29Y3VhnwxyIA54+DbcwzRmoJNGA9lGP+JiBmLCGweFkcJjVoyiSlkiFxW49Nmsx\",\"tranCode\":\"SMZF018\",\"encryptKey\":\"F8S63fIo04aeXg+IW1mcdvoA3xNWrwPVAohDtQ1HAQwNYoWPGFtGi0pf/VQkxK2vSitvKjIyi274hbf+br9nKhyqNyG+itIO2Hq4mMt3XaK/KcvZ7Z9BAJD8XLQ0U413VmSCxghvnCaat9wbLG8pqUcto1fnulSoNvHe3lmldtMO1jiBQC+05yX5pmcHOWfNqt8IsLcO4onmHA6ud0oiBXHunKHNrfySXLVaJx4tTj0Rnc/Yhit/hBg4pDbgv4cyvM+wzGbNl6nIR/hL/7qfGv57DgukzAQ79PdWljE2FKDekBVKZK9hjNSz4v6Xisc7SPGpwTSEABeNNNk0fJIz+g==\",\"reqMsgId\":\"20240301142354367U9PB2\"}";
        JSONObject resp = JSON.parseObject(aa);
        String resEncryptData = resp.getString("encryptData");
        String resEncryptKey = resp.getString("encryptKey");
        String cooPaiKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDConKuuoKWDDpUDDW+aSlhxpoPHEIk3RKJG0PGNh/qlOfR8QYgGRyK4Ml1W3TgAvG+W50H3o2HK7k4Prr0nHSgvte/NgYbrTUo+M0eJyWyMkKY+A0PczeYtX0rmeWd1xjnR7BnXM/Rxj1NFP13+gkecBSnBmShH9T6CJnnD4rrJLG9PbCuq1CevXNSRklAMuufncW9uXeKT946cALDZwpNQKTHHLJUuxV4OjE1nCbSTk6HuLSf7pt3mooilEo9GoqGOOV4Zc/8TgpzCHBJnm81VatEytYcm/4FDcOzt/VJSQIYJWJJni07hmpXqIqG0TFGFsJaYqp84yznj1I73fmZAgMBAAECggEBAJ33Rmi1iyLIzdZFCqF9rE5k58h3wN82ZcL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ID8CpIfegpr+2YT9Ma2jHfHHNl/BkAdNx59aiQTcKOoE/7Z84ojMIqfDSHcbRy4y92XDfAVHrDHj503M8/CYpBhdjuuf38Y/W/0U=";
        byte[] keyBytes = Base64.getDecoder().decode(resEncryptKey.getBytes(StandardCharsets.UTF_8));
        RSA rsa = SecureUtil.rsa(cooPaiKey,null);
        byte[] merchantAESKeyBytes = rsa.decrypt(keyBytes, KeyType.PrivateKey);
        byte[] dataBytes = Base64.getDecoder().decode(resEncryptData.getBytes(StandardCharsets.UTF_8));
        AES aes = new AES(Mode.ECB, Padding.PKCS5Padding, merchantAESKeyBytes);
        String resXml = aes.decryptStr(dataBytes);
        System.out.println("【瑞银信】返回参数解析结果:"+resXml);
    }
}
