[英]How to check is transaction free trial in android subscription?
Is it possible to know is subscription was purchased as free trial? 是否有可能知道订阅是作为免费试用购买的? For now I can't find a way how to do it on server/device side.
目前我找不到如何在服务器/设备端执行此操作的方法。
Does anybody has suggestions how to do it? 有人有建议怎么做吗?
2017年6月9日,在页面https://developers.google.com/android-publisher/api-ref/purchases/subscriptions上显示了有关paymentState
新值的信息 - 2
表示“免费试用”。
[--------- NOT SECURE ---------] [---------不安全---------]
After spending few days i will share my solution(on client side). 花了几天后,我将分享我的解决方案(在客户端)。
First you need to allow API access in Google play developer console and create service account
( docs ). 首先,您需要在Google Play开发者控制台中允许API访问并创建
service account
( docs )。
You will get account id similar to this : your-service@api-621**********846-16504.iam.gserviceaccount.com
您将获得与此类
your-service@api-621**********846-16504.iam.gserviceaccount.com
帐户ID: your-service@api-621**********846-16504.iam.gserviceaccount.com
IMPORTANT : add "View financial data" permission in Users & permissions section in Google developer account. 重要提示 :在Google开发者帐户的“用户和权限”部分添加“查看财务数据”权限。
Also download p12 key and put it in your assets folder. 还要下载p12键并将其放入资源文件夹中。
Code to get subscription details(you need to know the subscription token): 获取订阅详细信息的代码(您需要知道订阅令牌):
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<String>();
scopes.add(AndroidPublisherScopes.ANDROIDPUBLISHER);
HttpRequestInitializer credential = null;
try {
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(context.getAssets().open("key.p12"), "notasecret".toCharArray());
PrivateKey pk = (PrivateKey) keystore.getKey("privatekey", "notasecret".toCharArray());
credential = new GoogleCredential.Builder().setJsonFactory(jsonFactory)
.setTransport(httpTransport)
.setServiceAccountId("your-service@api-621**********846-16504.iam.gserviceaccount.com")
.setServiceAccountPrivateKey(pk)
.setServiceAccountScopes(scopes)
.build();
} catch (GeneralSecurityException e) {
} catch (IOException e) {
}
AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential).build();
AndroidPublisher.Purchases purchases = publisher.purchases();
try {
AndroidPublisher.Purchases.Subscriptions.Get get = purchases.subscriptions()
.get(context.getPackageName(), Constants.SUBSCRIPTION_ID, subscription_token);
final SubscriptionPurchase purchase = get.execute();
if (purchase != null && purchase.getPaymentState() != null) {
int paymentState = purchase.getPaymentState();
if (paymentState == 0) {
// Subscriber with payment pending
} else if (paymentState == 1) {
// Subscriber in good standing (paid)
} else if (paymentState == 2) {
// Subscriber in free trial
}
}
} catch (IOException e) { }
get.execute() is network request so you need to run this code on separate thread(i used RxJava) get.execute()是网络请求所以你需要在单独的线程上运行此代码(我使用RxJava)
You need to use AndroidPublisher and Play Services Auth libraries 您需要使用AndroidPublisher和Play Services Auth库
implementation 'com.google.apis:google-api-services-androidpublisher:v3-rev22-1.25.0'
implementation 'com.google.android.gms:play-services-auth:15.0.1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.