Snippets
Created by
Waqar Sadiq
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | /*
* -----------------------------------------------------------------------
* (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)
You can clone a snippet to your computer for local editing. Learn more.