4

I'm trying to validate a purchase with the Google play API. I'm getting a purchaseId and I'm trying to get its status with a call to Purchases.products.

I've been following the steps on this question (Android : inApp purchase receipt validation google play) and while it's really great to get an idea of what I need, I'm unable to get an easy example of working Java code.

Right now I have a JWT token, and I know I need to use it to create an access token (creating a Credentials object, maybe?) to be able to call the api, but I've been unable to complete it.

Can anybody provide me a code example to know how to do it?

Community
  • 1
  • 1
Robles
  • 123
  • 1
  • 10
  • How about this answer?:http://stackoverflow.com/questions/11115381/unable-to-get-the-subscription-information-from-google-play-android-developer-ap/29728826#29728826 – Tomcat Jun 20 '16 at 10:58

1 Answers1

4

I finally could check my purchases with Google play. When I buy something from my mobile App, Google Play send me a receipt. Then, I send this receipt to my server, and I just had to do two things server side:

-Use the previously generated service account Json to get my google credentials

-Use the google credentials and the receipt to get the purchase info from google play.

And I used these two functions to perform it:

private static GoogleCredential getGoogleCredential() throws IOException {
    List<String> scopes = new ArrayList<String>();
    scopes.add(AndroidPublisherScopes.ANDROIDPUBLISHER);

    ClassLoader classLoader = MY_CLASS.class.getClassLoader();
    GoogleCredential credential = GoogleCredential.fromStream(classLoader.getResourceAsStream(GOOGLE_KEY_FILE_PATH))
            .createScoped(scopes);
    return credential;
}

private static ProductPurchase getPurchase(GoogleReceipt receipt, GoogleCredential credential)
        throws GeneralSecurityException, IOException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential)
            .setApplicationName(YOUR_APPLICATION_NAME).build();
    AndroidPublisher.Purchases purchases = publisher.purchases();

    final Get request = purchases.products().get(receipt.getPackageName(), receipt.getProductId(),
            receipt.getPurchaseToken());
    final ProductPurchase purchase = request.execute();
    return purchase;
}

With the purchase object I was able to validate the buy in just a minute.

Edit: Oh, dont look for GoogleReceipt class on the API packages. It's just a basic class I created to parse the receipt info.

Robles
  • 123
  • 1
  • 10
  • When I implement your solution, the server(google appengine) throws the below exception. Do you know why? Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient$Builder.setBatchPath(Ljava/lang/String;)Lcom/google/api/client/googleapis/services/AbstractGoogleClient$Builder; – tolgatanriverdi Oct 18 '17 at 12:11