@seayoo-io/combo-sdk-node 0.1.9 → 0.1.11

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 CHANGED
@@ -52,13 +52,21 @@ const client = new ApiClient({
52
52
  ### 创建订单 CreateOrder
53
53
 
54
54
  ```js
55
+ // 导入枚举变量
56
+ import { Platform } from "@seayoo-io/combo-sdk-node"
57
+
55
58
  // 创建订单
56
59
  const createOrderResult = await client.createOrder({
57
60
  /** 用于标识创建订单请求的唯一 ID */
58
61
  reference_id: "<游戏订单ID>",
59
62
  /** 发起购买的用户的唯一标识 */
60
63
  combo_id: "<ComboID>",
61
- /** 要购买的商品 ID */
64
+ /**
65
+ * 要购买的商品 ID
66
+ * 这里指的是在世游发行平台管理的商品 ID,其中重要的两个约定是:
67
+ * - 商品价格是正整数
68
+ * - 商品价格单位为 分 或 美分,即商品币种所支持的最小流通单位
69
+ */
62
70
  product_id: "<ProductID>",
63
71
  /** 平台,支持类型见源码类型定义 Platform */
64
72
  platform: Platform.iOS,
@@ -67,7 +75,7 @@ const createOrderResult = await client.createOrder({
67
75
  * 这个地址对应的服务端应该通过 Notify 模块实现路由处理
68
76
  */
69
77
  notify_url: "https://<YourSite>/<YourPath>"
70
- /** 要购买的商品的数量,最小为 1 */
78
+ /** 要购买的商品的数量,最小为 1,必须为正整数 */
71
79
  quantity: 1,
72
80
  /** [可选]订单上下文,在发货通知中透传回游戏 */
73
81
  context: "",
@@ -109,12 +117,25 @@ interface CreateOrderResponse {
109
117
 
110
118
  ### 进入/离开游戏
111
119
 
120
+ 下述两个接口对应的功能是**中宣部防沉迷系统**的上报功能,这里的 SessionID 是一次游戏会话的标识,也就是每次玩家进入游戏+离开游戏,算一个 Game Session。单次游戏会话的上下线动作必须使用同一个 SessionID 上报。
121
+
112
122
  ```js
113
123
  // 进入游戏
114
124
  await client.enterGame("<ComboID>", "<SessionID>");
115
125
 
116
126
  // 离开游戏
117
127
  await client.leaveGame("<ComboID>", "<SessionID>");
128
+
129
+ ```
130
+
131
+ > 注意,SessionID 不可重复使用,可以使用 SDK 提供的工具函数生成 SessionID
132
+
133
+ ```js
134
+ import { genSessionID } from "@seayoo-io/combo-sdk-node"
135
+
136
+ // genSessionID 接受唯一参数 comboId 作为输入,返回一个 32 位固定长度的不重复字符串
137
+ const userSessionID = genSessionID("<ComboID>")
138
+
118
139
  ```
119
140
 
120
141
  ## Notify
@@ -128,11 +149,31 @@ import { NotificationType, Endpoint } from "@seayoo-io/combo-sdk-node"
128
149
  function notificationHandler(type, payload) {
129
150
  switch(type) {
130
151
  case NotificationType.ShipOrder:
131
- // Do your work with payload
152
+ // Do your work with payload:定义见 ShipOrderNotification
132
153
  break;
133
154
  }
134
155
  }
135
156
 
157
+ /** ShipOrderNotification 是订单发货通知的数据结构,包含了订单的详细信息 */
158
+ interface ShipOrderNotification {
159
+ /** 世游服务端创建的,标识订单的唯一 ID */
160
+ order_id: string
161
+ /** 游戏侧用于标识创建订单请求的唯一 ID */
162
+ reference_id: string
163
+ /** 发起购买的用户的唯一标识 */
164
+ combo_id: string
165
+ /** 购买的商品 ID,指的是在世游发行平台管理的商品 ID */
166
+ product_id: string
167
+ /** 购买的商品的数量 */
168
+ quantity: number
169
+ /** 订单币种代码。例如 USD CNY */
170
+ currency: string
171
+ /** 订单金额,单位为分,如果币种为美元,则单位为美分。 */
172
+ amount: number
173
+ /** 游戏侧创建订单时提供的订单上下文,透传回游戏 */
174
+ context?: string
175
+ }
176
+
136
177
  // 1.2 创建配置,类型定义参见源码 SDKBaseConfig
137
178
  const config = {
138
179
  game: "<GameId>",
package/dist/const.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /** SDK 名称 */
2
2
  export declare const SDKName = "combo-sdk-node";
3
3
  /** SDK 版本,在编译前会被动态替换 */
4
- export declare const SDKVersion = "0.1.9";
4
+ export declare const SDKVersion = "0.1.11";
5
5
  /**
6
6
  * 世游支持的服务端点列表
7
7
  */
package/dist/index.cjs CHANGED
@@ -12,7 +12,7 @@ var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
12
12
  /** SDK 名称 */
13
13
  const SDKName = "combo-sdk-node";
14
14
  /** SDK 版本,在编译前会被动态替换 */
15
- const SDKVersion = "0.1.9";
15
+ const SDKVersion = "0.1.11";
16
16
  /**
17
17
  * 世游支持的服务端点列表
18
18
  */
@@ -349,6 +349,15 @@ async function done(res, statusCode, message) {
349
349
  res.end();
350
350
  }
351
351
 
352
+ /**
353
+ * 生成一个 SessionID 以用作用户上下线的上报标记
354
+ */
355
+ function genSessionID(comboId) {
356
+ const ts = process.hrtime.bigint();
357
+ const r = Math.random() + Math.random();
358
+ return crypto.createHash("md5").update(`${comboId}${ts}${r}`).digest("hex").toLowerCase();
359
+ }
360
+
352
361
  const defaultDataField = "data";
353
362
  const defaultMessageField = "message";
354
363
  /**
@@ -637,7 +646,7 @@ function convertBody(body) {
637
646
  if (!body) {
638
647
  return;
639
648
  }
640
- if (body instanceof URLSearchParams || typeof body === "string") {
649
+ if (typeof body === "string" || body instanceof URLSearchParams) {
641
650
  return body;
642
651
  }
643
652
  return JSON.stringify(body);
@@ -963,18 +972,6 @@ class ApiClient {
963
972
  * 创建订单
964
973
  */
965
974
  async createOrder(option) {
966
- if (!option.combo_id || !option.notify_url || !option.product_id || !option.reference_id) {
967
- throw new Error("createOrder: 必要参数缺失");
968
- }
969
- // 检查通知回调地址
970
- if (!isFullURL(option.notify_url)) {
971
- throw new Error("createOrder: notify_url 需要是一个完整的 url 地址");
972
- }
973
- // 检查购买数量
974
- const quantity = Math.min(Math.max(1, Math.ceil(option.quantity)), Number.MAX_SAFE_INTEGER);
975
- if (quantity !== option.quantity) {
976
- throw new Error("createOrder: 购买数量 quantity 必须是一个有限的正整数,且不能小于 1");
977
- }
978
975
  const {
979
976
  ok,
980
977
  data,
@@ -1004,9 +1001,6 @@ class ApiClient {
1004
1001
  * @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
1005
1002
  */
1006
1003
  async enterGame(comboId, sessionId) {
1007
- if (!comboId || !sessionId) {
1008
- throw new Error("enterGame: 必要参数缺失");
1009
- }
1010
1004
  const {
1011
1005
  ok,
1012
1006
  status,
@@ -1037,9 +1031,6 @@ class ApiClient {
1037
1031
  * @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
1038
1032
  */
1039
1033
  async leaveGame(comboId, sessionId) {
1040
- if (!comboId || !sessionId) {
1041
- throw new Error("enterGame: 必要参数缺失");
1042
- }
1043
1034
  const {
1044
1035
  ok,
1045
1036
  status,
@@ -1296,6 +1287,7 @@ exports.ApiClient = ApiClient;
1296
1287
  exports.TokenVerifier = TokenVerifier;
1297
1288
  exports.calcAuthorizationHeader = calcAuthorizationHeader;
1298
1289
  exports.checkHttpAuthInfo = checkHttpAuthInfo;
1290
+ exports.genSessionID = genSessionID;
1299
1291
  exports.getNotificationHandler = getNotificationHandler;
1300
1292
  exports.getNotificationHandlerForExpress = getNotificationHandlerForExpress;
1301
1293
  exports.getNotificationHandlerForKoa = getNotificationHandlerForKoa;
package/dist/index.d.ts CHANGED
@@ -9,4 +9,4 @@ export { NotificationType } from "./notify/types";
9
9
  export type { NotificationHandler } from "./notify/types";
10
10
  export type { SDKBaseConfig } from "./utils";
11
11
  export { Endpoint, Platform, IdP } from "./const";
12
- export { parseAuthorizationHeader, calcAuthorizationHeader, checkHttpAuthInfo } from "./utils";
12
+ export { genSessionID, parseAuthorizationHeader, calcAuthorizationHeader, checkHttpAuthInfo } from "./utils";
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import { verify } from 'jsonwebtoken';
7
7
  /** SDK 名称 */
8
8
  const SDKName = "combo-sdk-node";
9
9
  /** SDK 版本,在编译前会被动态替换 */
10
- const SDKVersion = "0.1.9";
10
+ const SDKVersion = "0.1.11";
11
11
  /**
12
12
  * 世游支持的服务端点列表
13
13
  */
@@ -344,6 +344,15 @@ async function done(res, statusCode, message) {
344
344
  res.end();
345
345
  }
346
346
 
347
+ /**
348
+ * 生成一个 SessionID 以用作用户上下线的上报标记
349
+ */
350
+ function genSessionID(comboId) {
351
+ const ts = process.hrtime.bigint();
352
+ const r = Math.random() + Math.random();
353
+ return createHash("md5").update(`${comboId}${ts}${r}`).digest("hex").toLowerCase();
354
+ }
355
+
347
356
  const defaultDataField = "data";
348
357
  const defaultMessageField = "message";
349
358
  /**
@@ -632,7 +641,7 @@ function convertBody(body) {
632
641
  if (!body) {
633
642
  return;
634
643
  }
635
- if (body instanceof URLSearchParams || typeof body === "string") {
644
+ if (typeof body === "string" || body instanceof URLSearchParams) {
636
645
  return body;
637
646
  }
638
647
  return JSON.stringify(body);
@@ -958,18 +967,6 @@ class ApiClient {
958
967
  * 创建订单
959
968
  */
960
969
  async createOrder(option) {
961
- if (!option.combo_id || !option.notify_url || !option.product_id || !option.reference_id) {
962
- throw new Error("createOrder: 必要参数缺失");
963
- }
964
- // 检查通知回调地址
965
- if (!isFullURL(option.notify_url)) {
966
- throw new Error("createOrder: notify_url 需要是一个完整的 url 地址");
967
- }
968
- // 检查购买数量
969
- const quantity = Math.min(Math.max(1, Math.ceil(option.quantity)), Number.MAX_SAFE_INTEGER);
970
- if (quantity !== option.quantity) {
971
- throw new Error("createOrder: 购买数量 quantity 必须是一个有限的正整数,且不能小于 1");
972
- }
973
970
  const {
974
971
  ok,
975
972
  data,
@@ -999,9 +996,6 @@ class ApiClient {
999
996
  * @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
1000
997
  */
1001
998
  async enterGame(comboId, sessionId) {
1002
- if (!comboId || !sessionId) {
1003
- throw new Error("enterGame: 必要参数缺失");
1004
- }
1005
999
  const {
1006
1000
  ok,
1007
1001
  status,
@@ -1032,9 +1026,6 @@ class ApiClient {
1032
1026
  * @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
1033
1027
  */
1034
1028
  async leaveGame(comboId, sessionId) {
1035
- if (!comboId || !sessionId) {
1036
- throw new Error("enterGame: 必要参数缺失");
1037
- }
1038
1029
  const {
1039
1030
  ok,
1040
1031
  status,
@@ -1287,4 +1278,4 @@ function getNotificationMiddlewareForKoa(paths, config, handler) {
1287
1278
  };
1288
1279
  }
1289
1280
 
1290
- export { ApiClient, Endpoint, IdP, Platform, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
1281
+ export { ApiClient, Endpoint, IdP, Platform, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
@@ -3,3 +3,7 @@ export * from "./types";
3
3
  export * from "./config";
4
4
  export * from "./signer";
5
5
  export * from "./http";
6
+ /**
7
+ * 生成一个 SessionID 以用作用户上下线的上报标记
8
+ */
9
+ export declare function genSessionID(comboId: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-io/combo-sdk-node",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "Combo SDK for Node.js",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",