The product receipt does not contain enough information, the ‘developerPayload‘ field is missing

Unity IAP subscription error resolution

First of all, I recommend that you no longer use the checkIfProductIsAvailableForSubscriptionManager detection method in the IAP Demo.

The second is the related processing source code of subscription

using UnityEngine.Purchasing;
using UnityEngine.Purchasing.Security;
using UnityEngine;
using System;
using System.Collections.Generic;
using Google.Play.Billing;
using IGooglePlayStoreExtensions = UnityEngine.Purchasing.IGooglePlayStoreExtensions;

/// <summary>
/// 
/// 订阅
/// 
/// 
/// 
/// </summary>
public class SubscriptionController : SingleTon<SubscriptionController>
{
    
    
    private IStoreController m_Controller = null;
    private IExtensionProvider m_Extensions = null;

    /// <summary>
    /// 是否初始化完成
    /// </summary>
    public bool inited {
    
     private set; get; } = false;

    /// <summary>
    /// 订阅字典
    /// </summary>
    public Dictionary<string, bool> subscriptionDict {
    
     private set; get; } = new Dictionary<string, bool>( );

    /// <summary>
    /// 订阅状态字典
    /// key : 订阅产品 ( 仅包含有订阅过的产品id )
    /// value : 当前订阅状态 
    /// </summary>
    private Action<Dictionary<string, bool>> updateSubscriptionCallback;

    /// <summary>
    /// 订阅拉取并更新后回调处理
    /// </summary>
    public event Action<Dictionary<string, bool>> updateSubscriptionHandler
    {
    
    
        add
        {
    
    
            if ( inited )
            {
    
    
                value?.Invoke( subscriptionDict );
            }
            updateSubscriptionCallback += value;
        }

        remove
        {
    
    
            updateSubscriptionCallback -= value;
        }
    }

    public void OnInitialized( IStoreController storeController, IExtensionProvider extensions )
    {
    
    
        Debug.Log( "订阅Manager初始化开始..." );
        m_Controller = storeController;
        m_Extensions = extensions;
        UpdateSubscription( );
        inited = true;
        Debug.Log( "订阅Manager初始化结束..." );
    }

    public void UpdateSubscription( )
    {
    
    
        //        Dictionary<string, string> introductory_info_dict = null;
        //#if UNITY_ANDROID
        //        //var m_GooglePlayStoreExtensions = m_Extensions.GetExtension<IGooglePlayStoreExtensions>( );
        //#else
        //        var m_AppleExtensions = m_Extensions.GetExtension<IAppleExtensions>( );
        //        introductory_info_dict = m_AppleExtensions.GetIntroductoryPriceDictionary( );
        //#endif
        foreach ( var item in m_Controller.products.all )
        {
    
    
            if ( item.availableToPurchase )
            {
    
    
                Debug.Log( string.Join( " - ",
                    new[]
                    {
    
    
                        item.metadata.localizedTitle,
                        item.metadata.localizedDescription,
                        item.metadata.isoCurrencyCode,
                        item.metadata.localizedPrice.ToString(),
                        item.metadata.localizedPriceString,
                        item.transactionID,
                        item.receipt
                    } ) );
                //#if !UNITY_ANDROID
                //                // Set all these products to be visible in the user's App Store according to Apple's Promotional IAP feature
                //                // https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StoreKitGuide/PromotingIn-AppPurchases/PromotingIn-AppPurchases.html
                //                m_AppleExtensions.SetStorePromotionVisibility(item, AppleStorePromotionVisibility.Show);
                //#endif
                // this is the usage of SubscriptionManager class
                if ( item.receipt != null )
                {
    
    
                    if ( item.definition.type == ProductType.Subscription )
                    {
    
    
                        try
                        {
    
    
                            SubscriptionManager p = new SubscriptionManager( item, null );
                            SubscriptionInfo info = p.getSubscriptionInfo( );

                            if ( info.isAutoRenewing( ) == Result.True || info.isSubscribed( ) == Result.True )
                            {
    
    
                                subscriptionDict[ info.getProductId( ) ] = true;
                            }
                            else
                            {
    
    
                                //非订阅状态判断是否到期
                                if ( info.isExpired( ) == Result.False )
                                {
    
    
                                    subscriptionDict[ info.getProductId( ) ] = true;
                                }
                                else
                                {
    
    
                                    subscriptionDict[ info.getProductId( ) ] = false;
                                }
                            }
                        }
                        catch ( Exception e1 ) //StoreSubscriptionInfoNotSupportedException es
                        {
    
    
                            //老版本数据解析失败 使用自己定义的方式来解析 通过autoRenewing来判读
                            Debug.Log( "This product is not available for SubscriptionManager class, only products that are purchase by 1.19+ SDK can use this class." );
                            try
                            {
    
    
                                GooglePurchaseData data = new GooglePurchaseData( item.receipt );
                                subscriptionDict[ data.json.productId ] = data.json.autoRenewing;
                            }
                            catch ( Exception e )
                            {
    
    
                                Debug.LogWarning( e );
                            }
                        }
                    }
                }
            }
        }
        updateSubscriptionCallback?.Invoke( subscriptionDict );
    }


