2012-12-15 15 views
12

Google ha eseguito l'aggiornamento a IAB3 (in App Billing versione 3). Prima di tutto, quale problema nel codice di esempio .. super.onDestroy() è mancato.Ripristina acquisti in Fatturazione in-app (Versione IAB 3) android

ho implementato v3 con l'aiuto di http://developer.android.com/google/play/billing/billing_integrate.html

E 'testato sul telefono, non funziona in emulator.It bloccato in emulatore.

Il mio problema è che non ho visto l'API per il ripristino delle transazioni. Come posso ripristinare gli acquisti con IAB3? È mService.getPurchases(apiVersion, packageName, type, continuationToken). Qualcuno ha provato questo ?? Ciò restituisce gli articoli acquistati dagli articoli memorizzati localmente o ripristina gli articoli acquistati? La disinstallazione dell'applicazione non ha continuationToken. Dovrebbe essere null?

E che dire quando lo stato dell'acquisto cambia ??

Si prega di aiuto!

Grazie in anticipo.

EDIT:

Google ha aggiornato la libreria in app billing e risolto il problema super.onDestroy(). Hanno anche aggiunto alcune funzionalità aggiuntive.

+0

L'acquisto di query che si verifica subito dopo l'installazione esegue questa operazione. Sto cercando lo stesso, ma ho un altro problema dopo aver ottenuto il risultato "L'ELEMENTO è già di proprietà e di nuovo provo a chiamare launchPurchaseFlow. Rinuncia un'eccezione" java.lang.IllegalStateException: Impossibile avviare l'operazione asincrona (launchPurchaseFlow) perché è in corso un'altra operazione asincrona (launchPurchaseFlow). "." Non so come attraversarlo. – LuminiousAndroid

+0

Per il test, dovrei pubblicare l'applicazione o lo farò con bozza ?? .. La mia applicazione non è pubblicata in gioco (non una singola versione) –

+1

Hai solo bisogno di pubblica il tuo prodotto in-app e non l'app. – LuminiousAndroid

risposta

2

Per rendere l'articolo di consumo è necessario inviare una richiesta di consumo e devi farlo in un thread separato.

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 1111) { 
     int responseCode = data.getIntExtra("RESPONSE_CODE", 0); 
     String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); 
     String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE"); 
     Logger.printMessage(TAG, "on activity result reponse" 
       + responseCode, Logger.DEBUG); 
     if (resultCode == RESULT_OK && responseCode == 0) { 
      try { 
       JSONObject jo = new JSONObject(purchaseData); 
       String sku = jo.getString("productId"); 
       String title = jo.getString("title"); 
       addChipsToBalance(sku); 
       final String token = jo.getString("purchaseToken"); 
       Toast.makeText(BuyChipsActivity.this, 
         "You have bought " + title + ". Enjoy the game!", 
         Toast.LENGTH_SHORT).show(); 

       new Thread(new Runnable() { 

        @Override 
        public void run() { 
         // TODO Auto-generated method stub 
         Logger.printMessage(TAG, "inside run", Logger.DEBUG); 
         try { 
          int response = mService.consumePurchase(3, 
            getPackageName(), token); 
          Logger.printMessage(TAG, "inside run response" 
            + response, Logger.DEBUG); 
         } catch (RemoteException e) { 
          // TODO Auto-generated catch block 
          Logger.printMessage(TAG, "exception here 1", 
            Logger.DEBUG); 
          e.printStackTrace(); 
         } 
        } 
       }).start(); 
       // alert("You have bought the " + sku + 
       // ". Excellent choice, adventurer!"); 
      } catch (JSONException e) { 
       // alert("Failed to parse purchase data."); 
       e.printStackTrace(); 
      } 
     } 
    } 

Ma a volte consumano richiesta non viene completato il fine google quindi si consiglia di interrogare l'elenco oggetto acquistato e consumarlo con il token di acquisto. Ho fatto come questo

private void showPreviousPurchases() { 
    Logger.printMessage(TAG, "previous purchases", Logger.DEBUG); 
    if (mService == null) { 
     Toast.makeText(this, "Something Went Wrong. Try later", 
       Toast.LENGTH_LONG).show(); 
     return; 
    } 
    Bundle ownedItems = null; 
    ; 
    try { 
     ownedItems = mService.getPurchases(3, getPackageName(), "inapp", 
       null); 
    } catch (RemoteException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    if (ownedItems == null) { 
     Logger.printMessage(TAG, "criical error ", Logger.DEBUG); 
     return; 
    } 
    int response = ownedItems.getInt("RESPONSE_CODE"); 
    if (response == 0) { 
     ArrayList<String> ownedSkus = ownedItems 
       .getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); 
     ArrayList<String> purchaseDataList = ownedItems 
       .getStringArrayList("INAPP_PURCHASE_DATA_LIST"); 
    /* ArrayList<String> signatureList = ownedItems 
       .getStringArrayList("INAPP_DATA_SIGNATURE"); 
     String continuationToken = ownedItems 
       .getString("INAPP_CONTINUATION_TOKEN");*/ 

     for (int i = 0; i < purchaseDataList.size(); ++i) { 
      String purchaseData = purchaseDataList.get(i); 
      Logger.printMessage(TAG, "json = " + purchaseData, 
        Logger.DEBUG); 
      // String signature = signatureList.get(i); 
      String sku = ownedSkus.get(i); 

      addChipsAndMakeItConsumable(purchaseData); 
      // do something with this purchase information 
      // e.g. display the updated list of products owned by user 
     } 

     // if continuationToken != null, call getPurchases again 
     // and pass in the token to retrieve more items 
    } 

} 

