Google IAP 获取订阅详细信息并存储在数据库中

Google IAP get subscription details and store in DB

我有一个应用可以接收用户的订阅。我已经实施了接收付款的方法,并且可以完成交易。在我的数据库中存储所有详细信息时,我可以获得购买 ID、购买日期和有效期(1 年订阅返回 P1Y)等详细信息,但我还想获取订阅结束日期是否可能?以及如何检查用户是否取消订阅或续订?

收入猫:

而不是使用 in_app_purchase I started using purchases_flutter (RevenueCat)

您可以参考他们的 documentation 了解更多信息。

I currently use this method for in-app subscriptions.

Google Api:

googleapis and googleapis_auth

的帮助下,我还设法使用 google API 获取订阅详细信息

IAP 助手 class

按照此 documentation 完成初始设置

Google API 凭证初始化

  final _credentials = ServiceAccountCredentials.fromJson(r'''
        {
          "private_key_id": "keyid",
          "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADAN<private key>Sf\nbB9OjCOGt7ybJmDkMBe2U5Tq\n-----END PRIVATE KEY-----\n",
          "client_email": "mail",
          "client_id": "clientid",
          "type": "service_account"
        }
        ''');
  static const _scopes = [AndroidPublisherApi.androidpublisherScope];

获取订阅详情的函数

 Future getSubData({@required String token , @required String productId})async{
    SubscriptionPurchase res;
    final httpClient = await clientViaServiceAccount(_credentials, _scopes);
    try {
      final pubApi = AndroidPublisherApi(httpClient);
      res = await pubApi.purchases.subscriptions.get('com.yourcompany.package', productId, token);
    } finally {
      httpClient.close();
    }
    return res;
  }

交付产品

_deliverProduct({@required PurchaseDetails purchaseDetails})async{
     await iap.completePurchase(purchaseDetails);
     final firestore.DocumentReference userDocReference = firebase.doc("Users/$_id/userdata/data");
     final firestore.CollectionReference historyDocReference = firebase.collection("Users/$_id/history/");
     final SubscriptionPurchase  apiRes = await getSubData(token:purchaseDetails.billingClientPurchase.purchaseToken,productId:purchaseDetails.productID);
     var data = {
       "TXN ID": purchaseDetails.billingClientPurchase.purchaseToken,
       "Order ID":purchaseDetails.billingClientPurchase.orderId,
       "Product ID":purchaseDetails.productID,
       "TXN Date": firestore.Timestamp.fromMillisecondsSinceEpoch(int.parse(purchaseDetails.transactionDate)),
       "Subscribed":true,
       "Start Date":firestore.Timestamp.fromMillisecondsSinceEpoch(int.parse(apiRes.startTimeMillis)),
       "Expiry Date": firestore.Timestamp.fromMillisecondsSinceEpoch(int.parse(apiRes.expiryTimeMillis)),
       "Payment Status": apiRes.paymentState,
       "isFreeTrail": (apiRes.paymentState==1)?false:true,
       "acknowledgementState":apiRes.acknowledgementState
     };
    await userDocReference.set({"Subscription":data,"Sub_Raw":apiRes.toJson()},firestore.SetOptions(merge: true)); //set to main user
    await historyDocReference.doc('${purchaseDetails.billingClientPurchase.purchaseToken}').set(data); //put in purchase history
  }

现在您可以从您的 firstore BD 中检查用户是否已订阅

This method may provide temporary FIX (Google PlayStore only) but I do not recommend this method as it contains a lot of security issues.

注意:Google每天只提供200K次API调用,避免反复调用API查询订阅。

注意:Google API 方法仅适用于 in_app_purchase: ^0.5.2

注意:Google API 方法仅适用于 Android 而不适用于 IOS.

其他替补

你可以在 node.js/cloud firestore 中编写自己的服务器端代码,你也可以实现同样的效果。

参考:Cook book in_app_purchase

免责声明

我没有收到来自 revenuecat 的任何 incentive/payment 因为在此答案中提及他们的产品。