退款用户的钱

时间:2018-04-26 13:36:09

标签: android in-app-purchase google-play-services in-app-billing in-app

我将应用程序内的Billing API集成到我的应用程序(android)中,以允许用户通过应用程序中的其他内容购买硬币... in onPurchases如果响应是&#34,则更新。确定"我让Volley请求增加用户平衡...... 问题是......如果请求因任何错误(服务器错误 - 超时错误)而失败, 任何人都知道如何以编程方式退还用户的钱? 这是我的代码..

@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
    Log.e("responseCode", responseCode + " ===== " + BillingClient.BillingResponse.OK);
    if (responseCode == BillingClient.BillingResponse.OK) {
        if (purchases != null) {
            Log.e("purchases size", purchases.size() + "");
            for (int i = 0; i < purchases.size(); i++) {
                for (int j = 0; j < SKUS.get(BillingClient.SkuType.INAPP).size(); j++) {
                    if (purchases.get(i).getSku().equals(SKUS.get(BillingClient.SkuType.INAPP).get(j).getId())) {
                        deposit(activity.getApplicationContext(), SKUS.get(BillingClient.SkuType.INAPP).get(j).getPrice(), purchases.get(i).getPurchaseToken());
                    }
                }
            }
        }
        Log.e("OPU response", "true");

    } else {
        Log.e("OPU error", "false");
    }
}

这是我的请求......

protected void deposit(final Context context, double price, final String token) {
    JSONObject amount = new JSONObject();
    try {
        amount.put("amount", price);
        amount.put("payment_type", "playStore");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.e("amount", amount.toString());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
            URLs.DEPOSIT,
            amount,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("deposit res", response.toString());
                    billingClient.consumeAsync(token, new ConsumeResponseListener() {
                        @Override
                        public void onConsumeResponse(int responseCode, String purchaseToken) {
                            if (responseCode == BillingClient.BillingResponse.OK) {
                                Log.e("Done", "done");
                            }
                        }
                    });
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("deposit", error.toString());
                    billingClient.consumeAsync(token, new ConsumeResponseListener() {
                        @Override
                        public void onConsumeResponse(int responseCode, String purchaseToken) {
                            if (responseCode == BillingClient.BillingResponse.OK) {
                                Log.e("Done", "done");
                            }
                        }
                    });
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            String token = "Bearer " + MySingleton.getmInstance(context).getUserToken();
            headers.put("Content-Type", "application/json; charset=utf-8");
            headers.put("Authorization", token);
            return headers;
        }
    };
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    jsonObjectRequest.setShouldCache(false);
    MySingleton.getmInstance(context).addToRQ(jsonObjectRequest);
}

提前致谢

0 个答案:

没有答案