Google 播放:从 IInAppBillingService 更新价格

Google Play: update prices from IInAppBillingService

我创建了一个 class 用于处理商店中的订阅。 一开始我从服务器收到带有价格的订阅列表 该代码正在运行,并且在大多数设备上都没有问题。 但一些用户不接受价格。就像服务器没有响应一样。我研究了 Google 文档中的实施示例,但我无法理解代码中哪里会出现问题。 我的部分代码:

public class BillingActivity extends AppCompatActivity {

RelativeLayout imageLayout;
View payButton;
// WebView payWebView;
// TextView useCodeButton;

ProgressBar progress1;
ProgressBar progress2;
ProgressBar progress3;

IInAppBillingService inAppBillingService;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_billing_subscription);
    price_1monthTextView = (TextView) findViewById(R.id.price_1monthTextView);
    relative_1month = (RelativeLayout) findViewById(R.id.relative_1month);
    relative_1month.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (MainActivity.myAccount == null) return;
            if (MainActivity.myAccount.getUniqid() == null) return;
            if (subscription1m != null) {
                try {
                    Log.d("my", "purchase..." + subscription1m.storeName);
                    purchaseProduct(subscription1m);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("my", "purchase error = " + e.toString());
                }
            }
        }
    });


    progress1 = (ProgressBar) findViewById(R.id.progress1);
    startGoogleBilling();
}


private void startGoogleBilling() {
    if (serviceConnection != null) {
        unbindService(serviceConnection);
    }

    progress1.setVisibility(View.VISIBLE);


    serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            inAppBillingService = IInAppBillingService.Stub.asInterface(service);

            getSubscribtionsList();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            inAppBillingService = null;
        }
    };

    Intent serviceIntent =
            new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}


private static final int PR_CNT = 3;

List<InAppProduct> subscriptions = null;

InAppProduct subscription1m = null;
InAppProduct subscription3m = null;
InAppProduct subscription1y = null;


String[] productIds = {"eq.subscription.1m", "eq.subscription.3m.2", "eq.subscription.1y"};

private void getSubscribtionsList() {
    mSwipeRefreshLayout.setRefreshing(false);
    progress1.setVisibility(View.GONE);
    try {

        subscriptions =
                getInAppPurchases("subs", productIds[0], productIds[1], productIds[2]);

        if (subscriptions.size() == PR_CNT) {
            for (InAppProduct inAppProduct : subscriptions) {
                String productId = inAppProduct.productId;
                Log.d("my", "productId= " + productId);
                if (productId.contains(productIds[0])) subscription1m = inAppProduct;
                if (productId.contains(productIds[1])) subscription3m = inAppProduct;
                if (productId.contains(productIds[2])) subscription1y = inAppProduct;
            }
            Log.d("my", "1m= " + subscription1m.storeName + " pr=" + subscription1m.price + "\n\r  " +
                    "3m= " + subscription3m.storeName + " pr=" + subscription3m.price + "\n\r   " +
                    "1y= " + subscription1y.storeName + " pr=" + subscription1y.price + "\n\r   ");
            ///----------------------!!!!
            // purchaseProduct(inAppProduct);
        }

        updatePrices();

    } catch (Exception e) {
        Log.d("my", "exc  = " + e.toString());
        if (e.toString().contains("Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getSkuDetails")) {
            if (attempt < 4) {
                //getSubscribtionsList();

                // startGoogleBilling();
            } else {
            }

        }
        //  Toast.makeText(this, "Google inApp connection error", Toast.LENGTH_SHORT).show();
        //  refreshButton.setVisibility(View.VISIBLE);
        startGoogleBilling();

    }
}

private int attempt = 0;

@Override
public void onDestroy() {
    super.onDestroy();
    if (serviceConnection != null) {
        unbindService(serviceConnection);
    }
}

class InAppProduct {

    public String productId;
    public String storeName;
    public String storeDescription;
    public String price;
    public boolean isSubscription;
    public int priceAmountMicros;
    public String currencyIsoCode;

    public String getSku() {
        return productId;
    }

    String getType() {
        return isSubscription ? "subs" : "inapp";
    }
}

List<InAppProduct> getInAppPurchases(String type, String... productIds) throws Exception {
    ArrayList<String> skuList = new ArrayList<>(Arrays.asList(productIds));
    Bundle query = new Bundle();
    query.putStringArrayList("ITEM_ID_LIST", skuList);
    Bundle skuDetails = inAppBillingService.getSkuDetails(
            3, getPackageName(), type, query);
    ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
    List<InAppProduct> result = new ArrayList<>();
    for (String responseItem : responseList) {
        JSONObject jsonObject = new JSONObject(responseItem);
        InAppProduct product = new InAppProduct();
        // "com.example.myapp_testing_inapp1"
        product.productId = jsonObject.getString("productId");
        // Покупка
        product.storeName = jsonObject.getString("title");
        // Детали покупки
        product.storeDescription = jsonObject.getString("description");
        // "0.99USD"
        product.price = jsonObject.getString("price");
        // "true/false"
        product.isSubscription = jsonObject.getString("type").equals("subs");
        // "990000" = цена x 1000000
        product.priceAmountMicros =
                Integer.parseInt(jsonObject.getString("price_amount_micros"));
        // USD
        product.currencyIsoCode = jsonObject.getString("price_currency_code");
        result.add(product);
    }
    return result;
}

private void updatePrices() {
    if (subscriptions.size() == PR_CNT) {

        price_1monthTextView.setText(subscription1m.price);
        price_3monthTextView.setText(subscription3m.price);
        price_1yearTextView.setText(subscription1y.price);
    }

}


}

我的问题是 X 变量的位深度 priceAmountMicros

playmarket 的价格乘以 1000000,哈萨克坚戈的汇率 = 1 美元 = 331,3 KZT。 如果我的价格是 18 000 KTZ 和 * 1000 000。 对于价格变量,我必须使用 long type