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);}#endregionprivate 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

Unity IAP接入google支付文档(2022年最新)相关推荐

  1. 如何离线安装Unity并添加离线帮助文档

    在学习和工作中,可能由于某些原因所用的电脑无法联网,这时相信很多朋友为如何离线安装Unity发愁,本文根据个人经验针对如何离线安装Unity进行详细的阐述. 步骤1:登陆Unity官网下载指定版本的安 ...

  2. 银联微信支付宝支付文档参考,不用再四处寻摸了

    银联微信支付宝支付文档参考,不用再四处寻摸了 银联.支付宝.微信的支付文档API讲解汇总 银联 银联开放平台 链接:https://open.unionpay.com/tjweb/index API文 ...

  3. Google Docs Download - 快速批量下载 Google Docs 文档

    Google Docs Download是Firefox 的 Greasemonkey 脚本,用来下载 Google Docs 文档,配合 DownThemAll 扩展,可实现批量下载 . 今天和 G ...

  4. TBS腾讯浏览器服务x5内核打开本地doc(doc docx xlsx pdf ppt)文档(官网最新43903版本SDK)

    目前最新版本(官网最新43903版本SDK)仅支持本地文件查看 测试Demo:TBS腾讯浏览器服务x5内核打开本地doc(docdocxxlsxpdfppt)文档(官网最新43903版本SDK)-互联 ...

  5. unity内购-Google支付(unity In-App Purchasing)

    unity In-App Purchasing 支付 支持二次验证,json解析完之后,把解析出来的参数发给服务器,因为是unity封装的sdk,code参数需要在购买成功之后的回调里给服务器发个in ...

  6. 微信sdk服务器支付文档,微信支付-普通下单开发者文档

    3.2. API接入(含示例代码) 本章节展示了如何使用微信支付服务端 SDK 快速接入小程序支付产品,完成与微信支付对接的部分. 注意: 文档中的代码示例是用来阐述 API 基本使用方法,代码中的示 ...

  7. Unity各个版本的离线文档下载和配置方法

    Unity各版本离线文档的下载链接: Unity2020.2版本的英文离线文档 Unity2020.1版本的中文离线文档 Unity2019.4版本的中文离线文档 Unity2019.3版本的中文离线 ...

  8. cocos creator Android 接入Google支付sdk

    准备工作 1.手机上安装Google服务 2.一个绑定信用卡的Google账号: 3.Google play客户端: 4..Google Play开发者后台创建应用 5.科学上网的工具 -等等 这些就 ...

  9. Unity 读取和修改XML文档

    1.Unity 读取XML文档 (1) 创建一个无继承实体类Class01,一个纯C#无继承的实体类Class02. (2) Class01根据XML文档中的内容设置具体的字段,并且设置get/set ...

  10. google的文档在线预览

    因为暂时没有好用的方法来生成预览文件. 临时使用google的在线生成功能,然后下载. 在处理时发现: 从文档说明看: 技术文档 - 构建自己的网址的说明

最新文章

  1. 如何在系统崩溃时从C++中获取函数调用栈信息?
  2. P2774 方格取数问题 网络最大流 割
  3. Android开发把项目打包成apk
  4. 八数码问题——双向广度优先搜索解决
  5. boost::proto::switch_相关的测试程序
  6. iOS之给WebView导航栏添加“返回”与“关闭”按钮
  7. SAP Hybris里搜索显示结果的实现原理
  8. 基于 EntityFramework 生成 Repository 模式代码
  9. ArcGIS 10.2 Calculate Value(Data Management) 工具的使用
  10. Weblogic的管理服务器与受管服务器
  11. 2016,你最不应该错过的热门技术文章
  12. 支持向量机---SVM 最小二乘支持向量机---LSSVM
  13. slidebox使用教程 设定焦点数量
  14. Mybatis-Plus 基础知识点
  15. Windows10系统添加打印机步骤
  16. HDU 1859 最小长方形
  17. 新一代智能手机发布前,iPhone地位无法撼动
  18. Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例
  19. 正态分布(近似正态分布)
  20. 修改360浏览器模式为极速模式

热门文章

  1. 知识图谱构建(概念,工具,实例调研)
  2. 目前颜值最高的开源BI工具-Superset
  3. 360携手HarmonyOS打造独特的“天气大师”
  4. GandCrab4.0勒索病毒解密工具
  5. 7-5 判断上三角矩阵
  6. knockout select默认选中
  7. 初识ubuntu 安装steam
  8. stm8s003f3使用杂记
  9. 虚拟机怎么启动共享文件服务器,VMware虚拟机中ubuntu启用本地文件共享的设置方法...
  10. IDL 读取葵花8(Himawari-8)HSD数据