处理付费墙事件

本指南涵盖购买、恢复购买、产品选择及付费墙渲染相关的事件处理。您还必须实现按钮处理(关闭付费墙、打开链接等)。详情请参阅按钮操作处理指南

使用付费墙编辑工具配置的付费墙无需额外代码即可完成购买和恢复购买。但它们会生成一些您的应用可以响应的事件,包括按钮按下(关闭按钮、URL、产品选择等)以及付费墙上购买相关操作的通知。请阅读以下内容了解如何响应这些事件。

本指南仅适用于新版付费墙编辑工具付费墙,需要 Adapty SDK v3.3.0 或更高版本。如需了解在 Adapty SDK v2 中展示使用旧版付费墙编辑工具设计的付费墙,请参阅处理旧版付费墙编辑工具设计的付费墙事件

想了解 Adapty SDK 如何集成到移动应用中的真实示例?请查看我们的示例应用,其中展示了完整的配置过程,包括显示付费墙、完成购买以及其他基本功能。

处理事件

要控制或监控移动应用中付费墙屏幕上发生的流程,请实现 AdaptyPaywallsEventsListener 接口:

using UnityEngine;
using AdaptySDK;

public class PaywallEventsHandler : MonoBehaviour, AdaptyPaywallsEventsListener
{
    void Start()
    {
        Adapty.SetPaywallsEventsListener(this);
    }

    // Implement all required interface methods below
}

用户生成的事件

付费墙出现

在付费墙视图显示到屏幕上时触发。

在 iOS 上,当用户点击付费墙内的网页付费墙按钮且网页付费墙在应用内浏览器中打开时,同样会触发此事件。

public void PaywallViewDidAppear(AdaptyUIPaywallView view) { }

付费墙消失

在付费墙视图从屏幕上关闭时触发。

在 iOS 上,当从付费墙打开的网页付费墙在应用内浏览器中从屏幕消失时,同样会触发此事件。

public void PaywallViewDidDisappear(AdaptyUIPaywallView view) { }

产品选择

在用户或系统选择某个产品进行购买时触发。

public void PaywallViewDidSelectProduct(
    AdaptyUIPaywallView view, 
    string productId
) { }
事件示例(点击展开)
{
  "productId": "premium_monthly"
}

开始购买

在用户发起购买流程时触发。

public void PaywallViewDidStartPurchase(
    AdaptyUIPaywallView view, 
    AdaptyPaywallProduct product
) { }
事件示例(点击展开)
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "localizedPrice": "$9.99",
    "price": 9.99,
    "currencyCode": "USD"
  }
}

购买成功、取消或待处理

当购买成功、用户取消购买或购买处于待处理状态时,将触发此方法。用户取消和待处理付款(例如需要家长批准)会触发此方法,而非 PaywallViewDidFailPurchase

public void PaywallViewDidFinishPurchase(
    AdaptyUIPaywallView view, 
    AdaptyPaywallProduct product, 
    AdaptyPurchaseResult purchasedResult
) { }
事件示例(点击展开)
// Successful purchase
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "localizedPrice": "$9.99",
    "price": 9.99,
    "currencyCode": "USD"
  },
  "purchaseResult": {
    "type": "Success",
    "profile": {
      "accessLevels": {
        "premium": {
          "id": "premium",
          "isActive": true,
          "expiresAt": "2024-02-15T10:30:00Z"
        }
      }
    }
  }
}

// Cancelled purchase
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "localizedPrice": "$9.99",
    "price": 9.99,
    "currencyCode": "USD"
  },
  "purchaseResult": {
    "type": "UserCancelled"
  }
}

// Pending purchase
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "localizedPrice": "$9.99",
    "price": 9.99,
    "currencyCode": "USD"
  },
  "purchaseResult": {
    "type": "Pending"
  }
}

在这种情况下,我们建议关闭当前屏幕。

购买失败

当购买因错误而失败时,将触发此方法。包括 StoreKit/Google Play Billing 错误(付款限制、无效产品、网络故障)、交易验证失败以及系统错误。请注意,用户取消操作会触发 PaywallViewDidFinishPurchase 并返回已取消结果,待处理付款不会触发此方法。

