基础类

类别描述
应用程序内产品在商店中注册的产品。
应用程序商店管理应用内购买的主要入口。
应用程序交易包含外部应用程序商店中的交易信息。
应用内购买后端与外部商店通信。

应用程序产品

InAppProduct 封装在 InAppStore 中注册并确认存在的外部商店中的产品。它有一个与外部商店产品标识符相匹配的标识符,它有一个从外部商店获取的价格,它还有一个产品类型。

产品类型可以是ConsumableUnlockable 。只要每次交易都由应用程序明确完成,可消耗产品就可以多次购买。可解锁类型只能购买一次。

InAppProduct 有 5 个可返回变量price,title,description identifierproductType 。除了 productType 之外,所有变量都返回QString

ProductType 是一个枚举类型,如果是消耗类型,则返回0 ;如果是解锁类型,则返回1

enum ProductType
{
   Consumable,
   Unlockable
};

请查看 Android 的派生类AndroidInAppProduct 和 iOS 的派生类IosInAppProduct

应用程序商店

管理应用内购买的主要入口。它是AndroidInAppProductIosInAppProduct 的基类。

InAppStore 用于跨平台管理应用程序中的应用内购买。根据编译器的不同,InAppStore 会使用Macros 检查哪些平台可用。

void InAppStore::setupBackend()
{
   #ifdef Q_OS_ANDROID
   d->backend = new AndroidInAppPurchaseBackend;
   #endif

   #ifdef Q_OS_IOS
   d->backend = new IosInAppPurchaseBackend;
   #endif

   d->backend->setStore(this);

   ...

初始化商店

进入演示中的商店页面后,InAppStore 会在 setupBackend() 函数中将所有信号连接到相关插槽。然后使用 registerProduct() 函数将外部商店中注册的每个产品的产品 ID 和产品类型注册到QHash registeredProducts 中。

注册产品是异步的,当从外部商店找到产品时,会在某个时刻产生 productRegistered() 信号。

void InAppStore::registerProduct(InAppProduct *product)
{
   d->registeredProducts[product->identifier()] = product;
   emit productRegistered(product);
}

完成购买

一旦商品在商店注册成功,用户就可以在应用程序商店页面上点击商品进行购买。QML 将调用 AndroidInAppProduct 或IosInAppProduct 中的 product.purchase() 函数,打开外部商店的购买流程。

购买完成后,无论成功与否,都会向InAppProductQmlType 发送 transactionRedy 信号,通知 QML 开始完成交易。

恢复购买

在演示中,可解锁的购买将保存在应用程序的存储空间中。清除存储后,用户将失去可解锁的购买记录,而且无法再次购买,因为根据外部商店的记录,该购买记录已被拥有。

您可以使用应用程序商店页面中的restore purchases 按钮来恢复解锁购买。恢复购买按钮会调用 restorePurchases() 函数,并检查外部商店中是否有已拥有的购买。它会发出 transactionRedy() 信号,以最终完成并恢复购买。

InAppStore 没有派生类。

应用程序交易

InAppTransaction 包含外部应用程序商店中的交易信息,通常是调用 InAppProduct::purchase() 的结果。当用户完成购买流程(确认购买,例如输入密码)后,包含产品的 InAppStore 实例将发出 InAppStore::transactionReady() 信号,其中包含有关交易的数据。

status() 函数提供交易是否成功的信息。如果交易成功,应用程序就应采取适当措施。执行必要操作后,应调用 finalize()。无论事务的状态如何,都应调用 finalize() 函数。

请查看派生类AndroidInAppTransaction (适用于 Android)和IosInAppTransaction (适用于 iOS)。

InAppPurchaseBackend

InAppPurchaseBackend 用于创建 和 的派生类。AndroidInAppPurchaseBackend IosInAppPurchaseBackend

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.