处理付费墙事件
本指南涵盖购买、恢复、产品选择以及付费墙渲染的事件处理。你还必须实现按钮处理(关闭付费墙、打开链接等)。详情请参阅处理按钮操作指南。
使用付费墙编辑工具配置的付费墙无需额外代码即可完成购买和恢复操作。但它们会触发一些事件供应用响应,包括按钮点击(关闭按钮、URL、产品选择等)以及付费墙上购买相关操作的通知。以下介绍如何响应这些事件。
本指南仅适用于新版付费墙编辑工具付费墙,需要 Adapty SDK v3.3.0 或更高版本。
想了解 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"
}
}
}正常情况下不应出现此类错误,如果你遇到了,请告知我们。