恢复应用内购买无法正常工作

时间:2016-08-13 13:22:02

标签: ios actionscript-3 in-app-purchase

我正在使用Flash CS6为iOS和Android构建应用程序。 我已经构建了6个正在运行的应用程序和应用程序内购买和恢复工作正常。现在,我正在构建我的第7个应用程序,并测试了我的应用内购买。购买效果很好但是当我删除应用程序并重新安装并购买产品时。它告诉我已经购买了产品并且它将免费恢复它,但它继续加载并且没有任何反应。 这也适用于所有旧应用程序。

*注意:我使用Flash发布SWF然后使用cmd构建IPA

我用这个as3代码:

function log(s:String):void
{
    trace(s);
    log_txt.text = s;
}
function initStorekit():void
{
    log("initializing StoreKit..");
    if (! StoreKit.isSupported())
    {
        log("Store Kit iOS purchases is not supported on this platform.");
        return;
    }
    else
    {
        supported = true;
    }

    StoreKit.create();

    log("StoreKit Initialized.");

    // make sure that purchases will actually work on this device before continuing!
    // (for example, parental controls may be preventing them.)
    if (! StoreKit.storeKit.isStoreKitAvailable())
    {
        log("Store is disabled on this device.");
        return;
    }
    // add listeners here
    StoreKit.storeKit.addEventListener(StoreKitEvent.PRODUCT_DETAILS_LOADED,onProductsLoaded);
    StoreKit.storeKit.addEventListener(StoreKitEvent.PURCHASE_SUCCEEDED,onPurchaseSuccess);
    StoreKit.storeKit.addEventListener(StoreKitEvent.PURCHASE_CANCELLED,onPurchaseUserCancelled);
    StoreKit.storeKit.addEventListener(StoreKitEvent.TRANSACTIONS_RESTORED, onTransactionsRestored);
    // adding error events. always listen for these to avoid your program failing.;
    StoreKit.storeKit.addEventListener(StoreKitErrorEvent.PRODUCT_DETAILS_FAILED,onProductDetailsFailed);
    StoreKit.storeKit.addEventListener(StoreKitErrorEvent.PURCHASE_FAILED,onPurchaseFailed);
    StoreKit.storeKit.addEventListener(StoreKitErrorEvent.TRANSACTION_RESTORE_FAILED, onTransactionRestoreFailed);
    // initialize a sharedobject that's holding our inventory.;
    initSharedObject();
    var productIdList:Vector.<String>=new Vector.<String>();
    productIdList.push(LEVELPACK1_ID);
    productIdList.push(LEVELPACK2_ID);
    productIdList.push(LEVELPACK3_ID);
    productIdList.push(REMOVEADS_ID);
    productIdList.push(ALLINPACKAGE_ID);
    // when this is done, we'll get a PRODUCT_DETAILS_LOADED or PRODUCT_DETAILS_FAILED event and go on from there...;
    log("Loading product details...");
    StoreKit.storeKit.loadProductDetails(productIdList);
}
function onProductsLoaded(e:StoreKitEvent):void
{
    log("products loaded.");
    for each (var product:StoreKitProduct in e.validProducts)
    {
        trace("ID: "+product.productId);
        trace("Title: "+product.title);
        trace("Description: "+product.description);
        trace("String Price: "+product.localizedPrice);
        trace("Price: "+product.price);
    }
    log("Loaded "+e.validProducts.length+" Products.");
    // if any of the product ids we tried to pass in were not found on the server,
    // we won't be able to by them so something is wrong.
    if (e.invalidProductIds.length > 0)
    {
        log("[ERR]: these products not valid:"+e.invalidProductIds.join(","));
        return;
    }
}
function onProductDetailsFailed(e:StoreKitErrorEvent):void
{
    log("ERR loading products:"+e.text);
}
function initSharedObject():void
{

    this.sharedObject = SharedObject.getLocal("myPurchases");

    // check if the application has been loaded before.  if not, create a store of our purchases in the sharedobject.
    if (sharedObject.data["inventory"] == null)
    {
        sharedObject.data["inventory"]=new Object();
    }

    updateInventoryMessage();

}
/** Update Inventory Message */
function updateInventoryMessage():void
{
    var inventory:Object = sharedObject.data["inventory"];

    // if the value is set to something, you have it
    if (inventory[LEVELPACK1_ID] != null)
    {
        unlockLP1_fnc();
    }
    if (inventory[LEVELPACK2_ID] != null)
    {
        unlockLP2_fnc();
    }
    if (inventory[LEVELPACK3_ID] != null)
    {
        unlockLP3_fnc();
    }
    if (inventory[REMOVEADS_ID] != null)
    {
        hideAds_fnc();
    }
    if (inventory[ALLINPACKAGE_ID] != null)
    {
        unlockLP1_fnc();unlockLP2_fnc();unlockLP3_fnc();hideAds_fnc();
    }

    log("Has hasUnlocked? ");
}
function purchaseUnlock(s:String):void
{
    enableLoading();
    // for this to work, you must have added the value of LEVELPACK_PRODUCT_ID in the iTunes Connect website
    log("start purchase of non-consumable '"+s+"'...");
    // we won't let you purchase it if its already in your inventory!
    var inventory:Object = sharedObject.data["inventory"];
    if (inventory[s] != null)
    {
        log("You already have unlocked this!");
        return;
    }

    StoreKit.storeKit.purchaseProduct(s);
}
/** Example of how to restore transactions */
function restoreTransactions():void
{
    enableLoading();
    log("requesting transaction restore...");
    StoreKit.storeKit.restoreTransactions();
}
function onPurchaseSuccess(e:StoreKitEvent):void
{
    log("Successful purchase of '"+e.productId+"'");
    disableLoading();
    // update our sharedobject with the state of this inventory item.
    // this is just an example to make the process clear.  you will
    // want to make your own inventory manager class to handle these
    // types of things.
    var inventory:Object = sharedObject.data["inventory"];
    switch (e.productId)
    {
        case ALLINPACKAGE_ID :
            inventory[ALLINPACKAGE_ID] = "purchased";
            break;
        case LEVELPACK1_ID :
            inventory[LEVELPACK1_ID] = "purchased";
            break;
        case LEVELPACK2_ID :
            inventory[LEVELPACK2_ID] = "purchased";
            gotoAndStop("lvls");
            break;
        case LEVELPACK3_ID :
            inventory[LEVELPACK3_ID] = "purchased";
            gotoAndStop("lvls");
            break;
        case REMOVEADS_ID :
            inventory[REMOVEADS_ID] = "purchased";
            gotoAndStop("mm");
            break;
        default :
            log("xxxxx");
            // we don't do anything for unknown items.
    }

    // save state!
    sharedObject.flush();

    // update the message on screen;
    updateInventoryMessage();

}
function onPurchaseFailed(e:StoreKitErrorEvent):void
{
    disableLoading();
    log("FAILED purchase="+e.productId+",t="+e.transactionId+",o="+e.originalTransactionId);
}
function onPurchaseUserCancelled(e:StoreKitEvent):void
{
    disableLoading();
    log("CANCELLED purchase="+e.productId+","+e.transactionId);
}
function onTransactionsRestored(e:StoreKitEvent):void
{
    disableLoading();
    log("All previous transactions restored!");
    var inventory:Object = sharedObject.data["inventory"];
    switch (e.productId)
    {
        case ALLINPACKAGE_ID :
            inventory[ALLINPACKAGE_ID] = "purchased";
            break;
        case LEVELPACK1_ID :
            inventory[LEVELPACK1_ID] = "purchased";
            break;
        case LEVELPACK2_ID :
            inventory[LEVELPACK2_ID] = "purchased";
            gotoAndStop("lvls");
            break;
        case LEVELPACK3_ID :
            inventory[LEVELPACK3_ID] = "purchased";
            gotoAndStop("lvls");
            break;
        case REMOVEADS_ID :
            inventory[REMOVEADS_ID] = "purchased";
            gotoAndStop("mm");
            break;
        default :
            log("xxxxx");
            // we don't do anything for unknown items.
    }

    // save state!
    sharedObject.flush();
    updateInventoryMessage();
}
function onTransactionRestoreFailed(e:StoreKitErrorEvent):void
{
    disableLoading();
    log("an error occurred in restore purchases:"+e.text);
}

0 个答案:

没有答案