1.初始化:
WX.InitSDK((jsCode) =>
{
Debug.Log("*** InitSDK success ");
FunGetSysten();
Init1();
});
2.获取系统信息
private void FunGetSysten()
{
Debug.Log("*** GetSystemInfo ");
GetSystemInfoOption getSystemInfoOption = new GetSystemInfoOption();
getSystemInfoOption.success = ((e) =>
{
systemInfo = e;
switch (e.platform)
{
case "android":
weChatSystemType = WeChatSystemType.android;
break;
case "ios":
weChatSystemType = WeChatSystemType.ios;
break;
case "windows":
weChatSystemType = WeChatSystemType.windows;
break;
case "mac":
weChatSystemType = WeChatSystemType.mac;
break;
case "devtools":
weChatSystemType = WeChatSystemType.devtools;
break;
default:
weChatSystemType = WeChatSystemType.other;
break;
}
if (e.platform == "android")
{
}
else
if (e.platform == "ios")
{
weChatSystemType = WeChatSystemType.ios;
}
Debug.Log("*** GetSystemInfo success " + e.platform);
});
getSystemInfoOption.fail = ((e) =>
{
Debug.Log("****** FunGetSysten fail :" + e.errMsg);
});
WX.GetSystemInfo(getSystemInfoOption);
var info = WX.GetSystemInfoSync();
if (info != null)
{
Systen2_top = (float)info.safeArea.top;
Systen2_windowHeight = (float)info.windowHeight;
Systen2_pixelRatio = (float)info.pixelRatio;
}
}
3.获取code
public void Init1(Action action = null)
{
LoginOption loginOption = new LoginOption();
loginOption.success = ((e) =>
{
jsCode = e.code;
Debug.Log("****** Login success " + jsCode);
string url = SERVERURLConfig.WEWECHATINFO + jsCode;
StartCoroutine(GetRequest(url));
});
loginOption.fail = ((e) =>
{
Debug.Log("****** e.errMsg " + e.errMsg);
});
WX.Login(loginOption);
}
4.根据code获取openid
IEnumerator GetRequest(string url)
{
Debug.Log("*** GetRequest " + url);
var webRequest = new UnityEngine.Networking.UnityWebRequest(url, "GET");
webRequest.downloadHandler = (UnityEngine.Networking.DownloadHandler)new UnityEngine.Networking.DownloadHandlerBuffer();
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError || webRequest.isHttpError)
{
Debug.Log("*** " + webRequest.error);
}
else
{
yield return new WaitUntil(() => webRequest.isDone);
Debug.Log("success:" + webRequest.downloadHandler.text);
var result = GersonFrame.LitJson.JsonMapper.ToObject<GetOpenIDAndUnionIDDataToken>(webRequest.downloadHandler.text);
if (result != null && result.code == 200)
{
if (result.data != null)
{
Debug.Log("*** 获取成功 " + webRequest.downloadHandler.text);
openId = result.data.openid;
unionId = result.data.unionid;
isInit = true;
if (sendShowNum==0)
{
FunOnShow(0);
sendShowNum = 1;
}
}
}
else
{
}
}
}
6.充值
#region 安卓充值
public void OnPay(OrderBackData orderBackData)
{
#if UNITY_WEBGL
Debug.Log("****** 安卓充值 ");
RequestMidasPaymentOption requestMidasPayment = new RequestMidasPaymentOption();
requestMidasPayment.currencyType = orderBackData.currencyType;
requestMidasPayment.mode = "game";
requestMidasPayment.offerId = orderBackData.offerId;
requestMidasPayment.buyQuantity = orderBackData.buyQuantity;
requestMidasPayment.env = System.Convert.ToInt32(orderBackData.env);
requestMidasPayment.platform = orderBackData.platform;
requestMidasPayment.zoneId = orderBackData.zoneId;
requestMidasPayment.outTradeNo = "";
requestMidasPayment.success = (e) =>
{
string GN_SERVER_HEAD = string.Format("https://cwdj.mantianfei.net/gpays/wx_notify_url2?appId={0}&orderNo={1}", orderBackData.appId, orderBackData.orderNo);
StartCoroutine(GetPayOverInfo(GN_SERVER_HEAD));
};
requestMidasPayment.fail = async (e) =>
{
UIManager.HideView("WaitMsgRespon");
await UIManager.ShowView("TopTips", true, "支付失败");
};
WX.RequestMidasPayment(requestMidasPayment);
#endif
}
IEnumerator GetPayOverInfo(string url)
{
Uri uri = new Uri(url);
UnityWebRequest unityWebRequest = UnityWebRequest.Get(uri);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)
{
UIManager.HideView("WaitMsgRespon");
Debug.Log("*** 支付信息返回失败 " + url);
}
else
{
var result = GersonFrame.LitJson.JsonMapper.ToObject<PayBackInfoData>(unityWebRequest.downloadHandler.text);
if (result != null && result.code == 200)
{
UIManager.HideView("WaitMsgRespon");
}
else
{
UIManager.HideView("WaitMsgRespon");
Debug.Log("*** 支付信息返回失败 " + unityWebRequest.downloadHandler.text);
}
}
}
#endregion
#region ios 支付
public void FunIosPay(OrderBackData data2, GetOrderInfoConmfig getOrderInfoConmfig)
{
#if UNITY_WEBGL
Debug.Log("****** ios充值 ");
getOrderInfoConmfig.orderNo = data2.orderNo;
getOrderInfoConmfig.price *= 10;
Debug.Log("****** 打开客服 " + LitJson.JsonMapper.ToJson(getOrderInfoConmfig));
string jsonInfo= LitJson.JsonMapper.ToJson(getOrderInfoConmfig);
OpenWechatServiceConversation(jsonInfo,"点我充值");
#endif
}
public void OpenWechatServiceConversation(string fromInfo,string title)
{
#if UNITY_WEBGL
WX.OpenCustomerServiceConversation(new OpenCustomerServiceConversationOption
{
sessionFrom = fromInfo,
showMessageCard = true,
sendMessageTitle =title ,
success = (e) =>
{
UIManager.HideView("WaitMsgRespon");
Debug.Log("****** 打开客服 success " + e.errMsg);
},
fail = (e) =>
{
UIManager.HideView("WaitMsgRespon");
Debug.Log("****** 打开客服 fail " + e.errMsg);
}
});
#endif
}
#endregion
public class GetOrderInfoConmfig
{
public string uid;
public string serverId;
public string roleUID;
public int lv;
public string ItemId;
public float price;
public string channel;
public string appId;
public string product_name;
public string timestamp;
public string orderNo;
}
public class GetOpenIDAndUnionIDDataToken
{
public string msg;
public int code;
public WechatInfoData data;
}
public class WechatInfoData
{
public string appId;
public string platform;
public string code;
public string openid;
public string session_key;
public string unionid;
}
public class PayBackDataConfig
{
public string msg;
public int code;
public OrderBackData data;
}
public class OrderBackData
{
public string currencyType;
public string uid;
public string orderNo;
public string appId;
public string offerId;
public int buyQuantity;
public string zoneId;
public string env;
public string platform;
}
public class PayBackInfoData
{
public string msg;
public int code;
public string data;
}
public class OnShowClass
{
public string app_id;
public string open_id;
public int event_type;
public string app_version;
public string sub_app_id;
public long ts;
public string os;
public int pay_amount;
public string union_id;
public string other;
public string request_id;
}