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 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
1 初始化商品(注意包名)
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点击打开链接