zoukankan      html  css  js  c++  java
  • Unity自带IAP插件使用(googleplay)

    https://blog.csdn.net/ar__ha/article/details/64439872

    Unity Services里的Unity IAP对于IOS和GooglePlay的支付用这个插件就足够了。

    Unity官方文档

    1.集成插件

    Window-Services(Ctrl+0)在Services面板Link你的工程,启用In-App Purchase,Import/Update一下,插件就在Assets/Plugins/UnityPurchasing下。

    2.设置

    以GooglePlay的设置为例

    Window - Unity Iap - Android - Target Google Play :选择GooglePlay,

    Window - Unity Iap - Receipt Validation Obfuscator :输入GooglePlay的PublicKey,

    点击Obfuscate secrets后,在Assets/Plugins/UnityPurchasing/generated下会生产GooglePlayTangle自动生成的文件,不用管他。

    但是要注意到他的宏定义

    #if UNITY_ANDROID || UNITY_IPHONE || UNITY_STANDALONE_OSX || UNITY_TVOS
     

    3.DEMO

    插件的demo在Assets/Plugins/UnityPurchasing/scenes/IAP Demo 这个场景里

    也可以直接看cs文件,Assets/Plugins/UnityPurchasing/script/IAPDemo.cs

    主要用到的是UnityEngine.Purchasing.IStoreListener 这个接口

    4.初始化

    1.  
      //使用这个解析IAP成功后的receipt
    2.  
      private UnityEngine.Purchasing.Security.CrossPlatformValidator validator;
    3.  
       
    4.  
       
    5.  
      private void InitUnityPurchase() {
    6.  
      var module = StandardPurchasingModule.Instance();
    7.  
      var builder = ConfigurationBuilder.Instance (module);
    8.  
       
    9.  
      //添加计费点
    10.  
      // UnityEngine.Purchasing.ProductType
    11.  
      builder.AddProduct("item1", ProductType.Consumable, new IDs
    12.  
      {
    13.  
      {"苹果计费点", AppleAppStore.Name },
    14.  
      {"谷歌计费点", GooglePlay.Name}
    15.  
      }
    16.  
      );
    17.  
      #if !UNITY_EDITOR
    18.  
      validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.bundleIdentifier);
    19.  
      #endif
    20.  
      UnityPurchasing.Initialize (this, builder);
    21.  
      }
     
    实现IStoreListener 接口初始化回调
     
    成功:
    1.  
      private IStoreController m_Controller;
    2.  
       
    3.  
       
    4.  
      //UNITY IAP初始化成功
    5.  
      public void OnInitialized (IStoreController controller, IExtensionProvider extensions) {
    6.  
      m_Controller = controller;
    7.  
       
    8.  
      // On Apple platforms we need to handle deferred purchases caused by Apple's Ask to Buy feature.
    9.  
      // On non-Apple platforms this will have no effect; OnDeferred will never be called.
    10.  
      var m_AppleExtensions = extensions.GetExtension<IAppleExtensions> ();
    11.  
      m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
    12.  
       
    13.  
      var product = m_Controller.products.WithID("item1");
    14.  
      //价格 (带货币单位的字符串)
    15.  
      var priceString = product.metadata.localizedPriceString;
    16.  
      //价格 (换算汇率后的价格)
    17.  
      var price = product.metadata.localizedPrice;
    18.  
      }
    失败:
    1.  
      //初始化失败(没有网络的情况下并不会调起,而是一直等到有网络连接再尝试初始化)
    2.  
      public void OnInitializeFailed (InitializationFailureReason error) {
    3.  
       
    4.  
      Debug.Log("Billing failed to initialize!");
    5.  
      switch (error) {
    6.  
      case InitializationFailureReason.AppNotKnown:
    7.  
      Debug.LogError("Is your App correctly uploaded on the relevant publisher console?");
    8.  
      break;
    9.  
      case InitializationFailureReason.PurchasingUnavailable:
    10.  
      // Ask the user if billing is disabled in device settings.
    11.  
      Debug.Log("Billing disabled!");
    12.  
      break;
    13.  
      case InitializationFailureReason.NoProductsAvailable:
    14.  
      // Developer configuration error; check product metadata.
    15.  
      Debug.Log("No products available for purchase!");
    16.  
      break;
    17.  
      }
    18.  
      }
     
    5.发起支付
    1.  
      public void DoIapPurchase (Action<bool, string> callback) {
    2.  
      if (m_Controller != null) {
    3.  
      var product = m_Controller.products.WithID ("item1");
    4.  
      if (product != null && product.availableToPurchase) {
    5.  
      //调起支付
    6.  
      m_Controller.InitiatePurchase(product);
    7.  
      }
    8.  
      else {
    9.  
      callback (false, "no available product");
    10.  
      }
    11.  
      }
    12.  
      else {
    13.  
      callback ( false, "m_Controller is null");
    14.  
      }
    15.  
      }
     
    实现IStoreListener支付回调
     
    成功:
    1.  
      public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e) {
    2.  
      try {
    3.  
      var result = validator.Validate (e.purchasedProduct.receipt);
    4.  
      Debug.Log ("Receipt is valid. Contents:");
    5.  
      foreach (IPurchaseReceipt productReceipt in result) {
    6.  
      Debug.Log(productReceipt.productID);
    7.  
      Debug.Log(productReceipt.purchaseDate);
    8.  
      Debug.Log(productReceipt.transactionID);
    9.  
       
    10.  
      AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt;
    11.  
      if (null != apple) {
    12.  
      Debug.Log(apple.originalTransactionIdentifier);
    13.  
      Debug.Log(apple.subscriptionExpirationDate);
    14.  
      Debug.Log(apple.cancellationDate);
    15.  
      Debug.Log(apple.quantity);
    16.  
       
    17.  
       
    18.  
      //如果有服务器,服务器用这个receipt去苹果验证。
    19.  
      var receiptJson = JSONObject.Parse(e.purchasedProduct.receipt);
    20.  
      var receipt = receiptJson.GetString("Payload");
    21.  
      }
    22.  
       
    23.  
      GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
    24.  
      if (null != google) {
    25.  
      Debug.Log(google.purchaseState);
    26.  
      Debug.Log(google.purchaseToken);
    27.  
      }
    28.  
      }
    29.  
      return PurchaseProcessingResult.Complete;
    30.  
      } catch (IAPSecurityException) {
    31.  
      Debug.Log("Invalid receipt, not unlocking content");
    32.  
      return PurchaseProcessingResult.Complete;
    33.  
      }
    34.  
      return PurchaseProcessingResult.Complete;
    35.  
      }
    失败:
    1.  
      public void OnPurchaseFailed(Product i, PurchaseFailureReason p) {
    2.  
      Logger.Warning("purchase failed of reason : " + p.ToString());
    3.  
      }
    IOS deferred:
    1.  
      /// <summary>
    2.  
      /// iOS Specific.
    3.  
      /// This is called as part of Apple's 'Ask to buy' functionality,
    4.  
      /// when a purchase is requested by a minor and referred to a parent
    5.  
      /// for approval.
    6.  
      ///
    7.  
      /// When the purchase is approved or rejected, the normal purchase events
    8.  
      /// will fire.
    9.  
      /// </summary>
    10.  
      /// <param name="item">Item.</param>
    11.  
      private void OnDeferred(Product item)
    12.  
      {
    13.  
      Logger.Warning("Purchase deferred: " + item.definition.id);
    14.  
      }
  • 相关阅读:
    kafka官方的kafka-server-start.sh不能关闭kafka进程解决办法
    Linux如何切换图形界面和命令行界面
    2019-9-28:渗透测试,基础学习,DNS投毒
    2019-9-28:渗透测试,基础学习,pgp常量,逻辑运算,DNS投毒,笔记
    2019-9-10:渗透测试,基础学习,nmap扫描命令,php基本语法学习,笔记
    2019-9-11:渗透测试,Kill远控软件,初接触
    2019-9-27:渗透测试,metasploit-framework初接触
    2019-9-26:渗透测试,基础学习,js正则以及什么是目录扫描,笔记
    2019-9-26:渗透测试,基础学习,nmap扫描kali虚拟机服务
    2019-9-25:渗透测试,基础学习,Hydra BP爆破,js基本知识,banner信息收集笔记
  • 原文地址:https://www.cnblogs.com/nafio/p/9566706.html
Copyright © 2011-2022 走看看