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

Posted KindSuper_liu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

UNITY接入支付宝(未测试可行)

来源:https://www.jianshu.com/p/f8ff4f3fb4ce

支付宝App支付快速接入文档 https://docs.open.alipay.com/204/105297/
介绍了如何接入APP支付

第一步:创建应用并获取APPID

获取APPID需要申请支付宝开发者账号
在开发者中心中创建应用,生成应用唯一标识(APPID)

 
技术图片
Paste_Image.png

第二步:配置应用

应用创建完成后,需要给应用添加App支付功能,这样就可以在你的应用里使用App支付能力。此时该应用为开发中状态,只能在沙箱环境下进行调试。应用开发完成后,请开发者自行进行验收和安全性检查(安全性检查可参考《开放平台第三方应用安全开发指南》),验收检查完成后可申请上线。应用申请上线后,会同时申请此列表的功能,接口即生效,这个状态下的应用能够调用生产环境的接口。

根据需要签约不同的功能,这里我们选择APP支付


 
技术图片
Paste_Image.png

还需要配置密钥
密钥生成工具在客户端SDK包里
生成后复制到应用里保存


 
技术图片
Paste_Image.png

第三步:集成和开发

首先下载 App支付客户端DEMO&SDK
接下来我们使用android studio开发SDK插件

App支付Android集成流程

官方文档 https://docs.open.alipay.com/204/105296/

  1. 导入开发资源(SDK包)
  2. 修改Manifest
    在商户应用工程的AndroidManifest.xml文件里面添加声明:
<activity
    android:name="com.alipay.sdk.app.H5PayActivity"
    android:configChanges="orientation|keyboardHidden|navigation|screenSize"
    android:exported="false"
    android:screenOrientation="behind"
    android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
 <activity
    android:name="com.alipay.sdk.app.H5AuthActivity"
    android:configChanges="orientation|keyboardHidden|navigation"
    android:exported="false"
    android:screenOrientation="behind"
    android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>

和权限声明:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. 添加混淆规则

在商户应用工程的proguard-project.txt里添加以下相关规则:

-keep class com.alipay.android.app.IAlixPay{*;}
-keep class com.alipay.android.app.IAlixPay$Stub{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback$Stub{*;}
-keep class com.alipay.sdk.app.PayTask{ public *;}
-keep class com.alipay.sdk.app.AuthTask{ public *;}
-keep class com.alipay.sdk.app.H5PayCallback {
    <fields>;
    <methods>;
}
-keep class com.alipay.android.phone.mrpc.core.** { *; }
-keep class com.alipay.apmobilesecuritysdk.** { *; }
-keep class com.alipay.mobile.framework.service.annotation.** { *; }
-keep class com.alipay.mobilesecuritysdk.face.** { *; }
-keep class com.alipay.tscenter.biz.rpc.** { *; }
-keep class org.json.alipay.** { *; }
-keep class com.alipay.tscenter.** { *; }
-keep class com.ta.utdid2.** { *;}
-keep class com.ut.device.** { *;}

经测试,不加入混淆规则,也可以调试成功,对andoird还不是很熟悉,还是建议按照官方建议加上

  1. 支付接口调用
    需要在新线程中调用支付接口。(可参考alipay_demo实现)
    PayTask对象主要为商户提供订单支付、查询功能,及获取当前开发包版本号。
    获取PayTask支付对象调用支付(支付行为需要在独立的非ui线程中执行),代码示例:
final String orderInfo = info;   // 订单信息

        Runnable payRunnable = new Runnable() {

            

这是呼起支付宝支付界面的方法
客户端呼起并不需要密钥等信息,只需要获取服务器订单信息,完成支付后,支付宝会回调给服务器
服务器收到回调后,会通知客户端支付成功,刷新钻石等,详细见接口调用流程

SDK给UNITY调用的接口

public void StartAliPay(String orderInfo) {
        if (AliPayHelper.GetInstance().getPayActivity() == null)
            AliPayHelper.GetInstance().setPayActivity(this);

        Log.i(AppTag, orderInfo);
        //Toast.makeText(mContext, orderInfo, Toast.LENGTH_SHORT).show();
        AliPayHelper.GetInstance().StartAliPay(orderInfo);
    }

SDK调用支付宝接口

    /**
     * 开始支付
     *
     * @param orderInfo 订单信息
     */
    public void StartAliPay(final String orderInfo) {
        Activity ac = getPayActivity();

        if (ac == null) {
            Log.e(UnityActivity.AppTag, "没有设置PayTask Activity");
            return;
        }

        Runnable payRunnable = new Runnable() {

            

orderStr示例如下,参数说明见"请求参数说明",orderStr的获取必须来源于服务端:

app_id=2015052600090779&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D&charset=utf-8&method=alipay.trade.app.pay&sign_type=RSA2&timestamp=2016-08-15%2012%3A12%3A15&version=1.0&sign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJdhIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D

这里不能加入自定义的参数,否则可能会报 ALI40247(系统繁忙,请稍后再试) 错误

SDK同步返回结果处理

    

支付结果获取和处理

调用pay方法支付后,将通过2种途径获得支付结果:

同步返回

商户应用客户端通过当前调用支付的Activity的Handler对象,通过它的回调函数获取支付结果。(可参考alipay_demo实现)
代码示例:

private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            Result result = new Result((String) msg.obj);
            Toast.makeText(DemoActivity.this, result.getResult(),
                        Toast.LENGTH_LONG).show();
        };
    };
异步通知

商户需要提供一个http协议的接口,包含在请求支付的入参中,其key对应notify_url。支付宝服务器在支付完成后,会以POST方式调用notify_url传输数据。

UNITY调用SDK

    public static void StartAliPay(string orderInfo)
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject m_jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

#if UNITY_ANDROID
        m_jo.Call("StartAliPay", orderInfo);
#elif UNITY_IOS

#else
        print("暂不支持...");
#endif
    }

如果顺利的话,到这里应该可以支付成功了。

参考
http://blog.csdn.net/zhangdi2017/article/details/63254563

以上是关于Unity IAP接入google支付文档(2022年最新)的主要内容,如果未能解决你的问题,请参考以下文章

Unity 接入IAP(上)Android篇

使用Unity3d内置IAP插件 实现IOS和Google Play双端支付

Bmob Unity Android支付接入文档

FAQ接入HMS Core应用内支付服务过程中一些常见问题总结

Unity接入谷歌支付

unity iap 可以直接在unity调试吗