Skip to content

Link Account

This functionality enables users to seamlessly link their accounts with the merchant app. In Android, if the customer has the Amazon application installed on their device, the SDK's Authorize operation can authenticate the login through the Amazon Pay application. Upon successful authentication, the SDK provides an authCode, exchangeable for a pair of auth tokens, granting access to server-side APIs.

AmazonPay.startAuthorize(String clientId, String codeChallenge, Activity activity, Int launchCode, List<String> additionalAuthScopes) 

Throws: AmazonPayError

This method returns the response in onActivityResult().

Request Parameters

Parameter Type Description
clientId String Seller ID provided by Amazon Pay
codeChallenge String SHA-256 hashed code challenge for auth code gen
activity Context Current Activity Context
launchCode Int Unique request code to identify the response
additionalAuthScopes List<String> List of additional Auth Scopes to be passed if required, please check with the Integration POC. By default, pass it as null.

Response Parameters

Parameter Type Description
AmazonAuthorizationResponse Object Contains necessary response details

Amazon Authorization Response

Parameter Type Description
Status String Enum: GRANTED, DENIED, FAILURE (Authorize consent status: Granted, Denied, or Failed)
authCode String Code exchangeable for a pair of auth tokens
LwaClientId String Merchant application's mobile client ID
redirectUri String Redirect URI of the merchant application

Upon successful completion of the authorization process, you'll receive the aforementioned response parameters.

Sample Code for Calling the API

// Do the following within your activity: 
//1) define a request code for this operation, will be used later
private static final int AUTH_REQUEST_CODE = 1; //code can be anything 

//2) Generate a code Challenge, see Code Challenge Section For details 
String codeChallenge = getCodeChallenge();

try { 
    AmazonPay.startAuthorize("ZZ4ZZZZZZ2ZZ0", codeChallenge, Payment_Page.this, AUTH_REQUEST_CODE, null);
} catch (AmazonPayError aPayError) {
    Log.e("Linking Auth Failed", "Linking Auth Failed");
    Log.e("Error Message", aPayError.getErrorMessage());
    Log.e("Error Type", aPayError.getErrorType());
    throw new RuntimeException("Error occurred during auth");
} catch (Exception e) {
    Log.e("Exception while linking auth", "Uncaught exception");
    throw new RuntimeException(e);
} 

Sample Code for Handling The Response

//1) Override your activity's onActivityResultMethod 
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    //2) this block will execute only if the response is being sent by the APay SDK for the authorize operation 
    if (resultCode == RESULT_OK) {
        Log.e("resultCode==", "resultOK");
        Log.e("request code", "" + requestCode); 

        if (requestCode == AUTH_REQUEST_CODE) {
            Log.e("Auth Link Response", "AUTH link response");

            AmazonAuthorizationResponse amazonAuthorizationResponse = AmazonAuthorizationResponse.Companion.fromIntent(data);

            Log.e("Link status APB", "" + amazonAuthorizationResponse.getStatus());

            if (amazonAuthorizationResponse.getStatus().equals("GRANTED")) {
                Log.e("Auth status", "Auth Success");

                //From here you can fetch the required tokens and pass it to backend securely for generating access tokens.
                fetchBalance(amazonAuthorizationResponse);
            }

            //When the Auth if Failed.
            else if (amazonAuthorizationResponse.getStatus().equals("FAILURE")) {
                Log.e("Auth startus", "Auth FAILURE");

                AlertDialog.Builder builder = new AlertDialog.Builder(Payment_Page.this);

                builder.setMessage("User failed consent for linking");
                builder.setTitle("Alert !");
                builder.setPositiveButton("OK", (DialogInterface.OnClickListener) (dialog, which) -> {});

                AlertDialog alertDialog = builder.create();

                // Show the Alert Dialog box
                alertDialog.show();

                Toast.makeText(this, "Consent Failed by cx", Toast.LENGTH_LONG).show();
            }

            //When the user clicked cancel on Auth Screen.
            else if (amazonAuthorizationResponse.getStatus().equals("DENIED")) {
                Log.e("Auth startus", "Auth DENIED"); 

                AlertDialog.Builder builder = new AlertDialog.Builder(Payment_Page.this);

                builder.setMessage("User denied consent for linking");
                builder.setTitle("Alert !");
                builder.setPositiveButton("OK", (DialogInterface.OnClickListener) (dialog, which) -> {finish();});

                AlertDialog alertDialog = builder.create();

                // Show the Alert Dialog box
                alertDialog.show();
            }
        }
    }
}