private void addChipsAndMakeItConsumable(String purchaseData) { 

    try { 
     JSONObject jo = new JSONObject(purchaseData); 
     String sku = jo.getString("productId"); 
     // String title = jo.getString("title"); 
     addChipsToBalance(sku); 
     final String token = jo.getString("purchaseToken"); 
     Logger.printMessage(TAG, "id = " + sku, Logger.DEBUG); 

     Logger.printMessage(TAG, "inside run", Logger.DEBUG); 
     try { 
      int response = mService.consumePurchase(3, getPackageName(), 
        token); 
      Logger.printMessage(TAG, "inside run response" + response, 
        Logger.DEBUG); 
     } catch (RemoteException e) { 
      // TODO Auto-generated catch block 
      Logger.printMessage(TAG, "exception here 1", Logger.DEBUG); 
      e.printStackTrace(); 
     } 

     // alert("You have bought the " + sku + 
     // ". Excellent choice, adventurer!"); 
    } catch (JSONException e) { 
     // alert("Failed to parse purchase data."); 
     e.printStackTrace(); 
    } 
} 
+0

consuma significa che l'oggetto sarà nuovamente disponibile per l'acquisto. È così ??? ... Voglio fare un acquisto unico, una volta che l'utente ha acquistato. Se l'app verrà disinstallata, desidero ripristinare gli acquisti per quel particolare utente. Fammi sapere come posso ripristinare la transazione/acquisto.Se rimborso articolo, come viene notificato l'utente ?? –

+0

Hey user1194037, Ho provato la tua funzione per ottenere informazioni sull'ultimo acquisto funziona molto bene per la prima volta, ma se ho provato di nuovo dopo aver disinstallato la mia app, non fa nulla. Se il caso è che Google ci fornisce un ID prodotto che può essere in grado di acquistare una volta in tutta l'app, non ha senso ripristinare la transazione. Hai qualche idea riguardo questo? Bene, sto usando "android.test.purchase" come id prodotto. Si prega di provare questo dopo disinstallare la tua app e gentilmente fammi sapere se funziona alla fine grazie..kabir :) – LuminiousAndroid

+0

Se l'utente sta eseguendo la vostra applicazione per la prima volta, in tal caso è possibile controllare se ci sono eventuali preowned in prodotti app, se ci sono poi ripristinare quella particolare caratteristica. – Deepanshu

0

Nel vostro IabHelper.java che è un esempio nel vostro/android-sdk/extras/google/play_billing/campioni/inserire questo codice per ottenere tutte le articolo che è stato acquistato dall'utente . Ciò restituirà una matrice JSON di dati acquistati. È anche possibile utilizzare Purchase.java con cui analizzare, che è disponibile anche nella cartella degli esempi.

 public ArrayList<String> getAllPurchases() throws RemoteException{ 
    Bundle ownedItems = mService.getPurchases(3, mContext.getPackageName(),"inapp", null); 

    int response = getResponseCodeFromBundle(ownedItems); 
    logDebug("Owned items response: " + String.valueOf(response)); 
    if (response != BILLING_RESPONSE_RESULT_OK) { 
     logDebug("getPurchases() failed: " + getResponseDesc(response)); 

    } 
    if (!ownedItems.containsKey(RESPONSE_INAPP_ITEM_LIST) 
      || !ownedItems.containsKey(RESPONSE_INAPP_PURCHASE_DATA_LIST) 
      || !ownedItems.containsKey(RESPONSE_INAPP_SIGNATURE_LIST)) { 
     logError("Bundle returned from getPurchases() doesn't contain required fields."); 
    } 

    ArrayList<String> purchaseDataList = ownedItems.getStringArrayList(RESPONSE_INAPP_PURCHASE_DATA_LIST); 
    return purchaseDataList; 
} 

e nella vostra attività principale

public class MainActivity extends Activity{ 
    private IabHelper mHelper; 
     private String arrayString; 
     public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mHelper = new IabHelper(this,"YOUR PUBLIC KEY"); 
     mHelper.enableDebugLogging(true); 
     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 

     public void onIabSetupFinished(IabResult result) { 

     if (!result.isSuccess()) { // Oh noes, there was a problem. 
      Toast.makeText(this,"Problem setting up in-app billing: " + result,Toast.LENGTH_LONG).show(); 
       return; 
      } 

     arrayString=mHelper.getAllPurchases().toString(); 

     Log.d("Purchases: ",""+arrayString); 


     array = new JSONArray(arrayString); 

     for (int i = 0; i < array.length(); i++) { 
      JSONObject row = array.getJSONObject(i);  
      productId=row.getString("productId"); //this will get the product id's that has been purchased. 

      Log.e("To be restored:", " PRODUCT ID's "+productId); 
     } 

     });   
    } 
} 

Spero che questo vi aiuterà.^_^Grazie.

Problemi correlati