我正在尝试获取到期时间或订阅状态,以确保用户是否定期为我的项目付款。当我使用
查询时Purchase monthlySubscription = inv.getPurchase("itemName");
或
ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
返回以下数据
{
"packageName":"com.abcPackage",
"productId":"auto1week",
"purchaseTime":1453369299644,
"purchaseState":0,
"developerPayload":"PAY_LOAD",
"purchaseToken":"TOKEN",
"autoRenewing":true
}
问题是,购买时间在几周之后保持不变,应该在每次购买后更改。
我试过google Play开发者API
https://developers.google.com/android-publisher/#subscriptions
但我很难在我的Android设备上实现它。
如果有人可以一步一步地指导我在Android设备上获取此数据,我将不胜感激。
https://developers.google.com/android-publisher/api-ref/purchases/subscriptions
在这方面的任何帮助将受到高度赞赏。
答案 0 :(得分:1)
不确定这是否有帮助,但这是我的服务器端代码(在java中),它连接到开发人员API并返回订阅的到期时间。
我在Google Developer Console中创建了一个服务帐户,并按照一些有点钝的说明在src / resources / keys.json中创建了一个密钥文件。 APPLICATION_NAME是我的应用的套餐名称,PRODUCT_ID是来自Google PLAY开发者控制台的订阅ID。
很抱歉,这并不像你要求的那样'一步一步',但我也在服务器端进行验证,而不是在客户端进行验证。我想在客户端上你可以通过检查purchaseState == 0(1 =取消,2 =退款)和autoRenewing == true来进行某种软验证。如果他们取消,你可能会被困在那里,因为你仍然应该在订阅期间提供服务。
public static Long doSomeWork(String token){
log.debug("Google Validation: Doing some work:" + token);
try{
// Creating new Trusted Transport
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
// Getting Auth Creds
Credential cred = getAuthCredential();
// Building Android Publisher API call
AndroidPublisher ap = new AndroidPublisher.Builder(httpTransport, JSON_FACTORY, cred)
.setApplicationName(APPLICATION_NAME).build();
// Get Subscription
AndroidPublisher.Purchases.Subscriptions.Get get = ap.purchases().subscriptions().get(
APPLICATION_NAME,
PRODUCT_ID,
token);
SubscriptionPurchase subscription = get.execute();
log.debug(subscription.toPrettyString());
log.debug("DONE (not null)");
return subscription.getExpiryTimeMillis();
} catch(IOException ex) {
ex.printStackTrace();
} catch (GeneralSecurityException ex2) {
ex2.printStackTrace();
}
log.debug("DONE (failure) (0)");
return 0L;
}
private static Credential getAuthCredential(){
log.debug("getAuthCredential");
try{
//Read the credentials from the keys file. This file is obtained from the
// Google Developer Console (not the Play Developer Console
InputStream is = GoogleReceiptValidation.class.getClassLoader().getResourceAsStream("keys.json");
String str = IOUtils.toString(is);
is.close();
JSONObject obj = new JSONObject(str);
InputStream stream = new ByteArrayInputStream(obj.toString().getBytes());
//This is apparently "beta functionality".
GoogleCredential creds = GoogleCredential.fromStream(stream);
creds = creds.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));
return creds;
} catch (IOException ex){
ex.printStackTrace();
} catch (JSONException ex2){
ex2.printStackTrace();
}
log.debug("No Creds found - returning null");
return null;
}