蘋果內購挖坑實錄
1 首先第一步 去蘋果官網配置各個引數 Itunes connect 然後配置各個引數。 點選開啟連結 這裡有詳細的方式啦 配置的時候,有個銀行卡的資訊 需要百度查一個什麼碼什麼的。
2 然後新增app ,新增內購專案, 這裡有個坑。包名一定要com.xxx.xxx.xxx 否則 彈不出購買框
3 下面就是在unity啦, 使用再帶的外掛5.5版本的 Ctrl+0 彈出service
選擇這個然後,點選improt
4 支援匯入了自動購買外掛 ,然後新鍵指令碼,來處理邏輯響應
1 初始化商品(注意包名)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 initialization void 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); } #region int _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(); } #endregion public 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()); } } } #endif
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點選開啟連結