본문 바로가기
개발/C#

유니티 C# 인앱결제 영수증 검증하는 방법 간단 구현

by SPNK 2024. 2. 14.
반응형
  • 코드 작성
using UnityEngine;
using UnityEngine.Purchasing;

public class InAppPurchaseManager : MonoBehaviour
{
    // 이 스크립트를 인앱 결제를 초기화하는 개체에 부착하세요.

    private void Start()
    {
        // 여기에서 인앱 결제 시스템을 초기화하세요.

        // 예시: Unity IAP 초기화
        InitializeUnityIAP();
    }

    private void InitializeUnityIAP()
    {
        // Unity IAP를 초기화합니다.
        if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
        {
            var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

            // 여기에 인앱 제품 식별자를 추가하세요.
            builder.AddProduct("여러분의_제품_ID_여기에", ProductType.Consumable);

            UnityPurchasing.Initialize(OnInitialized, OnInitializeFailed);
        }
    }

    private void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        Debug.Log("Unity IAP가 성공적으로 초기화되었습니다");
    }

    private void OnInitializeFailed(InitializationFailureReason error)
    {
        Debug.LogError("Unity IAP 초기화 실패: " + error.ToString());
    }

    // 구매가 성공했을 때 호출됩니다.
    public void OnPurchaseComplete(Product product)
    {
        // 영수증을 검색하고 확인합니다.
        string receipt = product.receipt;

        // 영수증 확인을 수행합니다 (실제 영수증 확인 로직으로 교체하세요).
        bool isVerified = VerifyReceipt(receipt);

        if (isVerified)
        {
            // 구매가 유효하면 구입한 항목을 부여하거나 필요한 작업을 수행합니다.
            Debug.Log("구매가 성공적으로 확인되었습니다");
        }
        else
        {
            // 구매가 유효하지 않으면 해당 대로 처리하세요.
            Debug.LogError("구매 확인 실패");
        }
    }

    private bool VerifyReceipt(string receipt)
    {
        // 실제 영수증 확인 로직으로 교체하세요.
        // 영수증을 서버에 보내거나 인앱 결제 제공업체와 통신하거나 확인을 위한 타사 서비스를 사용할 수 있습니다.
        // 영수증 확인에 대한 권장 방법은 인앱 결제 제공업체의 문서를 참조하세요.

        // 데모 목적으로 항상 true를 반환합니다.
        return true;
    }
}
반응형

댓글