Check Google play purchase from Java Server
I'm trying to verify the purchase using the Google play API. I'm buying a Buyid and I'm trying to get its status by calling purchases.products
I've been paying attention to the steps of this problem (Android: inApp purchase receipt validation Google play). Although it's great to know what I need, I can't get a simple working example of Java code
Now I have a JWT token. I know I need it to create an access token (create a credentials object, maybe?) to call the API, but I have been unable to complete it
Can anyone provide a code example to see how?
resolvent:
I can finally check my purchase through Google play. When I buy something from my mobile app, Google play will send me a receipt. Then, I send this receipt to my server. I only need to do two things on the server:
– get my Google credentials using the previously generated service account Jason
– use Google credentials and receipts to get purchase information from Google play
I use these two functions to execute 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;
}
Through the purchase object, I can verify the purchase in one minute
Editor: Oh, don't look for the googlereceive class on the API package. It's just the basic class I created to parse the receipt information