首页 > 解决方案 > 亚马逊 IAP 未启动购买对话框

问题描述

我正在将应用程序从 Play 商店迁移到 Amazon App Store,考虑到这样它也适用于 Windows 11。

为了让事情变得非常快速和简单,我做了一个名为 PurchaseActivity 的活动,其中包含亚马逊 IAP 指南 PDF 带来的代码。

该活动从对话框窗口的“立即购买”按钮调用,代码如下:

public class PurchaseActivity extends Activity {
String parentSKU = "com.amazon.sample.iap.subscription.mymagazine";
//Define UserId and MarketPlace
private String currentUserId;
private String currentMarketplace;
private ProgressDialog progress;

@Override
protected void onStart(){
    super.onStart();
    progress = new ProgressDialog(this);
    progress.setTitle("Purchasing");
    progress.setMessage("Wait while making the purchase...");
    progress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            progress.dismiss();//dismiss dialog
            finish();
        }
    });
    progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
    progress.show();

    PurchasingService.registerListener(this, purchasingListener);

    PurchasingService.purchase(parentSKU);
}

@Override
protected void onResume() {
    super.onResume();

    //getUserData() will query the Appstore for the Users information
    PurchasingService.getUserData();
    //getPurchaseUpdates() will query the Appstore for any previous purchase
    PurchasingService.getPurchaseUpdates(true);
    //getProductData will validate the SKUs with Amazon Appstore
    final Set<String> productSkus = new HashSet<String>();
    productSkus.add(parentSKU);
    PurchasingService.getProductData(productSkus);
    Log.v("Validating SKUs", "Validating SKUs with Amazon");
}


PurchasingListener purchasingListener = new PurchasingListener() {
    @Override
    public void onUserDataResponse(UserDataResponse response) {
        final UserDataResponse.RequestStatus status = response.getRequestStatus();
        switch (status) {
            case SUCCESSFUL:
                currentUserId = response.getUserData().getUserId();
                currentMarketplace = response.getUserData().getMarketplace();
                Log.v("IAP SDK", "loaded userdataResponse");
                break;
            case FAILED:
            case NOT_SUPPORTED:
                // Fail gracefully.
                Log.v("IAP SDK", "loading failed");
                break;
        }
    }

    @Override
    public void onProductDataResponse(ProductDataResponse productDataResponse) {
        switch (productDataResponse.getRequestStatus()) {
            case SUCCESSFUL:

                //get informations for all IAP Items (parent SKUs)
                final Map<String, Product> products = productDataResponse.getProductData();
                for (String key : products.keySet()) {
                    Product product = products.get(key);
                    Log.v("Product:", 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()));
                }
                //get all unavailable SKUs
                for (String s : productDataResponse.getUnavailableSkus()) {
                    Log.v("Unavailable SKU:" + s, "Unavailable SKU:" + s);
                }
                break;
            case FAILED:
                Log.v("FAILED", "FAILED");
                progress.dismiss();
                finish();
                break;
        }
    }

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

    @Override
    public void onPurchaseUpdatesResponse(PurchaseUpdatesResponse response) {

        // Process receipts
        switch (response.getRequestStatus()) {
            case SUCCESSFUL:
                for (final Receipt receipt : response.getReceipts()) {
                    // Process receipts
                    if (!receipt.isCanceled()) {
                        // sharedprefs
                         SharedPreferences sharedPreference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor sharedPrefEditor = sharedPreference.edit();
                        sharedPrefEditor.putBoolean("isPro",true);
                        sharedPrefEditor.apply();
                        progress.dismiss();
                        finish();
                    }
                }
                if (response.hasMore()) {
                    PurchasingService.getPurchaseUpdates(true);
                }
                break;
            case FAILED:
                Log.d("FAILED", "FAILED");
                progress.dismiss();
                finish();
                break;
        }
    }
};

}

是的,我知道我不应该在 onStart() 方法中调用所有这些东西,但我稍后会用 UI 制作一个 onCreate()。

正如您从这段代码中看到的,我正在沙盒模式下进行测试。

问题:实际上,当活动开始时,我看到了progressDialog,并且我在调试日志中读到“V/Validating SKU:Validating SKU with Amazon”,但我没有看到亚马逊购买窗口。似乎从未调用过侦听器代码,即使我在其中放置了一些断点,它们也永远不会到达,考虑到它显然是通过方法“PurchasingService.registerListener(this,purchaseListener)初始化并成功调用的,这很奇怪"

任何帮助都会非常感激!

谢谢,晚上好

标签: javaandroid

解决方案


推荐阅读