Reader SDK: Code Cookbook

Deauthorize Reader SDK

Learn how to deauthorize the current Square account location in a Reader SDK app.

Android
iOS
Java (Android)

Before you start
Permalink Get a link to this section

  • You need to have installed and configured Reader SDK.

Step 1: Display a "Deauthorize" button
Permalink Get a link to this section

Add code to your Checkout activity to display a button that deauthorizes Reader SDK by calling AuthorizationManager#deauthorize():

import com.squareup.sdk.reader.authorization.AuthorizationManager;
import com.squareup.sdk.reader.authorization.DeauthorizeErrorCode;

public class CheckoutActivity extends Activity {
  // ...

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.checkout_activity);

    View deauthorizeButton = findViewById(R.id.deauthorize_button);
    deauthorizeButton.setOnClickListener(view -> deauthorize());
  }

  private void deauthorize() {
    AuthorizationManager authorizationManager = ReaderSdk.authorizationManager();
    if (authorizationManager.getAuthorizationState().canDeauthorize()) {
      authorizationManager.deauthorize();
    } else {
      showDialog("Unable to deauthorize",
          "There are Square payments on this device that need to be " +
          "uploaded. Please ensure you are connected to the Internet.");
    }
  }
}

Important

The application may not be able to deauthorize if there are payments stored on the device waiting to be uploaded to Square servers. In this case, instruct the user of the application to connect to the network so that the uploads can complete before attempting to deauthorize again.

Step 2: Add an Authorization Manager callback
Permalink Get a link to this section

Add a callback (onDeauthorizeResult()) to the Authorization Manager in the onCreate method of your checkout activity using AuthorizationManager.addDeauthorizeCallback:

public class CheckoutActivity extends Activity {

  private CallbackReference deauthorizeCallbackRef;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ...

    AuthorizationManager authorizationManager = ReaderSdk.authorizationManager();
    deauthorizeCallbackRef =
        authorizationManager.addDeauthorizeCallback(this::onDeauthorizeResult);

  }

  private void onDeauthorizeResult(
      Result<Void, ResultError<DeauthorizeErrorCode>> result) {
    // TODO: Handle the deathorize result
  }
}

Step 3: Handle the Authorization result
Permalink Get a link to this section

Implement the onDeauthorizeResult method by adding code to respond to the deauthorization result:

private void onDeauthorizeResult(
    Result<Void, ResultError<DeauthorizeErrorCode>> result) {
  if (result.isSuccess()) {
    goToAuthorizeActivity();
  } else {
    ResultError<DeauthorizeErrorCode> error = result.getError();
    if (error.getCode()) {
      showErrorDialog(error);
    }
  }
}

Note

The result object provided to DeauthorizeCallback.onResult() does not contain a success value. It only contains a ResultError with a DeauthorizeErrorCode in case of error.

Step 4: Update onDestroy
Permalink Get a link to this section

Add code to clear the deauthorization callback reference in your onDestroy() method to avoid memory leaks:

public class CheckoutActivity extends Activity {

  // ...

  @Override protected void onDestroy() {
    super.onDestroy();
    deauthorizeCallbackRef.clear();
  }
}