modify scripts
This commit is contained in:
@ -25,6 +25,7 @@ namespace WordsToolkit.Scripts.Services.IAP
|
||||
private IExtensionProvider extensionProvider;
|
||||
|
||||
public static event Action<string> OnSuccessfulPurchase;
|
||||
public static event Action<(string,string)> OnFailedPurchase;
|
||||
public static event Action<bool, List<string>> OnRestorePurchasesFinished;
|
||||
|
||||
public void InitializePurchasing(IEnumerable<(string productId, ProductTypeWrapper.ProductType productType)> products)
|
||||
@ -111,20 +112,24 @@ namespace WordsToolkit.Scripts.Services.IAP
|
||||
{
|
||||
Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
|
||||
storeController.InitiatePurchase(product);
|
||||
OnFailedPurchase?.Invoke((productId, "Product not found or not available for purchase.")); // debug only
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase {productId}");
|
||||
OnFailedPurchase?.Invoke((productId, "InvalidProductID: product not found or not available for purchase."));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("BuyProductID FAIL. Not initialized.");
|
||||
Debug.Log("IAPInitFailed: BuyProductID FAIL. Not initialized.");
|
||||
OnFailedPurchase?.Invoke((productId, "IAPInitFailed: Not initialized."));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.Log("BuyProductID: FAIL. Exception during purchase. " + e);
|
||||
OnFailedPurchase?.Invoke((productId, "Exception during purchase: " + e.Message));
|
||||
}
|
||||
}
|
||||
|
||||
@ -159,11 +164,13 @@ namespace WordsToolkit.Scripts.Services.IAP
|
||||
public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
|
||||
{
|
||||
Debug.Log("OnPurchaseFailed: FAIL. Product: " + product.definition.id + " PurchaseFailureDescription: " + failureDescription);
|
||||
OnFailedPurchase?.Invoke((product.definition.id, failureDescription.message));
|
||||
}
|
||||
|
||||
public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
|
||||
{
|
||||
Debug.Log($"OnPurchaseFailed: FAIL. Product: '{i.definition.id}', PurchaseFailureReason: {p}");
|
||||
OnFailedPurchase?.Invoke((i.definition.id, p.ToString()));
|
||||
}
|
||||
|
||||
public void OnInitializeFailed(InitializationFailureReason reason)
|
||||
|
||||
57
Assets/WordConnectGameToolkit/Scripts/Services/IAP/IAPErr.cs
Normal file
57
Assets/WordConnectGameToolkit/Scripts/Services/IAP/IAPErr.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
|
||||
namespace WordsToolkit.Scripts.Services.IAP
|
||||
{
|
||||
/// <summary>
|
||||
/// IAP 典型错误类型枚举
|
||||
/// </summary>
|
||||
public enum IAPErrorType
|
||||
{
|
||||
None,
|
||||
UserCancelled,
|
||||
PaymentDeclined,
|
||||
InsufficientFunds,
|
||||
ProductUnavailable,
|
||||
DuplicateTransaction,
|
||||
NetworkError,
|
||||
NetworkUnavailable,
|
||||
IAPInitFailed,
|
||||
InvalidProductID,
|
||||
Unknown
|
||||
}
|
||||
|
||||
public static class IAPErrorHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据错误信息字符串,提取并映射到 IAPErrorType 枚举
|
||||
/// </summary>
|
||||
public static IAPErrorType ParseError(string errorMessage)
|
||||
{
|
||||
if (string.IsNullOrEmpty(errorMessage))
|
||||
return IAPErrorType.Unknown;
|
||||
|
||||
errorMessage = errorMessage.ToLowerInvariant();
|
||||
|
||||
if (errorMessage.Contains("usercancelled") || errorMessage.Contains("user cancelled"))
|
||||
return IAPErrorType.UserCancelled;
|
||||
if (errorMessage.Contains("paymentdeclined") || errorMessage.Contains("payment declined"))
|
||||
return IAPErrorType.PaymentDeclined;
|
||||
if (errorMessage.Contains("insufficientfunds") || errorMessage.Contains("insufficient funds"))
|
||||
return IAPErrorType.InsufficientFunds;
|
||||
if (errorMessage.Contains("productunavailable") || errorMessage.Contains("product unavailable"))
|
||||
return IAPErrorType.ProductUnavailable;
|
||||
if (errorMessage.Contains("duplicatetransaction") || errorMessage.Contains("duplicate transaction"))
|
||||
return IAPErrorType.DuplicateTransaction;
|
||||
if (errorMessage.Contains("networkerror") || errorMessage.Contains("network error"))
|
||||
return IAPErrorType.NetworkError;
|
||||
if (errorMessage.Contains("networkunavailable") || errorMessage.Contains("network unavailable"))
|
||||
return IAPErrorType.NetworkUnavailable;
|
||||
if (errorMessage.Contains("InvalidProductID".ToLowerInvariant()) )
|
||||
return IAPErrorType.InvalidProductID;
|
||||
if (errorMessage.Contains("IAPInitFailed".ToLowerInvariant()))
|
||||
return IAPErrorType.IAPInitFailed;
|
||||
|
||||
return IAPErrorType.Unknown;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 03eff658508844cfd91bab5657295e76
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -50,6 +50,12 @@ namespace WordsToolkit.Scripts.Services.IAP
|
||||
IAPController.OnSuccessfulPurchase += purchaseHandler;
|
||||
#endif
|
||||
}
|
||||
public void SubscribeToPurchaseFailedEvent(Action<(string, string)> purchaseHandler)
|
||||
{
|
||||
#if UNITY_PURCHASING
|
||||
IAPController.OnFailedPurchase += purchaseHandler;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void UnsubscribeFromPurchaseEvent(Action<string> purchaseHandler)
|
||||
{
|
||||
|
||||
@ -13,6 +13,8 @@ namespace WordsToolkit.Scripts.Services.IAP
|
||||
bool IsProductPurchased(string productId);
|
||||
void RestorePurchases(Action<bool, List<string>> action);
|
||||
void SubscribeToPurchaseEvent(Action<string> purchaseHandler);
|
||||
|
||||
void SubscribeToPurchaseFailedEvent(Action<(string, string)> purchaseHandler);
|
||||
void UnsubscribeFromPurchaseEvent(Action<string> purchaseHandler);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user