Android : inApp purchase receipt validation (Part

2019-04-02 21:25发布

问题:

I am implement inApp purchase in my application and add product as "Manage Product". After purchase I want to do receipt validation.For that I configure in Developer console like

The Steps are

  1. Developer Console -> Settings -> API access
  2. Create Server account
  3. Google Developer console -> Add a credential in Service account and put it as JSON

  4. After adding I got a file from developer console it contains

    {
      "type": "service_account",
      "project_id": "api-xxxxx-xxx",
      "private_key_id": "xxxxxxxxxxxxxxxxxxxxxx",
      "private_key": "-----BEGIN PRIVATE KEY-----\ixxxxxxxxxxxxxxxx=\n-----END PRIVATE KEY-----\n",
       "client_email": "test-in-app-receipt-validation@api-xxxxxxx.iam.gserviceaccount.com",
       "client_id": "xxxxxxxxxxxxxxx",
       "auth_uri": "https://accounts.google.com/o/oauth2/auth",
       "token_uri": "https://accounts.google.com/o/oauth2/token",
       "auth_provider_x509_cert_url":   "https://www.googleapis.com/oauth2/v1/certs",
       "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-in-app-receipt-validation%40api-9129873511008807687-564452.iam.gserviceaccount.com"
       }
    

But I don't know how to do the next step. How to use this file Receipt validation

After purchasing am getting the reponse as

{ 
 "orderId":"12999763169054705758.1371079406387615", 
 "packageName":"com.example.app",
 "productId":"exampleSku",
 "purchaseTime":1345678900000,
 "purchaseState":0,
 "developerPayload":"bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ",
 "purchaseToken":"rojeslcdyyiapnqcynkjyyjh"
}

Can anyone please help me how to handle the next step for Receipt validation for the make use of downloaded file that contain JSON value

回答1:

Following up on your original question now that I know you're using Java for your server side code, you will want to use the Google API Client Library for Java. I'm not comfortable with Java myself but the documentation is easy to follow.

First of, you need to authenticate, take a look at the example code Google provides. You currently have a JWT (JSON Web Token) but you can keep to the example by instead downloading the p12 certificate file from the same place on the Google API Dashboard.

Your authentication code should look something like this:

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance()

GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
  .setJsonFactory(jsonFactory)
  .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
  .setServiceAccountScopes(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
  .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
  .build();

Also don't forget to set your SERVICE_ACCOUNT_EMAIL and store the key.p12 file somewhere accessible to your server.

If you're interested, this is the docs AndroidPublisherScopes.

As I mentioned, my knowledge of Java is limited so please use your disregard any mistakes I have made.

Google have made some code samples available with downloads and links to GitHub repos. I suggest checking those out.

From here you can use the AndroidPublisher.Purchases.Products.Get method to retrieve the details of a purchase. The docs list the signature as following:

AndroidPublisher.Purchases.Products.Get(java.lang.String packageName, java.lang.String productId, java.lang.String token)

For subscriptions use AndroidPublisher.Purchases.Subscriptions.Get.

If you need more help, consider asking a question with the java and google-api-java-client tags.