    private bool checkIfProductIsAvailableForSubscriptionManager( string receipt )
    {
    
    
        var receipt_wrapper = ( Dictionary<string, object> ) MiniJson.JsonDecode( receipt );
        if ( !receipt_wrapper.ContainsKey( "Store" ) || !receipt_wrapper.ContainsKey( "Payload" ) )
        {
    
    
            Debug.Log( "The product receipt does not contain enough information" );
            return false;
        }
        var store = ( string ) receipt_wrapper[ "Store" ];
        var payload = ( string ) receipt_wrapper[ "Payload" ];

        if ( payload != null )
        {
    
    
            switch ( store )
            {
    
    
                case GooglePlay.Name:
                    {
    
    
                        var payload_wrapper = ( Dictionary<string, object> ) MiniJson.JsonDecode( payload );
                        if ( !payload_wrapper.ContainsKey( "json" ) )
                        {
    
    
                            Debug.Log( "The product receipt does not contain enough information, the 'json' field is missing" );
                            return false;
                        }
                        var original_json_payload_wrapper = ( Dictionary<string, object> ) MiniJson.JsonDecode( ( string ) payload_wrapper[ "json" ] );
                        if ( original_json_payload_wrapper == null || !original_json_payload_wrapper.ContainsKey( "developerPayload" ) )
                        {
    
    
                            Debug.Log( "The product receipt does not contain enough information, the 'developerPayload' field is missing" );
                            return false;
                        }
                        var developerPayloadJSON = ( string ) original_json_payload_wrapper[ "developerPayload" ];
                        var developerPayload_wrapper = ( Dictionary<string, object> ) MiniJson.JsonDecode( developerPayloadJSON );
                        if ( developerPayload_wrapper == null || !developerPayload_wrapper.ContainsKey( "is_free_trial" ) || !developerPayload_wrapper.ContainsKey( "has_introductory_price_trial" ) )
                        {
    
    
                            Debug.Log( "The product receipt does not contain enough information, the product is not purchased using 1.19 or later" );
                            return false;
                        }
                        return true;
                    }
                case AppleAppStore.Name:
                case AmazonApps.Name:
                case MacAppStore.Name:
                    {
    
    
                        return true;
                    }
                default:
                    {
    
    
                        return false;
                    }
            }
        }
        return false;
    }

}

Usage

Called after payment initialization is successful

Insert image description here

and updates after purchasing a subscription

Insert image description here

Subscription processing

The subscriptionDict dictionary returns the purchased subscription product name and subscription status dictionary.

True: Subscription | Auto-renewal on
False: Cancel subscription&& Close subscription&& Subscription expires
Insert image description here

Guess you like

Origin blog.csdn.net/qq_39162566/article/details/131563774