taptap游戏登录支付接入遇到的问题
导包
手动导包的话手机运行就会崩溃闪退,只能用清单导包。可能要导几次,会遇到清单json格式不对或者网络问题或者找不到这个包,试过换unity版本或者换包的版本或者用npm导入都没用,多试导几次。
"com.leancloud.realtime": "https://github.com/leancloud/csharp-sdk-upm.git#realtime-1.0.5",
"com.leancloud.storage": "https://github.com/leancloud/csharp-sdk-upm.git#storage-1.0.5",
"com.tapsdk.androiddependencyresolver": "https://github.com/TapTap/Android_Dependency_Resolver.git#1.1.6",
"com.taptap.tds.common": "https://github.com/TapTap/TapCommon-Unity.git#3.19.1",
"com.taptap.tds.login": "https://github.com/TapTap/TapLogin-Unity.git#3.19.1",
"com.taptap.tds.payments.global": "https://github.com/taptap/TapPaymentsGlobal-Unity.git#3.19.1",
记得改AndroidManifest.xml文件,不然运行会报错
android:name="com.taptap.payment.api.UnityApplication"
登录
第一次登录需要联网,后面可以静默登录
async Task LoginStateAsync()
{
// 获取登录状态
try
{
var accesstoken = await TapLogin.GetAccessToken();
Debug.Log("已登录");
// 直接进入游戏
}
catch (Exception e)
{
Debug.Log("当前未登录");
// 开始登录
var accessToken = await TapLogin.Login();
Debug.Log($"TapTap 登录成功 accessToken: {accessToken.ToJson()}");
// 获取用户信息
await TapLogin.GetProfile();
// 获取实时更新的用户信息
await TapLogin.FetchProfile();
}
}
商品支付
看文档写的是部分代码我一个小白刚开始也不懂怎么拼起来
这边是先用查询商品的方法把回调的数据用数组Item[] item存起来,因为要获取不同国家的币种与价格显示在ui上面
然后购买部分就ui按钮绑定商品id,通过id购买并发送商品
void InitItem()
{
string[] itemIds = new String[Products.Count];
int i = 0;
foreach (var Product in Products)
{
itemIds[i++] = Product.Value.Id;
}
var d = new Item();
TapPayment.QueryItems ( itemIds, new TapPayment.Callback < Item [] > ()
{
OnFinish = items =>
{
if ( items == null || items.Length == 0 )
{
Debug.LogError ( "未查询到相关商品" );
}
else
{
item = items;
initialized = true;
}
},
OnError = error =>
{
Debug.LogError ( "Error :" + error );
}
} );
}
//购买商品
public override EBuyError Buy(ProductConfig cfg)
{
if (initialized)
{
TapPayment.RequestPayFlow (cfg.Id, 1, "", new TapPayment.PayCallback()
{
OnCancel = ( extra ) =>
{
Debug.LogError ( $"支付已取消\n{extra}" );
},
OnError = ( error, extra ) =>
{
if (error.code == TapPayment.Error.Code.NETWORK_ERROR)
{
Debug.LogError("网络错误");//这里可以弹ui反馈给玩家
}
Debug.LogError ( $"错误代码:{error.code}\n错误信息:{error.Message}\n{extra}");
},
OnFinish = ( order, extra ) =>
{
Debug.LogError ( $"订单信息:{order}\n{extra}" );
//发送商品
DeliverGoods(cfg);
}
} );
}
else
{
initialized = false;
Debug.LogError("初始化失败");
}
return error;
}
//获取币种跟价格
public override string price(ProductConfig cfg)
{
string price = default;
if (initialized)
{
foreach (var items in item)
{
if (items.id == cfg.Id)
{
price = items.currency + items.price.ToString();
}
}
}
else
{
Debug.LogError("初始化失败");
}
return price;
}
}
商品查询的小问题
代码
跟着官方文档写,运行的时候会报错:
MissingMethodException: Default constructor not found for type TapTap.Payments.Global.bean.Item
因为有些版本Unity会把默认的构造函数剔除掉,尝试过把
Build Sttings -> Player Settings -> Seetings for Android -> Other Settings -> Optimization -> Strip Engine Code
取消勾选,结果还是没用,所以在回调商品信息item之前先new一下item:
var d = new Item();
配置
商品没上架的话
在游戏服务->TapTap Payments->商品与订单->添加测试用户,不然会报:未查询到商品
最后就是多打点日志,反复打包调试真的/(ㄒoㄒ)/~~
有什么写错的地方请多多指教,bye~