当前位置: 首页 > 工具软件 > PaySDK > 使用案例 >

PaySDK

安轶
2023-12-01
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using LitJson;

// void weixinpay(String productName ,String price,String outTradNo ,String notify_url)
public class PayUIViewMediator : BaseMediator
{
    class ObjectData
    {
        public string productName;
        public string price;
        public string outTradNo;
        public string notify_url;
    }

    public static string NAME = "PayUIViewMediator";

    public PayUIViewMediator (string mediatorName, object viewComponent) : base (mediatorName, viewComponent)
    {

        UIEventListener.Get (GetPayUIView ().closeBtn.gameObject).onClick = NoHandler;
        UIEventListener.Get (GetPayUIView ().messagePay.gameObject).onClick = MessageHandler;
        UIEventListener.Get (GetPayUIView ().weChatPay.gameObject).onClick = WeiXinHandler;
        UIEventListener.Get (GetPayUIView ().zhiFuBaoPay.gameObject).onClick = ZhiFuBaoHandler;
    }

    public PayUIView GetPayUIView ()
    {
        return ViewComponent as PayUIView;
    }

    void InitClearPayPlatform ()
    {
        SDKPlatformFactory.GetInstance ().platformType = (int)PayPlatformType.None;

        WeiXinSDKPlatform weChat = SDKPlatformFactory.GetInstance ().GetSDKPlatform ().gameObject.GetComponent<WeiXinSDKPlatform> ();
        if (weChat != null) {
            GameObject.Destroy (weChat);
        }

        ZhiFuBaoSDKPlatform zhufubao = SDKPlatformFactory.GetInstance ().GetSDKPlatform ().gameObject.GetComponent<ZhiFuBaoSDKPlatform> ();
        if (zhufubao != null) {
            GameObject.Destroy (zhufubao);
        }
    }
    //返回该 Mediator 对象所关心的所有 Notification    ,消息处理中心SendNotification()
    public override IList<string> ListNotificationInterests ()
    {
        //处理服务器返回回来的处理函数
//      AddToHandlerFunList (LoginNotification.BuyCornResponse, YesResponseHandler);
//      AddToHandlerFunList (GetMainViewNotification.BuyCorn, BuyCornResponseHandler);
        AddToHandlerFunList (GetMainViewNotification.BuyDiamond, BuyDiamondResponseHandler);
        AddToHandlerFunList (GameNotification.DeletePayUI, DeletePayUIHandler);

        return new List<string> (HandlerFun.Keys);
    }

    private void NoHandler (GameObject gb)
    {
        AudioMngr.GetInstance ().PlayEffect (AudioConfig.ui_menu_button_01);
        DestroySelf ();
    }

    void DestroySelf ()
    {
        if (UserInfos.GetInstance ().isClickPayBtn) {
            InitClearPayPlatform ();
        }
        UserInfos.GetInstance ().isClickPayBtn = false;
        GetPayUIView ().DestorySelf ();
    }

    /// <summary>
    /// 在游戏中成功购买钻石之后,删除此窗口
    /// </summary>
    void DeletePayUIHandler (object ob)
    {
        DestroySelf ();
    }

    public string orderNo = null;

