使用Flutter确认购买 [英] acknowledge a purchase using Flutter

查看:255
本文介绍了使用Flutter确认购买的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flutter的in_app_purchase插件已更新至版本0.3.0,并将Google Play库迁移到2.0.3.
根据Google Play库v2,所有购买必须在3天之内(对于测试订单为5分钟)确认,否则将自动取消.
in_app_purchase的最新提交中,称为acknowledgePurchase的方法被添加到BillingClient类中. 但是,当前记录的应用内购买方式是通过InAppPurchaseConnection.instance进行的,该方式未提供任何确认购买的方法.

Flutter's in_app_purchase plugin has been updated to version 0.3.0, migrating the Google Play Library to 2.0.3.
According to the Google Play Library v2, all purchases have to be acknowledged within 3 days (or 5 minutes in the case of test orders) or they will be canceled automatically.
In the latest commit of in_app_purchase, a method called acknowledgePurchase is added to the BillingClient class. However, the current documented way of making an in-app purchase is via InAppPurchaseConnection.instance which does not provide any method to acknowledge a purchase.

in_app_purchase的通用实现如下所示:

A generic implementation of in_app_purchase looks like this:

// Listening for new purchases
final Stream purchaseUpdates = InAppPurchaseConnection.instance.purchaseUpdatedStream;
StreamSubscription<List<PurchaseDetails>> _subscription = purchaseUpdates.listen((purchases) {
    _handlePurchaseUpdates(purchases);
});

// Product Details
ProductDetailsResponse _productDetailsResponse = await InAppPurchaseConnection.instance.queryProductDetails(this.productIDs.toSet());

// Past Purchases
QueryPurchaseDetailsResponse _purchaseDetailsResponse = await InAppPurchaseConnection.instance.queryPastPurchases();

// Making an nonConsumable purchase
PurchaseParam param = PurchaseParam(
    productDetails: productDetails,
    applicationUserName: null,
    sandboxTesting: sandboxTesting
);
InAppPurchaseConnection.instance.buyNonConsumable(purchaseParam: param);

完成购买并验证后,产品就会交付.但是,我们如何在聆听新购买的同时确认新购买的商品,而在获取过去购买的清单时又如何确认旧购买的商品(如果未确认)呢?

Once the purchase is made and verified, the product is delivered. But how can we acknowledge the new purchases while listening for new purchases and acknowledge the old ones (if they are not acknowledged) while getting the list of past purchases?

推荐答案

我也在寻找解决方案,看起来就像发行说明中描述的那样. https://pub.dev/packages/in_app_purchase#-changelog-tab-

I was looking for the solution as well, looks like it is described within release notes. https://pub.dev/packages/in_app_purchase#-changelog-tab-

例如,在获取过去的购买商品时:

For example, while fetching past purchases:


// at top class level (i.e. App wrapper)
InAppPurchaseConnection.enablePendingPurchases();

// inside class handling the purchases
final InAppPurchaseConnection _connection = InAppPurchaseConnection.instance;

// example use

final QueryPurchaseDetailsResponse purchaseResponse = await _connection.queryPastPurchases();

if (purchaseResponse.error != null) {
  // (handle error)
} else {
  // (purge local store)

  await Future.forEach<PurchaseDetails>(
      purchaseResponse.pastPurchases, (purchaseDetails) async {
          if (await _verifyPurchase(purchaseDetails)) {
            await //(insert purchase to local store);

            final pending = Platform.isIOS
                ? purchaseDetails.pendingCompletePurchase
                : !purchaseDetails.billingClientPurchase.isAcknowledged;

            if (pending) {
              await _connection.completePurchase(purchaseDetails);
            }
          }
        }
    );
}

以前,步骤_connection.competePurchase仅适用于iOS,现在Android也必需.

Formerly, step _connection.competePurchase was intended for iOS only, now it is required for Android as well.

purchaseDetails.pendingCompletePurchase为true.

purchaseDetails.billingClientPurchase.isAcknowledged对Android进行了附加检查,以确保在需要时执行completePurchase.

purchaseDetails.billingClientPurchase.isAcknowledged additional check for Android added to make sure the completePurchase is executed when needed.

0.3.0

0.3.0

"billing_client_wrappers"的其他更新: 更新PurchaseWrapper:添加developerPayload,purchaseState和isAcknowledged字段.

Other Updates to the "billing_client_wrappers": Updates to the PurchaseWrapper: Add developerPayload, purchaseState and isAcknowledged fields.

更新为"InAppPurchaseConnection": [重大更改]:InAppPurchaseConnection.completePurchase现在返回一个Future而不是Future.新的可选参数{String developerPayload}也已添加到API.在Android上,此API不会再引发异常,而是会确认购买.如果在3天内未在Android上完成购买,则该用户将获得退款.

Updates to the "InAppPurchaseConnection": [Breaking Change]: InAppPurchaseConnection.completePurchase now returns a Future instead of Future. A new optional parameter {String developerPayload} has also been added to the API. On Android, this API does not throw an exception anymore, it instead acknowledge the purchase. If a purchase is not completed within 3 days on Android, the user will be refunded.

[重大更改]:InAppPurchaseConnection.consumePurchase现在返回一个Future而不是Future.新的可选参数{String developerPayload}也已添加到API. 一个新的布尔字段未决CompletePurchase已添加到PurchaseDetails类中.可以用作指示是否在购买时调用InAppPurchaseConnection.completePurchase的指示符.

[Breaking Change]: InAppPurchaseConnection.consumePurchase now returns a Future instead of Future. A new optional parameter {String developerPayload} has also been added to the API. A new boolean field pendingCompletePurchase has been added to the PurchaseDetails class. Which can be used as an indicator of whether to call InAppPurchaseConnection.completePurchase on the purchase.

[重大更改]:在InAppPurchaseConnection中添加了enablePendingPurchases.在Android上初始化InAppPurchaseConnection时,应用程序必须调用此方法.有关更多信息,请参见enablePendingPurchases.

[Breaking Change]: Added enablePendingPurchases in InAppPurchaseConnection. The application has to call this method when initializing the InAppPurchaseConnection on Android. See enablePendingPurchases for more information.

这篇关于使用Flutter确认购买的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