Luno v7.28.0版本的 MD5 值为:08d81b3b88ef7df48c89cb5d56dc11a2

以下内容为反编译后的 CheckoutKit.java 源代码,内容仅作参考


package com.checkout;

import com.checkout.exceptions.CheckoutException;
import com.checkout.httpconnector.HttpConnector;
import com.checkout.httpconnector.Response;
import com.checkout.logger.Log;
import com.checkout.models.Card;
import com.checkout.models.CardProviderResponse;
import com.checkout.models.CardTokenResponse;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.regex.Pattern;

public class CheckoutKit {
    private static String PUBLIC_KEY_REGEX_VALIDATION = "^pk_(?:test_)?(?:\\w{8})-(?:\\w{4})-(?:\\w{4})-(?:\\w{4})-(?:\\w{12})$";
    private static CheckoutKit instance;
    private Environment baseUrl;
    protected String baseUrlOverride;
    protected Gson gson = new Gson();
    protected HttpConnector httpClient;
    private Log logger;
    private boolean logging;
    private String publicKey;

    public enum Environment {
        SANDBOX("https://sandbox.checkout.com/api2/v2/"),
        LIVE("https://api2.checkout.com/v2/");

        private String url;

        Environment(String str) {
            this.url = str;
        }
    }

    public enum RESTFunctions {
        GETCARDPROVIDERS("providers/cards", HttpConnector.HttpMethods.GET),
        CREATECARDTOKEN("tokens/card", HttpConnector.HttpMethods.POST);

        private HttpConnector.HttpMethods method;
        private String url;

        RESTFunctions(String str, HttpConnector.HttpMethods httpMethods) {
            this.url = str;
            this.method = httpMethods;
        }
    }

    private CheckoutKit(String str, Environment environment, boolean z, Log log) {
        this.publicKey = str;
        this.baseUrl = environment;
        this.logging = z;
        this.logger = log;
        this.httpClient = new HttpConnector(this.gson, z, this.logger);
        if (this.logging) {
            this.logger.info("**CheckoutKit created**  \t" + str);
        }
    }

    private static boolean checkPK(String str) throws CheckoutException {
        if (Pattern.matches(PUBLIC_KEY_REGEX_VALIDATION, str)) {
            return true;
        }
        throw new CheckoutException(CheckoutException.CKExceptionType.INVALID_PUBLIC_KEY);
    }

    public static void destroy() {
        instance = null;
    }

    public static CheckoutKit getInstance(String str) throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit == null) {
            if (checkPK(str)) {
                instance = new CheckoutKit(str, Environment.SANDBOX, true, Log.getLog());
            }
        } else {
            checkoutKit.setPublicKey(str);
        }
        return instance;
    }

    public Response<CardTokenResponse> createCardToken(Card card) throws CheckoutException, IOException {
        if (this.logging) {
            this.logger.info("**CreateCardToken called**  \t" + this.publicKey);
        }
        return this.httpClient.postRequest(getUrl(RESTFunctions.CREATECARDTOKEN), this.publicKey, this.gson.toJson(card), CardTokenResponse.class);
    }

    public Response<CardProviderResponse> getCardProviders() throws CheckoutException, IOException {
        if (this.logging) {
            this.logger.info("**GetCardProviders called**  \t" + this.publicKey);
        }
        return this.httpClient.getRequest(getUrl(RESTFunctions.GETCARDPROVIDERS), this.publicKey, CardProviderResponse.class);
    }

    public boolean getDebugMode() {
        return this.logging;
    }

    public Environment getEnvironment() {
        return this.baseUrl;
    }

    public String getEnvironmentUrl() {
        return this.baseUrl.url;
    }

    public Log getLogger() {
        return this.logger;
    }

    public String getPublicKey() {
        return this.publicKey;
    }

    public String getUrl(RESTFunctions rESTFunctions) {
        StringBuilder sb = new StringBuilder();
        String str = this.baseUrlOverride;
        if (str == null) {
            str = this.baseUrl.url;
        }
        sb.append(str);
        sb.append(rESTFunctions.url);
        return sb.toString();
    }

    public void setDebugMode(boolean z) {
        this.logging = z;
        this.httpClient.setDebug(z);
    }

    public void setEnvironment(Environment environment) {
        this.baseUrl = environment;
    }

    public void setLogger(Log log) {
        this.logger = log;
    }

    public void setPublicKey(String str) throws CheckoutException {
        if (checkPK(str)) {
            this.publicKey = str;
        }
    }

    public static CheckoutKit getInstance(String str, Environment environment) throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit == null) {
            if (checkPK(str)) {
                instance = new CheckoutKit(str, environment, true, Log.getLog());
            }
        } else {
            checkoutKit.setPublicKey(str);
            instance.setEnvironment(environment);
        }
        return instance;
    }

    public static CheckoutKit getInstance(String str, boolean z) throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit == null) {
            if (checkPK(str)) {
                instance = new CheckoutKit(str, Environment.SANDBOX, z, Log.getLog());
            }
        } else {
            checkoutKit.setPublicKey(str);
            instance.setDebugMode(z);
        }
        return instance;
    }

    public static CheckoutKit getInstance(String str, Environment environment, boolean z) throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit == null) {
            if (checkPK(str)) {
                instance = new CheckoutKit(str, environment, z, Log.getLog());
            }
        } else {
            checkoutKit.setPublicKey(str);
            instance.setEnvironment(environment);
            instance.setDebugMode(z);
        }
        return instance;
    }

    public static CheckoutKit getInstance(String str, boolean z, Log log) throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit == null) {
            if (checkPK(str)) {
                instance = new CheckoutKit(str, Environment.SANDBOX, z, log);
            }
        } else {
            checkoutKit.setPublicKey(str);
            instance.setLogger(log);
            instance.setDebugMode(z);
        }
        return instance;
    }

    public static CheckoutKit getInstance(String str, Environment environment, boolean z, Log log) throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit == null) {
            if (checkPK(str)) {
                instance = new CheckoutKit(str, environment, z, log);
            }
        } else {
            checkoutKit.setPublicKey(str);
            instance.setEnvironment(environment);
            instance.setDebugMode(z);
            instance.setLogger(log);
        }
        return instance;
    }

    public static CheckoutKit getInstance(String str, Environment environment, Log log) throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit == null) {
            if (checkPK(str)) {
                instance = new CheckoutKit(str, environment, true, log);
            }
        } else {
            checkoutKit.setPublicKey(str);
            instance.setEnvironment(environment);
            instance.setLogger(log);
        }
        return instance;
    }

    public static CheckoutKit getInstance(String str, Log log) throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit == null) {
            if (checkPK(str)) {
                instance = new CheckoutKit(str, Environment.SANDBOX, true, log);
            }
        } else {
            checkoutKit.setPublicKey(str);
            instance.setLogger(log);
        }
        return instance;
    }

    public static CheckoutKit getInstance() throws CheckoutException {
        CheckoutKit checkoutKit = instance;
        if (checkoutKit != null) {
            return checkoutKit;
        }
        throw new CheckoutException(CheckoutException.CKExceptionType.NO_PUBLIC_KEY);
    }
}