    void BuyDiamondResponseHandler (object ob)
    {

        BuyDiamondResponse result = ob as BuyDiamondResponse;

        if (result.code == 200) {
            CommonMesgTips.GetInstance ().ShowTipsMsg ("成功返回订单号!");
            GLogger.LogError (">>>>>>>>>>>>>订单号>>>>>>>>>>>>>>>>>>>>: " + result.tradeNo);
            orderNo = result.tradeNo;
            if (isWeChat) {
                isWeChat = false;
                UserInfos.GetInstance ().isClickPayBtn = true;

                SDKPlatformFactory.GetInstance ().platformType = (int)PayPlatformType.WeChat;
                JsonData weChatData = GetJsonData (result.tradeNo, PayPlatformType.WeChat);

                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().SDKInit ();
                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().rechargeFinishCallback = payCallBack;
                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().SDKPay (weChatData);

            } else if (isZhiFuBao) {
                isZhiFuBao = false;
                UserInfos.GetInstance ().isClickPayBtn = true;

                SDKPlatformFactory.GetInstance ().platformType = (int)PayPlatformType.ZhiFuBao;
                JsonData zhiFuBaoData = GetJsonData (result.tradeNo, PayPlatformType.ZhiFuBao);

                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().SDKInit ();
                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().rechargeFinishCallback = payCallBack;
                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().SDKPay (zhiFuBaoData);

            } else if (isMessage) {
                isMessage = false;
                UserInfos.GetInstance ().isClickPayBtn = true;

                SDKPlatformFactory.GetInstance ().platformType = (int)PayPlatformType.Message;
                JsonData messageData = GetJsonData (result.tradeNo, PayPlatformType.Message);

                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().SDKInit ();
                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().rechargeFinishCallback = payCallBack;
                SDKPlatformFactory.GetInstance ().GetSDKPlatform ().SDKPay (messageData);

            }

        } else {
            CommonMesgTips.GetInstance ().ShowTipsMsg ("获取订单号失败。。。。。。!");
        }

        //      if (TempValue.GetInstance ().isBuyDiamondInGameScene) {
        //          TempValue.GetInstance ().isBuyDiamondInGameScene = false;
        //          SendNotification (GameNotification.BuyLifeAfterBuyDiamond, ob);
        //      } else {
        //          CommonMesgTips.GetInstance ().ShowTipsMsg (string.Format("{0}{1}{2}",CNString.BUYSUCCESS,CNString.DiamongPlus,UserInfos.GetInstance().buyedReturnDaimondCount));
        //      }
        //      GetBuyWarningTipsView ().DestorySelf ();
    }

    bool isWeChat = false;

    void WeiXinHandler (GameObject gb)
    {
        if (GetPayUIView ().isClick) {
            BuyDiamondRequest vo = new BuyDiamondRequest ();
            vo.shopId = GetPayUIView ().shopId; 
            vo.buyType = GetPayUIView ().buyType;
            SendNotification (BuyDiamondCommand.Name, vo);
            isWeChat = true;
            isZhiFuBao = false;
            isMessage = false;
            GetPayUIView ().isClick = false;
            GLogger.LogError (">>>>>>>>>>>>>>>>>>点击微信支付>>>>>>>>>>>>>>");
//          GetPayUIView ().WaitSecondsAfterClick ();
        }

    }

    bool isZhiFuBao = false;

    void ZhiFuBaoHandler (GameObject gb)
    {
        if (GetPayUIView ().isClick) {
            BuyDiamondRequest vo = new BuyDiamondRequest ();
            vo.shopId = GetPayUIView ().shopId; 
            vo.buyType = GetPayUIView ().buyType;
            SendNotification (BuyDiamondCommand.Name, vo);
            isZhiFuBao = true;
            isWeChat = false;
            isMessage = false;
            GetPayUIView ().isClick = false;
            GLogger.LogError (">>>>>>>>>>>>>>>>>>点击支付宝支付>>>>>>>>>>>>>>");
//          GetPayUIView ().WaitSecondsAfterClick ();
        }
    }

    bool isMessage = false;

    void MessageHandler (GameObject ob)
    {
//      if (GetPayUIView ().isClick) {

//          if (Application.loadedLevel == (int)GameLevel.Game) {
//              SendNotification (GameNotification.BuyLifeAfterBuyDiamond, GetPayUIView ());
//          }

            CommonMesgTips.GetInstance ().ShowTipsMsg ("功能暂未开放!");

//          BuyDiamondRequest vo = new BuyDiamondRequest ();
//          vo.shopId = GetPayUIView ().shopId; 
//          SendNotification (BuyDiamondCommand.Name, vo);
//          isMessage = true;
//          isZhiFuBao = false;
//          isWeChat = false;

//          GetPayUIView ().isClick = false;
            GLogger.LogError (">>>>>>>>>>>>>>>>>>点击短信支付>>>>>>>>>>>>>>");


//      }
    }

