-
Notifications
You must be signed in to change notification settings - Fork 5
/
validate.lua
163 lines (126 loc) · 4.77 KB
/
validate.lua
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
--====================================================================--
-- Module: Validation of Auto Renewable in-app Purchases
-- Author : Satheesh
--
-- License:
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in the
-- Software without restriction, including without limitation the rights to use, copy,
-- modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
-- and to permit persons to whom the Software is furnished to do so, subject to the
-- following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies
-- or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--
-- Overview:
--
-- This module can be used to verify and validate auto-renewable in-app purchases using Corona
--
--
-- Version : 1.0
--
--
-- Usage:
--
--
-- local validate = require "validate"
-- validate.start
-- {
-- receipt = "Your Receipt Here",
-- password = "Your shared secret key here",
-- listener = listener,
-- testing = true, --Should be true if you use sandbox receipt, false if you use actual receipt
--The following lines must be uncommented if you want your receipt to be verified by your server.
--The php for receipt verification is also included within the project
--[[
serverValidation = true,
serverLink = "Link of your php file"
--]]
--
--====================================================================--
--
local json = require "json"
local base64 = require "base64"
local errorMap =
{
["0"] = "No Error",
["21000"] = "The App Store could not read the JSON object you provided.",
["21002"] = "The data in the receipt-data property was malformed.",
["21003"] = "The receipt could not be authenticated.",
["21004"] = "The shared secret you provided does not match the shared secret on file for your account.",
["21005"] = "The receipt server is not currently available.",
["21006"] = "This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response.",
["21007"] = "This receipt is a sandbox receipt, but it was sent to the production service for verification.",
["21008"] = "This receipt is a production receipt, but it was sent to the sandbox service for verification.",
}
local function preparePostData(receipt,params)
local password = params.password
local serverValidation = params.serverValidation
--remove unwanted characters
receipt = receipt:sub(2,-2)
receipt = receipt:gsub(" ","")
--Convert to ascii
local ascii = ""
local l = receipt:len()
for i=1,l,2 do
local hex = receipt:sub(i,i+1)
local dec = tonumber(hex, 16)
if dec then
local char = string.char(dec)
ascii = ascii..char
end
end
--Encode to base 64
local b64encode = base64.encode(ascii)
if serverValidation then
--dont send password in case of server validation
return b64encode
end
--Convert to json
local jsn = json.encode
{
receipt_data = b64encode,
password = password,
}
jsn = jsn:gsub("receipt_data","receipt-data")
return jsn
end
local function start(params)
local listener = params.listener or function(event) end
local testing = params.testing
local link = testing and "https://sandbox.itunes.apple.com/verifyReceipt"
or "https://buy.itunes.apple.com/verifyReceipt"
local receipt = params.receipt or "<>"
local password = params.password or nil
local serverValidation = params.serverValidation
local serverLink = params.serverLink
local postData
if serverValidation then
postData = preparePostData(receipt,{serverValidation = true})
postData = "receipt="..postData.."&testing="..(testing and 1 or 0)
link = serverLink
else
postData = preparePostData(receipt,{password = password})
end
local function localListener(event)
local response = event.response
local decoded = json.decode(response)
if type(decoded) == "table" then
event.iTunes_StatusCode = decoded.status
event.iTunes_Response = decoded
event.iTunes_StatusCodeDescription = errorMap[tostring(event.iTunes_StatusCode)]
end
listener(event)
end
network.request(link,"POST",localListener,{body=postData})
end
return {start = start}