简体   繁体   中英

Android in app purchase . in which case payment is successful?

I want to know in which case the payment is successful and in which case it is not ? Here is my onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1001) {
            String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");

            if (resultCode == RESULT_OK) {
                try {
                    JSONObject jo = new JSONObject(purchaseData);
                    String sku = jo.getString(inappid);
                    Toast.makeText(
                            AndroidLauncher.this,
                            "You have bought the " + sku
                                    + ". Excellent choice,adventurer!",
                            Toast.LENGTH_LONG).show();

                } catch (JSONException e) {
                    System.out.println("Failed to parse purchase data.");
                    e.printStackTrace();
                }
            }
        }
    }

You can use below code to check whether purchase was successful or not.

if(requestCode==YOUR_REQUEST_CODE)
{
    int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
    String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
    String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

    if (resultCode == RESULT_OK) {
        try {
            JSONObject jo = new JSONObject(purchaseData);
            String sku = jo.getString("productId");
            //purchase is successful
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else if (resultCode == RESULT_CANCELED) {
        //purchase is unsuccessful                
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM