@seayoo-io/combo-sdk-node 1.9.0 → 1.11.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/README.md +44 -640
- package/dist/index.cjs +21 -2
- package/dist/index.d.cts +16 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +21 -3
- package/docs/api-client.md +224 -0
- package/docs/gm.md +153 -0
- package/docs/notify.md +155 -0
- package/docs/utils.md +74 -0
- package/docs/verify.md +122 -0
- package/package.json +1 -1
package/docs/notify.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
> [Combo SDK for Node.js](../README.md) / Notify
|
|
2
|
+
|
|
3
|
+
# Notify
|
|
4
|
+
|
|
5
|
+
世游服务端目前会推送以下几种通知:
|
|
6
|
+
|
|
7
|
+
| NotificationType | 说明 | payload |
|
|
8
|
+
| ---------------- | ---------------------------------------------------------- | ----------------------- |
|
|
9
|
+
| `ShipOrder` | 订单状态变更为已支付时推送,游戏侧据此发货 | `ShipOrderNotification` |
|
|
10
|
+
| `Refund` | 订单发生退款时推送 | `RefundNotification` |
|
|
11
|
+
| `DataTags` | 世游服务端批量推送约定好的数据标签,游戏侧应将其持久化存储 | `DataTagsNotification` |
|
|
12
|
+
|
|
13
|
+
> 通知处理函数正常返回(`void` 或 `Promise<void>`)表示处理成功;如果处理过程中出现错误,应当 `throw Error`,世游服务端会在稍后重试推送。
|
|
14
|
+
|
|
15
|
+
## Step 1 准备参数
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { NotificationType, Endpoint } from "@seayoo-io/combo-sdk-node"
|
|
19
|
+
|
|
20
|
+
// 1.1 定义消息处理函数,类型定义参见源码 NotificationHandler
|
|
21
|
+
function notificationHandler(type, payload) {
|
|
22
|
+
switch(type) {
|
|
23
|
+
case NotificationType.ShipOrder:
|
|
24
|
+
// Do your work with payload:定义见 ShipOrderNotification
|
|
25
|
+
break;
|
|
26
|
+
case NotificationType.Refund:
|
|
27
|
+
// Do your work with payload:定义见 RefundNotification
|
|
28
|
+
break;
|
|
29
|
+
case NotificationType.DataTags:
|
|
30
|
+
// Do your work with payload:定义见 DataTagsNotification
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** ShipOrderNotification 是订单发货通知的数据结构,包含了订单的详细信息 */
|
|
36
|
+
interface ShipOrderNotification {
|
|
37
|
+
/** 世游服务端创建的,标识订单的唯一 ID */
|
|
38
|
+
order_id: string
|
|
39
|
+
/** 游戏侧用于标识创建订单请求的唯一 ID */
|
|
40
|
+
reference_id: string
|
|
41
|
+
/** 发起购买的用户的唯一标识 */
|
|
42
|
+
combo_id: string
|
|
43
|
+
/** 购买的商品 ID,指的是在世游发行平台管理的商品 ID */
|
|
44
|
+
product_id: string
|
|
45
|
+
/** 购买的商品的数量 */
|
|
46
|
+
quantity: number
|
|
47
|
+
/** 订单币种代码。例如 USD CNY */
|
|
48
|
+
currency: string
|
|
49
|
+
/** 订单金额,单位为分,如果币种为美元,则单位为美分。 */
|
|
50
|
+
amount: number
|
|
51
|
+
/** 游戏侧创建订单时提供的订单上下文,透传回游戏 */
|
|
52
|
+
context?: string
|
|
53
|
+
/**
|
|
54
|
+
* 是否是沙盒订单。沙盒订单意味着此订单并未产生真实的付款。
|
|
55
|
+
* 预期此字段仅用于记录日志和数据埋点。无论是否是沙盒订单,游戏侧都应当发货。
|
|
56
|
+
*/
|
|
57
|
+
is_sandbox?: boolean
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface RefundNotification {
|
|
61
|
+
// 结构同 ShipOrderNotification
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** DataTag 表示单条数据标签,即某个数据实体的某个标签 */
|
|
65
|
+
interface DataTag {
|
|
66
|
+
/** 标签所属实体的类型。例如 `role` 代表实体类型为游戏角色 */
|
|
67
|
+
entity_type: string
|
|
68
|
+
/** 标签所属实体的唯一标识 */
|
|
69
|
+
entity_id: string
|
|
70
|
+
/** 标签名称 */
|
|
71
|
+
tag_name: string
|
|
72
|
+
/** 标签值 */
|
|
73
|
+
tag_value: string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** DataTagsNotification 是数据标签通知的数据结构,包含一批数据标签 */
|
|
77
|
+
interface DataTagsNotification {
|
|
78
|
+
/** 一批数据标签,数组长度不定 */
|
|
79
|
+
tags: DataTag[]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 1.2 创建配置,类型定义参见源码 SDKBaseConfig
|
|
83
|
+
const config = {
|
|
84
|
+
game: "<GameId>",
|
|
85
|
+
secret: "<SecretKey>",
|
|
86
|
+
endpoint: Endpoint.China
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Step 2 处理通知
|
|
91
|
+
|
|
92
|
+
SDK 提供两种对接方式,推荐使用方式二。
|
|
93
|
+
|
|
94
|
+
### 方式一 使用 RequestHandler
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
// 使用 http 模块的处理函数
|
|
98
|
+
import { getNotificationHandler } from "@seayoo-io/combo-sdk-node"
|
|
99
|
+
const notifyHandler = getNotificationHandler(config, notificationHandler)
|
|
100
|
+
http.createServer(async function (req, res) {
|
|
101
|
+
if (req.path === "<YourNotifyUrl>" && req.method === "POST") {
|
|
102
|
+
await notifyHandler(req, res)
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// 或,使用 Express 处理函数
|
|
107
|
+
import { getNotificationHandlerForExpress } from "@seayoo-io/combo-sdk-node"
|
|
108
|
+
const expressHandler = getNotificationHandlerForExpress(config, notificationHandler)
|
|
109
|
+
app.post("/path/to/your/notify/url", expressHandler)
|
|
110
|
+
|
|
111
|
+
// 或,使用 Koa 处理函数
|
|
112
|
+
import { getNotificationHandlerForKoa } from "@seayoo-io/combo-sdk-node"
|
|
113
|
+
const koaHandler = getNotificationHandlerForKoa(config, notificationHandler)
|
|
114
|
+
app.post("/path/to/your/notify/url", koaHandler)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
> ⚠️ 注意事项
|
|
118
|
+
>
|
|
119
|
+
> 由于 http request 的 body 只能被消费(读取)一次,如果您使用了 express 或 koa 框架并优先加载了 bodyParse 类似的插件,那么 NotificationHandler 将无法再次读取 request 内容用于签名计算,此时需要提供请求的原始 rawBody 才可以继续。
|
|
120
|
+
>
|
|
121
|
+
> 网上有一些方法可以在 express 或 koa 框架下获取 rawBody 内容,但都具有一定的破坏性风险。
|
|
122
|
+
>
|
|
123
|
+
> 推荐以下解决方案:
|
|
124
|
+
|
|
125
|
+
### 方式二 使用 RequestMiddleware(推荐)
|
|
126
|
+
|
|
127
|
+
针对 express 或 koa 框架,可以创建一个高优先级的插件来处理回调通知,其原理如下
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
// 插件需要先于 bodyParse 类似的插件执行,以确保可以获取原始请求的内容
|
|
131
|
+
app.use(function (req, res, next) {
|
|
132
|
+
// 在其他插件处理之前,检查是否为通知请求的 url,方法是否为 POST
|
|
133
|
+
if (req.method === "POST" && req.path === "/path/to/your/notify/url") {
|
|
134
|
+
// 如果是,则交由插件处理后续响应
|
|
135
|
+
notifyMiddleware(req, res)
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
// 否则其他一切正常
|
|
139
|
+
next()
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
SDK 提供了两个插件来处理:
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
// express 插件
|
|
147
|
+
import { getNotificationMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
|
|
148
|
+
const notifyMiddleware = getNotificationMiddlewareForExpress("/path/to/your/notify/url", config, notificationHandler)
|
|
149
|
+
app.use(notifyMiddleware)
|
|
150
|
+
|
|
151
|
+
// koa 插件
|
|
152
|
+
import { getNotificationMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
|
|
153
|
+
const notifyMiddleware = getNotificationMiddlewareForKoa("/path/to/your/notify/url", config, notificationHandler)
|
|
154
|
+
app.use(notifyMiddleware)
|
|
155
|
+
```
|
package/docs/utils.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
> [Combo SDK for Node.js](../README.md) / Utils
|
|
2
|
+
|
|
3
|
+
# Utils
|
|
4
|
+
|
|
5
|
+
## genSessionID
|
|
6
|
+
|
|
7
|
+
生成用于标记用户上下线的 SessionID。
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import { genSessionID } from "@seayoo-io/combo-sdk-node"
|
|
11
|
+
|
|
12
|
+
const sessionID = genSessionID(combo_id)
|
|
13
|
+
// 32 位小写十六进制字符串
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## mayBeComboId
|
|
17
|
+
|
|
18
|
+
检查一个值是否为 Combo ID 的格式,仅做格式检查,不检查该 ID 是否真实存在。
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { mayBeComboId } from "@seayoo-io/combo-sdk-node"
|
|
22
|
+
|
|
23
|
+
mayBeComboId("1260418509000005") // true
|
|
24
|
+
mayBeComboId("95231223346130001") // true
|
|
25
|
+
mayBeComboId("1260418509000000") // false
|
|
26
|
+
mayBeComboId(1260418509000005) // false,只接受字符串
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## parseAuthorizationHeader
|
|
30
|
+
|
|
31
|
+
解析 Authorization 请求头,返回 `{ Game, Timestamp, Signature }`,格式不正确时返回 `null`。
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { parseAuthorizationHeader } from "@seayoo-io/combo-sdk-node"
|
|
35
|
+
|
|
36
|
+
const info = parseAuthorizationHeader(authString)
|
|
37
|
+
if (!info) {
|
|
38
|
+
// 请求头格式不正确
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## calcAuthorizationHeader
|
|
43
|
+
|
|
44
|
+
计算 HTTP 请求的 Authorization 请求头。
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import { calcAuthorizationHeader } from "@seayoo-io/combo-sdk-node"
|
|
48
|
+
|
|
49
|
+
const authString = calcAuthorizationHeader({
|
|
50
|
+
game: "<GameId>",
|
|
51
|
+
secret: "<SecretKey>",
|
|
52
|
+
endpoint: "https://api.seayoo.com",
|
|
53
|
+
method: "POST",
|
|
54
|
+
url: "/path/to/api",
|
|
55
|
+
data: { combo_id: "<ComboID>" },
|
|
56
|
+
})
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## checkHttpAuthInfo
|
|
60
|
+
|
|
61
|
+
校验 HTTP 请求的签名信息是否正确,签名不合法或时间戳超出允许范围时返回 `false`。
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { checkHttpAuthInfo } from "@seayoo-io/combo-sdk-node"
|
|
65
|
+
|
|
66
|
+
const ok = checkHttpAuthInfo(authString, {
|
|
67
|
+
// 省略 game 时不校验签发方
|
|
68
|
+
secret: "<SecretKey>",
|
|
69
|
+
endpoint: "https://api.seayoo.com",
|
|
70
|
+
method: "POST",
|
|
71
|
+
url: "/path/to/api",
|
|
72
|
+
data: { combo_id: "<ComboID>" },
|
|
73
|
+
})
|
|
74
|
+
```
|
package/docs/verify.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
> [Combo SDK for Node.js](../README.md) / Verify
|
|
2
|
+
|
|
3
|
+
# Verify
|
|
4
|
+
|
|
5
|
+
## 初始化
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { TokenVerifier, Endpoint } from "@seayoo-io/combo-sdk-node"
|
|
9
|
+
|
|
10
|
+
const verifier = new TokenVerifier({
|
|
11
|
+
game: "<GameId>",
|
|
12
|
+
secret: "<SecretKey>",
|
|
13
|
+
endpoint: Endpoint.China,
|
|
14
|
+
})
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## IdentityToken 验证
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { IdP } from "@seayoo-io/combo-sdk-node"
|
|
21
|
+
// verifyIdentityToken 对 IdentityToken 进行验证
|
|
22
|
+
// 返回 IdentityPayload 数据,如果解析出错则返回 Error 对象
|
|
23
|
+
const identityPayload = verifier.verifyIdentityToken(token)
|
|
24
|
+
if(identityPayload instanceof Error) {
|
|
25
|
+
console.error(result.message)
|
|
26
|
+
} else {
|
|
27
|
+
// WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏
|
|
28
|
+
if(identityPayload.idp === IdP.WebGL) {
|
|
29
|
+
// do something
|
|
30
|
+
}
|
|
31
|
+
// 游客登录判断
|
|
32
|
+
if(identityPayload.idp === IdP.Guest) {
|
|
33
|
+
// do something
|
|
34
|
+
}
|
|
35
|
+
// 更多 IdP 枚举可以查看源码定义 src/const.ts
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// IdentityPayload 数据定义
|
|
39
|
+
interface IdentityPayload {
|
|
40
|
+
/** combo_id 是世游分配的聚合用户 ID 游戏侧应当使用 combo_id 作为用户的唯一标识。*/
|
|
41
|
+
combo_id: string
|
|
42
|
+
/** IdP (Identity Provider) 是用户身份的提供者 */
|
|
43
|
+
idp: IdP
|
|
44
|
+
/**
|
|
45
|
+
* external_id 是用户在外部 IdP 中的唯一标识
|
|
46
|
+
*
|
|
47
|
+
* 例如:
|
|
48
|
+
* - 如果用户使用世游通行证登录,那么 external_id 就是用户的世游通行证 ID。
|
|
49
|
+
* - 如果用户使用 Google Account 登录,那么 external_id 就是用户在 Google 中的账号标识。
|
|
50
|
+
* - 如果用户使用微信登录,那么 external_id 就是用户在微信中的 OpenId。
|
|
51
|
+
*
|
|
52
|
+
* 注意:
|
|
53
|
+
* 游戏侧不应当使用 external_id 作为用户标识,但可以将 external_id 用于特定的业务逻辑。
|
|
54
|
+
*/
|
|
55
|
+
external_id: string
|
|
56
|
+
/** external_name 是用户在外部 IdP 中的名称,通常是用户的昵称 */
|
|
57
|
+
external_name: string
|
|
58
|
+
/**
|
|
59
|
+
* weixin_session_key 是用户在微信小游戏登录时,从微信服务端获得的会话密钥 session_key。
|
|
60
|
+
* 该字段在 Identity Token 中以 AES-256-GCM 加密存储,SDK 会自动解密。
|
|
61
|
+
* weixin_session_key 只在 IdP 为 MinigameWeixin 时才会有值。
|
|
62
|
+
*/
|
|
63
|
+
weixin_session_key: string
|
|
64
|
+
/**
|
|
65
|
+
* device_id 是用户在登录时使用的设备的唯一 ID。
|
|
66
|
+
*/
|
|
67
|
+
device_id: string
|
|
68
|
+
/**
|
|
69
|
+
* distro 是游戏客户端的发行版本标识。
|
|
70
|
+
* 游戏侧可将 distro 用于服务端数据埋点,以及特定的业务逻辑判断。
|
|
71
|
+
*/
|
|
72
|
+
distro: string
|
|
73
|
+
/**
|
|
74
|
+
* variant 是游戏客户端的分包标识。
|
|
75
|
+
* 游戏侧可将 variant 用于服务端数据埋点,以及特定的业务逻辑判断。
|
|
76
|
+
* 注意:Variant 只在客户端是分包时才会有值。当客户端不是分包的情况下,variant 为空字符串。
|
|
77
|
+
*/
|
|
78
|
+
variant: string
|
|
79
|
+
/**
|
|
80
|
+
* age 是根据用户的实名认证信息得到的年龄。
|
|
81
|
+
*
|
|
82
|
+
* 0 表示未知。
|
|
83
|
+
*
|
|
84
|
+
* 在某些特殊场景下,游戏侧可用 age 来自行处理防沉迷。
|
|
85
|
+
*
|
|
86
|
+
* 注意:age 不保证返回精确的年龄信息,仅保证用于防沉迷处理时的准确度够用。
|
|
87
|
+
*
|
|
88
|
+
* 例如:
|
|
89
|
+
*
|
|
90
|
+
* 当某个用户真实年龄为 35 岁时,age 可能返回 18,
|
|
91
|
+
* 当某个用户真实年龄为 17 岁时,age 可能返回 16。
|
|
92
|
+
*/
|
|
93
|
+
age: number
|
|
94
|
+
/**
|
|
95
|
+
* reg_time 是用户 ID(Combo ID)在的注册时间。Unix timestamp in seconds。
|
|
96
|
+
*/
|
|
97
|
+
reg_time: number
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## AdToken 验证
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
// verifyAdToken 对 AdToken 进行验证
|
|
105
|
+
// 返回 AdPayload 数据,如果解析出错则返回 Error 对象
|
|
106
|
+
const adInfo = verifier.verifyAdToken(token)
|
|
107
|
+
if(result instanceof Error) {
|
|
108
|
+
console.error(result.message)
|
|
109
|
+
} else {
|
|
110
|
+
// got ad info: { combo_id, placement_id, impression_id }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// AdPayload 定义
|
|
114
|
+
interface AdPayload {
|
|
115
|
+
/** combo_id 是世游分配的聚合用户 ID,游戏侧应当使用 combo_id 作为用户的唯一标识。*/
|
|
116
|
+
combo_id: string
|
|
117
|
+
/** placement_id 是广告位 ID,游戏侧用它确定发放什么样的广告激励。*/
|
|
118
|
+
placement_id: string
|
|
119
|
+
/** impression_id 是世游服务端创建的,标识单次广告播放的唯一 ID。*/
|
|
120
|
+
impression_id: string
|
|
121
|
+
}
|
|
122
|
+
```
|