苹果内购挖坑实录
1 ?首先第一步 去蘋果官網配置各個參數 ?Itunes connect? 然后配置各個參數。 ?點擊打開鏈接? 這里有詳細的方式啦 ?配置的時候,有個銀行卡的信息 需要百度查一個什么碼什么的。
2 ?然后添加app ?,添加內購項目, 這里有個坑。包名一定要com.xxx.xxx.xxx ? 否則 彈不出購買框
3 ?下面就是在unity啦, 使用再帶的插件5.5版本的 ?Ctrl+0 彈出service
選擇這個然后,點擊improt
4 ?支持導入了自動購買插件 ,然后新鍵腳本,來處理邏輯響應
?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; using System.IO; using System; using UnityEngine.Purchasing; using UnityEngine.Purchasing.Extension; using UnityEngine.Store; #if UNITY_IPHONE || UNITY_IOS using UnityEngine.Purchasing.Security; using System.Runtime.InteropServices;public class IAP : MonoBehaviour , IStoreListener { // [DllImport("__Internal")] // private static extern void _InitLogin(); // [DllImport("__Internal")] // private static extern void _Login(); // [DllImport("__Internal")] // private static extern void _LoginOut(); private bool bInitStore = false;private static IAP iap = null;private IStoreController controller;// Use this for initializationvoid Start () {if (iap == null) iap = this; #if UNITY_IOS || UNITY_IPHONE//InitStore();////_InitLogin(); #endif}public static IAP GetInstance(){return iap;}private void InitStore(){ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());builder.AddProduct("com.jiuying.twelveAnimal.6", ProductType.Consumable);builder.AddProduct("com.jiuying.twelveAnimal.12", ProductType.Consumable);builder.AddProduct("com.jiuying.twelveAnimal.18", ProductType.Consumable);builder.AddProduct("com.jiuying.twelveAnimal.30", ProductType.Consumable);builder.AddProduct("com.jiuying.twelveAnimal.45", ProductType.Consumable);builder.AddProduct("com.jiuying.twelveAnimal.73", ProductType.Consumable);builder.AddProduct("com.jiuying.twelveAnimal.348", ProductType.Consumable);builder.AddProduct("com.jiuying.twelveAnimal.88", ProductType.Consumable);UnityPurchasing.Initialize(this, builder);NetMngr.GetSingleton().MyTransmit.AddEventListener("doIosRequest", ChargeCallBack);NetMngr.GetSingleton().MyTransmit.AddEventListener("chargeComplete", ChargeCompleteCallBack);}public void OnInitialized(IStoreController controller, IExtensionProvider extensions) {this.controller = controller;bInitStore = true;}public void OnInitializeFailed(InitializationFailureReason error) {Debug.Log("OnInitializeFailed "+error.ToString());bInitStore = false;}public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e){ Debug.Log("購買回調 "+ e.purchasedProduct.receipt);Dictionary<string, object> obj = MiniJSON.Json.Deserialize(e.purchasedProduct.receipt) as Dictionary<string, object>;validate(obj["TransactionID"].ToString(), obj["Payload"].ToString());return PurchaseProcessingResult.Complete;}public void OnPurchaseFailed(Product item, PurchaseFailureReason r) {Debug.Log(item+" 購買失敗 OnPurchaseFailed"+ r.ToString());//啟用SystemModel.GetSingleton().eventSystem.SetActive(true);}public void OnPurchaseClicked(string productId){if(!bInitStore){InitStore();return;}//_id = id;Debug.Log("開始購買!");controller.InitiatePurchase(productId);} #regionint _id = 0;public void Callback_productid(string productid){Debug.Log("nbnbnbnbnbnbnbnbnbnbnb" + "Callback_productidSignID :" + productid); // GoldShopData_IOS[] data = GoldShopDataManager_IOS.Instance.GetAllItem(); // for(int i = 0; i < data.Length; ++ i) // { // if(data[i].iosid == productid) // { // _id = data[i].id; // break; // } // }Debug.Log("nbnbnbnbnbnbnbnbnbnbnb" + "服務器要的ID :" + _id);}string _rece;public void CallBackRece(string str){_rece = str;Debug.Log("xxxxxxxxxxxxxx" + "Callback_SignID :" + str);}public void Callback(string str){//NetRequest.Send_ios_pay_sign(_id,str,_rece);Debug.Log("xxxxxxxxxxxxxx" + "Callback_SignID :" + str);}public void InitLogin(){//_InitLogin();Debug.Log("xxxxxxxxxxxxxx" + "IOS_Login ");}public void Login(){// _Login();}private string loginID;public void GetLoginID(string id){ // ClientLog.Instance.LogError("id ===="+id);loginID = id;}public void LoginCallBack(string token){ // AccountCtrl.Instance.m_LogOnView.LoginSDKFinish(); // CommonDefines._Account = SystemInfo.deviceUniqueIdentifier; // MyNetWork.Close(); // MyNetWork.ConnectServer(CommonDefines._loginIp, CommonDefines._loginPort, () => { // NetRequest.Send_Login_Msg(loginID,token,SystemInfo.deviceUniqueIdentifier); // });Debug.LogError("登錄成功,進入游戲");}public void LoginOut(){// _LoginOut();} #endregionpublic void validate(string id,string msg){object[] param = new object[]{id,msg};Debug.Log(id+"====="+msg);NetMngr.GetSingleton().SendMessage("user/doIosRequest",param, false);}private void ChargeCallBack(object[] args){//啟用SystemModel.GetSingleton().eventSystem.SetActive(true);Dictionary<string, object> obj = args[0] as Dictionary<string, object>;if (!(bool)obj["success"]){SoundController.GetSingleton().StartPlay(Config.SOUND_BAD);PopupRemind.GetRemind().ShowRemind((string)obj["message"]);}}private void ChargeCompleteCallBack(object[] args){//啟用SystemModel.GetSingleton().eventSystem.SetActive(true);SoundController.GetSingleton().StartPlay("Sound/System/buySuccess");Dictionary<string, object> obj = args[0] as Dictionary<string, object>;int types = (int)obj["type"];int diamond = (int)obj["newDiamondNum"];string gold = (string)obj["newGoldNum"];if (types == 0){SystemModel.GetSingleton().user.vip = 1;UIManager.GetUIManager().mainView.SetVIPImage();PopupRemind.GetRemind().ShowRemind("禮包購買成功,可至背包中查看", null);}else{PopupRemind.GetRemind().ShowRemind("充值成功", null);}UIManager.GetUIManager().SetDiamondText(diamond);SystemModel.GetSingleton().user.diamondNum = diamond;SystemModel.GetSingleton().user.goldNum = gold;UIManager.GetUIManager().SetGoldText(SystemModel.GetSingleton().user.GetGoldStr());// UIManager.GetUIManager().HideTwoAlpha(this.gameObject);}void Update(){if (Input.GetKeyDown(KeyCode.A)){Debug.Log("A 按下了");StreamReader sr = File.OpenText(Application.dataPath + "/a.txt");string input = sr.ReadToEnd();sr.Dispose();sr.Close();Debug.Log(input);Dictionary<string, object> obj = MiniJSON.Json.Deserialize(input) as Dictionary<string, object>;Debug.Log(obj["TransactionID"] + " "+obj["Payload"]);validate(obj["TransactionID"].ToString(), obj["Payload"].ToString());}}} #endif1 ?初始化商品(注意包名)
?
2 ? 購買商品
public void OnPurchaseClicked(string productId)
{
if(!bInitStore)
{
InitStore();
return;
}
? ? ? ? //_id = id;
? ? ? ? Debug.Log("開始購買!");
controller.InitiatePurchase(productId);
}
3 ?購買后,蘋果給的回調。這里需要把這個數據,拿到蘋果驗證
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
{?
Debug.Log("購買回調 "+ e.purchasedProduct.receipt);
? ? ? ? Dictionary<string, object> obj = MiniJSON.Json.Deserialize(e.purchasedProduct.receipt) as Dictionary<string, object>;
? ? ? ? validate(obj["TransactionID"].ToString(), obj["Payload"].ToString());
? ? ? ? return PurchaseProcessingResult.Complete;
}
4 ?后端驗證的坑:
如果上線App Store,拿到數據,需要先到正式服驗證,根據返回碼再到測試服驗證!
否則蘋果不讓上架,具體可以參考這個文章
http://blog.csdn.net/zzzzzdddddxxxxx/article/details/53220098點擊打開鏈接
總結
- 上一篇: Linux 摸索:使用VM安装CenOS
- 下一篇: bootstrap 表单提交验证