Unity3D Integration MobileCharging TrueDigitalPlus

 이번 포스팅은 중국에 이어 태국의 최대 통신사인지 방송사인지 암튼 True 그룹의 계열사인 트루 디지털 플러스(True Digital Plus)와 유니티3D 연동을 정리 해보겠습니다. 트루와 연동은 웹이 주된 작업이라 안드로이드 플러그인 작업은 없습니다. 첫번째로 모바일 충전 처리에 대한 내용입니다.


1. 트루 모바일 충전 요청

 먼저 유니티3D에서 트루에 POST로 충전 요청하는 내용입니다.

public class TrueManager : MonoBehaviour
{
static TrueManager _instance;
public string strLogMsg = "Unity3D Android True Digital Plus Test";
private string AppID = "1";
        private string SecretKey = "ababababababababababababababab";

public static TrueManager GetInstance()
{
if( _instance == null )
{
_instance = new GameObject("TrueManager").AddComponent<TrueManager>();
}

return _instance;
}
}
 먼저 TrueManager 컴포넌트 기본부분입니다. <> 때문에 신텍스하이라이터 오류가 발생해서 따로 빼봤습니다. AppID와 SecretKey는 트루로부터 할당 받습니다.
 
 public void RequestCharging(int amount)
 {
  string TDP_URL = "http://smart.gg.in.th/MobileCharging/ProfileAuthen.aspx";
  string EncryptSecret = Md5Sum(AppID+SecretKey);
  string DeviceID = SystemInfo.deviceUniqueIdentifier;
  string MobileAccountID = "12341234";

  string strUrl = string.Format("{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}&{9}={10}"
   , TDP_URL
   , "AppID", this.AppID
   , "EncryptSecret", this.EncryptSecret
   , "DeviceID", this.DeviceID
   , "MobileAccountID", this.MobileAccountID
   , "Price", amount);
  
  Debug.Log(strUrl);
  Application.OpenURL(strUrl);
 }
  
 public string Md5Sum(string strToEncrypt)
 {
  System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
  byte[] bytes = ue.GetBytes(strToEncrypt);
  
  // encrypt bytes
  System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  byte[] hashBytes = md5.ComputeHash(bytes);
  
  // Convert the encrypted bytes back to a string (base 16)
  string hashString = "";
  
  for (int i = 0; i < hashBytes.Length; i++)
  {
   hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
  }
  
  return hashString.PadLeft(32, '0');
 }
 RequestCharging을 통해서 TDP에서 준비한 웹페이지를 GET 방식으로 호출합니다. EncryptSecret에는 앱ID와 시크릿키값을 사용해서 MD5 암호화 해줍니다. DeviceID에는 유니티에서 지원하는 SystemInfo.deviceUniqueIdentifier을 사용해서 디바이스의 UDID값으로 지정합니다. 마지막으로 MobileAccountID에는 해당 게임 유저의 ID를 지정하고 Price에는 25, 50, 90 중에서 하나를 넘겨준 후 WWW를 사용하지 않고 역시나 유니티에서 지원하는 브라우저를 호출해주는 Application.OpenURL을 통해서 브라우저에서 백 버튼 이슈 때문에 웹뷰(WebView)를 통해서 트루의 충전페이지를 호출합니다.

 안드로이드 태블릿에서 충전을 요청한 결과입니다.


2. 모바일 충전 WCF 웹 서비스

 위에서 충전 요청에 대한 결과를 트루로부터 받을 웹서비스를 준비 해야합니다. 기존에 정리했던 WCF 내용을 바탕으로 아래와 같은 Charging 서비스를 만듭니다. 꼭 WCF가 아니더라도 ASP.NET이나 C#의 System.Web.UI.Page등 POST를 처리할 수 있게 구현하면 되는 것 같습니다. 아니면 PHP나 ASP, JAVA도 가능하겠습니다.

namespace Charging
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        // TODO: Add your service operations here
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        string ResponseCharging(string TranID, string MobileAccountID, decimal Price
            , string ResultCode, string ResultMessage);
    }
}
 WCF 서비스의 인터페이스 부분입니다.

namespace Charging
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        /// 
        /// Partner will prepare web service or function for TDP response call back form payment.
        /// 
        /// Transaction ID
        /// Mobile Account ID / User ID
        /// Money or amount charged Select In:[25,50,90]
        /// Result Code
        /// Result Message
        /// 
        public string ResponseCharging(string TranID, string MobileAccountID, decimal Price
            , string ResultCode, string ResultMessage)
        {
            string strResult = string.Empty;

            if (ResultCode == "200")
            {
                // Success
                strResult = "200";
            }
            else
            {
                strResult = "500";
            }

            return strResult;
            /*
             * Output Parameters 
                oResult  String 
                200  :   Success 
 
                Else  :  Error Message
             */
        }
    }
}
 서비스 구현부입니다. 샘플이라 간단히 바로 결과를 리턴해주게 되어있는데 실제로는 DB와의 연동등도 필요 하겠습니다. 이부분은 제가 서버 개발자가 아니다보니 이렇게 마무리합니다.

  다음에는 유니티3D와 트루 모바일 플러스의 선결제(Prepaid) 연동을 정리 해보겠습니다.

댓글

이 블로그의 인기 게시물

'xxx.exe' 프로그램을 시작할 수 없습니다. 지정된 파일을 찾을 수 없습니다.

goorm IDE에서 node.js 프로젝트로 Hello World Simple Server 만들어 띄워보기

애드센스 수익을 웨스턴 유니온으로 수표대신 현금으로 지급 받아보자.