Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Handles an activity result that's part of the purchase flow in in-app billing. If you
- * are calling {@link #launchPurchaseFlow}, then you must call this method from your
- * Activity's {@link android.app.Activity@onActivityResult} method. This method
- * MUST be called from the UI thread of the Activity.
- *
- * @param requestCode The requestCode as you received it.
- * @param resultCode The resultCode as you received it.
- * @param data The data (Intent) as you received it.
- * @return Returns true if the result was related to a purchase flow and was handled;
- * false if the result was not related to a purchase, in which case you should
- * handle it normally.
- */
- public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
- IabResult result;
- if (requestCode != mRequestCode) return false;
- checkNotDisposed();
- checkSetupDone("handleActivityResult");
- // end of async purchase operation that started on launchPurchaseFlow
- flagEndAsync();
- if (data == null) {
- logError("Null data in IAB activity result.");
- result = new IabResult(IABHELPER_BAD_RESPONSE, "Null data in IAB result");
- if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
- return true;
- }
- int responseCode = getResponseCodeFromIntent(data);
- String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
- String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);
- if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {
- logDebug("Successful resultcode from purchase activity.");
- logDebug("Purchase data: " + purchaseData);
- logDebug("Data signature: " + dataSignature);
- logDebug("Extras: " + data.getExtras());
- logDebug("Expected item type: " + mPurchasingItemType);
- if (purchaseData == null || dataSignature == null) {
- logError("BUG: either purchaseData or dataSignature is null.");
- logDebug("Extras: " + data.getExtras().toString());
- result = new IabResult(IABHELPER_UNKNOWN_ERROR, "IAB returned null purchaseData or dataSignature");
- if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
- return true;
- }
- Purchase purchase = null;
- try {
- purchase = new Purchase(mPurchasingItemType, purchaseData, dataSignature);
- String sku = purchase.getSku();
- // Verify signature
- if (!Security.verifyPurchase(mSignatureBase64, purchaseData, dataSignature)) {
- logError("Purchase signature verification FAILED for sku " + sku);
- result = new IabResult(IABHELPER_VERIFICATION_FAILED, "Signature verification failed for sku " + sku);
- if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, purchase);
- return true;
- }
- logDebug("Purchase signature successfully verified.");
- }
- catch (JSONException e) {
- logError("Failed to parse purchase data.");
- e.printStackTrace();
- result = new IabResult(IABHELPER_BAD_RESPONSE, "Failed to parse purchase data.");
- if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
- return true;
- }
- if (mPurchaseListener != null) {
- mPurchaseListener.onIabPurchaseFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Success"), purchase);
- }
- }
- else if (resultCode == Activity.RESULT_OK) {
- // result code was OK, but in-app billing response was not OK.
- logDebug("Result code was OK but in-app billing response was not OK: " + getResponseDesc(responseCode));
- if (mPurchaseListener != null) {
- result = new IabResult(responseCode, "Problem purchashing item.");
- mPurchaseListener.onIabPurchaseFinished(result, null);
- }
- }
- else if (resultCode == Activity.RESULT_CANCELED) {
- logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
- result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
- if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
- }
- else {
- logError("Purchase failed. Result code: " + Integer.toString(resultCode)
- + ". Response: " + getResponseDesc(responseCode));
- result = new IabResult(IABHELPER_UNKNOWN_PURCHASE_RESPONSE, "Unknown purchase response.");
- if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement