导航菜单

页面标题

页面副标题

Clash Legends v0.0.1 - Checkout.java 源代码

正在查看: Clash Legends v0.0.1 应用的 Checkout.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.razorpay;

import android.app.Activity;
import android.app.Fragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.razorpay.AnalyticsProperty;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public class Checkout extends Fragment {
    static final String EVENT_CALLBACK_DNE = "dne";
    static final String EVENT_CALLBACK_THREW_ERROR = "threw_error";
    static final int EXTERNAL_WALLET = 4;
    public static final int INCOMPATIBLE_PLUGIN = 7;
    static final int INTEGRATION_ONE = 1;
    static final int INTEGRATION_THREE = 3;
    static final int INTEGRATION_TWO = 2;
    public static final int INVALID_OPTIONS = 3;
    public static final int NETWORK_ERROR = 2;
    public static final int PAYMENT_CANCELED = 0;
    static final int PAYMENT_SUCCESS = 1;
    public static final int RZP_REQUEST_CODE = 62442;
    public static final int TLS_ERROR = 6;
    private static PaymentData paymentData;
    private static long sPreloadAbortDuration;
    private static long sPreloadCompleteDuration;
    private static boolean sPreloadFailed;
    private static WebView sPreloadView;
    private int checkoutImage;
    private boolean disableFullScreen;
    private boolean isMethodOverridden;
    private String key;
    private Activity merchantActivity;
    private String merchantClassName;
    private JSONObject options;

    public static class Builder {
        private int builderCheckoutImage;
        private boolean builderDisableFullScreen;
        private String builderKey;

        public Builder setKeyId(String public_key) {
            this.builderKey = public_key;
            return this;
        }

        public Builder setImage(int image) {
            this.builderCheckoutImage = image;
            return this;
        }

        public Builder disableFullscreen(boolean isDisable) {
            this.builderDisableFullScreen = isDisable;
            return this;
        }

        public Checkout build() {
            Checkout checkout = new Checkout();
            checkout.setFullScreenDisable(this.builderDisableFullScreen);
            checkout.setImage(this.builderCheckoutImage);
            String str = this.builderKey;
            if (str != null) {
                checkout.setKeyID(str);
            }
            return checkout;
        }
    }

    @Deprecated
    public final void setPublicKey(String public_key) {
        this.key = public_key;
    }

    public final void setImage(int image) {
        this.checkoutImage = image;
    }

    public final void setKeyID(String public_key) {
        setPublicKey(public_key);
    }

    public final void setFullScreenDisable(boolean isDisable) {
        this.disableFullScreen = isDisable;
    }

    public final void open(Activity a, JSONObject o) {
        if (TextUtils.isEmpty(this.key)) {
            this.key = BaseUtils.getKeyId(a);
        }
        Config.getAdvertisingIdFromUtil(a);
        if (TextUtils.isEmpty(this.key)) {
            throw new RuntimeException("Please set your Razorpay API key in AndroidManifest.xml");
        }
        if (o == null || o.length() == 0) {
            throw new RuntimeException("Checkout options cannot be null or empty");
        }
        try {
            o.put("key", this.key);
        } catch (JSONException e) {
            AnalyticsUtil.reportError(e, "warning", e.getMessage());
        }
        abortPreloadIfRequired();
        this.options = o;
        this.merchantClassName = a.getClass().getName();
        this.merchantActivity = a;
        a.getFragmentManager().beginTransaction().add(this, (String) null).commitAllowingStateLoss();
    }

    public static void preload(Context context) {
        sPreloadCompleteDuration = 0L;
        sPreloadAbortDuration = 0L;
        sPreloadFailed = false;
        Context context2 = context.getApplicationContext();
        WebView webView = new WebView(context2);
        sPreloadView = webView;
        BaseUtils.setWebViewSettings(context2, webView, false);
        sPreloadView.setWebViewClient(new WebViewClient() {
            long pageStartAt;

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                this.pageStartAt = System.nanoTime();
                if (Checkout.sPreloadView != null) {
                    Checkout.sPreloadView.setTag(Long.valueOf(this.pageStartAt));
                    Logger.d("Preload started!");
                } else {
                    Map<String, Object> eventProperties = new HashMap<>();
                    eventProperties.put("error_location", "Checkout->Preload()->onPageStarted");
                    AnalyticsUtil.trackEvent(AnalyticsEvent.WEB_VIEW_UNEXPECTED_NULL, eventProperties);
                }
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Logger.d("onReceivedError OLD while pre-loading!");
                Checkout.failPreload();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                Logger.d("onReceivedError NEW while pre-loading!");
                Checkout.failPreload();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                long now = System.nanoTime();
                if (!Checkout.sPreloadFailed) {
                    long unused = Checkout.sPreloadCompleteDuration = now - this.pageStartAt;
                    String message = "Preload finished in " + BaseUtils.nanoTimeToSecondsString(Checkout.sPreloadCompleteDuration, 2) + " sec.";
                    Logger.d(message);
                }
                Checkout.destroyPreloadView();
            }
        });
        sPreloadView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                Logger.d("Preload progress: " + newProgress);
            }
        });
        sPreloadView.loadUrl("https://api.razorpay.com/v1/checkout/public");
    }

    public static void failPreload() {
        sPreloadFailed = true;
    }

    public static void destroyPreloadView() {
        try {
            sPreloadView.stopLoading();
        } catch (Exception e) {
        }
        sPreloadView = null;
    }

    private static void abortPreloadIfRequired() {
        long now = System.nanoTime();
        try {
            WebView webView = sPreloadView;
            if (webView != null) {
                long pageStartAt = ((Long) webView.getTag()).longValue();
                sPreloadAbortDuration = now - pageStartAt;
                String message = "Preload aborted in " + BaseUtils.nanoTimeToSecondsString(sPreloadAbortDuration, 2) + " sec.";
                Logger.d(message);
            }
        } catch (Exception e) {
        }
        destroyPreloadView();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this.options != null) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(getActivity().getPackageName(), "com.razorpay.CheckoutActivity"));
            long j = sPreloadCompleteDuration;
            if (j > 0) {
                intent.putExtra("PRELOAD_COMPLETE_DURATION", j);
            } else {
                long j2 = sPreloadAbortDuration;
                if (j2 > 0) {
                    intent.putExtra("PRELOAD_ABORT_DURATION", j2);
                }
            }
            intent.putExtra(CheckoutConstants.OPTIONS, this.options.toString());
            intent.putExtra(CheckoutConstants.IMAGE, this.checkoutImage);
            intent.putExtra(CheckoutConstants.DISABLE_FULL_SCREEN, this.disableFullScreen);
            this.options = null;
            startActivityForResult(intent, RZP_REQUEST_CODE);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode != 62442) {
            return;
        }
        Activity activity = getActivity();
        this.merchantActivity = activity;
        this.merchantClassName = activity.getClass().getName();
        String result = getPaymentResult(data);
        if (result == null) {
            result = "Payment Error";
        } else if (result.contains("cancelled")) {
            result = BaseUtils.getPaymentCancelledResponse("");
        }
        trackOnActivityResultEvent(resultCode, result);
        if (resultCode == 1) {
            createPaymentData(this.merchantActivity, result);
            handleOnSuccess();
        } else if (resultCode == 4) {
            handleExternalWalletSelected();
        } else {
            handleOnError(resultCode, result);
        }
        try {
            getActivity().getFragmentManager().beginTransaction().remove(this).commit();
        } catch (Exception e) {
            AnalyticsUtil.reportError(e, "error", e.getMessage());
        }
    }

    private void handleExternalWalletSelected() {
        if (getActivity() instanceof ExternalWalletListener) {
            try {
                ExternalWalletListener externalWalletListener = (ExternalWalletListener) getActivity();
                String externalWallet = paymentData.getExternalWallet();
                if (!TextUtils.isEmpty(externalWallet)) {
                    externalWalletListener.onExternalWalletSelected(externalWallet, paymentData);
                    AnalyticsUtil.trackEvent(AnalyticsEvent.MERCHANT_EXTERNAL_WALLET_SELECTED_CALLED);
                    AnalyticsUtil.postData();
                    return;
                }
                return;
            } catch (Exception e) {
                handleMerchantCallbackError(this.merchantActivity, 4, EVENT_CALLBACK_THREW_ERROR, e);
                return;
            }
        }
        handleMerchantCallbackError(this.merchantActivity, 4, EVENT_CALLBACK_DNE, new Exception());
    }

    private void handleOnError(int resultCode, String result) {
        this.isMethodOverridden = true;
        onError(resultCode, result);
        if (this.isMethodOverridden) {
            trackIntegrationType(resultCode, 1);
        }
    }

    private void handleOnSuccess() {
        String payment_id = paymentData.getPaymentId();
        if (payment_id != null) {
            this.isMethodOverridden = true;
            onSuccess(payment_id);
        }
        if (this.isMethodOverridden) {
            trackIntegrationType(1, 1);
        }
    }

    public void onSuccess(String razorpayPaymentID) {
        this.isMethodOverridden = false;
        boolean handled = handleOnSuccessViaInterface(razorpayPaymentID);
        if (!handled) {
            handleOnSuccessViaReflection(razorpayPaymentID);
        }
    }

    public void onError(int code, String message) {
        this.isMethodOverridden = false;
        boolean handled = handleOnErrorViaInterface(code, message);
        if (!handled) {
            handleOnErrorViaReflection(code, message);
        }
    }

    private void handleOnErrorViaReflection(int code, String message) {
        Method method = null;
        try {
            method = getMerchantClassMethod("onPaymentError", Integer.TYPE, String.class);
        } catch (Exception e) {
            handleMerchantCallbackError(this.merchantActivity, code, EVENT_CALLBACK_DNE, e);
        }
        try {
            invokeMethod(method, Integer.valueOf(code), message);
            trackIntegrationType(code, 2);
        } catch (Exception e2) {
            handleMerchantCallbackError(this.merchantActivity, code, EVENT_CALLBACK_THREW_ERROR, e2);
        }
    }

    private boolean handleOnErrorViaInterface(int code, String message) {
        if (getActivity() instanceof PaymentResultListener) {
            try {
                PaymentResultListener paymentResultListener = (PaymentResultListener) getActivity();
                paymentResultListener.onPaymentError(code, message);
                trackIntegrationType(code, 3);
            } catch (Exception e) {
                handleMerchantCallbackError(this.merchantActivity, code, EVENT_CALLBACK_THREW_ERROR, e);
            }
            return true;
        }
        if (getActivity() instanceof PaymentResultWithDataListener) {
            try {
                PaymentResultWithDataListener paymentResultWithDataListener = (PaymentResultWithDataListener) getActivity();
                paymentResultWithDataListener.onPaymentError(code, message, paymentData);
                trackIntegrationType(code, 3);
            } catch (Exception e2) {
                handleMerchantCallbackError(this.merchantActivity, code, EVENT_CALLBACK_THREW_ERROR, e2);
            }
            return true;
        }
        return false;
    }

    private boolean handleOnSuccessViaInterface(String razorpayPaymentID) {
        if (getActivity() instanceof PaymentResultListener) {
            try {
                PaymentResultListener paymentResultListener = (PaymentResultListener) getActivity();
                paymentResultListener.onPaymentSuccess(razorpayPaymentID);
                trackIntegrationType(1, 3);
            } catch (Exception e) {
                handleMerchantCallbackError(this.merchantActivity, 1, EVENT_CALLBACK_THREW_ERROR, e);
            }
            return true;
        }
        if (getActivity() instanceof PaymentResultWithDataListener) {
            try {
                PaymentResultWithDataListener paymentResultWithDataListener = (PaymentResultWithDataListener) getActivity();
                paymentResultWithDataListener.onPaymentSuccess(razorpayPaymentID, paymentData);
                trackIntegrationType(1, 3);
            } catch (Exception e2) {
                handleMerchantCallbackError(this.merchantActivity, 1, EVENT_CALLBACK_THREW_ERROR, e2);
            }
            return true;
        }
        return false;
    }

    private void handleOnSuccessViaReflection(String razorpayPaymentID) {
        Method method = null;
        try {
            method = getMerchantClassMethod("onPaymentSuccess", String.class);
        } catch (Exception e) {
            handleMerchantCallbackError(this.merchantActivity, 1, EVENT_CALLBACK_DNE, e);
        }
        try {
            invokeMethod(method, razorpayPaymentID);
            trackIntegrationType(1, 2);
        } catch (Exception e2) {
            handleMerchantCallbackError(this.merchantActivity, 1, EVENT_CALLBACK_THREW_ERROR, e2);
        }
    }

    private void invokeMethod(Method method, Object... params) throws Exception {
        if (method == null) {
            return;
        }
        method.invoke(this.merchantActivity, params);
    }

    private Method getMerchantClassMethod(String methodName, Class... paramTypes) throws Exception {
        Class merchantClass = Class.forName(this.merchantClassName);
        Method method = merchantClass.getMethod(methodName, paramTypes);
        return method;
    }

    private static void trackIntegrationType(int resultCode, int integration_type) {
        try {
            Map<String, Object> properties = new HashMap<>();
            properties.put("integration_type", Integer.toString(integration_type));
            if (resultCode == 1) {
                AnalyticsUtil.trackEvent(AnalyticsEvent.MERCHANT_ON_SUCCESS_CALLED, properties);
            } else {
                AnalyticsUtil.trackEvent(AnalyticsEvent.MERCHANT_ON_ERROR_CALLED, properties);
            }
            AnalyticsUtil.postData();
        } catch (Exception e) {
            AnalyticsUtil.reportError(e, "warning", e.getMessage());
        }
    }

    private static void trackOnActivityResultEvent(int resultCode, String result) {
        try {
            AnalyticsUtil.addProperty("onActivityResult result", new AnalyticsProperty(result, AnalyticsProperty.Scope.ORDER));
            AnalyticsUtil.addProperty("onActivityResult resultCode", new AnalyticsProperty(String.valueOf(resultCode), AnalyticsProperty.Scope.ORDER));
            if (resultCode == 1) {
                AnalyticsUtil.trackEvent(AnalyticsEvent.CALLING_ON_SUCCESS);
            } else if (resultCode == 4) {
                AnalyticsUtil.trackEvent(AnalyticsEvent.CALLING_EXTERNAL_WALLET_SELECTED);
            } else {
                AnalyticsUtil.trackEvent(AnalyticsEvent.CALLING_ON_ERROR);
            }
            AnalyticsUtil.postData();
        } catch (Exception e) {
            AnalyticsUtil.reportError(e, "warning", e.getMessage());
        }
    }

    private static void handleMerchantCallbackError(Activity activity, int resultCode, String eventType, Exception e) {
        String methodName;
        String paymentStatus;
        if (resultCode == 1) {
            methodName = "onPaymentSuccess";
            paymentStatus = "success";
        } else if (resultCode == 4) {
            methodName = "onExternalWalletSelected";
            paymentStatus = "redirected";
        } else {
            methodName = "onPaymentError";
            paymentStatus = "error";
        }
        try {
            HashMap<String, Object> properties = new HashMap<>();
            properties.put("event_details", e.getMessage());
            properties.put("event_type", e.getMessage());
            properties.put("payment_status", paymentStatus);
            AnalyticsUtil.trackEvent(AnalyticsEvent.HANDOVER_ERROR, properties);
            AnalyticsUtil.postData();
        } catch (Exception exception) {
            AnalyticsUtil.reportError(exception, "error", exception.getMessage());
        }
        if (eventType.equals(EVENT_CALLBACK_DNE)) {
            if (resultCode == 4) {
                Toast.makeText(activity, "Error: ExternalWalletListener probably not implemented in your activity", 0).show();
            } else {
                Toast.makeText(activity, "Error: " + methodName + " probably not implemented in your activity", 0).show();
            }
        } else if (eventType.equals(EVENT_CALLBACK_THREW_ERROR)) {
            Toast.makeText(activity, "Your " + methodName + " method is throwing an error. Wrap the entire code of the method inside a try catch.", 0).show();
        }
        AnalyticsUtil.reportError(e, "error", e.getMessage());
    }

    private static void createPaymentData(Activity merchantActivity, String result) {
        PaymentData paymentData2 = new PaymentData();
        paymentData = paymentData2;
        paymentData2.setUserContact(CheckoutUtils.getUserContact(merchantActivity));
        paymentData.setUserEmail(CheckoutUtils.getUserEmail(merchantActivity));
        try {
            JSONObject resultJSON = new JSONObject(result);
            paymentData.setData(resultJSON);
            if (resultJSON.has("razorpay_payment_id")) {
                paymentData.setPaymentId(resultJSON.getString("razorpay_payment_id"));
            }
            if (resultJSON.has("razorpay_order_id")) {
                paymentData.setOrderId(resultJSON.getString("razorpay_order_id"));
            }
            if (resultJSON.has("razorpay_signature")) {
                paymentData.setSignature(resultJSON.getString("razorpay_signature"));
            }
            if (resultJSON.has("external_wallet")) {
                paymentData.setExternalWallet(resultJSON.getString("external_wallet"));
            }
        } catch (JSONException e) {
            AnalyticsUtil.reportError(e, "error", e.getMessage());
        }
    }

    public static void handleActivityResult(Activity activity, int requestCode, int resultCode, Intent data, PaymentResultWithDataListener listener, ExternalWalletListener externalWalletListener) {
        if (requestCode != 62442) {
            return;
        }
        String result = getPaymentResult(data);
        if (resultCode == 0) {
            result = BaseUtils.getPaymentCancelledResponse("");
        }
        createPaymentData(activity, result);
        trackOnActivityResultEvent(resultCode, result);
        if (resultCode == 1) {
            try {
                String payment_id = paymentData.getPaymentId();
                listener.onPaymentSuccess(payment_id, paymentData);
                return;
            } catch (Exception e) {
                handleMerchantCallbackError(activity, resultCode, EVENT_CALLBACK_THREW_ERROR, e);
                return;
            }
        }
        if (resultCode == 4) {
            if (externalWalletListener != null) {
                try {
                    externalWalletListener.onExternalWalletSelected(paymentData.getExternalWallet(), paymentData);
                    return;
                } catch (Exception e2) {
                    handleMerchantCallbackError(activity, resultCode, EVENT_CALLBACK_THREW_ERROR, e2);
                    return;
                }
            }
            return;
        }
        try {
            listener.onPaymentError(resultCode, result, paymentData);
        } catch (Exception e3) {
            handleMerchantCallbackError(activity, resultCode, EVENT_CALLBACK_THREW_ERROR, e3);
        }
    }

    public static void clearUserData(Context context) {
        CheckoutUtils.clearUserData(context);
    }

    private static String getPaymentResult(Intent data) {
        Bundle extras;
        if (data != null && (extras = data.getExtras()) != null) {
            return extras.getString("RESULT");
        }
        return null;
    }
}