Unity接入IAP、服务器验单(Google Play)
最近因为项目需要,被分配来做项目SDK接入以及上架相关事宜。搞了好几天关于Unity接入支付的SDK,接入很简单,卡的最久的就是服务器验单,google相关文档也不是很全,走通之后觉得可以发出来共享一下,第一次写文章,有什么不足多多见谅
一.Unity接入In App Purchasing SDK
Unity已经集成了Google Pay、Apple App Store的支付,Unity会根据不同的平台唤起相应的支付。
1.安装IAP
通过Unity自带的Package Manager安装 Window > Package Manager
打开后搜索In App Purchasing安装
2.关于IAP在Unity内的设置
参考文档:
链接: https://docs.unity3d.com/530/Documentation/Manual/UnityIAP.html
安装IAP后需要开启Unity内IAP服务
Window-Services(Ctrl+0)在Services面板Link你的工程,启用In-App Purchase
这里Options下需要填入GooglePlay开发者后台的Key
进入开发者后台创建的应用内,找到创收设置,把key复制过来
3.GooglePlay商店设置
参考文档:
链接: https://docs.unity3d.com/cn/current/Manual/UnityIAPGoogleConfiguration.html
①创建商品:
在Google开发者后台创建好应用后添加应用内商品
②内部测试
添加测试用户
如果没有添加测试用户,即使应用在测试轨道,测试人员仍旧需要购买商品,所以需要在Google后台将测试人员添加进测试用户内
这里的测试用户是指测试用户可以通过下载链接在GooglePlay内下载你的测试版本,如果想要内购免费测试购买还需要进一步添加测试账号
GooglePlayConsole返回所有应用,找到许可测试,再次添加测试账号,并且将许可设置LICENSED
下载内部测试版本
上传内部测试aab包后,在添加测试用户界面最下方可以找到链接,分享给测试人员即可
4.关于IAP初始化
参考文档:
链接: https://docs.unity3d.com/cn/current/Manual/UnityIAPInitialization.html
直接上代码
①开始初始化,把Google的商品id添加进来
public void Initialize()
{
if (IsInitializd())
return;
Log.Info($"开始初始化IAP");
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
// 消耗品添加
var shopConfig = ShopCashConfigCategory.Instance.GetCashs();
//这里根据自己项目情况添加,我这里是读的配置表
//添加商品 builder.AddProduct("xxx.xxx.xxx", ProductType.Consumable);
for (int i = 0; i < shopConfig.Count; i++)
{
builder.AddProduct(shopConfig[i].IAP, ProductType.Consumable);
Log.Info($"添加商品 :{shopConfig[i].IAP}");
}
// 非消耗品添加
//初始化
UnityPurchasing.Initialize(this,builder);
}
②初始化成功
/// <summary>
/// Unity IAP 准备好可以进行购买时调用。
/// IAP初始化成功回掉函数
/// </summary>
/// <param name="controller"></param>
/// <param name="extensions"></param>
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
Log.Info("OnInitialized Succ !");
// Overall Purchasing system, configured with products for this application.
// 整体采购系统,为该应用程序配置了产品。
this.storeController = controller;
// Store specific subsystem, for accessing device-specific store features.
// 存储特定子系统,用于访问设备特定的存储特性。
this.storeExtensionProvider = extensions;
}
如果网络不好没有挂VPN,或者没有安装Google服务初始化不会成功
③根据ID购买商品
public async ETTask BuyProductByID(int id)
{
Log.Info($"是否存在订单purchaseInProgress = {this.purchaseInProgress}");
//尝试初始化
if (!this.IsInitializd())
{
this.Initialize();
}
//初始化失败
if (!this.IsInitializd())
{
// report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or retrying initiailization.
// 报告采购尚未成功初始化的事实。考虑等待更长时间或重新尝试初始化。
Log.Info("没有初始化UnityIAP或初始化失败.");
return;
}
//正在购买中
if (this.purchaseInProgress)
{
Log.Info($"未完成购买商品:{this.productId}");
return;
}
// system's products collection.
// 系统的产品集合。
this.productId = id;
var config = ShopGoodsConfigCategory.Instance.Get(this.productId);
if (config == null)
{
Log.Info($"购买商品:{this.productId} 失败。没有找到 ShopGoodsConfig");
return;
}
//将商品id添加进Product
Product product = storeController.products.WithID(config.Price.IAP);
if (product == null)
{
Log.Info($"购买商品:{this.productId} 失败。storeController.products.WithID return null");
return;
}
// If the look up found a product for this device's store and that product is ready to be sold ...
// 如果查找找到了该设备的商店的产品,该产品准备出售…
if (!product.availableToPurchase)
{
Log.Info($"购买商品:{this.productId} 失败。product.availableToPurchase is false");
return;
}
Log.Info($"开始购买商品: {this.productId} --> {product.definition.id} --> {product.transactionID}");
// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
// ……购买产品。期待通过ProcessPurchase或onpurchasfailed的响应
storeController.InitiatePurchase(product);
this.purchaseInProgress = true;
await ETTask.CompletedTask;
}
④购买成功后发货(成功回调)
这里可以选择客户端发货或者服务器发货,客户端发货,在成功回调时已经进行过一次验单,如果是服务器发货,则需要将商品信息发送给服务器进行二次验单
/// <summary>
/// 购买完成时调用。
///
/// 可能在 OnInitialized() 之后的任何时间调用。
/// </summary>
/// <param name="purchaseEvent"></param>
/// <returns></returns>
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
{
var product = purchaseEvent.purchasedProduct;
Log.Info($"购买成功 需要验单: {this.productId} --> {product.definition.id} --> {product.transactionID} --> receipt:{product.receipt}");
// TODO::向服务器发送验单
this.DoConfirmPendingPurchase(product).Coroutine();
// //获取并解析你需要上传的数据。解析成string类型
// var wrapper = (Dictionary<string, object>) MiniJson.JsonDecode (product.receipt);
// // Corresponds to http://docs.unity3d.com/Manual/UnityIAPPurchaseReceipts.html
// // 正在使用的商店的名称,例如 GooglePlay 或 AppleAppStore
// var store = (string)wrapper ["Store"];
// //下面的payload 验证商品信息的数据。即我们需要上传的部分。
// // For Apple this will be the base64 encoded ASN.1 receipt
// // 对于苹果来说,这将是base64编码的ASN.1收据
// var payload = (string)wrapper ["Payload"];
// //苹果验单直接传入 payload
// #if UNITY_IPHONE
// //苹果验单直接传入 payload
// DoConfirmPendingPurchaseByID(purchaseEvent.purchasedProduct.definition.id,payload).Coroutine();
//#endif
// // For GooglePlay payload contains more JSON
// // 对于GooglePlay有效负载包含更多JSON
// if (Application.platform == RuntimePlatform.Android)
// {
// var gpDetails = (Dictionary<string, object>)MiniJson.JsonDecode(payload);
// var gpJson = (string)gpDetails["json"];
// var gpSig = (string)gpDetails["signature"];
// //Google验证商品信息的数据包含在gpJson里面还需要在服务端进行解析一下,对应的键是"purchaseToken"。
// DoConfirmPendingPurchaseByID(purchaseEvent.purchasedProduct.definition.id,gpJson).Coroutine();
// }
return PurchaseProcessingResult.Pending;
}
这里直接将商品的所有信息全部都传给了服务器,服务器自己进行订单信息的解析
注释部分为客户端解析订单信息,然后再做下一步处理
注:如果不需要服务器验单,可以直接在这里处理购买完成后的逻辑,这里的返回应该是
return PurchaseProcessingResult.Complete;
需要服务器验单的话需要返回,等服务器验单后再结束订单
return PurchaseProcessingResult.Pending;
// 验单 确认购买产品成功;
public async ETTask DoConfirmPendingPurchase(Product product)
{
var response = await ShopHelper.Buy(this.iapComponent.ZoneScene(), this.productId, getPayType(product), OrderStateType.PayFinish, product.transactionID, product.receipt);
if (response.Error == ErrorCode.ERR_Success)
{
Log.Info($"验单结束,购买成功");
storeController.ConfirmPendingPurchase(product);
}
this.purchaseInProgress = false;
Log.Info($"订单结束 purchaseInProgress = {purchaseInProgress}");
await ETTask.CompletedTask;
}
⑤完整代码
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Purchasing;
namespace ET
{
public class UnityIAPHanlder : IIAPHandler, IStoreListener
{
private readonly UnityIAPComponent iapComponent;
private IStoreController storeController;//存储商品信息
private IExtensionProvider storeExtensionProvider;//IAP扩展工具
private bool purchaseInProgress = false;//是否处于付费中
private int productId;
public UnityIAPHanlder(UnityIAPComponent iapComponent)
{
this.iapComponent = iapComponent;
}
public void Initialize()
{
if (IsInitializd())
return;
Log.Info($"开始初始化IAP");
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
// 消耗品添加
var shopConfig = ShopCashConfigCategory.Instance.GetCashs();
for (int i = 0; i < shopConfig.Count; i++)
{
builder.AddProduct(shopConfig[i].IAP, ProductType.Consumable);
Log.Info($"添加商品 :{shopConfig[i].IAP}");
}
// 非消耗品添加
UnityPurchasing.Initialize(this,builder);
}
public void Dispose()
{
this.purchaseInProgress = false;
this.productId = 0;
this.storeController = null;
this.storeExtensionProvider = null;
}
public bool IsInitializd()
{
return storeController != null && storeExtensionProvider != null;
}
public async ETTask BuyProductByID(int id)
{
Log.Info($"是否存在订单purchaseInProgress = {this.purchaseInProgress}");
//尝试初始化
if (!this.IsInitializd())
{
this.Initialize();
}
//初始化失败
if (!this.IsInitializd())
{
// report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or retrying initiailization.
// 报告采购尚未成功初始化的事实。考虑等待更长时间或重新尝试初始化。
Log.Info("没有初始化UnityIAP或初始化失败.");
return;
}
//正在购买中
if (this.purchaseInProgress)
{
Log.Info($"未完成购买商品:{this.productId}");
return;
}
// system's products collection.
// 系统的产品集合。
this.productId = id;
var config = ShopGoodsConfigCategory.Instance.Get(this.productId);
if (config == null)
{
Log.Info($"购买商品:{this.productId} 失败。没有找到 ShopGoodsConfig");
return;
}
Product product = storeController.products.WithID(config.Price.IAP);
if (product == null)
{
Log.Info($"购买商品:{this.productId} 失败。storeController.products.WithID return null");
return;
}
// If the look up found a product for this device's store and that product is ready to be sold ...
// 如果查找找到了该设备的商店的产品,该产品准备出售…
if (!product.availableToPurchase)
{
Log.Info($"购买商品:{this.productId} 失败。product.availableToPurchase is false");
return;
}
Log.Info($"开始购买商品: {this.productId} --> {product.definition.id} --> {product.transactionID}");
// var response = await ShopHelper.Buy(UnityIAPComponent.Instance.ZoneScene(), productId);
// if (response.Error != ErrorCode.ERR_Success)
// {
// Log.Info($"购买商品:{this.productId} 失败。向逻辑服下单异常 error:{response.Error}");
// return;
// }
// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
// ……购买产品。期待通过ProcessPurchase或onpurchasfailed的响应
storeController.InitiatePurchase(product);
this.purchaseInProgress = true;
await ETTask.CompletedTask;
}
#region IStoreListener
/// <summary>
/// 购买完成时调用。
///
/// 可能在 OnInitialized() 之后的任何时间调用。
/// </summary>
/// <param name="purchaseEvent"></param>
/// <returns></returns>
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
{
var product = purchaseEvent.purchasedProduct;
Log.Info($"购买成功 需要验单: {this.productId} --> {product.definition.id} --> {product.transactionID} --> receipt:{product.receipt}");
// TODO::向服务器发送验单
this.DoConfirmPendingPurchase(product).Coroutine();
// //获取并解析你需要上传的数据。解析成string类型
// var wrapper = (Dictionary<string, object>) MiniJson.JsonDecode (product.receipt);
// // Corresponds to http://docs.unity3d.com/Manual/UnityIAPPurchaseReceipts.html
// // 正在使用的商店的名称,例如 GooglePlay 或 AppleAppStore
// var store = (string)wrapper ["Store"];
// //下面的payload 验证商品信息的数据。即我们需要上传的部分。
// // For Apple this will be the base64 encoded ASN.1 receipt
// // 对于苹果来说,这将是base64编码的ASN.1收据
// var payload = (string)wrapper ["Payload"];
// //苹果验单直接传入 payload
// #if UNITY_IPHONE
// //苹果验单直接传入 payload
// DoConfirmPendingPurchaseByID(purchaseEvent.purchasedProduct.definition.id,payload).Coroutine();
//#endif
// // For GooglePlay payload contains more JSON
// // 对于GooglePlay有效负载包含更多JSON
// if (Application.platform == RuntimePlatform.Android)
// {
// var gpDetails = (Dictionary<string, object>)MiniJson.JsonDecode(payload);
// var gpJson = (string)gpDetails["json"];
// var gpSig = (string)gpDetails["signature"];
// //Google验证商品信息的数据包含在gpJson里面还需要在服务端进行解析一下,对应的键是"purchaseToken"。
// DoConfirmPendingPurchaseByID(purchaseEvent.purchasedProduct.definition.id,gpJson).Coroutine();
// }
return PurchaseProcessingResult.Pending;
}
private int getPayType(Product product)
{
var wrapper = (Dictionary<string, object>) MiniJson.JsonDecode (product.receipt);
// 正在使用的商店的名称,例如 GooglePlay 或 AppleAppStore
var store = (string)wrapper ["Store"];
if (store == "GooglePlay")
return PayType.Google;
if (store == "AppleAppStore")
return PayType.IOS;
return PayType.Test;
}
// 验单 确认购买产品成功;
public async ETTask DoConfirmPendingPurchase(Product product)
{
var response = await ShopHelper.Buy(this.iapComponent.ZoneScene(), this.productId, getPayType(product), OrderStateType.PayFinish, product.transactionID, product.receipt);
if (response.Error == ErrorCode.ERR_Success)
{
Log.Info($"验单结束,购买成功");
storeController.ConfirmPendingPurchase(product);
}
this.purchaseInProgress = false;
Log.Info($"订单结束 purchaseInProgress = {purchaseInProgress}");
await ETTask.CompletedTask;
}
/// <summary>
/// 购买失败时调用。
/// </summary>
/// <param name="product"></param>
/// <param name="failureReason"></param>
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
this.purchaseInProgress = false;
this.iapComponent.OnPurchaseFailed(this.productId, failureReason.ToString());
Log.Info("购买失败" );
}
/// <summary>
/// Unity IAP 准备好可以进行购买时调用。
/// IAP初始化成功回掉函数
/// </summary>
/// <param name="controller"></param>
/// <param name="extensions"></param>
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
Log.Info("OnInitialized Succ !");
// Overall Purchasing system, configured with products for this application.
// 整体采购系统,为该应用程序配置了产品。
this.storeController = controller;
// Store specific subsystem, for accessing device-specific store features.
// 存储特定子系统,用于访问设备特定的存储特性。
this.storeExtensionProvider = extensions;
}
/// <summary>
/// Unity IAP 遇到不可恢复的初始化错误时调用。
/// IAP初始化失败回掉函数(没有网络的情况下并不会调起,而是一直等到有网络连接再尝试初始化);
/// 请注意,如果互联网不可用,则不会调用此项;
/// 将尝试初始化,直到互联网变为可用。
/// </summary>
/// <param name="error"></param>
public void OnInitializeFailed(InitializationFailureReason error)
{
string errorDes = "";
switch (error)
{
case InitializationFailureReason.AppNotKnown:
errorDes = "你的应用是否正确上传到相关发行商控制台?";
break;
case InitializationFailureReason.PurchasingUnavailable:
errorDes = "计费禁用!用户是否在设备设置中禁用了计费。";
break;
case InitializationFailureReason.NoProductsAvailable:
errorDes = "没有可供购买的产品!开发者配置错误;检查产品配置数据!";
break;
default:
errorDes = $"初始化未处理异常 {error}";
break;
}
Log.Info("Unity Iap 初始化失败: "+errorDes);
this.iapComponent.OnInitializeFailed(errorDes);
}
#endregion
// 恢复购买;
public void RestorePurchases()
{
if (!this.IsInitializd())
{
Log.Info("没有初始化UnityIAP或初始化失败.");
return;
}
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer)
{
Log.Info("开始恢复购买 ...");
var apple = storeExtensionProvider.GetExtension<IAppleExtensions>();
apple.RestoreTransactions((result) =>
{
// 返回一个bool值,如果成功,则会多次调用支付回调,然后根据支付回调中的参数得到商品id,最后做处理(ProcessPurchase);
Log.Info("恢复购买中: " + result + ". 如果没有进一步的消息,则没有可恢复的购买.");
OnTransactionsRestored(result);
});
}
else
{
Log.Info("恢复购买失败。当前平台不支持。当前的平台 = " + Application.platform);
}
}
/// <summary>
/// 恢复购买功能执行回掉函数;
/// </summary>
/// <param name="success"></param>
private void OnTransactionsRestored(bool success)
{
if (!success)
{
Log.Info("恢复购买失败或者没有可恢复的商品");
return;
}
//恢复购买成功的处理
}
}
}
参考:
链接: https://blog.csdn.net/KindSuper_liu/article/details/123254052?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166936044716782388086288%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=166936044716782388086288&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-1-123254052-null-null.142v66control,201v3control_1,213v2t3_control2&utm_term=unity%20iap&spm=1018.2226.3001.4187
⑥不同平台接入参考
链接: https://docs.unity3d.com/cn/current/Manual/UnityIAP.html
二.服务器验单
由于Google关于服务器验单相关的文档很少,也没有相关Demo,所以Googl官方相关的文档只能部分参考,这里提供我跑通的一篇文章参考,流程基本一致,我补充一些容易忽略的细节(我跟我们的后端大佬就是栽在了这些细节上QAQ)
流程参考:
链接: https://www.jianshu.com/p/76416ebc0db0
1.创建API项目
2.开启Google Play Android Developer API
搜索“Google Play Android Developer API”
3.开启同意屏幕
注:完善必填项即可
4.创建OAuth2客户端ID
注意:
1.这里选择的应用类型为Web应用,不要被Android、IOS之类的选项迷惑
2.重定向URI是用来接收code的,当登陆成功后重新定向,重定向地址后带有code,我们这里不需要服务端接收code,所以随便填一个即可,这里我填的是http://127.0.0.1:51355/,如果有服务器接收code的需求,填写接收code的地址即可
创建成功后可以获取到clientId和clientSecret
5.GooglePay后台关联API项目
6.获取code
地址:
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri={填写的重定向地址}&client_id={创建的clientId}
请求方式:浏览器中打开
将上面的{XX}替换成创建api项目时填写的重定向地址,和clientId,然后将连接放到浏览器中打开,就会调起授权界面,使用你的开发者账号授权登录
授权后会打开一个网页,查看该网页的地址即可看到code
这里可以看到,重定向地址上有两个参数code和scope,我们只需要code就行了,这里的code是urlencode后的,使用时需要decode
7.使用code换取refreshToken
地址:
https://accounts.google.com/o/oauth2/token
请求方式:post
参数:grant_type=authorization_code
code=获取到的code(需要看看code中是否有%号,如果有需要urldecode)
client_id=创建api项目是的clientId(客户端ID)
client_secret=创建api项目时的clientSecret(客户端密钥)
redirect_uri=创建api项目时的重定向地址
注意:
①.code是一次性的!!!一定要注意!!!
②.refreshToken一定要保存下来,因为code是一次性的,只能获取一次。refreshToken可以作为一个常量保存起来
③.如果出现如下的返回,大概率这个code已经不能用了
④.如果code已经使用过,并且没有保存refreshToken。需要删除之前创建的OAuth2客户端,重新创建,并且重复6、7步,重新获取refreshToken
8.使用refreshToken获取accessToken
地址:
请求方式:post
https://accounts.google.com/o/oauth2/token
参数:grant_type=refresh_token
refresh_token=刚刚获取到的refreshToken
client_id=创建api项目是的clientId(客户端ID)
client_secret=创建api项目时的clientSecret(客户端密钥)文章来源:https://www.toymoban.com/news/detail-410598.html
9.查询订单状态
请求方式:get
地址:
https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/products/{productId}/tokens/{token}?access_token={access_token}
packageName:app包名,必须是创建登录api项目时,创建android客户端Id使用包名
productId:对应购买商品的商品ID
token:购买成功后Purchase对象的getPurchaseToken()
access_token:上面获取到的accessToken
返回值:文章来源地址https://www.toymoban.com/news/detail-410598.html
VerifyOrder json:
{
"purchaseTimeMillis": "1670494175424",//购买产品的时间,自纪元(1970 年 1 月 1 日)以来的毫秒数。
"purchaseState": 0,//订单的购买状态。可能的值为:0. 已购买 1. 已取消 2. 待定
"consumptionState": 0,//产品的消费状态。可能的值为: 0. 尚未消耗 1. 已消耗
"developerPayload": "",
"orderId": "GPA.3377-3702-0099-36461",//google订单号
"purchaseType": 0,
"acknowledgementState": 0,
"kind": "androidpublisher#productPurchase",//上面客户支付时的透传字段,google指导是用来存放用户信息的,不能过长,否则客户端不能支付
"regionCode": "HK"
}
关于RefreshToken过期问题
- api项目-同意屏幕,发布状态为测试(有效期7天)
- RefreshToken 6个月都未使用,这个要维护accessToken的有效性,应该可以不必考虑
- 授权账号改密码了(未测试,修改开发者账号密码是否会导致过期)
- 授权超过50个刷新令牌,最先的刷新令牌就会失效(这里50个应该够用了,除了测试时,可能会授权多个)
- 取消了授权
- 属于具有有效会话控制策略的 Google Cloud Platform 组织
到了这里,关于Unity接入IAP、服务器验单(Google Play)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!