Search code examples
in-app-purchaseamazon-appstore

Error Exception occurred while processing task when using Iap API from Amazon purchase


followed the instructions on amazon developer page to be able to add IAP API to my app. But it doesn't work and throws an error:

 AbstractCommandTask: Exception occurred while processing task: com.amazon.a.a.n.a.a.a: AUTH_TOKEN_VERIFICATION_FAILURE: null: null
com.amazon.a.a.n.a.a.a: AUTH_TOKEN_VERIFICATION_FAILURE: null: null
    at com.amazon.a.a.n.a.b.a(AuthenticationTokenVerifier.java:94)
    at com.amazon.a.a.n.a.a.a(AbstractCommandTask.java:204)
    at com.amazon.a.a.n.a.a.a(AbstractCommandTask.java:131)
    at com.amazon.a.a.n.b.b$1.run(SimpleTaskPipeline.java:179)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.os.HandlerThread.run(HandlerThread.java:61)

I downloaded the amazon app tester and added the amazon.sdkteste.json file to the sdcard folder as shown in the instructions and in the app tester app also recognized this file. I also downloaded the AppstoreAuthenticationKey.pem file and saved it to the app's app/src/main/assets folder. Can anyone tell me what am I missing or can someone give me a solution to fix this problem.

Thank Below Is My AndroidManifest.xml Manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.amazontinhtoannhanh">
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
    android:theme="@style/Theme.TinhToanNhanh"
    tools:targetApi="31">
    <activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <receiver android:name = "com.amazon.device.iap.ResponseReceiver"
     android:permission = "com.amazon.inapp.purchasing.Permission.NOTIFY"
     android:exported="true">
    <intent-filter>
    <action android:name = "com.amazon.inapp.purchasing.NOTIFY" />
    </intent-filter>
    </receiver>
    </application>
</manifest>

MainActivity.java:

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import com.amazon.device.drm.LicensingService;
    import com.amazon.device.iap.PurchasingService;
    import java.util.HashSet;

    import java.util.Set;

    public class MainActivity extends AppCompatActivity {

    Button btn_subcire;
     Set <String>productSkus=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LicensingService.verifyLicense(this.getApplicationContext(), new LicensingCallback());
        TinhToanNhanhPurchasingLIstener purchasingListener= new TinhToanNhanhPurchasingLIstener();
        PurchasingService.registerListener(this.getApplicationContext(), purchasingListener);
        btn_subcire= (Button) findViewById(R.id.btn_subcire);

        btn_subcire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              PurchasingService.purchase("com.match.payment");
            }
        });

    }


    @Override
    protected void onResume() {
        super.onResume();
        PurchasingService.getUserData();
        productSkus =  new HashSet<String>();
        productSkus.add( "com.mathterm.payment" );
        productSkus.add( "com.mathterm2.payment" );
        productSkus.add( "com.mathterm3.payment" );
        productSkus.add( "com.mathterm4.payment" );
        productSkus.add("25");
        PurchasingService.getProductData(productSkus);
        PurchasingService.getPurchaseUpdates(false);
    }
}

LicensingCallback.java:

import static android.content.ContentValues.TAG;

import android.util.Log;

import com.amazon.device.drm.model.LicenseResponse;

public class LicensingCallback implements com.amazon.device.drm.LicensingListener {
public void onLicenseCommandResponse(final LicenseResponse licenseResponse) {
    final LicenseResponse.RequestStatus status = licenseResponse.getRequestStatus();
    Log.d(TAG, "onLicenseCommandResponse: RequestStatus (" + status + ")");
    switch (status) {
        case LICENSED:
            Log.d(TAG, "onLicenseCommandResponse: LICENSED");
            break;
        case NOT_LICENSED:
            Log.d(TAG, "onLicenseCommandResponse: NOT_LICENSED");
            break;
        case ERROR_VERIFICATION:
            Log.d(TAG, "onLicenseCommandResponse: ERROR_VERIFICATION");
            break;
        case ERROR_INVALID_LICENSING_KEYS:
            Log.d(TAG, "onLicenseCommandResponse: ERROR_INVALID_LICENSING_KEYS");
            break;
        case EXPIRED:
            Log.d(TAG, "onLicenseCommandResponse: EXPIRED");
            break;
        case UNKNOWN_ERROR:
            Log.d(TAG, "onLicenseCommandResponse: ERROR");
    } } }

TinhToanNhanhPurchasingLIstener:

import static android.content.ContentValues.TAG;

import android.util.Log;

import com.amazon.device.iap.PurchasingListener;
import com.amazon.device.iap.PurchasingService;
import com.amazon.device.iap.model.FulfillmentResult;
import com.amazon.device.iap.model.Product;
import com.amazon.device.iap.model.ProductDataResponse;
import com.amazon.device.iap.model.PurchaseResponse;
import com.amazon.device.iap.model.PurchaseUpdatesResponse;
import com.amazon.device.iap.model.Receipt;
import com.amazon.device.iap.model.UserDataResponse;

import java.util.Map;

public class TinhToanNhanhPurchasingLIstener implements PurchasingListener {

private String currentUserId =  null ;
private String currentMarketplace =  null ;
boolean reset =  false ;

@Override
public void onUserDataResponse(UserDataResponse userDataResponse) {
    final UserDataResponse.RequestStatus status = userDataResponse.getRequestStatus();

    switch (status) {
        case SUCCESSFUL:
            currentUserId = userDataResponse.getUserData().getUserId();
            currentMarketplace = userDataResponse.getUserData().getMarketplace();
            break ;

        case FAILED:
        case NOT_SUPPORTED:

            break ;
    }
}

@Override
public void onProductDataResponse(ProductDataResponse response) {
    switch (response.getRequestStatus()) {
        case SUCCESSFUL:
            for ( final String s : response.getUnavailableSkus()) {
                Log.v(TAG,  "Unavailable SKU:" + s);
            }

            final Map<String, Product> products = response.getProductData();
            for ( final String key : products.keySet()) {
                Product product = products.get(key);
                Log.v(TAG, String.format( "Product: %s\n Type: %s\n SKU: %s\n Price: %s\n Description: %s\n" , product.getTitle(), product.getProductType(), product.getSku(), product.getPrice(), product.getDescription()));
            }
            break ;

        case FAILED:
            Log.v(TAG,  "ProductDataRequestStatus: FAILED" );
            break ;
    }

}

@Override
public void onPurchaseResponse(PurchaseResponse purchaseResponse) {
        switch (purchaseResponse.getRequestStatus())
        {
            case SUCCESSFUL:
                PurchasingService. notifyFulfillment(purchaseResponse.getReceipt().getReceiptId(), FulfillmentResult.FULFILLED);
                break;
            case FAILED:
                break;
        }
}

@Override
public void onPurchaseUpdatesResponse(PurchaseUpdatesResponse response) {
    switch (response.getRequestStatus()) {
        case SUCCESSFUL:
            for ( final Receipt receipt : response.getReceipts()) {
                // Process receipts
            }
            if (response.hasMore()) {
                PurchasingService.getPurchaseUpdates(true);
            }


            break ;
        case FAILED:
            break ;

    }

}

}

Solution

  • I found the solution for it you just need to run the following adb command on the device where you want to run the app:

    adb shell setprop debug.amazon.sandboxmode debug