2011-12-26 19 views
11

Tôi đang thêm mua hàng trong ứng dụng trong mã hóa của mình, nó hoạt động tốt trong khi mua nhưng cho lỗi và ứng dụng đóng khi tôi cố gắng thêm mã Restore_Transaction khi ứng dụng bị xóa và cài đặt một lần nữa, tôi đã thêm bên dưới mã hóaInApp Purchase RESTORE_TRANSACTIONS, tôi không thể tìm ra mã trong số

trong onCreate i viết

startService(new Intent(mContext, BillingService.class)); 
     BillingHelper.setCompletedHandler(mTransactionHandler); 

     if (BillingHelper.isBillingSupported()) { 
      BillingHelper.restoreTransactionInformation(BillingSecurity 
        .generateNonce()); 
     } 

và sau đó tôi gọi là xử lý bằng

public Handler mTransactionHandler = new Handler() { 
     public void handleMessage(android.os.Message msg) { 
      if (BillingHelper.latestPurchase.isPurchased()) { 
       showItem(); 
      } 
     }; 
    }; 

    private void showItem() { 
     purchased = Purchased.getPurchaseInfo(getApplicationContext()); 
     if (purchased == null) { 
      Date d = new Date(); 
      Toast.makeText(getApplicationContext(), "--- Upgrated ---", 
        Toast.LENGTH_LONG).show(); 
      purchased = new Purchased(getApplicationContext()); 
      purchased.isPurchased = 1; 
      purchased.purchasedDate = d.getTime(); 
      purchased.save(); 
      Intent intent = new Intent(ActorGenieActivity.this, 
        SplashScreen.class); 
      startActivity(intent); 
     } 
    } 

Trả lời

5

Tôi tìm thấy câu trả lời cho câu hỏi của tôi, thanx cho anddev

Bạn phải kiểm tra cho việc mua bán không được null

public static void verifyPurchase(String signedData, String signature) { 
    ArrayList<VerifiedPurchase> purchases = BillingSecurity.verifyPurchase(
      signedData, signature); 
    if (purchases != null && !purchases.isEmpty()) { 
     latestPurchase = purchases.get(0); 
     confirmTransaction(new String[] { latestPurchase.notificationId }); 
     if (mCompletedHandler != null) { 
      mCompletedHandler.sendEmptyMessage(0); 
     } else { 
      Log 
        .e(
          TAG, 
          "verifyPurchase error. Handler not instantiated. Have you called setCompletedHandler()?"); 
     } 
    } 
} 

và trong Confirm_Notification u hav để kiểm tra

if (notifyIds[0] != null) 
+0

giải pháp là gì? – Pabluez

+0

Bạn có làm việc này không? Tôi không nhận được gì ngoài lỗi và lực lượng gần gũi, altho, tôi không thể thực sự làm việc ra những gì bạn đã làm từ câu trả lời này. Mã cho lớp "Đã mua" là gì? – Hippyjim

+0

Tôi nên vượt qua những giá trị nào như signedData và chữ ký? –

1

Làm theo cách sau:

confirmTransaction(new String[] { latestPurchase.notificationId }); 

đây và làm điều này:

protected static void confirmTransaction(String[] notifyIds) { 
     if (amIDead()) { 
      return; 
     } 
     // there isn't a notifyid then this was the restore transaction call and this should be skipped 
     if (notifyIds[0] != null){ 
     Log.i(TAG, "confirmTransaction()"); 
     Bundle request = makeRequestBundle("CONFIRM_NOTIFICATIONS"); 
      ...... 
      ...... 
} 

trình như một say mê tạo cho tôi .. Cảm ơn Guys ...

1

Bạn có thể sử dụng mã dưới đây để có được lịch sử mua hàng:

public static ArrayList<VerifiedPurchase> verifyPurchase(String signedData, 
      String signature) { 
     if (signedData == null) { 
      //Log.e(TAG, "data is null"); 
      return null; 
     } 
     if (Constans.DEBUG) { 
      //Log.i(TAG, "signedData: " + signedData); 
     } 
     boolean verified = false; 
     if (!TextUtils.isEmpty(signature)) { 
      /** 
      * Compute your public key (that you got from the Android Market 
      * publisher site). 
      * 
      * Instead of just storing the entire literal string here embedded 
      * in the program, construct the key at runtime from pieces or use 
      * bit manipulation (for example, XOR with some other string) to 
      * hide the actual key. The key itself is not secret information, 
      * but we don't want to make it easy for an adversary to replace the 
      * public key with one of their own and then fake messages from the 
      * server. 
      * 
      * Generally, encryption keys/passwords should only be kept in 
      * memory long enough to perform the operation they need to perform. 
      */ 
      String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuKgldGQPL/xV9WKLmY62UVgEm7gsPI/T/nQxRKpYN17m8Sq3gO9nWD17wXew4oNaHmMAmArS7s7eFi3Z+XiyWil1iZvEOdBOdZD502BzujPoBa4Fu9eITPBO9tzBEdvNLXf8amnsRj53TA4bcxB2O6OcXrQIv3t3n5Dg5Nn+rJpoKSNUv7NEzJagG/2NhyjIysAObbvQ5SBQ5NgRtZlvhsTeQJPMLhRAoRcTK/+47VkhrxM3PppeGjoNRryn6d+RhMjs/nydvoQtP2V76UcUu4m+daDnK3PxOnwLt50hNtQhNf3VgixVrSKfHUWp240uEz9MHstjj8BWPH9BFF/TewIDAQAB"; 
      PublicKey key = Security.generatePublicKey(base64EncodedPublicKey); 
      verified = Security.verify(key, signedData, signature); 
      if (!verified) { 
       //Log.w(TAG, "signature does not match data."); 
       return null; 
      } 
     } 

     JSONObject jObject; 
     JSONArray jTransactionsArray = null; 
     int numTransactions = 0; 
     long nonce = 0L; 
     try { 
      jObject = new JSONObject(signedData); 

      // The nonce might be null if the user backed out of the buy page. 
      nonce = jObject.optLong("nonce"); 
      jTransactionsArray = jObject.optJSONArray("orders"); 
      if (jTransactionsArray != null) { 
       numTransactions = jTransactionsArray.length(); 
      } 
     } catch (JSONException e) { 
      return null; 
     } 

     if (!Security.isNonceKnown(nonce)) { 
      //Log.w(TAG, "Nonce not found: " + nonce); 
      return null; 
     } 

     ArrayList<VerifiedPurchase> purchases = new ArrayList<VerifiedPurchase>(); 
     try { 
      for (int i = 0; i < numTransactions; i++) { 
       JSONObject jElement = jTransactionsArray.getJSONObject(i); 
       int response = jElement.getInt("purchaseState"); 
       PurchaseState purchaseState = PurchaseState.valueOf(response); 
       String productId = jElement.getString("productId"); 
       String packageName = jElement.getString("packageName"); 
       long purchaseTime = jElement.getLong("purchaseTime"); 
       String orderId = jElement.optString("orderId", ""); 
       String notifyId = null; 
       if (jElement.has("notificationId")) { 
        notifyId = jElement.getString("notificationId"); 
       } 
       String developerPayload = jElement.optString(
         "developerPayload", null); 

       // If the purchase state is PURCHASED, then we require a 
       // verified nonce. 
       if (purchaseState == PurchaseState.PURCHASED && !verified) { 
        continue; 
       } 
       purchases.add(new VerifiedPurchase(purchaseState, notifyId, 
         productId, orderId, purchaseTime, developerPayload)); 
      } 
     } catch (JSONException e) { 
      //Log.e(TAG, "JSON exception: ", e); 
      return null; 
     } 
     removeNonce(nonce); 
     return purchases; 
    } 

Bạn có thể gọi phương thức này từ phương thức bên dưới trong lớp BillingService:

private void purchaseStateChanged(int startId, String signedData, 
      String signature) { 
     ArrayList<Security.VerifiedPurchase> purchases; 
     purchases = Security.verifyPurchase(signedData, signature); 
     if (purchases == null) { 
      return; 
     } 

     ArrayList<String> notifyList = new ArrayList<String>(); 
     for (VerifiedPurchase vp : purchases) { 
      if (vp.notificationId != null) { 
       notifyList.add(vp.notificationId); 
      } 
      ResponseHandler.purchaseResponse(this, vp.purchaseState, 
        vp.productId, vp.orderId, vp.purchaseTime, 
        vp.developerPayload); 
     } 
     if (!notifyList.isEmpty()) { 
      String[] notifyIds = notifyList.toArray(new String[notifyList 
        .size()]); 
      confirmNotifications(startId, notifyIds); 
     } 
    } 
+0

Nhưng chúng ta không thể gọi phương thức này trực tiếp như BilllingService.purchaseStateChanged (arg1, arg2), Thực ra tôi muốn dertermine id của mục đã mua bất cứ khi nào ứng dụng bắt đầu. SO làm thế nào tôi có thể làm điều này. – AndroidDev

+0

Bạn không cần phải gọi phương thức này như thế. Có một luồng được xác định trong phương thức sdk.Thanh toán ứng dụng sẽ được gọi từ phương thức handleCommand (Intent intent, int startId) sẽ được gọi khi phương thức BillingService onCreate() . –

Các vấn đề liên quan