使用OpenIAB提交到Amazon Appstore

时间:2015-03-11 16:00:17

标签: android in-app-purchase amazon

我们在应用购买的第一款Android应用中使用OpenIAB。我们设置了一切,并且能够使用Amazon App Tester在设备上成功测试,并使用终端中的./adb install -i com.amazon.venezia appname.apk命令安装应用程序。

但是,当我们提交到亚马逊应用商店时,我们会被他们的测试人员拒绝,因为该应用不会下载应用内购买/价格。我应该评论它在谷歌播放中也能很好地运行,它也使用OpenIAB。

我已经包含了下面编写的代码。我们还按照网站的建议设置了manifest和proguard-rules.pro。我们非常感谢任何帮助,因为我们无法获得亚马逊的支持:

public class InAppBilling {
private static final String TAG = "InAppBilling";
private static InAppBilling mInstance = null;

// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = <number goes here>;

private OpenIabHelper mHelper;
private NoteListFragment mFragment;
private Context mContext;

private InAppBilling() {}

public static InAppBilling get() {
    if (mInstance == null) {
        InAppConfig.init();
        mInstance = new InAppBilling();
    }
    return mInstance;
}

public OpenIabHelper getHelper() {
    return mHelper;
}

// Initialize OpenIAB library and when completed automatically kick off full product info download
public void init(Context context, NoteListFragment fragment) {
    mContext = context;

    // If library was already initialized, go straight to info download, don't init twice
    if (mHelper != null) {
        InAppBilling.get().queryPricesAndPurchases(fragment);
        return;
    }

    // Create the helper, passing it our context and the public keys to verify signatures with
    //Log.d(TAG, "Creating IAB helper.");
    OpenIabHelper.Options.Builder builder = new OpenIabHelper.Options.Builder()
            .setStoreSearchStrategy(OpenIabHelper.Options.SEARCH_STRATEGY_INSTALLER_THEN_BEST_FIT)
            .setVerifyMode(OpenIabHelper.Options.VERIFY_EVERYTHING)
            .addStoreKeys(InAppConfig.STORE_KEYS_MAP);
    mHelper = new OpenIabHelper(context, builder.build());

    // enable debug logging (for a production application, you should comment this out)
    //OpenIabHelper.enableDebugLogging(true);

    // Start setup. This is asynchronous and the embedded listener
    // will be called once setup completes.
    //Log.d(TAG, "Starting IAB setup.");
    mFragment = fragment;   // cache for use by callback
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            //Log.d(TAG, "IAB Setup finished.");

            if (!result.isSuccess()) {
                //Log.d(TAG, "Problem setting up in-app billing: " + result);
                mHelper = null;
                return;
            }

            //Log.d(TAG, "IAB Setup successful.");
            InAppBilling.get().queryPricesAndPurchases(null);   // use cached mFragment
        }
    });
}

// Launches a background download of info for all products
public void queryPricesAndPurchases(NoteListFragment fragment) {
    if (fragment != null)
        mFragment = fragment;
    //Log.d(TAG, "Launching product info query");
    mHelper.queryInventoryAsync(true, InAppConfig.ALL_SKUS, mGotInventoryListener);
}

// Listener that's called when we finish querying the product info
// Sets the price info for all packs, and clears and then re-sets the notes' purchased states
private IabHelper.QueryInventoryFinishedListener mGotInventoryListener =
        new IabHelper.QueryInventoryFinishedListener() {
            @Override
            public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                //Log.d(TAG, "Query inventory finished: " + inventory.getAllPurchases() + inventory.getAllOwnedSkus());
                if (result.isFailure()) {
                    //Log.d(TAG, "Failed to query inventory: " + result);
                    return;
                }
                //Log.d(TAG, "Query inventory was successful.");

                // Note & pack data controller singletons already exist (created at startup)
                // so we don't need to pass a Context here (which we don't really have handy)
                PackDataController pdc = PackDataController.get(null);
                NoteDataController ndc = NoteDataController.get(null);
                ndc.returnAllNotes();

                for (String sku : InAppConfig.ALL_SKUS) {
                    Pack pack = pdc.findPackWithAppStoreId(sku);
                    if (pack != null) {
                        if (inventory.hasDetails(sku)) {
                            SkuDetails details = inventory.getSkuDetails(sku);
                            String price = details.getPrice();
                            pack.setPrice(price);   // supposedly localized according to user's account
                            //Log.d(TAG, "SKU " + sku + " = " + price);
                        } else {
                            //Log.d(TAG, "SKU " + sku + " details not found");
                        }

                        if (inventory.hasPurchase(sku)) {
                            Purchase purchase = inventory.getPurchase(sku);
                            if (verifyDeveloperPayload(purchase)) {
                                //Log.d(TAG, "Purchased SKU " + sku);
                                ndc.purchaseNotesWithPackId(pack.getId(), true);
                            } else {
                                //Log.d(TAG, "Payload verification failed SKU " + sku);
                            }
                        }
                    }
                }

                ndc.updateNoteList();
                mFragment.updateNoteAdapter();
            }
        };

/**
 * Verifies the developer payload of a purchase.
 */
boolean verifyDeveloperPayload(Purchase p) {
    String store = mHelper.getConnectedAppstoreName();
    if (store != null && store.equals(OpenIabHelper.NAME_AMAZON))
        return true;    // Amazon doesn't support payload verification, so bypass it
    if (p == null || p.getSku() == null)
        return false;
    String payload = p.getDeveloperPayload();
    if (payload == null)
        return false;

    return payload.equals(p.getSku() + p.getSku().length() * 13);
}

// Launches a product purchase request in a background thread
public void purchase(Activity act, String sku) {
    if (!sku.contains("."))
        sku = InAppConfig.SKU_PREFIX + sku;
    String payload = sku + sku.length() * 13;
    mHelper.launchPurchaseFlow(act, sku, RC_REQUEST, mPurchaseFinishedListener, payload);
}

// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        //Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
        if (result.isFailure()) {
            //Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        if (!verifyDeveloperPayload(purchase)) {
            //Log.d(TAG, "Error purchasing. Authenticity verification failed.");
            return;
        }

        //Log.d(TAG, "Purchase successful: " + purchase.getAppstoreName() + ", SKU: " + purchase.getSku());
        PackDataController pdc = PackDataController.get(null);
        Pack pack = pdc.findPackWithAppStoreId(purchase.getSku());
        NoteDataController ndc = NoteDataController.get(null);
        ndc.purchaseNotesWithPackId(pack.getId(), true);
        ndc.updateNoteList();
        if (mFragment != null)
            mFragment.updateNoteAdapter();

        Intent intent = new Intent("purchase-completed");
        // You can also include some extra data.
        intent.putExtra("message", "Purchase completed, reload template");
        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
    }
};
}

1 个答案:

答案 0 :(得分:0)

您可能需要仔细检查并确保已提交您的应用内购买商品,并且这些商品已在Amazon Developer Console上直播。

Amazon App Tester允许您在沙盒环境中进行测试。根据我的经验,它不能完全复制生产环境。因此,即使您能够使用App Tester成功测试,我建议您使用Live App Testing服务在实际生产环境中进行测试。