Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2013
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.      * Handles an activity result that's part of the purchase flow in in-app billing. If you
  3.      * are calling {@link #launchPurchaseFlow}, then you must call this method from your
  4.      * Activity's {@link android.app.Activity@onActivityResult} method. This method
  5.      * MUST be called from the UI thread of the Activity.
  6.      *
  7.      * @param requestCode The requestCode as you received it.
  8.      * @param resultCode The resultCode as you received it.
  9.      * @param data The data (Intent) as you received it.
  10.      * @return Returns true if the result was related to a purchase flow and was handled;
  11.      *     false if the result was not related to a purchase, in which case you should
  12.      *     handle it normally.
  13.      */
  14.     public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
  15.         IabResult result;
  16.         if (requestCode != mRequestCode) return false;
  17.  
  18.         checkNotDisposed();
  19.         checkSetupDone("handleActivityResult");
  20.  
  21.         // end of async purchase operation that started on launchPurchaseFlow
  22.         flagEndAsync();
  23.  
  24.         if (data == null) {
  25.             logError("Null data in IAB activity result.");
  26.             result = new IabResult(IABHELPER_BAD_RESPONSE, "Null data in IAB result");
  27.             if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
  28.             return true;
  29.         }
  30.  
  31.         int responseCode = getResponseCodeFromIntent(data);
  32.         String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
  33.         String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);
  34.  
  35.         if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {
  36.             logDebug("Successful resultcode from purchase activity.");
  37.             logDebug("Purchase data: " + purchaseData);
  38.             logDebug("Data signature: " + dataSignature);
  39.             logDebug("Extras: " + data.getExtras());
  40.             logDebug("Expected item type: " + mPurchasingItemType);
  41.  
  42.             if (purchaseData == null || dataSignature == null) {
  43.                 logError("BUG: either purchaseData or dataSignature is null.");
  44.                 logDebug("Extras: " + data.getExtras().toString());
  45.                 result = new IabResult(IABHELPER_UNKNOWN_ERROR, "IAB returned null purchaseData or dataSignature");
  46.                 if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
  47.                 return true;
  48.             }
  49.  
  50.             Purchase purchase = null;
  51.             try {
  52.                 purchase = new Purchase(mPurchasingItemType, purchaseData, dataSignature);
  53.                 String sku = purchase.getSku();
  54.  
  55.                 // Verify signature
  56.                 if (!Security.verifyPurchase(mSignatureBase64, purchaseData, dataSignature)) {
  57.                     logError("Purchase signature verification FAILED for sku " + sku);
  58.                     result = new IabResult(IABHELPER_VERIFICATION_FAILED, "Signature verification failed for sku " + sku);
  59.                     if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, purchase);
  60.                     return true;
  61.                 }
  62.                 logDebug("Purchase signature successfully verified.");
  63.             }
  64.             catch (JSONException e) {
  65.                 logError("Failed to parse purchase data.");
  66.                 e.printStackTrace();
  67.                 result = new IabResult(IABHELPER_BAD_RESPONSE, "Failed to parse purchase data.");
  68.                 if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
  69.                 return true;
  70.             }
  71.  
  72.             if (mPurchaseListener != null) {
  73.                 mPurchaseListener.onIabPurchaseFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Success"), purchase);
  74.             }
  75.         }
  76.         else if (resultCode == Activity.RESULT_OK) {
  77.             // result code was OK, but in-app billing response was not OK.
  78.             logDebug("Result code was OK but in-app billing response was not OK: " + getResponseDesc(responseCode));
  79.             if (mPurchaseListener != null) {
  80.                 result = new IabResult(responseCode, "Problem purchashing item.");
  81.                 mPurchaseListener.onIabPurchaseFinished(result, null);
  82.             }
  83.         }
  84.         else if (resultCode == Activity.RESULT_CANCELED) {
  85.             logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
  86.             result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
  87.             if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
  88.         }
  89.         else {
  90.             logError("Purchase failed. Result code: " + Integer.toString(resultCode)
  91.                     + ". Response: " + getResponseDesc(responseCode));
  92.             result = new IabResult(IABHELPER_UNKNOWN_PURCHASE_RESPONSE, "Unknown purchase response.");
  93.             if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
  94.         }
  95.         return true;
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement