計費方式

您可以使用 Billing API 銷售數位商品和訂閱項目。C# 包裝函式提供基礎 Google Play 帳款服務程式庫的型別安全非同步介面。

命名空間: PlayPcSdkManaged.Billing

用戶端類別: BillingClient

建立用戶端

請務必使用工廠建立 BillingClient。這可確保系統自動註冊 Unity 安全的回呼。

using UnityEngine;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
// Required SDK Namespaces
using PlayPcSdkManaged.Billing;
using PlayPcSdkManaged.Unity;

public class BillingManager : MonoBehaviour
{
    private BillingClient _billingClient;

    public void SetupBilling()
    {
        try
        {
            // Creates the client with the required UnityBillingCallbacksHandler
            _billingClient = PlayPcSdkFactory.CreateBillingClient();
            Debug.Log("Billing Client created successfully.");
        }
        catch (Exception ex)
        {
            Debug.LogError($"Failed to create Billing Client: {ex.Message}");
        }
    }

    private void OnDestroy()
    {
        // Always dispose of the client to clean up native C++ resources
        _billingClient?.Dispose();
    }
}

查詢產品詳細資料

顯示待售商品前,您必須先從 Google Play 查詢價格、名稱和說明等詳細資料。

public async Task GetProductDetailsAsync()
{
    try
    {
        // Define the list of products you want to query
        var productList = new List
        {
            // Use ProductType.InApp for consumables/non-consumables
            new ProductId { Id = "gem_pack_100", ProductType = ProductType.InApp },
            // Use ProductType.Subs for subscriptions
            new ProductId { Id = "gold_subscription", ProductType = ProductType.InApp }
        };

        var queryParams = new QueryProductDetailsParams { ProductIds = productList };

        // Async call
        var result = await _billingClient.QueryProductDetailsAsync(queryParams);

        if (result.IsOk)
        {
            foreach (var product in result.Value.ProductDetailsList)
            {
                // The formatted price (e.g., "$0.99") is inside the ProductOffers list
                if (product.ProductOffers != null && product.ProductOffers.Count > 0)
                {
                    var price = product.ProductOffers[0].FormattedPrice;
                    var offerToken = product.ProductOffers[0].OfferToken;
                    Debug.Log($"Product: {product.Title} | Price: {price} | Token: {offerToken}");
                }
            }
        }
        else
        {
            Debug.LogError($"Query Failed: {result.Code} - {result.ErrorMessage}");
        }
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }
}

啟動購買流程

如要發起購買交易,請使用產品詳細資料步驟中的 OfferToken 呼叫 LaunchPurchaseFlowAsync

public async Task BuyItemAsync(string offerToken)
{
    try
    {
        var purchaseParams = new LaunchPurchaseFlowParams
        {
            OfferToken = offerToken,
            Quantity = 1,
            // Optional: Attach obfuscated IDs for fraud detection
            ObfuscatedAccountId = "user_12345_hash",
            ObfuscatedProfileId = "profile_abcde_hash"
        };

        var result = await _billingClient.LaunchPurchaseFlowAsync(purchaseParams);

        if (result.IsOk)
        {
            var purchase = result.Value.ProductPurchaseDetails;
            Debug.Log($"Purchase Successful! Order ID: {purchase.OrderId}");

            // IMPORTANT: You must now Acknowledge or Consume this purchase.
            // If you don't, Google Play will refund the user after a few days.
            if (!purchase.IsAcknowledged)
            {
                // Decide based on your game logic if it's consumable or permanent
                await HandlePurchaseAsync(purchase);
            }
        }
        else if (result.Code == BillingError.UserCanceled)
        {
            Debug.Log("User canceled the purchase flow.");
        }
        else
        {
            Debug.LogError($"Purchase Failed: {result.Code} - {result.ErrorMessage}");
        }
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }
}

查詢現有購買交易 (還原)

在遊戲啟動時呼叫 QueryPurchasesAsync,即可還原已擁有的項目 (例如在重新安裝遊戲後),或檢查待處理的交易。

這個範例說明如何將購買交易路徑導向 Acknowledge (永久性商品) 或 Consume (消耗性商品)。

public async Task CheckExistingPurchasesAsync()
{
    try
    {
        // Fetches all purchases owned by the user
        var result = await _billingClient.QueryPurchasesAsync();

        if (result.IsOk)
        {
            foreach (var purchase in result.Value.ProductPurchaseDetails)
            {
                Debug.Log($"User owns: {purchase.ProductId} | State: {purchase.PurchaseState}");

                // Process any purchase that hasn't been acknowledged yet
                if (purchase.PurchaseState == PurchaseState.Purchased && !purchase.IsAcknowledged)
                {
                    await HandlePurchaseAsync(purchase);
                }
            }
        }
        else
        {
            Debug.LogError($"Restore Failed: {result.Code} - {result.ErrorMessage}");
        }
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }
}

// Helper method to route purchases
private async Task HandlePurchaseAsync(ProductPurchaseDetails purchase)
{
    // Example logic: "gem_pack" is consumable, everything else is permanent
    if (purchase.ProductId.Contains("gem_pack"))
    {
        await ConsumeItemAsync(purchase.PurchaseToken);
    }
    else
    {
        await AcknowledgeItemAsync(purchase.PurchaseToken);
    }
}

確認購買交易

非消耗性商品 (例如「付費升級」或「關卡禮包」) 必須經過確認。這會向 Google Play 指出您已將商品授予使用者。

public async Task AcknowledgeItemAsync(string purchaseToken)
{
    try
    {
        var acknowledgeParams = new AcknowledgePurchaseParams
        {
            PurchaseToken = purchaseToken
        };

        var result = await _billingClient.AcknowledgePurchaseAsync(acknowledgeParams);

        if (result.IsOk)
        {
            Debug.Log("Purchase Acknowledged. Usage rights granted permanently.");
        }
        else
        {
            Debug.LogError($"Acknowledge Failed: {result.Code} - {result.ErrorMessage}");
        }
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }
}

消耗購買的產品

消耗性商品 (例如「100 顆寶石」或「生命藥水」) 必須消耗完畢,才能再次購買。

public async Task ConsumeItemAsync(string purchaseToken)
{
    try
    {
        var consumeParams = new ConsumePurchaseParams
        {
            PurchaseToken = purchaseToken
        };

        var result = await _billingClient.ConsumePurchaseAsync(consumeParams);

        if (result.IsOk)
        {
            Debug.Log("Item Consumed. User can buy it again.");
            // Add the gems/coins to the user's inventory here
        }
        else
        {
            Debug.LogError($"Consume Failed: {result.Code} - {result.ErrorMessage}");
        }
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }
}