package cn.pluss.platform.util;

import cn.pluss.platform.exception.MsgException;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;

import java.util.HashMap;
import java.util.Map;

/**
 *
 *@description:支付宝工具类
 *@author: bzg
 *@time: 2021/10/8 17:41
 */

public class AliUtil {


//    public static Map<String, Object> getAliUserId(String auth_code,String priKey,String pubKey,String appId) {
//        Map<String, Object> result = new HashMap<>(2);
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId,
//                priKey, "json", "GBK", pubKey, "RSA2");
//        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
//        request.setCode(auth_code);
//        request.setGrantType("authorization_code");
//        try {
//            AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
//            result.put("aliUserId", oauthTokenResponse.getUserId());
//            result.put("accessToken", oauthTokenResponse.getAccessToken());
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        return result;
//    }

    public static AlipaySystemOauthTokenResponse getAliUserIdV2(String auth_code,String priKey,String pubKey,String appId) {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId,
                priKey, "json", "GBK", pubKey, "RSA2");
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode(auth_code);
        request.setGrantType("authorization_code");
        try {
            AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
            if(!response.isSuccess()){
                MsgException.throwException("支付宝授权失败");
            }
            return response;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            MsgException.throwException("支付宝授权异常");
            return null;
        }
    }
    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCIn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GTg/c7lnnw+8ZtKitkkdCrJntMniTJiMc+76De2WwFK2XL14+rY9z3ftiidnYM01l19j8uBRak47WEn0NyXo40rcLFZM1sJn9tjJbnnyFxg0dHjaxcYQtpdPIxCet06fPihAoGAfaqJ3CnsYXH3H4KG5Qxa8fpRCWWCbBEkZVOx+TxVkLMr8hOKz2i3Y93qgHOisimPS8XZwL6/QUsaJU0wsVVldw7Blp3JnDN4TrUa7R7sw4A4Dr6glLSTKDiQTCmw8PYkTXAHURHV8/le9G+DfBwqM8eeE3p5bZTdHVovmlwrp6kCgYEA7uTSOa0phZ7ts++FVdEZf9TI/B4tRuP4aT31BHs9UoCW1g01/NEEBDiHV9udq9B6Cd5onq+QFj4jiwakTNzXjhQZqgmag1TBE9VpahLxYAD5pYn/Tvm8l5vWuSKcOSwQb2lDg5R2/bmCJRV/QHRkdkAAhHGC/TrOskUCRgosUac=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiQkrz+emAuS1mB3KKDOMmAZRd/BlPbh7fAIHAqAj1+QCZNcV3o2BTLIIqnuKpSlFXDG3uDzp2VsBxcizXuBbFyPGylnD9CgCj5abyh3+FIHPAZ2IM3TtpqImZ0TSPGXrMli4Nir7MvZktgccCqQKCC4o6iaDGz+UwWwJUIPna8fm2tiTZ+KH150CZbKVj4ZGNpBh5XSV/1dRgyQIV9D/EwSbkZ0n6VgKQLJBi0C2UE3QB17aL1Ir6+gDXIDbknN8O7GUD3aMGdThYdSRUb5wp9CZ5qfV7vCS/CgaRo38nhH3NOzkTL+7v0m1ZDHPmqEkn9VzZN6sCQdL7PoAOjHOCwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021003175619219");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("GBK");
        alipayConfig.setSignType("RSA2");

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode("14eb66d10e094da4aa3a4f615706WX33");
        request.setGrantType("authorization_code");
//        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }

    }
}