    JsonData GetJsonData (string tradeNo, PayPlatformType type)
    {
        JsonData data;
        ObjectData shopInfo = new ObjectData ();
        shopInfo.productName = GetPayUIView ().goodsDescribe;
        shopInfo.price = GetPayUIView ().diamongInfo.RMB.ToString ();
        shopInfo.outTradNo = tradeNo;

        switch (type) {
        case PayPlatformType.WeChat:
            shopInfo.notify_url = CNString2.WeChat_PAY_URL;
            break;
        case PayPlatformType.ZhiFuBao:
            shopInfo.notify_url = CNString2.ZhiFuBao_PAY_URL;
            break;
        case PayPlatformType.Message:
            shopInfo.notify_url = CNString2.Message_PAY_URL;
            break;
        default:
            break;
        }

        data = JsonMapper.ToObject (JsonMapper.ToJson (shopInfo));
        return data;
    }
    //code    msg
    void payCallBack (object value)
    {
        string result = value as string;
        if (result == "true") {
            CommonMesgTips.GetInstance ().ShowTipsMsg ("等待验证!");
            GLogger.LogError (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>等待验证!!!!!!!!!!!!!!!!!!!");
            string validateUrl = CNString2.Validate_PAY_URL + "?orderNo=" + orderNo;
//          string validateUrl = string.Format("{0}{1}{2}",CNString2.Validate_PAY_URL,"?orderNo=",orderNo);
            GetPayUIView ().ValidateOrderNo (validateUrl);
            GetPayUIView ().paySuccessQequest = SendSuccessRequest;

        } else {
            CommonMesgTips.GetInstance ().ShowTipsMsg ("充值失败!");
//          DestroySelf ();
            if (UserInfos.GetInstance ().isClickPayBtn) {
                InitClearPayPlatform ();
            }
            UserInfos.GetInstance ().isClickPayBtn = false;
            GetPayUIView ().isClick = true;
        }

    }

    void SendSuccessRequest ()
    {
        BuySuccussRequest request = new BuySuccussRequest ();
        request.shopId = GetPayUIView ().shopId;
        request.buyType = GetPayUIView ().buyType;
        SendNotification (BuyDiamondSuccessCommand.Name, request);
        if (Application.loadedLevel == (int)GameLevel.Game) {
            SendNotification (GameNotification.BuyLifeAfterBuyDiamond, GetPayUIView ());
        } 
        GetPayUIView ().isClick = true;
        DestroySelf ();
    }
}



public void ValidateOrderNo(string url){
        isValidate = true;
        ur = url;
        timesCount = 0;
        sumTime = 1; 
    }

    bool isValidate = false;
    string ur = null;
    float sumTime = 1;
    int timesCount = 0;
    void Update(){
        if (timesCount >= 7) {
            CommonMesgTips.GetInstance ().ShowTipsMsg ("充值失败!");
            isValidate = false;
        } else {
            if(isValidate){
                sumTime -= Time.deltaTime;
                if(sumTime < 0){
                    StartCoroutine (Get(ur));
                    sumTime = 1;
                    ++timesCount;
                }
            }
        }
    }


    IEnumerator Get (string url)
    {
        WWW request = new WWW (url);
        yield return request;
        if (request.error != null) {
            GLogger.LogError ("验证错误:" + request.error);
        } else {
            JsonData jd = JsonMapper.ToObject (request.text);
            int code = (int)jd ["code"];
            string msg = jd ["msg"].ToString();
            if (code == 200) {
                CommonMesgTips.GetInstance ().ShowTipsMsg ("购买成功!");
                if(paySuccessQequest != null){
                    isValidate = false;
                    paySuccessQequest ();
                }
            } else {
                isValidate = true;
            }

        }
    }

相关阅读

相关文章

相关问答