93 lines
No EOL
3.7 KiB
C#
93 lines
No EOL
3.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Purchasing;
|
|
|
|
namespace ShiroginSDK.Runtime.Modules.Events
|
|
{
|
|
public static partial class ShiroginEvents
|
|
{
|
|
public static class IAP
|
|
{
|
|
/// <summary>
|
|
/// Fired when an in-app purchase is successfully completed.
|
|
/// Contains both logical and analytical data for tracking and rewards.
|
|
/// </summary>
|
|
[Serializable]
|
|
public class PurchaseCompletedEvent
|
|
{
|
|
public int Quantity;
|
|
public string TransactionId;
|
|
public string PaymentType;
|
|
public Product Product;
|
|
public DateTime Timestamp;
|
|
|
|
public PurchaseCompletedEvent(Product product, int quantity = 1)
|
|
{
|
|
Product = product;
|
|
Quantity = quantity;
|
|
|
|
TransactionId = product?.transactionID ?? Guid.NewGuid().ToString();
|
|
PaymentType = Application.platform == RuntimePlatform.Android
|
|
? "Google Play"
|
|
: "App Store";
|
|
|
|
Timestamp = DateTime.UtcNow;
|
|
}
|
|
|
|
// Computed properties
|
|
public string ItemId => Product?.definition?.id ?? "undefined";
|
|
public string ItemName => Product?.metadata?.localizedTitle ?? "undefined";
|
|
public string ItemCategory => Product?.definition?.type.ToString() ?? "unknown";
|
|
public double Price => (double)(Product?.metadata?.localizedPrice ?? 0);
|
|
public double Revenue => Price * Quantity;
|
|
public string Currency => Product?.metadata?.isoCurrencyCode ?? "USD";
|
|
|
|
public override string ToString()
|
|
{
|
|
return
|
|
$"[PurchaseCompletedEvent] {ItemName} ({ItemId}) | {Price:F2} {Currency} x{Quantity} | {PaymentType} | {TransactionId}";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fired when an in-app purchase fails.
|
|
/// Used for error handling, analytics, and failure reporting.
|
|
/// </summary>
|
|
[Serializable]
|
|
public class PurchaseFailedEvent
|
|
{
|
|
public string Error;
|
|
public string FailureReason;
|
|
public string PaymentType;
|
|
public Product Product;
|
|
public DateTime Timestamp;
|
|
|
|
public PurchaseFailedEvent(Product product, string error, string reason = "")
|
|
{
|
|
Product = product;
|
|
Error = error;
|
|
FailureReason = reason;
|
|
|
|
PaymentType = Application.platform == RuntimePlatform.Android
|
|
? "Google Play"
|
|
: "App Store";
|
|
|
|
Timestamp = DateTime.UtcNow;
|
|
}
|
|
|
|
// Computed properties
|
|
public string ItemId => Product?.definition?.id ?? "undefined";
|
|
public string ItemName => Product?.metadata?.localizedTitle ?? "undefined";
|
|
public string ItemCategory => Product?.definition?.type.ToString() ?? "unknown";
|
|
public double Price => (double)(Product?.metadata?.localizedPrice ?? 0);
|
|
public string Currency => Product?.metadata?.isoCurrencyCode ?? "USD";
|
|
|
|
public override string ToString()
|
|
{
|
|
return
|
|
$"[PurchaseFailedEvent] {ItemName} ({ItemId}) | {Price:F2} {Currency} | Error={Error} | Reason={FailureReason}";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |