public void launchPurchaseFlow(
      Activity activity,
      String sku,
      int requestCode,
      OnPurchaseFinishedListener listener,
      String extraData) {
    checkSetupDone("launchPurchaseFlow");
    asyncFlag.start("launchPurchaseFlow");
    Result result;

    try {
      Bundle buyIntentBundle = connection.getBuyIntent(sku, extraData);
      int response = getResponseCodeFromBundle(buyIntentBundle);
      if (response != BILLING_RESPONSE_RESULT_OK) {
        result = new Result(response, "Unable to buy item");
        if (listener != null) listener.onPurchaseFinished(result, null);
      } else {
        PendingIntent intent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        this.requestCode = requestCode;
        purchaseListener = listener;
        activity.startIntentSenderForResult(
            intent.getIntentSender(), requestCode, new Intent(), 0, 0, 0);
      }
    } catch (IntentSender.SendIntentException e) {
      result = new Result(HELPER_SEND_INTENT_FAILED, "Failed to send intent.");
      if (listener != null) listener.onPurchaseFinished(result, null);
    } catch (RemoteException e) {
      result = new Result(HELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
      if (listener != null) listener.onPurchaseFinished(result, null);
    }
  }
  private void resolveSignInError() {

    Log.e(tag, "Has a error. Resolving !");

    if (mConnectionResult != null && mConnectionResult.hasResolution()) {
      Log.e(tag, "ERROR CODE: " + mConnectionResult.getErrorCode());
      try {
        mIntentInProgress = true;
        ((Activity) mContext)
            .startIntentSenderForResult(
                mConnectionResult.getResolution().getIntentSender(),
                RC_GOOGLE_SIGN_IN,
                null,
                0,
                0,
                0);
      } catch (SendIntentException e) {
        mIntentInProgress = false;
        mGoogleApiClient.connect();
      }
    } else {
      Log.d(tag, "mConnectionResult is null:" + (mConnectionResult == null));
      mGoogleApiClient.connect();
    }
  }
  /**
   * Initiate the UI flow for an in-app purchase. Call this method to initiate an in-app purchase,
   * which will involve bringing up the Google Play screen. The calling activity will be paused
   * while the user interacts with Google Play, and the result will be delivered via the activity's
   * {@link android.app.Activity#onActivityResult} method, at which point you must call this
   * object's {@link #handleActivityResult} method to continue the purchase flow. This method MUST
   * be called from the UI thread of the Activity.
   *
   * @param act The calling activity.
   * @param sku The sku of the item to purchase.
   * @param itemType indicates if it's a product or a subscription (ITEM_TYPE_INAPP or
   *     ITEM_TYPE_SUBS)
   * @param requestCode A request code (to differentiate from other responses -- as in {@link
   *     android.app.Activity#startActivityForResult}).
   * @param listener The listener to notify when the purchase process finishes
   * @param extraData Extra data (developer payload), which will be returned with the purchase data
   *     when the purchase completes. This extra data will be permanently bound to that purchase and
   *     will always be returned when the purchase is queried.
   */
  public void launchPurchaseFlow(
      Activity act,
      String sku,
      String itemType,
      int requestCode,
      OnIabPurchaseFinishedListener listener,
      String extraData) {
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    IabResult result;

    if (itemType.equals(ITEM_TYPE_SUBS) && !mSubscriptionsSupported) {
      IabResult r =
          new IabResult(IABHELPER_SUBSCRIPTIONS_NOT_AVAILABLE, "Subscriptions are not available.");
      if (listener != null) listener.onIabPurchaseFinished(r, null);
      return;
    }

    try {
      logDebug("Constructing buy intent for " + sku + ", item type: " + itemType);
      Bundle buyIntentBundle =
          mService.getBuyIntent(3, mContext.getPackageName(), sku, itemType, extraData);
      int response = getResponseCodeFromBundle(buyIntentBundle);
      if (response != BILLING_RESPONSE_RESULT_OK) {
        logError("Unable to buy item, Error response: " + getResponseDesc(response));

        result = new IabResult(response, "Unable to buy item");
        if (listener != null) listener.onIabPurchaseFinished(result, null);
        return;
      }

      PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
      logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
      mRequestCode = requestCode;
      mPurchaseListener = listener;
      mPurchasingItemType = itemType;
      act.startIntentSenderForResult(
          pendingIntent.getIntentSender(),
          requestCode,
          new Intent(),
          Integer.valueOf(0),
          Integer.valueOf(0),
          Integer.valueOf(0));
    } catch (SendIntentException e) {
      logError("SendIntentException while launching purchase flow for sku " + sku);
      e.printStackTrace();

      result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
      if (listener != null) listener.onIabPurchaseFinished(result, null);
    } catch (RemoteException e) {
      logError("RemoteException while launching purchase flow for sku " + sku);
      e.printStackTrace();

      result =
          new IabResult(
              IABHELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
      if (listener != null) listener.onIabPurchaseFinished(result, null);
    }
  }
 public void startResolutionForResult(Activity activity, int i)
     throws android.content.IntentSender.SendIntentException {
   if (!hasResolution()) {
     return;
   } else {
     activity.startIntentSenderForResult(mPendingIntent.getIntentSender(), i, null, 0, 0, 0);
     return;
   }
 }
  public void purchase(final String sku, final String transactionId) {
    Log.d("XXX", "Starting purchase for: " + sku);
    PaymentsCache pc = new PaymentsCache(context);
    Boolean isBlocked = pc.getConsumableFlag("block", sku);
    //		if(isBlocked){
    //			Log.d("XXX", "Is awaiting payment confirmation");
    //			error("Awaiting payment confirmation");
    //			return;
    //		}
    final String hash = transactionId;

    Bundle buyIntentBundle;
    try {
      buyIntentBundle =
          mService.getBuyIntent(
              3, context.getApplicationContext().getPackageName(), sku, "inapp", hash);
    } catch (RemoteException e) {
      //			Log.d("XXX", "Error: " + e.getMessage());
      error(e.getMessage());
      return;
    }
    Object rc = buyIntentBundle.get("RESPONSE_CODE");
    int responseCode = 0;
    if (rc == null) {
      responseCode = PaymentsManager.BILLING_RESPONSE_RESULT_OK;
    } else if (rc instanceof Integer) {
      responseCode = ((Integer) rc).intValue();
    } else if (rc instanceof Long) {
      responseCode = (int) ((Long) rc).longValue();
    }
    //		Log.d("XXX", "Buy intent response code: " + responseCode);
    if (responseCode == 1 || responseCode == 3 || responseCode == 4) {
      canceled();
      return;
    }

    PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
    pc.setConsumableValue("validation_hash", sku, hash);
    try {
      if (context == null) {
        //				Log.d("XXX", "No context!");
      }
      if (pendingIntent == null) {
        //				Log.d("XXX", "No pending intent");
      }
      //			Log.d("XXX", "Starting activity for purchase!");
      context.startIntentSenderForResult(
          pendingIntent.getIntentSender(),
          PaymentsManager.REQUEST_CODE_FOR_PURCHASE,
          new Intent(),
          Integer.valueOf(0),
          Integer.valueOf(0),
          Integer.valueOf(0));
    } catch (SendIntentException e) {
      error(e.getMessage());
    }
  }
Exemple #6
0
  /**
   * Initiate the UI flow for an in-app purchase. Call this method to initiate an in-app purchase,
   * which will involve bringing up the Google Play screen. The calling activity will be paused
   * while the user interacts with Google Play, and the result will be delivered via the activity's
   * {@link android.app.Activity#onActivityResult} method, at which point you must call this
   * object's {@link #handleActivityResult} method to continue the purchase flow. This method MUST
   * be called from the UI thread of the Activity.
   *
   * @param act The calling activity.
   * @param sku The sku of the item to purchase.
   * @param requestCode A request code (to differentiate from other responses -- as in {@link
   *     android.app.Activity#startActivityForResult}).
   * @param listener The listener to notify when the purchase process finishes
   * @param extraData Extra data (developer payload), which will be returned with the purchase data
   *     when the purchase completes. This extra data will be permanently bound to that purchase and
   *     will always be returned when the purchase is queried.
   */
  public void launchPurchaseFlow(
      Activity act,
      String sku,
      int requestCode,
      OnIabPurchaseFinishedListener listener,
      String extraData) {
    checkSetupDone("launchPurchaseFlow");
    flagStartAsync("launchPurchaseFlow");
    IabResult result;

    try {
      logDebug("Constructing buy intent for " + sku);
      Bundle buyIntentBundle =
          mService.getBuyIntent(
              BILLING_API_VERSION, mContext.getPackageName(), sku, ITEM_TYPE_INAPP, extraData);
      int response = getResponseCodeFromBundle(buyIntentBundle);
      if (response != BILLING_RESPONSE_RESULT_OK) {
        logError("Unable to buy item, Error response: " + getResponseDesc(response));

        result = new IabResult(response, "Unable to buy item", requestCode);
        if (listener != null) listener.onIabPurchaseFinished(result, null);
      } else {
        PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
        SYSLOG("MoSync Launching buy intent for " + sku + ". Request code: " + requestCode);
        mRequestCode = requestCode;
        mPurchaseListener = listener;
        act.startIntentSenderForResult(
            pendingIntent.getIntentSender(),
            requestCode,
            new Intent(Consts.METHOD_REQUEST_PURCHASE),
            0,
            0,
            0);
      }
    } catch (SendIntentException e) {
      logError("SendIntentException while launching purchase flow for sku " + sku);
      SYSLOG("MoSync SendIntentException while launching purchase flow for sku " + sku);
      e.printStackTrace();

      result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.", requestCode);
      if (listener != null) listener.onIabPurchaseFinished(result, null);
    } catch (RemoteException e) {
      logError("RemoteException while launching purchase flow for sku " + sku);
      SYSLOG("MoSync RemoteException while launching purchase flow for sku " + sku);
      e.printStackTrace();

      result =
          new IabResult(
              IABHELPER_REMOTE_EXCEPTION,
              "Remote exception while starting purchase flow",
              requestCode);
      if (listener != null) listener.onIabPurchaseFinished(result, null);
    }
  }
  @Override
  public void onConnectionFailed(ConnectionResult result) {
    Log.e(tag, "onConnectionFailed, ConnectionResult is null: " + (result == null));

    if (!mIntentInProgress && result.hasResolution()) {
      try {
        mIntentInProgress = true;
        ((Activity) mContext)
            .startIntentSenderForResult(
                result.getResolution().getIntentSender(), RC_GOOGLE_SIGN_IN, null, 0, 0, 0);
        Log.e(tag, "call startIntentSenderForResult");
      } catch (SendIntentException e) {
        e.printStackTrace();
        mIntentInProgress = false;
        mGoogleApiClient.connect();
      }
    }

    // if (!mIntentInProgress) {
    // if (mSignInClicked && result.hasResolution()) {
    // // The user has already clicked 'sign-in' so we attempt to
    // // resolve all
    // // errors until the user is signed in, or they cancel.
    // try {
    // result.startResolutionForResult(((Activity) context),
    // RC_SIGN_IN);
    // mIntentInProgress = true;
    // } catch (SendIntentException e) {
    // // The intent was canceled before it was sent. Return to the
    // // default
    // // state and attempt to connect to get an updated
    // // ConnectionResult.
    // mIntentInProgress = false;
    // mGoogleApiClient.connect();
    // }
    // }
    // }

    // if (!result.hasResolution()) {
    // Log.d(TAG, "ERROR CODE: " + result.getErrorCode());
    // GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
    // (Activity) context, 0).show();
    // return;
    // }
    // if (!mIntentInProgress) {
    // mConnectionResult = result;
    // if (mSignInClicked) {
    // Log.d(TAG, "REsolve in connect fail");
    // resolveSignInError();
    // }
    // }
  }