Snippets

Waqar Sadiq CloverAppUtil.java

Created by Waqar Sadiq
/*
 * -----------------------------------------------------------------------
 * (C) Copyright 2016/2017 Customer Connect LLC. All Rights Reserved
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Customer Connect LLC.
 * The copyright notice above does not evidence any actual or intended
 * publication of such source code.
 * -----------------------------------------------------------------------
 */

package com.customerconnect.kikkle.clover.utilities;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;

import android.accounts.Account;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.util.CloverAuth;
import com.clover.sdk.util.Platform;
import com.clover.sdk.v1.merchant.Merchant;
import com.clover.sdk.v1.merchant.MerchantConnector;
import com.clover.sdk.v3.apps.AppBillingInfo;
import com.clover.sdk.v3.apps.AppsConnector;
import com.clover.sdk.v3.apps.MerchantBillingStatus;
import com.clover.sdk.v3.employees.Employee;
import com.clover.sdk.v3.employees.EmployeeConnector;
import com.customerconnect.kikkle.clover.MyApp;
import com.customerconnect.kikkle.clover.R;
import com.customerconnect.kikkle.clover.activities.InitializationActivity;
import com.customerconnect.kikkle.clover.model.BroadcastEvent;
import com.customerconnect.partnersdk.partnerapi.PartnerApiController;
import com.customerconnect.partnersdk.partnerapi.PartnerApiException;
import com.customerconnect.partnersdk.partnerapi.model.CCAccount;
import com.customerconnect.partnersdk.utilities.AppConstants;
import com.customerconnect.partnersdk.utilities.CCUtils;
import com.customerconnect.partnersdk.utilities.DialogUtils;
import com.customerconnect.partnersdk.utilities.OttoBusManager;
import com.customerconnect.partnersdk.utilities.PartnerSDKHelper;
import com.customerconnect.partnersdk.utilities.ToastUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Created by waqar on 12/6/2016.
 */

public class CloverAppUtil {

    private static final String TAG = "CloverAppUtil";

    public static void initializeClover(final Context context) {
        new AsyncTask<Void, Void, Void>(){
            private Account cloverAccount;
            private EmployeeConnector employeeConnector;
            private MerchantConnector merchantConnector;
            private AppsConnector appsConnector;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                DialogUtils.startProgressDialog(context, R.string.clover_initialization);
                cloverAccount = CloverAccount.getAccount(context);
                if ( cloverAccount != null ) {
                    employeeConnector = new EmployeeConnector(context, cloverAccount, null);
                    employeeConnector.connect();
                } else {
                    ToastUtils.showLongToast(context, CCUtils.getStringValue(context,R.string.error_clover_account));
                }
                merchantConnector = new MerchantConnector(context, cloverAccount, null);
                merchantConnector.connect();

                appsConnector = new AppsConnector(context, cloverAccount);
                appsConnector.connect();
            }


            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                if ( employeeConnector != null ){
                    employeeConnector.disconnect();
                    employeeConnector = null;
                }
                if ( merchantConnector != null ){
                    merchantConnector.disconnect();
                    merchantConnector = null;
                }
                if ( appsConnector != null ){
                    appsConnector.disconnect();
                }
                DialogUtils.stopProgressDialog();
                appsConnector = null;
            }


            @Override
            protected Void doInBackground(Void... params) {
                if ( !CCUtils.isInternetConnectionAvailable(context) ){
                    DialogUtils.showErrorDialog(context, "Internet Error","Customer Connect cannot detect an internet connection.", true, null);
                } else {
                    authenticateCloverAccount();
                    getCurrentEmployee();
                    getMerchant();
                    getBillingInfo();
                }
                return null;
            }

            private void getCurrentEmployee(){
                try {
                    Employee currentEmployee = employeeConnector.getEmployee();
                    MyApp.getInstance().setCurrentEmployee(currentEmployee);
                } catch(Exception ex){
                    ToastUtils.showLongToast(context, ex.getMessage());
                }
            }


            private void getMerchant(){
                try {
                    Merchant merchant = merchantConnector.getMerchant();
                    MyApp.getInstance().setMerchant(merchant);
                } catch(Exception ex){
                    ToastUtils.showLongToast(context, ex.getMessage());
                }
            }

            private void getBillingInfo(){
                try {
                    AppBillingInfo billingInfo = appsConnector.getAppBillingInfo();
                    MyApp.getInstance().setAppBillingInfo(billingInfo);
                } catch(Exception ex){
                    String appData = String.format("Unable to get billing information for %s.",MyApp.getInstance().getMerchant().getId());
                    CCUtils.logClientException(context, appData, ex);
                    ToastUtils.showLongToast(context, ex.getMessage());
                    DialogUtils.showErrorDialog(context, "Subscription Information  Error", appData, true, null);
                }
            }

            private void authenticateCloverAccount(){
                String authToken = getCloverAuthenticationToken(context, this.cloverAccount);
                if ( !CCUtils.isStringEmpty(authToken)){
                    CloverAppUtil.setCloverAuthToken(context, authToken);
                } else {
                    DialogUtils.showErrorDialog(context,"Authentication Error","Failed to authenticate clover account.",true, null);
                }
            }
        }.execute();
    }

    private static String getCloverAuthenticationToken(Context context, Account cloverAccount) {
        try {
            CloverAuth.AuthResult authResult = CloverAuth.authenticate(context, cloverAccount, true, 20L, TimeUnit.SECONDS);
            if ( authResult != null && authResult.authToken != null ) {
                return authResult.authToken;
            }
        } catch (Exception e) {
            Log.e(TAG,"Error getting forced auth token.",e);
        }
        return null;
    }
    
    public static boolean isEmployeeOwner(Employee employee){
        if (employee != null && employee.isNotNullIsOwner() && employee.getIsOwner()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isAppBillingCurrent(Context context, AppBillingInfo billingInfo){
        if ( billingInfo == null ){
            ToastUtils.showLongToast(context,"No billing information could be obtained.");
            return false;
        }

        try {
            if (billingInfo.getStatus() != MerchantBillingStatus.ACTIVE) {
                MerchantBillingStatus status = billingInfo.getStatus();
                //String statusName = status != null ? status.toString() : "Unknown";
                ToastUtils.showLongToast(context, "Merchant billing status is " + status.toString());
                return false;
            }
        } catch(Exception ex){
            Log.e(TAG,"Unable to determine app billing status...",ex);
        }

        Long daysLapsed = billingInfo.getDaysLapsed();
        if ( daysLapsed > 0 ){
            ToastUtils.showLongToast(context,String.format("Application subscription has been lapsed for %d days",daysLapsed));
            return false;
        }

        if ( billingInfo.getAppSubscription() == null){
            ToastUtils.showLongToast(context,"Unable to obtain application subscription information.");
            return false;
        }

        if ( billingInfo.getAppSubscription().getAmount() <= 0 ){
            ToastUtils.showLongToast(context,"Application subscription is incorrect");
            return false;
        }

        //ToastUtils.showToast(context,"Application subscription is current");
        return true;
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.