Unity IAP接入google支付文档(2022年最新)

Unity导入

我使用的版本是Unity2020.3.30f1c1。
Unity的操作主要是打开Services中的In-APP Purchasing。
并且在Package里面导入In App Purchasing。
导入成功后能在编辑器里看到Services-In-APP Purchasing.
在这里插入图片描述

官方的参考链接:
https://docs.unity3d.com/2020.3/Documentation/Manual/UnityIAPGoogleConfiguration.html

注意:切换到中文可能看不到图片,可以使用英文语言,然后翻译。关于为 Google Play 商店配置的步骤,谷歌商店已经更新,和最新版的图片不符合。

google play console

创建应用程序

关于整个在goole play console创建应用就不赘述,很繁琐。为了防止google账号被封,要使用跳板机登录。
然后遇到很烦的一点就是里面字是繁体的,弄了好久最后在个人资讯,里面可以增加简体中文语言。如果不会操作,可以在Google账户中搜索。

创建商品

前提:建立好应用后,上传了aab包,并且确保添加了BILLING权限。

 <uses-permission android:name="com.android.vending.BILLING" /> 

我这里游戏中一般的商品应该为消耗性商品。
在这里插入图片描述

创建商品时候价格要选择定价模板,在外面能看到所有应用的地方,点击设置,定价模板进行设置。

内部测试

如果您针对付费应用进行开放式测试或封闭式测试,测试人员仍需购买应用。如果您针对付费应用进行内部测试,测试人员可以免费安装您的应用。

你需要将测试用户的google账号添加到内部测试。然后将测试链接发给测试用户,去google商店下载应用。(注意更新应用有延迟)然后进行测试。每次都要上传aab包,然后再去商店下载测试,确实很麻烦。

在这里插入图片描述
PS:后面发现,上传abb包后也可以直接从googlePlay Console后台下载,位置在内部测试-发布版本-查看发布版本详情-然后点APP bundle那一行右边的右箭头,就出现了下载界面:
在这里插入图片描述

测试支付的时候,需要将你的测试账号添加到许可测试中。
然后账号支付时候,会出现“测试卡,一律批注",支付时候不需要真正付款。

编写代码

使用无代码 IAP 按钮为用户提供购买商品的方式,这种方式很方便,但是我在这里不使用这种方式,因为代码不够灵活。

主要分为三个步骤:

  1. 初始化IAP,把Google的商品Id全部添加进来
    public void InitUnityPurchase()
    {
        if (IsInitialized())  return; 
        // 标准采购模块;
        StandardPurchasingModule module = StandardPurchasingModule.Instance(); 
        // 配置模式;
        ConfigurationBuilder builder = ConfigurationBuilder.Instance(module); 
        builder.AddProduct("com.manhuang.tk.1", ProductType.Consumable);
        builder.AddProduct("com.manhuang.tk.2", ProductType.Consumable);
        builder.AddProduct("com.manhuang.tk.3", ProductType.Consumable);
        builder.AddProduct("com.manhuang.tk.4", ProductType.Consumable);
        builder.AddProduct("com.manhuang.tk.5", ProductType.Consumable);
        //初始化;
        UnityPurchasing.Initialize(this, builder);
    }

初始化成功:

  // IAP初始化成功回调函数;
    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        IAPDebugLog("OnInitialized Succ !");

        m_StoreController = controller;
        m_StoreExtensionProvider = extensions;

        // 这里可以获取您在AppStore和Google Play 上配置的商品;
        ProductCollection products = m_StoreController.products;
        Product[] all = products.all;
        for(int i = 0; i < all.Length; i++)
        {
            IAPDebugLog(all[i].metadata.localizedTitle  + "|" + all[i].metadata.localizedPriceString + "|" + all[i].metadata.localizedDescription + "|" + all[i].metadata.isoCurrencyCode);
        }

        #if UNITY_IOS
        // m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
        #endif
    }

如果网络VPN不行的话,或者手机上没有google服务的话,不会回调到这里,后面支付调不起来。

  1. 根据ID购买商品:
public void BuyProductByID(string productId)  
    {  
        if (IsInitialized())  
        {  
            if (m_PurchaseInProgress == true) return;

            Product product = m_StoreController.products.WithID(productId);  
            if (product != null && product.availableToPurchase)  
            {  
                IAPDebugLog(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));  
                m_StoreController.InitiatePurchase(product);  
                m_PurchaseInProgress = true;
            }  
            else  
            {  
                IAPDebugLog("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");  
            }  
        }  
        else  
        {  
            IAPDebugLog("BuyProductID FAIL. Not initialized.");
            Init();
        }  
    }
  1. 在购买成功后发货:
    注意:可以选择客户端发货,或者服务器确认发货。
    客户端处理的话,这个方法返回值立即返回PurchaseProcessingResult.Complete。
 // 支付成功处理函数;
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e){
	
}

