2014년 5월 4일 일요일

티스토어(T-Store) 인앱 영수증 검증 C#버전(t-store in-app purchase receipt verification)

 던전워즈
c#으로 작성된 서버 또는 유니티 등에서 티스토어의 인앱 결제 영수증 검증을 다음과 같이 하면된다.

유니티에서 사용하고자 하는경우 아래 코드를 추가하시고
InitCertificateValidationCallback()을 영수증 검증 루틴 실행전 한번 호출해줄필요가 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 
public static class COneStoreReceiptVerify
{
    public static void InitCertificateValidationCallback()
        {
            ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
        }
 
        static bool ValidateServerCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
 
//----------------------------------------------------------
readonly string TSTORE_RECEIPT_VERIFY_URL_PRODUCT = "https://iap.tstore.co.kr/digitalsignconfirm.iap";  //실서비스용서버주소
        readonly string TSTORE_RECEIPT_VERIFY_URL_DEV = "https://iapdev.tstore.co.kr/digitalsignconfirm.iap"//개발용서버주소
        readonly string TSTORE_RECEIPT_VERIFY_FORMAT = "{{\"txid\" : \"{0}\", \"appid\" : \"{1}\", \"signdata\" : \"{2}\"}}";
        //TStore 영수증 검중구현, 
//bProduct : 서비스/개발용 서버중 어느 서버에서 검증할지
//strAppID : 티스토어에 배포한 프로그램에 할당받은 ID
//strTXID : 인앱 결제후 결과 값으로 넘어온 것중 txid
//strReceipt : 인앱결제 후 결과 값으로 넘어온 영수증 정보
//리턴값이 true인경우 영수증검증에 성공한것이다.
//애플의 영수증을 검증하는 것처럼 먼저 bProduct에 true를 해서 영수증검증을 해보고 만일 실패 하는경우
//false를 줘서 검증을 시도하도록 하면 된다.
        bool VerifyTStoreReceipt(bool bProduct, string strAppID, string strTXID, string strReceipt)
        {
            try
            {
                // Verify the receipt with Apple
                string postString = String.Format(TSTORE_RECEIPT_VERIFY_FORMAT, strTXID, strAppID, strReceipt);
                ASCIIEncoding ascii = new ASCIIEncoding();
                byte[] postBytes = ascii.GetBytes(postString);
                HttpWebRequest request;
 
 
                if (bProduct)
                    request = WebRequest.Create(TSTORE_RECEIPT_VERIFY_URL_PRODUCT) as HttpWebRequest;
                else
                    request = WebRequest.Create(TSTORE_RECEIPT_VERIFY_URL_DEV) as HttpWebRequest;
 
                request.Method = "POST";
                request.ContentType = "application/json";
                request.ContentLength = postBytes.Length;
                Stream postStream = request.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Close();
 
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                StringBuilder sb = new StringBuilder();
                byte[] buf = new byte[8192];
                Stream resStream = response.GetResponseStream();
                string tempString = null;
 
                int count = 0;
 
                do
                {
                    count = resStream.Read(buf, 0, buf.Length);
                    if (count != 0)
                    {
                        tempString = Encoding.ASCII.GetString(buf, 0, count);
                        sb.Append(tempString);
                    }
                } while (count > 0);
 
                var fd = JObject.Parse(sb.ToString());
 
                try
                {
                    resStream.Close();
                    response.Close();
                }
                catch
                {
                }
 
                string strResult = fd["status"].ToString();
                string strDetail = fd["detail"].ToString();
 
                // Receipt not valid
                if (strResult == null || strResult != "0" || strDetail == null || strDetail != "0000")
                {
#if DEBUG
                    CLogMng.Instance().Debug("tstore purchase failed : " + sb.ToString());
                    CLogMng.Instance().Debug("txid : " + strTXID);
                    CLogMng.Instance().Debug("signdata : " + strReceipt);
#endif
                    return false;
                }
            }
            catch// (Exception ex)
            {
                // We crashed and burned -- do something intelligent
                return false;
            }
 
            return true;
        }
}
cs

댓글 없음:

댓글 쓰기