@quenty/receiptprocessing 1.1.0-canary.4b70f6d.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/LICENSE.md +21 -0
- package/README.md +23 -0
- package/default.project.json +6 -0
- package/package.json +39 -0
- package/src/Server/ReceiptProcessingService.lua +120 -0
- package/src/node_modules.project.json +7 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 1.1.0-canary.4b70f6d.0 (2023-04-20)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Add ReceiptProcessing ([6590e0f](https://github.com/Quenty/NevermoreEngine/commit/6590e0fbb091956f903ddebc3110e4f86ec020d1))
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2023 James Onnen (Quenty)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
## ReceiptProcessing
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<a href="http://quenty.github.io/NevermoreEngine/">
|
|
5
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://discord.gg/mhtGUS8">
|
|
8
|
+
<img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/Quenty/NevermoreEngine/actions">
|
|
11
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
|
|
12
|
+
</a>
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
Centralize receipt processing within games since this is a constrained resource.
|
|
16
|
+
|
|
17
|
+
<div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/ReceiptProcessingUtils">View docs →</a></div>
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
npm install @quenty/receiptprocessing --save
|
|
23
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quenty/receiptprocessing",
|
|
3
|
+
"version": "1.1.0-canary.4b70f6d.0",
|
|
4
|
+
"description": "Centralize receipt processing within games since this is a constrained resource.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Roblox",
|
|
7
|
+
"Nevermore",
|
|
8
|
+
"Lua",
|
|
9
|
+
"receiptprocessing"
|
|
10
|
+
],
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/Quenty/NevermoreEngine/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/Quenty/NevermoreEngine.git",
|
|
17
|
+
"directory": "src/receiptprocessing/"
|
|
18
|
+
},
|
|
19
|
+
"funding": {
|
|
20
|
+
"type": "patreon",
|
|
21
|
+
"url": "https://www.patreon.com/quenty"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"contributors": [
|
|
25
|
+
"Quenty"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@quenty/enumutils": "3.1.0",
|
|
29
|
+
"@quenty/loader": "6.2.1",
|
|
30
|
+
"@quenty/maid": "2.5.0",
|
|
31
|
+
"@quenty/promise": "6.5.0",
|
|
32
|
+
"@quenty/servicebag": "6.6.1",
|
|
33
|
+
"@quenty/signal": "2.3.0"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"gitHead": "4b70f6d72cee9393c70b1ce17e78d8e63771ac3c"
|
|
39
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
--[=[
|
|
2
|
+
Centralize receipt processing within games since this is a constrained resource.
|
|
3
|
+
|
|
4
|
+
@class ReceiptProcessingService
|
|
5
|
+
]=]
|
|
6
|
+
|
|
7
|
+
local require = require(script.Parent.loader).load(script)
|
|
8
|
+
|
|
9
|
+
local MarketplaceService = game:GetService("MarketplaceService")
|
|
10
|
+
|
|
11
|
+
local Promise = require("Promise")
|
|
12
|
+
local EnumUtils = require("EnumUtils")
|
|
13
|
+
local Signal = require("Signal")
|
|
14
|
+
local Maid = require("Maid")
|
|
15
|
+
|
|
16
|
+
local ReceiptProcessingService = {}
|
|
17
|
+
ReceiptProcessingService.ServiceName = "ReceiptProcessingService"
|
|
18
|
+
|
|
19
|
+
function ReceiptProcessingService:Init(serviceBag)
|
|
20
|
+
assert(not self._serviceBag, "Already initialized")
|
|
21
|
+
self._serviceBag = assert(serviceBag, "No serviceBag")
|
|
22
|
+
self._maid = Maid.new()
|
|
23
|
+
|
|
24
|
+
self.ReceiptCreated = Signal.new() -- :Fire(receiptInfo)
|
|
25
|
+
self._maid:GiveTask(self.ReceiptCreated)
|
|
26
|
+
|
|
27
|
+
self.ReceiptProcessed = Signal.new() -- :Fire(receiptInfo, productPurchaseDecision)
|
|
28
|
+
self._maid:GiveTask(self.ReceiptProcessed)
|
|
29
|
+
|
|
30
|
+
self._processors = {}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
function ReceiptProcessingService:Start()
|
|
34
|
+
MarketplaceService.ProcessReceipt = function(...)
|
|
35
|
+
return self:_handleProcessReceiptAsync(...)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
--[=[
|
|
40
|
+
Registers a new receipt processor. This works exactly like a normal receipt processor except it will also
|
|
41
|
+
take a Promise as a result (of which an error).
|
|
42
|
+
|
|
43
|
+
@param processor (receiptInfo) -> ProductPurchaseDecision | Promise<ProductPurchaseDecision> | nil
|
|
44
|
+
@param priority number
|
|
45
|
+
]=]
|
|
46
|
+
function ReceiptProcessingService:RegisterReceiptProcessor(processor, priority)
|
|
47
|
+
assert(self._processors, "Not initialized")
|
|
48
|
+
assert(type(processor) == "function", "Bad processor")
|
|
49
|
+
priority = priority or 0
|
|
50
|
+
|
|
51
|
+
local data = {
|
|
52
|
+
traceback = debug.traceback();
|
|
53
|
+
priority = priority;
|
|
54
|
+
timestamp = os.clock();
|
|
55
|
+
processor = processor;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
table.insert(self._processors, data)
|
|
59
|
+
table.sort(self._processors, function(a, b)
|
|
60
|
+
if a.priority ~= b.priority then
|
|
61
|
+
-- larger priority first
|
|
62
|
+
return a.priority > b.priority
|
|
63
|
+
else
|
|
64
|
+
-- earlier first
|
|
65
|
+
return a.timestamp < b.timestamp
|
|
66
|
+
end
|
|
67
|
+
end)
|
|
68
|
+
|
|
69
|
+
return function()
|
|
70
|
+
local index = table.find(self._handles, data)
|
|
71
|
+
if index then
|
|
72
|
+
table.remove(self._handles, data)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
function ReceiptProcessingService:_handleProcessReceiptAsync(receiptInfo)
|
|
78
|
+
if not self._processors then
|
|
79
|
+
warn("[ReceiptProcessingService._handleProcessReceiptAsync] - We're leaking memory. Receipt processing service is already cleaned up.")
|
|
80
|
+
return Enum.ProductPurchaseDecision.NotProcessedYet
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
self.ReceiptCreated:Fire(receiptInfo)
|
|
84
|
+
|
|
85
|
+
-- Chain of command this thing
|
|
86
|
+
for _, data in pairs(self._processors) do
|
|
87
|
+
local result = data.processor(receiptInfo)
|
|
88
|
+
|
|
89
|
+
-- Unpack pro
|
|
90
|
+
if Promise.isPromise(result) then
|
|
91
|
+
local ok, promiseResult = result:Yield()
|
|
92
|
+
if not ok then
|
|
93
|
+
warn(string.format("[ReceiptProcessingService._handleProcessReceiptAsync] - Promise failed with %q.\n%s", tostring(promiseResult), data.traceback))
|
|
94
|
+
continue
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
result = promiseResult
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
if EnumUtils.isOfType(Enum.ProductPurchaseDecision, result) then
|
|
101
|
+
self.ReceiptProcessed:Fire(receiptInfo, result)
|
|
102
|
+
return result
|
|
103
|
+
elseif result == nil then
|
|
104
|
+
continue
|
|
105
|
+
else
|
|
106
|
+
warn(string.format("[ReceiptProcessingService._handleProcessReceiptAsync] - Got unexpected result of type %q from receiptInfo.\n%s", typeof(result), data.traceback))
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
-- Retry in the future
|
|
111
|
+
self.ReceiptProcessed:Fire(receiptInfo, Enum.ProductPurchaseDecision.NotProcessedYet)
|
|
112
|
+
return Enum.ProductPurchaseDecision.NotProcessedYet
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
function ReceiptProcessingService:Destroy()
|
|
116
|
+
self._maid:DoCleaning()
|
|
117
|
+
self._processors = nil
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
return ReceiptProcessingService
|