如果是要等服务器发货的话,先返回PurchaseProcessingResult.Pending。等确认服务器发货后返回确认购买产品成功。

	// 确认购买产品成功;
	public void DoConfirmPendingPurchaseByID(string productId)
	{
		Product product = m_StoreController.products.WithID(productId);  
		if (product != null && product.availableToPurchase) 
		{
			if (m_PurchaseInProgress)
			{
				m_StoreController.ConfirmPendingPurchase(product);
				m_PurchaseInProgress = false;
			}	
		}
	}

完整代码参考:

/**
 * Copyright (C) 2021 Manhuang
 * User: lhc
 * Time: 2022年03月01日 星期二 17:38
 * Description: 
 */

using System;
using TK;
using UGF.Singleton;
using UGF.UI;
using UnityEngine;
using UnityEngine.Purchasing;

public class IAPTools  :MonoSingleton<IAPTools>, IStoreListener 
{
    private static IStoreController m_StoreController; // 存储商品信息;
    private static IExtensionProvider m_StoreExtensionProvider; // IAP扩展工具;
    private bool m_PurchaseInProgress = false; // 是否处于付费中;

    private const string C_ITEM_0 = "com.xxx.xxx.productname"; // 注意这里统一小写(IOS和Google Paly 公用);

    public void Init()
    {
        if(m_StoreController == null && m_StoreExtensionProvider == null)
            InitUnityPurchase();
    }

    private bool IsInitialized()  
    {  
        return m_StoreController != null && m_StoreExtensionProvider != null;  
    }  
        
    // 初始化IAP;
    public void InitUnityPurchase()
    {
        if (IsInitialized())  return; 
        // 标准采购模块;
        StandardPurchasingModule module = StandardPurchasingModule.Instance(); 
        // 配置模式;
        ConfigurationBuilder builder = ConfigurationBuilder.Instance(module); 
        builder.AddProduct("com.manhuang.tk.1", ProductType.Consumable);
        builder.AddProduct("com.manhuang.tk.2", ProductType.Consumable);
        builder.AddProduct("com.manhuang.tk.3", ProductType.Consumable);
        builder.AddProduct("com.manhuang.tk.4", ProductType.Consumable);
        builder.AddProduct("com.manhuang.tk.5", ProductType.Consumable);
        //初始化;
        UnityPurchasing.Initialize(this, builder);
    }

