Google Play:从IInAppBillingService更新价格

时间:2017-08-14 09:46:57

标签: android android-studio google-play billing

我创建了一个用于在商店中处理订阅的类。 在开始时,我从服务器收到包含价格的订阅列表 代码正常运行,在大多数设备上都没有问题。 但有些用户没有收回价格。这就像服务器没有响应。我已经研究了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);
    }

}


}

1 个答案:

答案 0 :(得分:1)

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

来自游戏市场的价格乘以1000000,而哈萨克坚戈的汇率= 1美元= 331,3 KZT。 如果我的价格是18 000 KTZ和* 1000 000。 对于价格变量,我必须使用类型