public void PaywallViewDidFailPurchase(
    AdaptyUIPaywallView view, 
    AdaptyPaywallProduct product, 
    AdaptyError error
) { }
事件示例(点击展开)
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "localizedPrice": "$9.99",
    "price": 9.99,
    "currencyCode": "USD"
  },
  "error": {
    "code": "purchase_failed",
    "message": "Purchase failed due to insufficient funds",
    "details": {
      "underlyingError": "Insufficient funds in account"
    }
  }
}

开始恢复购买

在用户发起恢复购买流程时触发:

public void PaywallViewDidStartRestore(AdaptyUIPaywallView view) { }

恢复购买成功

在购买恢复成功时触发:

public void PaywallViewDidFinishRestore(
    AdaptyUIPaywallView view, 
    AdaptyProfile profile
) { }
事件示例(点击展开)
{
  "profile": {
    "accessLevels": {
      "premium": {
        "id": "premium",
        "isActive": true,
        "expiresAt": "2024-02-15T10:30:00Z"
      }
    },
    "subscriptions": [
      {
        "vendorProductId": "premium_monthly",
        "isActive": true,
        "expiresAt": "2024-02-15T10:30:00Z"
      }
    ]
  }
}

如果用户拥有所需的 accessLevel,我们建议关闭当前屏幕。请参阅订阅状态主题了解如何检查。

恢复购买失败

在购买恢复失败时触发:

public void PaywallViewDidFailRestore(
    AdaptyUIPaywallView view, 
    AdaptyError error
) { }
事件示例(点击展开)
{
  "error": {
    "code": "restore_failed",
    "message": "Purchase restoration failed",
    "details": {
      "underlyingError": "No previous purchases found"
    }
  }
}

网页支付导航完成

在尝试打开网页付费墙进行购买后(无论成功与否),将触发此方法:

public void PaywallViewDidFinishWebPaymentNavigation(
    AdaptyUIPaywallView view, 
    AdaptyPaywallProduct product, 
    AdaptyError error
) { }

参数:

  • product:已打开(或尝试打开)网页付费墙的产品
  • error:若网页付费墙成功打开则为 null,若失败则为 AdaptyError
事件示例(点击展开)
// Successful navigation
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "localizedPrice": "$9.99",
    "price": 9.99,
    "currencyCode": "USD"
  },
  "error": null
}

// Failed navigation
{
  "product": {
    "vendorProductId": "premium_monthly",
    "localizedTitle": "Premium Monthly",
    "localizedDescription": "Premium subscription for 1 month",
    "localizedPrice": "$9.99",
    "price": 9.99,
    "currencyCode": "USD"
  },
  "error": {
    "code": "wrong_param",
    "message": "Current method is not available for this product",
    "details": {
      "underlyingError": "Product not configured for web purchases"
    }
  }
}

数据获取与渲染

产品加载错误

当产品加载失败时触发,并提供 AdaptyError。如果您在初始化时未传入产品数组,AdaptyUI 将自行从服务器获取所需对象。此操作可能失败,AdaptyUI 将通过触发此方法来报告错误:

public void PaywallViewDidFailLoadingProducts(
    AdaptyUIPaywallView view, 
    AdaptyError error
) { }
事件示例(点击展开)
{
  "error": {
    "code": "products_loading_failed",
    "message": "Failed to load products from the server",
    "details": {
      "underlyingError": "Network timeout"
    }
  }
}

渲染错误

在界面渲染过程中发生错误时触发,并提供 AdaptyError

public void PaywallViewDidFailRendering(
    AdaptyUIPaywallView view, 
    AdaptyError error
) { }
事件示例(点击展开)
{
  "error": {
    "code": "rendering_failed",
    "message": "Failed to render paywall interface",
    "details": {
      "underlyingError": "Invalid paywall configuration"
    }
  }
}

正常情况下不应出现此类错误,如果您遇到此类情况,请及时告知我们。