亚马逊 IAP 未启动购买对话框

时间:2021-07-13 19:13:24

标签: java android

我正在将应用程序从 Play 商店迁移到亚马逊应用商店,考虑到这样它也可用于 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 SKUs: Validating SKUs with Amazon”,但我没有看到亚马逊购买窗口。似乎从未调用过侦听器代码,即使我在其中放置了一些断点,也从未达到过它们,这很奇怪,因为它已通过方法“PurchasingService.registerListener(this, purchaseListener) 初始化并成功调用” "

非常感谢任何帮助!

谢谢,晚上好

0 个答案:

没有答案