[英]Best way to check if Google Play purchase has been refunded/voided?
[英]Check a Google Play Purchase from Java Server
我正在尝试使用Google Play API验证购买。 我正在购买buyId而我正试图通过调用Purchases.products来获得它的状态。
我一直在关注这个问题上的步骤( Android:inApp购买收据验证谷歌播放 ),虽然知道我需要什么真的很棒,但我无法得到一个简单的Java代码工作示例。
现在我有一个JWT令牌,我知道我需要用它来创建一个访问令牌(创建一个Credentials对象,也许?)才能调用api,但我一直无法完成它。
任何人都可以提供一个代码示例来了解如何操作吗?
我终于可以通过Google Play检查我的购买情况。 当我从我的移动应用程序购买东西时,Google Play会向我发送收据。 然后,我将此收据发送到我的服务器,我只需要在服务器端做两件事:
- 使用以前生成的服务帐户Json获取我的Google凭据
- 使用谷歌凭据和收据从谷歌播放获取购买信息。
我使用这两个函数来执行它:
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;
}
通过购买对象,我能够在一分钟内验证购买。
编辑:哦,不要在API包上寻找GoogleReceipt类。 它只是我创建的基本类来解析收据信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.