    #region Public Func
    // 根据ID给购买商品;
    public void BuyProductByID(string productId)  
    {  
        if (IsInitialized())  
        {  
            if (m_PurchaseInProgress == true) return;

            Product product = m_StoreController.products.WithID(productId);  
            if (product != null && product.availableToPurchase)  
            {  
                IAPDebugLog(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));  
                m_StoreController.InitiatePurchase(product);  
                m_PurchaseInProgress = true;
            }  
            else  
            {  
                IAPDebugLog("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");  
            }  
        }  
        else  
        {  
            IAPDebugLog("BuyProductID FAIL. Not initialized.");
            Init();
        }  
    }

    // 确认购买产品成功;
    public void DoConfirmPendingPurchaseByID(string productId)
    {
        Product product = m_StoreController.products.WithID(productId);  
        if (product != null && product.availableToPurchase) 
        {
            if (m_PurchaseInProgress)
            {
                m_StoreController.ConfirmPendingPurchase(product);
                m_PurchaseInProgress = false;
            }    
        }
    }

    // 恢复购买;
    public void RestorePurchases()  
    {  
        if (!IsInitialized())  
        {  
            IAPDebugLog("RestorePurchases FAIL. Not initialized.");  
            return;  
        }  
        if (Application.platform == RuntimePlatform.IPhonePlayer ||   
            Application.platform == RuntimePlatform.OSXPlayer)  
        {  
            IAPDebugLog("RestorePurchases started ...");  
            var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();  
            apple.RestoreTransactions((result) => {  
                // 返回一个bool值,如果成功,则会多次调用支付回调,然后根据支付回调中的参数得到商品id,最后做处理(ProcessPurchase); 
                IAPDebugLog("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");  
            });  
        }  
        else  
        {  
            IAPDebugLog("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);  
        }  
    } 
    #endregion

    #region IStoreListener Callback
    // IAP初始化成功回掉函数;
    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        IAPDebugLog("OnInitialized Succ !");

        m_StoreController = controller;
        m_StoreExtensionProvider = extensions;

        // 这里可以获取您在AppStore和Google Play 上配置的商品;
        ProductCollection products = m_StoreController.products;
        Product[] all = products.all;
        for(int i = 0; i < all.Length; i++)
        {
            IAPDebugLog(all[i].metadata.localizedTitle  + "|" + all[i].metadata.localizedPriceString + "|" + all[i].metadata.localizedDescription + "|" + all[i].metadata.isoCurrencyCode);
        }

        #if UNITY_IOS
        // m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
        #endif
    }

    // IAP初始化失败回掉函数(没有网络的情况下并不会调起,而是一直等到有网络连接再尝试初始化);
    public void OnInitializeFailed(InitializationFailureReason error)
    {
        switch (error)
        {
        case InitializationFailureReason.AppNotKnown:
            IAPDebugLogError("Is your App correctly uploaded on the relevant publisher console?");
            break;
        case InitializationFailureReason.PurchasingUnavailable:
            IAPDebugLog("Billing disabled! Ask the user if billing is disabled in device settings.");
            break;
        case InitializationFailureReason.NoProductsAvailable:
            IAPDebugLog("No products available for purchase! Developer configuration error; check product metadata!");
            break;
        }
    }

    // 支付成功处理函数;
    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
    {
        IAPDebugLog("Purchase OK: " + e.purchasedProduct.definition.id);

        // 消息结构 : Receipt: {"Store":"fake","TransactionID":"9c5c16a5-1ae4-468f-806d-bc709440448a","Payload":"{ \"this\" : \"is a fake receipt\" }"};
        IAPDebugLog("Receipt: " + e.purchasedProduct.receipt);

        // 根据不同的id,做对应的处理(这是一种处理方式,当然您可以根据自己的喜好来处理);
        if (String.Equals(e.purchasedProduct.definition.id, C_ITEM_0, StringComparison.Ordinal))  
        {
            // TODO::
            
        }
        Messenger.Raise("PurchaseSuccess",e.purchasedProduct.definition.id);

        // 我们自己后台完毕的话,通过代码设置成功(如果是不需要后台设置直接设置完毕,不要设置Pending);
        // return PurchaseProcessingResult.Pending;  
        m_PurchaseInProgress = false;
        return PurchaseProcessingResult.Complete;  
    }

    // 支付失败回掉函数;
    public void OnPurchaseFailed(Product item, PurchaseFailureReason r)
    {
        m_PurchaseInProgress = false;
        UIFloatingManager.Instance.Show("支付失败");
    }

    // 恢复购买功能执行回掉函数;
    private void OnTransactionsRestored(bool success)
    {
        IAPDebugLog("Transactions restored.");
    }

    // 购买延迟提示(这个看自己项目情况是否处理);
    private void OnDeferred(Product item)
    {
        IAPDebugLog("Purchase deferred: " + item.definition.id);
    }
    #endregion

    private void IAPDebugLogError(string arg)
    {
        GameUtil.ShowToast(arg);
        Debug.LogError("IAP------"+arg);
    }
    private void IAPDebugLog(string arg)
    {
        GameUtil.ShowToast(arg);
        Debug.Log("IAP------"+arg);
    }
}

常见问题

国内测试Google Play 注意点:

  1. 测试机上要有 Google Play 服务,切Google Play服务正常(验证Google Play服务正常可以在Google Play上下载一个游戏,在连vpn的前提下能打开google 支付界面就说明google 服务正常)

2.要测试的应用要Google Play商店发布(发布内部测试,首次提交需要审核,最多48小时通过),为了防止vpn连接地区没有发布导致调不起支付,建议内部测试发布区域为所有区域

3.内部测试发布成功后通过,内部测试的连接去下载游戏,并进行测试

4.谷歌支付 初始化失败,我的原因是这个:
在这里插入图片描述

可以参考:
https://www.freesion.com/article/74381278955/

参考其他人的资料:
https://www.jianshu.com/p/7cd04d9f8fb5
https://zhuanlan.zhihu.com/p/29948224

猜你喜欢

转载自blog.csdn.net/KindSuper_liu/article/details/123254052
今日推荐