glitch-javascript-sdk 4.0.0 → 4.1.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 +10 -0
- package/dist/cjs/index.js +236 -42
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/api/Microtransactions.d.ts +30 -5
- package/dist/esm/api/Preorders.d.ts +162 -0
- package/dist/esm/api/index.d.ts +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +221 -32
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/routes/PreordersRoute.d.ts +7 -0
- package/dist/index.d.ts +192 -6
- package/guides/microtransactions.md +318 -25
- package/package.json +4 -3
- package/src/api/Microtransactions.ts +30 -6
- package/src/api/Preorders.ts +234 -0
- package/src/api/index.ts +1 -0
- package/src/index.ts +3 -0
- package/src/routes/PreordersRoute.ts +34 -0
- package/src/util/MicrotransactionOverlay.ts +92 -27
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { AxiosPromise, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import PreordersRoute from '../routes/PreordersRoute';
|
|
3
|
+
import Requests from '../util/Requests';
|
|
4
|
+
import Response from '../util/Response';
|
|
5
|
+
|
|
6
|
+
export type PreorderFulfillmentType = 'glitch_license' | 'platform_key';
|
|
7
|
+
export type PreorderOfferStatus = 'draft' | 'active' | 'paused' | 'ended' | 'archived';
|
|
8
|
+
export type PreorderDerivedState = 'draft' | 'available' | 'scheduled' | 'sold_out' | 'paused' | 'ended' | 'released' | 'archived';
|
|
9
|
+
export type PreorderPaymentStatus = 'created' | 'requires_action' | 'processing' | 'paid' | 'failed' | 'canceled' | 'refunded' | 'disputed' | 'unknown';
|
|
10
|
+
export type PreorderFulfillmentStatus = 'waiting_for_release' | 'ready' | 'processing' | 'fulfilled' | 'blocked_missing_build' | 'blocked_missing_key' | 'canceled' | 'failed';
|
|
11
|
+
|
|
12
|
+
export interface PreorderRequestOptions extends Pick<AxiosRequestConfig, 'signal' | 'timeout'> {}
|
|
13
|
+
|
|
14
|
+
export interface PreorderPrice {
|
|
15
|
+
country: string;
|
|
16
|
+
currency: string;
|
|
17
|
+
amount_minor: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PreorderInventory {
|
|
21
|
+
available: number;
|
|
22
|
+
reserved: number;
|
|
23
|
+
delivered: number;
|
|
24
|
+
retired: number;
|
|
25
|
+
total: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface PreorderOfferInput {
|
|
29
|
+
sku: string;
|
|
30
|
+
platform_code: string;
|
|
31
|
+
platform_label: string;
|
|
32
|
+
fulfillment_type: PreorderFulfillmentType;
|
|
33
|
+
status?: PreorderOfferStatus;
|
|
34
|
+
sales_start_at?: string | null;
|
|
35
|
+
sales_end_at?: string | null;
|
|
36
|
+
release_at: string;
|
|
37
|
+
limit_total?: number | null;
|
|
38
|
+
max_per_user?: number;
|
|
39
|
+
custom_message?: string | null;
|
|
40
|
+
post_purchase_url?: string | null;
|
|
41
|
+
redeem_url?: string | null;
|
|
42
|
+
prices: PreorderPrice[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface PreorderOffer extends Omit<PreorderOfferInput, 'status'> {
|
|
46
|
+
id: string;
|
|
47
|
+
title_id: string;
|
|
48
|
+
status: PreorderOfferStatus;
|
|
49
|
+
state: PreorderDerivedState;
|
|
50
|
+
sold: number;
|
|
51
|
+
reserved_or_paid: number;
|
|
52
|
+
remaining: number | null;
|
|
53
|
+
revision: number;
|
|
54
|
+
inventory?: PreorderInventory;
|
|
55
|
+
owned_count?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface PreorderSettings {
|
|
59
|
+
enabled: boolean;
|
|
60
|
+
timezone: string;
|
|
61
|
+
readiness?: PreorderReadiness;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface PreorderCatalog {
|
|
65
|
+
enabled: boolean;
|
|
66
|
+
timezone?: string;
|
|
67
|
+
offers: PreorderOffer[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface PreorderPurchaseInput {
|
|
71
|
+
country?: string;
|
|
72
|
+
currency?: string;
|
|
73
|
+
idempotency_key: string;
|
|
74
|
+
payment_method_id?: string;
|
|
75
|
+
payment_intent_id?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface PreorderOrder {
|
|
79
|
+
id: string;
|
|
80
|
+
title_id: string;
|
|
81
|
+
offer_id: string;
|
|
82
|
+
user_id?: string;
|
|
83
|
+
platform_code: string;
|
|
84
|
+
platform_label: string;
|
|
85
|
+
fulfillment_type: PreorderFulfillmentType;
|
|
86
|
+
payment_status: PreorderPaymentStatus;
|
|
87
|
+
fulfillment_status: PreorderFulfillmentStatus;
|
|
88
|
+
currency: string;
|
|
89
|
+
subtotal_minor: number;
|
|
90
|
+
tax_minor: number;
|
|
91
|
+
total_minor: number;
|
|
92
|
+
promised_release_at: string;
|
|
93
|
+
current_release_at: string;
|
|
94
|
+
paid_at?: string;
|
|
95
|
+
fulfilled_at?: string;
|
|
96
|
+
custom_message?: string;
|
|
97
|
+
post_purchase_url?: string;
|
|
98
|
+
redeem_url?: string;
|
|
99
|
+
platform_key?: string;
|
|
100
|
+
key_masked_suffix?: string;
|
|
101
|
+
purchase_email?: string;
|
|
102
|
+
provider_reference?: string;
|
|
103
|
+
commission_minor?: number;
|
|
104
|
+
developer_share_minor?: number;
|
|
105
|
+
requires_action?: boolean;
|
|
106
|
+
client_secret?: string;
|
|
107
|
+
payment_intent_id?: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface PreorderReadinessIssue {
|
|
111
|
+
offer_id: string;
|
|
112
|
+
code: string;
|
|
113
|
+
[key: string]: unknown;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface PreorderReadiness {
|
|
117
|
+
enabled: boolean;
|
|
118
|
+
ready: boolean;
|
|
119
|
+
blockers: PreorderReadinessIssue[];
|
|
120
|
+
warnings: PreorderReadinessIssue[];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface PreorderAdminOrderFilters {
|
|
124
|
+
offer_id?: string;
|
|
125
|
+
payment_status?: string;
|
|
126
|
+
fulfillment_status?: string;
|
|
127
|
+
per_page?: number;
|
|
128
|
+
page?: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type PreorderAdminOrderAction = 'refund' | 'reconcile' | 'fulfill' | 'resend_receipt' | 'resend_access';
|
|
132
|
+
|
|
133
|
+
class Preorders {
|
|
134
|
+
public static catalog(title_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderCatalog>> {
|
|
135
|
+
return Requests.processRoute(PreordersRoute.routes.catalog, undefined, { title_id }, undefined, options);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public static purchase(title_id: string, offer_id: string, data: PreorderPurchaseInput, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOrder>> {
|
|
139
|
+
return Requests.processRoute(PreordersRoute.routes.purchase, data, { title_id, offer_id }, undefined, options);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public static myOrders(title_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<{ orders: PreorderOrder[] }>> {
|
|
143
|
+
return Requests.processRoute(PreordersRoute.routes.myOrders, undefined, { title_id }, undefined, options);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public static order(title_id: string, order_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOrder>> {
|
|
147
|
+
return Requests.processRoute(PreordersRoute.routes.order, undefined, { title_id, order_id }, undefined, options);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public static refundMine(title_id: string, order_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOrder>> {
|
|
151
|
+
return Requests.processRoute(PreordersRoute.routes.refundMine, {}, { title_id, order_id }, undefined, options);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public static resendMine(title_id: string, order_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<{ resent: boolean }>> {
|
|
155
|
+
return Requests.processRoute(PreordersRoute.routes.resendMine, {}, { title_id, order_id }, undefined, options);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public static settings(title_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderSettings>> {
|
|
159
|
+
return Requests.processRoute(PreordersRoute.routes.settings, undefined, { title_id }, undefined, options);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public static updateSettings(title_id: string, data: Partial<Pick<PreorderSettings, 'enabled' | 'timezone'>>, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderSettings>> {
|
|
163
|
+
return Requests.processRoute(PreordersRoute.routes.updateSettings, data, { title_id }, undefined, options);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
public static offers(title_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<{ offers: PreorderOffer[] }>> {
|
|
167
|
+
return Requests.processRoute(PreordersRoute.routes.offers, undefined, { title_id }, undefined, options);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public static offer(title_id: string, offer_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOffer>> {
|
|
171
|
+
return Requests.processRoute(PreordersRoute.routes.offer, undefined, { title_id, offer_id }, undefined, options);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
public static createOffer(title_id: string, data: PreorderOfferInput, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOffer>> {
|
|
175
|
+
return Requests.processRoute(PreordersRoute.routes.createOffer, data, { title_id }, undefined, options);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public static updateOffer(title_id: string, offer_id: string, data: PreorderOfferInput, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOffer>> {
|
|
179
|
+
return Requests.processRoute(PreordersRoute.routes.updateOffer, data, { title_id, offer_id }, undefined, options);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public static activateOffer(title_id: string, offer_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOffer>> {
|
|
183
|
+
return Requests.processRoute(PreordersRoute.routes.activateOffer, {}, { title_id, offer_id }, undefined, options);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public static pauseOffer(title_id: string, offer_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOffer>> {
|
|
187
|
+
return Requests.processRoute(PreordersRoute.routes.pauseOffer, {}, { title_id, offer_id }, undefined, options);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public static archiveOffer(title_id: string, offer_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOffer>> {
|
|
191
|
+
return Requests.processRoute(PreordersRoute.routes.archiveOffer, {}, { title_id, offer_id }, undefined, options);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public static inventory(title_id: string, offer_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderInventory>> {
|
|
195
|
+
return Requests.processRoute(PreordersRoute.routes.inventory, undefined, { title_id, offer_id }, undefined, options);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
public static importKeys(title_id: string, offer_id: string, keys: string[], options?: PreorderRequestOptions): AxiosPromise<Response<{ inserted: number; duplicate_in_upload: number; duplicate_existing: number; invalid: number; inventory: PreorderInventory }>> {
|
|
199
|
+
return Requests.processRoute(PreordersRoute.routes.importKeys, { keys }, { title_id, offer_id }, undefined, options);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public static importKeyFile<T>(title_id: string, offer_id: string, file: File | Blob): AxiosPromise<Response<T>> {
|
|
203
|
+
const url = PreordersRoute.routes.importKeys.url
|
|
204
|
+
.replace('{title_id}', title_id)
|
|
205
|
+
.replace('{offer_id}', offer_id);
|
|
206
|
+
return Requests.uploadFile<T>(url, 'key_file', file, {}, undefined, undefined, { excludeCommunityContext: true });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public static retireKey(title_id: string, offer_id: string, key_id: string, reason: string, options?: PreorderRequestOptions): AxiosPromise<Response<{ id: string; status: string; masked_suffix: string }>> {
|
|
210
|
+
return Requests.processRoute(PreordersRoute.routes.retireKey, { reason }, { title_id, offer_id, key_id }, undefined, options);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
public static adminOrders(title_id: string, filters?: PreorderAdminOrderFilters, options?: PreorderRequestOptions): AxiosPromise<Response<{ orders: PreorderOrder[]; pagination: Record<string, number> }>> {
|
|
214
|
+
return Requests.processRoute(PreordersRoute.routes.adminOrders, undefined, { title_id }, filters, options);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public static readiness(title_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderReadiness>> {
|
|
218
|
+
return Requests.processRoute(PreordersRoute.routes.readiness, undefined, { title_id }, undefined, options);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public static adminOrderAction(title_id: string, order_id: string, action: PreorderAdminOrderAction, options?: PreorderRequestOptions): AxiosPromise<Response<PreorderOrder>> {
|
|
222
|
+
return Requests.processRoute(PreordersRoute.routes.adminOrderAction, {}, { title_id, order_id, action }, undefined, options);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
public static mcpCapabilities<T = unknown>(title_id: string, options?: PreorderRequestOptions): AxiosPromise<Response<T>> {
|
|
226
|
+
return Requests.processRoute(PreordersRoute.routes.mcpCapabilities, undefined, { title_id }, undefined, options);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
public static mcpOperation<T = unknown>(title_id: string, operation: string, argumentsData: Record<string, unknown> = {}, options?: PreorderRequestOptions): AxiosPromise<Response<T>> {
|
|
230
|
+
return Requests.processRoute(PreordersRoute.routes.mcpOperation, { arguments: argumentsData }, { title_id, operation }, undefined, options);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export default Preorders;
|
package/src/api/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -61,7 +61,9 @@ import {GameAdvertising} from './api';
|
|
|
61
61
|
import {Hosting} from './api';
|
|
62
62
|
import {GameDesign} from './api';
|
|
63
63
|
import {Microtransactions} from './api';
|
|
64
|
+
import {Preorders} from './api';
|
|
64
65
|
export * from './api/Microtransactions';
|
|
66
|
+
export * from './api/Preorders';
|
|
65
67
|
export * from './util/MicrotransactionBridge';
|
|
66
68
|
export * from './util/MicrotransactionOverlay';
|
|
67
69
|
|
|
@@ -153,6 +155,7 @@ class Glitch {
|
|
|
153
155
|
Hosting: Hosting,
|
|
154
156
|
GameDesign: GameDesign,
|
|
155
157
|
Microtransactions: Microtransactions,
|
|
158
|
+
Preorders: Preorders,
|
|
156
159
|
}
|
|
157
160
|
|
|
158
161
|
public static util = {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import Route from './interface';
|
|
2
|
+
import HTTP_METHODS from '../constants/HttpMethods';
|
|
3
|
+
|
|
4
|
+
class PreordersRoute {
|
|
5
|
+
public static routes: { [key: string]: Route } = {
|
|
6
|
+
catalog: { url: '/titles/{title_id}/preorders', method: HTTP_METHODS.GET },
|
|
7
|
+
purchase: { url: '/titles/{title_id}/preorders/{offer_id}/purchase', method: HTTP_METHODS.POST },
|
|
8
|
+
myOrders: { url: '/titles/{title_id}/preorders/me/orders', method: HTTP_METHODS.GET },
|
|
9
|
+
order: { url: '/titles/{title_id}/preorders/orders/{order_id}', method: HTTP_METHODS.GET },
|
|
10
|
+
refundMine: { url: '/titles/{title_id}/preorders/orders/{order_id}/refund', method: HTTP_METHODS.POST },
|
|
11
|
+
resendMine: { url: '/titles/{title_id}/preorders/orders/{order_id}/resend-access', method: HTTP_METHODS.POST },
|
|
12
|
+
|
|
13
|
+
settings: { url: '/titles/{title_id}/preorders/settings', method: HTTP_METHODS.GET },
|
|
14
|
+
updateSettings: { url: '/titles/{title_id}/preorders/settings', method: HTTP_METHODS.PUT },
|
|
15
|
+
offers: { url: '/titles/{title_id}/preorders/admin/offers', method: HTTP_METHODS.GET },
|
|
16
|
+
createOffer: { url: '/titles/{title_id}/preorders/admin/offers', method: HTTP_METHODS.POST },
|
|
17
|
+
offer: { url: '/titles/{title_id}/preorders/admin/offers/{offer_id}', method: HTTP_METHODS.GET },
|
|
18
|
+
updateOffer: { url: '/titles/{title_id}/preorders/admin/offers/{offer_id}', method: HTTP_METHODS.PUT },
|
|
19
|
+
activateOffer: { url: '/titles/{title_id}/preorders/admin/offers/{offer_id}/activate', method: HTTP_METHODS.POST },
|
|
20
|
+
pauseOffer: { url: '/titles/{title_id}/preorders/admin/offers/{offer_id}/pause', method: HTTP_METHODS.POST },
|
|
21
|
+
archiveOffer: { url: '/titles/{title_id}/preorders/admin/offers/{offer_id}/archive', method: HTTP_METHODS.POST },
|
|
22
|
+
inventory: { url: '/titles/{title_id}/preorders/admin/offers/{offer_id}/keys', method: HTTP_METHODS.GET },
|
|
23
|
+
importKeys: { url: '/titles/{title_id}/preorders/admin/offers/{offer_id}/keys/import', method: HTTP_METHODS.POST },
|
|
24
|
+
retireKey: { url: '/titles/{title_id}/preorders/admin/offers/{offer_id}/keys/{key_id}/retire', method: HTTP_METHODS.POST },
|
|
25
|
+
adminOrders: { url: '/titles/{title_id}/preorders/admin/orders', method: HTTP_METHODS.GET },
|
|
26
|
+
readiness: { url: '/titles/{title_id}/preorders/admin/readiness', method: HTTP_METHODS.GET },
|
|
27
|
+
adminOrderAction: { url: '/titles/{title_id}/preorders/admin/orders/{order_id}/{action}', method: HTTP_METHODS.POST },
|
|
28
|
+
|
|
29
|
+
mcpCapabilities: { url: '/mcp/v1/titles/{title_id}/preorders/capabilities', method: HTTP_METHODS.GET },
|
|
30
|
+
mcpOperation: { url: '/mcp/v1/titles/{title_id}/preorders/operations/{operation}', method: HTTP_METHODS.POST },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default PreordersRoute;
|
|
@@ -68,6 +68,8 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
|
|
|
68
68
|
if (!win || !doc.body) throw new Error('A mounted game document is required.');
|
|
69
69
|
if (activeOverlays.has(doc)) throw new Error('A checkout is already open. Resume or close that same purchase first.');
|
|
70
70
|
const session = options.session;
|
|
71
|
+
// Older purchase-session creation responses need not include an intent.
|
|
72
|
+
const restoring = session.intent === 'restore';
|
|
71
73
|
const parsed = new URL(session.hosted_url);
|
|
72
74
|
const path = `/games/${encodeURIComponent(options.titleId)}/checkout/${encodeURIComponent(session.id)}`;
|
|
73
75
|
if (parsed.origin !== options.checkoutOrigin || parsed.pathname !== path || parsed.search || parsed.username || parsed.password ||
|
|
@@ -86,41 +88,55 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
|
|
|
86
88
|
dialog.setAttribute('aria-modal', 'true');
|
|
87
89
|
dialog.setAttribute('aria-labelledby', `${id}-title`);
|
|
88
90
|
dialog.setAttribute('role', 'dialog');
|
|
89
|
-
dialog.style.cssText = 'position:fixed;inset:
|
|
91
|
+
dialog.style.cssText = 'position:fixed;inset:0;margin:auto;padding:0;border:1px solid #cbd5e1;border-radius:16px;box-sizing:border-box;display:flex;flex-direction:column;width:min(1100px,calc(100vw - 32px));height:min(850px,calc(100vh - 32px));min-width:0;min-height:0;max-width:none;max-height:none;background:#fff;color:#172033;box-shadow:0 24px 90px #0008;z-index:2147483647;overflow:hidden;';
|
|
90
92
|
const style = doc.createElement('style');
|
|
91
|
-
style.textContent =
|
|
93
|
+
style.textContent = `
|
|
94
|
+
#${id}::backdrop{background:rgba(8,16,32,.72)}
|
|
95
|
+
#${id} button:focus-visible{outline:3px solid #4263eb;outline-offset:3px}
|
|
96
|
+
@supports(height:100dvh){#${id}{height:min(850px,calc(100dvh - 32px))!important}}
|
|
97
|
+
@media(max-width:600px),(max-height:500px){
|
|
98
|
+
#${id}{width:100%!important;height:100vh!important;border:0!important;border-radius:0!important;padding:env(safe-area-inset-top,0px) env(safe-area-inset-right,0px) env(safe-area-inset-bottom,0px) env(safe-area-inset-left,0px)!important}
|
|
99
|
+
@supports(height:100dvh){#${id}{height:100dvh!important}}
|
|
100
|
+
}`;
|
|
101
|
+
// Natural-height chrome can wrap/scroll without consuming the entire frame.
|
|
102
|
+
// No fixed header/status subtraction; the iframe owns all remaining space.
|
|
103
|
+
const chrome = doc.createElement('div');
|
|
104
|
+
chrome.style.cssText = 'flex:0 1 auto;min-width:0;min-height:0;max-height:50%;overflow:auto;overscroll-behavior:contain;';
|
|
92
105
|
const header = doc.createElement('div');
|
|
93
|
-
header.style.cssText = 'display:flex;align-items:center;justify-content:space-between;gap:16px;padding:12px 16px;border-bottom:1px solid #e2e8f0;
|
|
106
|
+
header.style.cssText = 'display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:8px 16px;padding:12px 16px;border-bottom:1px solid #e2e8f0;box-sizing:border-box;min-width:0;';
|
|
94
107
|
const heading = doc.createElement('h2');
|
|
95
108
|
heading.id = `${id}-title`;
|
|
96
|
-
heading.textContent = options.label || (
|
|
97
|
-
heading.style.cssText = 'margin:0;font:600 17px/1.3 system-ui,sans-serif;color:#172033;';
|
|
109
|
+
heading.textContent = options.label || (restoring ? 'Restore game purchases' : 'Secure game checkout');
|
|
110
|
+
heading.style.cssText = 'flex:1 1 10rem;min-width:0;margin:0;font:600 17px/1.3 system-ui,sans-serif;color:#172033;white-space:normal;overflow-wrap:anywhere;';
|
|
98
111
|
const closeButton = doc.createElement('button');
|
|
99
112
|
closeButton.type = 'button';
|
|
100
113
|
closeButton.textContent = 'Close';
|
|
101
|
-
closeButton.setAttribute('aria-label', 'Close checkout and return to game');
|
|
102
|
-
closeButton.style.cssText = 'border:1px solid #cbd5e1;border-radius:8px;padding:
|
|
114
|
+
closeButton.setAttribute('aria-label', restoring ? 'Close restore and return to game' : 'Close checkout and return to game');
|
|
115
|
+
closeButton.style.cssText = 'box-sizing:border-box;flex:1 1 auto;min-width:44px;min-height:44px;max-width:100%;height:auto;margin:0;border:1px solid #cbd5e1;border-radius:8px;padding:10px 14px;background:#fff;color:#172033;font:600 14px/1.4 system-ui,sans-serif;white-space:normal;overflow-wrap:anywhere;cursor:pointer;';
|
|
103
116
|
const retryButton = doc.createElement('button');
|
|
104
117
|
retryButton.type = 'button';
|
|
105
118
|
retryButton.textContent = 'Retry';
|
|
106
|
-
retryButton.setAttribute('aria-label', 'Reload this same checkout without starting another purchase');
|
|
119
|
+
retryButton.setAttribute('aria-label', restoring ? 'Reload this same restore session' : 'Reload this same checkout without starting another purchase');
|
|
107
120
|
retryButton.style.cssText = closeButton.style.cssText;
|
|
121
|
+
closeButton.style.background = '#172033';
|
|
122
|
+
closeButton.style.color = '#fff';
|
|
108
123
|
const controls = doc.createElement('div');
|
|
109
|
-
controls.style.cssText = 'display:flex;gap:8px;';
|
|
124
|
+
controls.style.cssText = 'display:flex;flex-wrap:wrap;gap:8px;min-width:0;max-width:100%;margin-left:auto;';
|
|
110
125
|
controls.append(retryButton, closeButton);
|
|
111
126
|
header.append(heading, controls);
|
|
112
127
|
const status = doc.createElement('div');
|
|
113
128
|
status.setAttribute('role', 'status');
|
|
114
|
-
status.style.cssText = 'padding:6px 16px;font:12px/1.4 system-ui,sans-serif;background:#f1f5f9;color:#172033;
|
|
115
|
-
status.textContent = 'Your game stays open. Loading secure checkout…';
|
|
129
|
+
status.style.cssText = 'padding:6px 16px;font:12px/1.4 system-ui,sans-serif;background:#f1f5f9;color:#172033;box-sizing:border-box;white-space:normal;overflow-wrap:anywhere;';
|
|
130
|
+
status.textContent = restoring ? 'Your game stays open. Loading secure restore…' : 'Your game stays open. Loading secure checkout…';
|
|
116
131
|
const iframe = doc.createElement('iframe');
|
|
117
132
|
iframe.title = heading.textContent;
|
|
118
133
|
iframe.setAttribute('allow', 'payment');
|
|
119
134
|
iframe.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin allow-popups allow-popups-to-escape-sandbox');
|
|
120
135
|
iframe.referrerPolicy = 'no-referrer';
|
|
121
|
-
iframe.style.cssText = 'display:block;width:100%;height:
|
|
136
|
+
iframe.style.cssText = 'display:block;flex:1 1 0%;min-width:0;min-height:0;width:100%;height:100%;margin:0;padding:0;border:0;background:#fff;';
|
|
122
137
|
iframe.src = session.hosted_url;
|
|
123
|
-
|
|
138
|
+
chrome.append(header, status);
|
|
139
|
+
dialog.append(style, chrome, iframe);
|
|
124
140
|
doc.body.append(dialog);
|
|
125
141
|
const frameWindow = iframe.contentWindow;
|
|
126
142
|
if (!frameWindow) { dialog.remove(); throw new Error('In-game checkout embedding is unavailable. Your game has not navigated.'); }
|
|
@@ -132,6 +148,14 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
|
|
|
132
148
|
let frameLoadTimer: number | undefined;
|
|
133
149
|
let appReadyTimer: number | undefined;
|
|
134
150
|
let applicationReady = false;
|
|
151
|
+
let retryPhase: 'idle' | 'blank' | 'checkout' | 'failed' = 'idle';
|
|
152
|
+
let retryReady = false;
|
|
153
|
+
const isBlankDocument = (): boolean => {
|
|
154
|
+
// about:blank inherits our origin. A src attribute alone says nothing about
|
|
155
|
+
// which document emitted load; an old checkout load can still be queued.
|
|
156
|
+
try { return iframe.contentDocument?.URL === 'about:blank' && iframe.contentDocument.readyState === 'complete'; }
|
|
157
|
+
catch { return false; }
|
|
158
|
+
};
|
|
135
159
|
const blocked: Array<{ element: HTMLElement; inert: boolean; ariaHidden: string | null }> = [];
|
|
136
160
|
const report = (error: unknown): void => { try { options.onError?.(error); } catch { /* Consumer error handlers cannot prevent modal cleanup. */ } };
|
|
137
161
|
const clearWatchdogs = (): void => {
|
|
@@ -143,20 +167,27 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
|
|
|
143
167
|
const unavailable = (reason: 'load' | 'ready' | 'error'): void => {
|
|
144
168
|
if (closed) return;
|
|
145
169
|
clearWatchdogs();
|
|
170
|
+
if (retryPhase !== 'idle') { retryPhase = 'failed'; retryReady = false; }
|
|
146
171
|
status.setAttribute('role', 'alert');
|
|
172
|
+
const flow = restoring ? 'Restore' : 'Checkout';
|
|
147
173
|
status.textContent = reason === 'ready'
|
|
148
|
-
?
|
|
149
|
-
:
|
|
150
|
-
report(new Error(
|
|
151
|
-
?
|
|
152
|
-
:
|
|
174
|
+
? `${flow} did not become ready. Retry this same session or Close; your game remains open.`
|
|
175
|
+
: `${flow} could not load here. Retry this same session or Close; your game remains open.`;
|
|
176
|
+
report(new Error(restoring
|
|
177
|
+
? `Embedded restore ${reason === 'ready' ? 'readiness timed out' : 'loading unavailable'}; no navigation was attempted.`
|
|
178
|
+
: reason === 'ready'
|
|
179
|
+
? 'Embedded checkout readiness timed out; no navigation or new payment was attempted.'
|
|
180
|
+
: 'Embedded checkout loading unavailable; no navigation or new payment was attempted.'));
|
|
153
181
|
};
|
|
154
182
|
const markApplicationReady = (): void => {
|
|
155
183
|
if (closed) return;
|
|
184
|
+
// A verified claim from the previous document may finish while Retry is
|
|
185
|
+
// navigating. Keep that claim, but it cannot certify the new document UI.
|
|
186
|
+
if (retryPhase !== 'idle') return;
|
|
156
187
|
applicationReady = true;
|
|
157
188
|
clearWatchdogs();
|
|
158
189
|
status.setAttribute('role', 'status');
|
|
159
|
-
status.textContent = 'Checkout is ready. Your game remains open underneath.';
|
|
190
|
+
status.textContent = restoring ? 'Restore is ready. Your game remains open underneath.' : 'Checkout is ready. Your game remains open underneath.';
|
|
160
191
|
};
|
|
161
192
|
const armLoadWatchdog = (): void => {
|
|
162
193
|
clearWatchdogs();
|
|
@@ -196,29 +227,54 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
|
|
|
196
227
|
};
|
|
197
228
|
const onKey = (event: KeyboardEvent): void => {
|
|
198
229
|
if (event.key === 'Escape') { event.preventDefault(); void overlay.close(); }
|
|
199
|
-
|
|
230
|
+
// Keep the trap while close waits for an in-flight claim/inventory callback.
|
|
231
|
+
if (event.key === 'Tab' && dialog.isConnected) {
|
|
200
232
|
if (event.shiftKey && doc.activeElement === retryButton) { event.preventDefault(); iframe.focus(); }
|
|
201
233
|
else if (!event.shiftKey && doc.activeElement === iframe) { event.preventDefault(); retryButton.focus(); }
|
|
202
234
|
}
|
|
203
235
|
};
|
|
204
236
|
const onCancel = (event: Event): void => { event.preventDefault(); void overlay.close(); };
|
|
237
|
+
// Keyboard events inside the cross-origin document do not reach onKey.
|
|
238
|
+
// The hosted UI forwards Escape via this existing, strictly bound close message.
|
|
205
239
|
const onCloseMessage = (event: MessageEvent<unknown>): void => {
|
|
206
240
|
if (closed || event.origin !== options.checkoutOrigin || event.source !== frameWindow || !event.data || typeof event.data !== 'object') return;
|
|
207
241
|
const message = event.data as Record<string, unknown>;
|
|
208
242
|
if (message.version !== 1 || message.title_id !== options.titleId ||
|
|
209
243
|
message.checkout_session_id !== session.id || message.nonce !== session.nonce) return;
|
|
210
|
-
if (message.type === 'glitch.microtransaction.ready') {
|
|
244
|
+
if (message.type === 'glitch.microtransaction.ready') {
|
|
245
|
+
if (retryPhase === 'checkout' && !isBlankDocument()) retryReady = true;
|
|
246
|
+
else if (retryPhase === 'idle') markApplicationReady();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
211
249
|
if (message.type !== 'glitch.microtransaction.close') return;
|
|
212
250
|
void overlay.close(verified ? 'completed' : 'dismissed');
|
|
213
251
|
};
|
|
214
|
-
const onFrameError = (): void => {
|
|
252
|
+
const onFrameError = (): void => {
|
|
253
|
+
// Events from the document being left cannot fail the blank navigation.
|
|
254
|
+
// Its watchdog still bounds a missing/failed intermediate load.
|
|
255
|
+
if (retryPhase === 'blank' || (retryPhase === 'checkout' && isBlankDocument())) return;
|
|
256
|
+
unavailable('error');
|
|
257
|
+
};
|
|
215
258
|
const onFrameLoad = (): void => {
|
|
216
259
|
if (closed) return;
|
|
260
|
+
if (retryPhase === 'failed') return;
|
|
261
|
+
if (retryPhase === 'blank') {
|
|
262
|
+
if (!isBlankDocument()) return;
|
|
263
|
+
retryPhase = 'checkout';
|
|
264
|
+
armLoadWatchdog();
|
|
265
|
+
iframe.src = session.hosted_url;
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (retryPhase === 'checkout') {
|
|
269
|
+
if (isBlankDocument()) return;
|
|
270
|
+
retryPhase = 'idle';
|
|
271
|
+
if (retryReady) { retryReady = false; markApplicationReady(); return; }
|
|
272
|
+
}
|
|
217
273
|
if (frameLoadTimer !== undefined) win.clearTimeout(frameLoadTimer);
|
|
218
274
|
frameLoadTimer = undefined;
|
|
219
275
|
if (applicationReady) return;
|
|
220
276
|
status.setAttribute('role', 'status');
|
|
221
|
-
status.textContent = 'Checkout document loaded. Waiting for the secure checkout interface…';
|
|
277
|
+
status.textContent = restoring ? 'Restore document loaded. Waiting for the secure restore interface…' : 'Checkout document loaded. Waiting for the secure checkout interface…';
|
|
222
278
|
// A document load is not proof the app rendered. Repeated loads cannot
|
|
223
279
|
// indefinitely extend this bounded wait; only explicit Retry resets it.
|
|
224
280
|
if (appReadyTimer === undefined) appReadyTimer = win.setTimeout(() => unavailable('ready'), frameTimeout);
|
|
@@ -226,18 +282,27 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
|
|
|
226
282
|
const overlay: MicrotransactionOverlay = {
|
|
227
283
|
element: dialog, iframe, refresh,
|
|
228
284
|
retry: () => {
|
|
229
|
-
if (closed) return;
|
|
230
|
-
status.textContent = 'Retrying the same secure checkout. Your game stays open.';
|
|
285
|
+
if (closed || retryPhase === 'blank' || retryPhase === 'checkout') return;
|
|
286
|
+
status.textContent = restoring ? 'Retrying the same secure restore session. Your game stays open.' : 'Retrying the same secure checkout. Your game stays open.';
|
|
231
287
|
status.setAttribute('role', 'status');
|
|
288
|
+
retryPhase = 'blank';
|
|
289
|
+
retryReady = false;
|
|
232
290
|
armLoadWatchdog();
|
|
233
|
-
|
|
291
|
+
// Bootstrap removes #token with replaceState. Reassigning the original
|
|
292
|
+
// URL then only changes the fragment; it does not reload checkout/API.
|
|
293
|
+
// Commit a harmless blank document first, then navigate this SAME iframe
|
|
294
|
+
// to the exact original capability URL. Its WindowProxy/bridge stay pinned.
|
|
295
|
+
iframe.src = 'about:blank';
|
|
234
296
|
},
|
|
235
297
|
close: (reason = 'dismissed') => {
|
|
236
298
|
if (closing) return closing;
|
|
237
299
|
if (closed) return Promise.resolve();
|
|
238
300
|
closed = true;
|
|
239
301
|
clearWatchdogs();
|
|
240
|
-
|
|
302
|
+
retryPhase = 'failed';
|
|
303
|
+
retryReady = false;
|
|
304
|
+
status.setAttribute('role', 'status');
|
|
305
|
+
status.textContent = restoring ? 'Finishing secure restore verification. Your game stays open.' : 'Finishing secure purchase verification. Your game stays open.';
|
|
241
306
|
closing = (async () => {
|
|
242
307
|
try {
|
|
243
308
|
// The dialog remains mounted until in-flight claim AND inventory callback finish.
|