question

Scott avatar image
Scott asked

getProductDataResponse IAP v2 Cordova project not returning data

Hello,

I'm hoping someone can tell what I am doing wrong in trying to implement IAP in a mobile app. I'm using AndroidStudio, with Amazon Phone API 28 as the emulator, with Amazon App Tester installed and running.,

getProductData successfully loads the product array from the json file, as shown in the AndroidStudio debug window. But I can't seem to latch onto that array to do anything with it or go any further.

Here is the relevant code:


function amazonProductResponse() {
var iapPlugin = window.AmazonIapV2;

// Define event handler
var eventHandler = function(event) {
    alert_universal("here",null,"Sorry ...","ok");
    var requestId = event.requestId; // string
    var productDataMap = event.productDataMap; // object containing key-value pairs where the key
                                               // is a SKU and the value is a ProductData object
    alert_universal(productDataMap,null,"Sorry ...","ok");
    var unavailableSkus = event.unavailableSkus; // array of strings
    var status = event.status; // string

    // for each item in the productDataMap you can get the following values for a given SKU
    // (replace "sku" with the actual SKU)
    var sku = productDataMap["fbl_team_membership"].sku; // string
    var productType = productDataMap["fbl_team_membership"].productType; // string
    var price = productDataMap["fbl_team_membership"].price; // string
    var title = productDataMap["fbl_team_membership"].title; // string
    var description = productDataMap["fbl_team_membership"].description; // string
    var smallIconUrl = productDataMap["fbl_team_membership"].smallIconUrl; // string
    alert_universal(price,null,"Sorry ...","ok");
};

// Register for an event
iapPlugin.addListener("getProductDataResponse", eventHandler);
};

function onDeviceReady() {
selectPlatformText();
cordova.getAppVersion.getVersionNumber(function (package) {
    if (package.match("100")=='100') {version='amazon'} else {version='testapp'};
    if (version == "amazon") {
        if (window.AmazonIapV2){
            //alert_universal("Store Exists",null,"Sorry ...","ok");
        } else {
            //alert_universal("Amazon Store Not Yet Available",null,"Sorry ...","ok");
        }
        var iapPlugin = window.AmazonIapV2;

        // Construct object passed to method as input
        var requestOptions = {
            "skus": ["fbl_team_membership", "fbl_team_renewal"]
        };

        amazonProductResponse();
        
        // Call method with input
        iapPlugin.getProductData(
            function (operationResponse) {
            // Handle operation response
                var requestId = operationResponse.requestId;
            alert_universal(requestId,null,"Sorry ...","ok");
        }, 
            function (errorResponse) {
                // Handle error response
            },
            [requestOptions]
        );
    
        
    } else { //for Google Play Store - this piece works
        if (window.store) {
            store.register({type: store.CONSUMABLE, id: 'fbl_team_membership'});
            store.when("fbl_team_membership").loaded(refreshmembership);
            store.when('fbl_team_membership').approved(function(p) {p.verify();});
            store.when('fbl_team_membership').verified(processMembership);
            store.error(function(error) {
                alert_universal(error.message,null,"Sorry ...","ok");
            });
            store.refresh();
        } else {
            return;
        }
    }
})
}

if ((/(ipad|iphone|ipod|android)/i.test(navigator.userAgent))) {
    document.addEventListener("deviceready", onDeviceReady,false);
}
</script>

At var sku=productDataMap["fbl_team_membership"].sku, the debugger reports:

"Uncaught TypeError: Cannot read property 'fbl_team_membership' of undefined",

Can anyone tell me what dumb error I am making?

Enternally grateful,

Scott

iap
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Scott avatar image
Scott answered

This appears to be an error in the Amazon example shown at https://developer.amazon.com/docs/cross-platform-plugins/cpp-use-the-iap-plugin-for-cordova.html

The event responses are not exposed directly to "event", but rather in a "response" item within "event". So where the example (and my code above) show, for example, event.requestID, it (and all the other data items) should start with event.response, and so it should read event.response.requestID That fixes the problem.

Can someone from Amazon respond and fix the docs or explain why I am wrong?

Thanks.

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.