payment-kit 1.21.7 → 1.21.9

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.
@@ -18,6 +18,44 @@ import {
18
18
  } from './types';
19
19
  import { formatVendorUrl } from './util';
20
20
 
21
+ const DOMAIN_CONFLICT_ERROR_CODE = 'ERROR_DOMAIN_NOT_AVAILABLE';
22
+
23
+ export const parseDomainLength = (len: number | string | undefined, defaultValue: number) => {
24
+ try {
25
+ if (len === undefined) {
26
+ return defaultValue;
27
+ }
28
+
29
+ if (typeof len === 'number') {
30
+ return len;
31
+ }
32
+
33
+ return parseInt(len, 10) || defaultValue;
34
+ } catch (error) {
35
+ logger.error('failed to parse domain length', {
36
+ error,
37
+ len,
38
+ defaultValue,
39
+ });
40
+
41
+ return defaultValue;
42
+ }
43
+ };
44
+
45
+ export const generateRandomSubdomain = (totalLength: number = 8): string => {
46
+ // Generate timestamp-based string for uniqueness
47
+ const timestamp = Date.now();
48
+ const timeStr = timestamp.toString(36);
49
+ let result = timeStr.slice(-Math.min(timeStr.length, totalLength));
50
+
51
+ // Pad with random chars if needed
52
+ while (result.length < totalLength) {
53
+ result = Math.floor(Math.random() * 36).toString(36) + result;
54
+ }
55
+
56
+ return result.slice(0, totalLength);
57
+ };
58
+
21
59
  export class DidnamesAdapter implements VendorAdapter {
22
60
  private vendorConfig: VendorConfig | null = null;
23
61
  private vendorKey: string;
@@ -33,53 +71,128 @@ export class DidnamesAdapter implements VendorAdapter {
33
71
  }
34
72
 
35
73
  /**
36
- * Generate random subdomain for documentation site
37
- * Format: [prefix][separator][timestamp-suffix] or pure timestamp
38
- * Configurable length, prefix, separator with timestamp-based uniqueness
74
+ * Submit domain order with retry logic for domain conflicts
39
75
  */
40
- private generateRandomSubdomain(
41
- options: {
42
- totalLength?: number; // Total subdomain length (default: 9)
43
- prefix?: string; // Prefix (default: 'doc')
44
- usePrefix?: boolean; // Whether to use prefix (default: true)
45
- separator?: string; // Separator between prefix and suffix (default: '-')
46
- } = {}
47
- ): string {
48
- const { totalLength = 8, prefix = 'doc', usePrefix = true, separator = '-' } = options;
49
-
50
- if (usePrefix) {
51
- // With prefix: 'doc' + separator + timestamp suffix
52
- const prefixWithSeparatorLength = prefix.length + separator.length;
53
- const suffixLength = totalLength - prefixWithSeparatorLength;
54
- if (suffixLength <= 0) {
55
- throw new Error(
56
- `Total length (${totalLength}) must be greater than prefix + separator length (${prefixWithSeparatorLength})`
57
- );
58
- }
76
+ private async submitDomainOrderWithRetry(
77
+ orderData: any,
78
+ url: string,
79
+ rootDomain: string,
80
+ totalLength: number,
81
+ maxRetries: number = 5
82
+ ): Promise<{ response: Response; subdomain: string; domain: string; bindDomainCap: any }> {
83
+ let lastError: Error | null = null;
84
+
85
+ // eslint-disable-next-line no-await-in-loop
86
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
87
+ const subdomain = generateRandomSubdomain(totalLength);
88
+ const domain = `${subdomain}.${rootDomain}`;
59
89
 
60
- const timestamp = Date.now();
61
- const timeStr = timestamp.toString(36);
62
- let suffix = timeStr.slice(-suffixLength);
90
+ logger.info(`Domain generation attempt ${attempt}/${maxRetries}`, {
91
+ subdomain,
92
+ domain,
93
+ checkoutSessionId: orderData.checkoutSessionId,
94
+ });
63
95
 
64
- // Pad with random chars if timestamp is shorter than needed
65
- while (suffix.length < suffixLength) {
66
- suffix = Math.floor(Math.random() * 36).toString(36) + suffix;
67
- }
96
+ // Generate bindDomainCap for this domain
97
+ const bindDomainCap = this.generateBindCap({
98
+ domain,
99
+ checkoutSessionId: orderData.checkoutSessionId,
100
+ });
68
101
 
69
- return `${prefix}${separator}${suffix}`;
70
- }
102
+ // Update order data with current domain info
103
+ const updatedOrderData = {
104
+ ...orderData,
105
+ deliveryParams: {
106
+ ...orderData.deliveryParams,
107
+ customParams: {
108
+ ...orderData.deliveryParams.customParams,
109
+ subdomain,
110
+ rootDomain,
111
+ domain,
112
+ bindDomainCap,
113
+ },
114
+ },
115
+ };
71
116
 
72
- // Pure timestamp-based without prefix
73
- const timestamp = Date.now();
74
- const timeStr = timestamp.toString(36);
75
- let result = timeStr.slice(-totalLength);
117
+ try {
118
+ const { headers, body } = VendorAuth.signRequestWithHeaders(updatedOrderData);
119
+
120
+ // eslint-disable-next-line no-await-in-loop
121
+ const response = await fetch(url, {
122
+ method: 'POST',
123
+ headers,
124
+ body,
125
+ });
126
+
127
+ if (response.ok) {
128
+ logger.info('domain registration successful', {
129
+ subdomain,
130
+ domain,
131
+ attempt,
132
+ checkoutSessionId: orderData.checkoutSessionId,
133
+ });
134
+ return { response, subdomain, domain, bindDomainCap };
135
+ }
136
+
137
+ // Handle error response
138
+ // eslint-disable-next-line no-await-in-loop
139
+ const errorBody = await response.text();
140
+ let errorData;
141
+
142
+ try {
143
+ errorData = JSON.parse(errorBody);
144
+ } catch {
145
+ errorData = { message: errorBody };
146
+ }
147
+
148
+ if (errorData.code === DOMAIN_CONFLICT_ERROR_CODE && attempt < maxRetries) {
149
+ logger.warn('domain not available, retrying with new domain', {
150
+ subdomain,
151
+ domain,
152
+ attempt,
153
+ remainingAttempts: maxRetries - attempt,
154
+ checkoutSessionId: orderData.checkoutSessionId,
155
+ });
156
+ // eslint-disable-next-line no-continue
157
+ continue; // Try again with new domain
158
+ }
159
+
160
+ const errorMsg =
161
+ errorData.code === DOMAIN_CONFLICT_ERROR_CODE
162
+ ? `failed to find available domain after ${maxRetries} attempts`
163
+ : `did names API error: ${response.status} ${response.statusText} - ${errorData.message || errorBody}`;
164
+
165
+ logger.error('did names API error', {
166
+ url,
167
+ status: response.status,
168
+ statusText: response.statusText,
169
+ body: errorBody,
170
+ subdomain,
171
+ attempt,
172
+ isDomainConflict: errorData.code === DOMAIN_CONFLICT_ERROR_CODE,
173
+ });
76
174
 
77
- // Pad with random chars if needed
78
- while (result.length < totalLength) {
79
- result = Math.floor(Math.random() * 36).toString(36) + result;
175
+ throw new Error(errorMsg);
176
+ } catch (error: any) {
177
+ lastError = error;
178
+
179
+ // If it's a network error and not max retries, continue
180
+ if (attempt < maxRetries && !error.message.includes('did names API error')) {
181
+ logger.warn('network error during domain registration, retrying', {
182
+ error: error.message,
183
+ attempt,
184
+ remainingAttempts: maxRetries - attempt,
185
+ checkoutSessionId: orderData.checkoutSessionId,
186
+ });
187
+ // eslint-disable-next-line no-continue
188
+ continue;
189
+ }
190
+
191
+ throw error;
192
+ }
80
193
  }
81
194
 
82
- return result;
195
+ throw lastError || new Error('Unknown error occurred during domain registration');
83
196
  }
84
197
 
85
198
  /**
@@ -132,57 +245,33 @@ export class DidnamesAdapter implements VendorAdapter {
132
245
 
133
246
  logger.info('didnames vendor rootDomain', { rootDomain });
134
247
 
135
- const subdomain = this.generateRandomSubdomain({ totalLength: 8 });
136
- const domain = `${subdomain}.${rootDomain}`;
137
-
138
- const { checkoutSessionId } = params;
139
- const bindDomainCap = this.generateBindCap({
140
- domain,
141
- checkoutSessionId,
142
- });
143
-
144
- params.deliveryParams.customParams = {
145
- ...params.deliveryParams.customParams,
146
- years: 1,
147
- whoisPrivacy: true,
148
- subdomain,
149
- rootDomain,
150
- domain,
151
- checkoutSessionId,
152
- bindDomainCap,
153
- };
248
+ const totalLength = parseDomainLength(vendorConfig.metadata?.subDomainLength, 8);
154
249
 
155
- const orderData = {
250
+ // Prepare base order data
251
+ const baseOrderData = {
156
252
  checkoutSessionId: params.checkoutSessionId,
157
253
  description: params.description,
158
254
  userInfo: params.userInfo,
159
- deliveryParams: params.deliveryParams,
255
+ deliveryParams: {
256
+ ...params.deliveryParams,
257
+ customParams: {
258
+ ...params.deliveryParams.customParams,
259
+ years: 1,
260
+ whoisPrivacy: true,
261
+ checkoutSessionId: params.checkoutSessionId,
262
+ },
263
+ },
160
264
  };
161
265
 
162
266
  const url = formatVendorUrl(vendorConfig, '/api/vendor/deliveries');
163
- logger.info('submitting domain delivery to DID Names', {
164
- subdomain,
165
- url,
166
- });
167
-
168
- const { headers, body } = VendorAuth.signRequestWithHeaders(orderData);
169
-
170
- const response = await fetch(url, {
171
- method: 'POST',
172
- headers,
173
- body,
174
- });
175
267
 
176
- if (!response.ok) {
177
- const errorBody = await response.text();
178
- logger.error('DID Names API error', {
179
- url,
180
- status: response.status,
181
- statusText: response.statusText,
182
- body: errorBody,
183
- });
184
- throw new Error(`DID Names API error: ${response.status} ${response.statusText}`);
185
- }
268
+ // Use the retry wrapper to submit order with domain conflict handling
269
+ const { response, subdomain, domain, bindDomainCap } = await this.submitDomainOrderWithRetry(
270
+ baseOrderData,
271
+ url,
272
+ rootDomain,
273
+ totalLength
274
+ );
186
275
 
187
276
  const didNamesResult = await response.json();
188
277
 
package/blocklet.yml CHANGED
@@ -14,7 +14,7 @@ repository:
14
14
  type: git
15
15
  url: git+https://github.com/blocklet/payment-kit.git
16
16
  specVersion: 1.2.8
17
- version: 1.21.7
17
+ version: 1.21.9
18
18
  logo: logo.png
19
19
  files:
20
20
  - dist
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payment-kit",
3
- "version": "1.21.7",
3
+ "version": "1.21.9",
4
4
  "scripts": {
5
5
  "dev": "blocklet dev --open",
6
6
  "lint": "tsc --noEmit && eslint src api/src --ext .mjs,.js,.jsx,.ts,.tsx",
@@ -56,9 +56,9 @@
56
56
  "@blocklet/error": "^0.2.5",
57
57
  "@blocklet/js-sdk": "^1.16.52",
58
58
  "@blocklet/logger": "^1.16.52",
59
- "@blocklet/payment-broker-client": "1.21.7",
60
- "@blocklet/payment-react": "1.21.7",
61
- "@blocklet/payment-vendor": "1.21.7",
59
+ "@blocklet/payment-broker-client": "1.21.9",
60
+ "@blocklet/payment-react": "1.21.9",
61
+ "@blocklet/payment-vendor": "1.21.9",
62
62
  "@blocklet/sdk": "^1.16.52",
63
63
  "@blocklet/ui-react": "^3.1.46",
64
64
  "@blocklet/uploader": "^0.2.13",
@@ -128,7 +128,7 @@
128
128
  "devDependencies": {
129
129
  "@abtnode/types": "^1.16.52",
130
130
  "@arcblock/eslint-config-ts": "^0.3.3",
131
- "@blocklet/payment-types": "1.21.7",
131
+ "@blocklet/payment-types": "1.21.9",
132
132
  "@types/cookie-parser": "^1.4.9",
133
133
  "@types/cors": "^2.8.19",
134
134
  "@types/debug": "^4.1.12",
@@ -175,5 +175,5 @@
175
175
  "parser": "typescript"
176
176
  }
177
177
  },
178
- "gitHead": "131f3b3dfcbc8ed75a46ccb32da1d2e75aae7654"
178
+ "gitHead": "dd41fc0eab97528eeb2f696e73073518ef360043"
179
179
  }