@frak-labs/core-sdk 0.1.0-beta.afa252b0 → 0.1.0-beta.b0bd1f8a

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.
Files changed (139) hide show
  1. package/cdn/bundle.iife.js +14 -0
  2. package/dist/actions-CEEObPYc.js +1 -0
  3. package/dist/actions-DbQhWYx8.cjs +1 -0
  4. package/dist/actions.cjs +1 -1
  5. package/dist/actions.d.cts +3 -1481
  6. package/dist/actions.d.ts +3 -1481
  7. package/dist/actions.js +1 -1
  8. package/dist/bundle.cjs +1 -13
  9. package/dist/bundle.d.cts +6 -2087
  10. package/dist/bundle.d.ts +6 -2087
  11. package/dist/bundle.js +1 -13
  12. package/dist/index-7OZ39x1U.d.ts +195 -0
  13. package/dist/index-C6FxkWPC.d.cts +511 -0
  14. package/dist/index-UFX7xCg3.d.ts +351 -0
  15. package/dist/index-d8xS4ryI.d.ts +511 -0
  16. package/dist/index-p4FqSp8z.d.cts +351 -0
  17. package/dist/index-zDq-VlKx.d.cts +195 -0
  18. package/dist/index.cjs +1 -13
  19. package/dist/index.d.cts +4 -1387
  20. package/dist/index.d.ts +4 -1387
  21. package/dist/index.js +1 -13
  22. package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
  23. package/dist/interaction-KX1h9a7V.d.ts +45 -0
  24. package/dist/interactions-DnfM3oe0.js +1 -0
  25. package/dist/interactions-EIXhNLf6.cjs +1 -0
  26. package/dist/interactions.cjs +1 -1
  27. package/dist/interactions.d.cts +2 -182
  28. package/dist/interactions.d.ts +2 -182
  29. package/dist/interactions.js +1 -1
  30. package/dist/openSso-D--Airj6.d.cts +1018 -0
  31. package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
  32. package/dist/productTypes-BUkXJKZ7.cjs +1 -0
  33. package/dist/productTypes-CGb1MmBF.js +1 -0
  34. package/dist/src-B_xO0AR6.cjs +13 -0
  35. package/dist/src-D2d52OZa.js +13 -0
  36. package/dist/trackEvent-CHnYa85W.js +1 -0
  37. package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
  38. package/package.json +23 -18
  39. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  40. package/src/actions/displayEmbeddedWallet.ts +20 -0
  41. package/src/actions/displayModal.test.ts +387 -0
  42. package/src/actions/displayModal.ts +131 -0
  43. package/src/actions/getProductInformation.test.ts +133 -0
  44. package/src/actions/getProductInformation.ts +14 -0
  45. package/src/actions/index.ts +29 -0
  46. package/src/actions/openSso.test.ts +407 -0
  47. package/src/actions/openSso.ts +116 -0
  48. package/src/actions/prepareSso.test.ts +223 -0
  49. package/src/actions/prepareSso.ts +48 -0
  50. package/src/actions/referral/processReferral.ts +230 -0
  51. package/src/actions/referral/referralInteraction.ts +57 -0
  52. package/src/actions/sendInteraction.test.ts +219 -0
  53. package/src/actions/sendInteraction.ts +32 -0
  54. package/src/actions/trackPurchaseStatus.test.ts +287 -0
  55. package/src/actions/trackPurchaseStatus.ts +53 -0
  56. package/src/actions/watchWalletStatus.test.ts +372 -0
  57. package/src/actions/watchWalletStatus.ts +94 -0
  58. package/src/actions/wrapper/modalBuilder.ts +212 -0
  59. package/src/actions/wrapper/sendTransaction.ts +62 -0
  60. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  61. package/src/bundle.ts +3 -0
  62. package/src/clients/DebugInfo.ts +182 -0
  63. package/src/clients/createIFrameFrakClient.ts +287 -0
  64. package/src/clients/index.ts +3 -0
  65. package/src/clients/setupClient.test.ts +343 -0
  66. package/src/clients/setupClient.ts +73 -0
  67. package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
  68. package/src/clients/transports/iframeLifecycleManager.ts +90 -0
  69. package/src/constants/interactionTypes.ts +44 -0
  70. package/src/constants/locales.ts +14 -0
  71. package/src/constants/productTypes.ts +33 -0
  72. package/src/index.ts +101 -0
  73. package/src/interactions/index.ts +5 -0
  74. package/src/interactions/pressEncoder.test.ts +215 -0
  75. package/src/interactions/pressEncoder.ts +53 -0
  76. package/src/interactions/purchaseEncoder.test.ts +291 -0
  77. package/src/interactions/purchaseEncoder.ts +99 -0
  78. package/src/interactions/referralEncoder.test.ts +170 -0
  79. package/src/interactions/referralEncoder.ts +47 -0
  80. package/src/interactions/retailEncoder.test.ts +107 -0
  81. package/src/interactions/retailEncoder.ts +37 -0
  82. package/src/interactions/webshopEncoder.test.ts +56 -0
  83. package/src/interactions/webshopEncoder.ts +30 -0
  84. package/src/types/client.ts +14 -0
  85. package/src/types/compression.ts +22 -0
  86. package/src/types/config.ts +111 -0
  87. package/src/types/context.ts +13 -0
  88. package/src/types/index.ts +71 -0
  89. package/src/types/lifecycle/client.ts +46 -0
  90. package/src/types/lifecycle/iframe.ts +35 -0
  91. package/src/types/lifecycle/index.ts +2 -0
  92. package/src/types/rpc/displayModal.ts +84 -0
  93. package/src/types/rpc/embedded/index.ts +68 -0
  94. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  95. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  96. package/src/types/rpc/interaction.ts +43 -0
  97. package/src/types/rpc/modal/final.ts +46 -0
  98. package/src/types/rpc/modal/generic.ts +46 -0
  99. package/src/types/rpc/modal/index.ts +20 -0
  100. package/src/types/rpc/modal/login.ts +32 -0
  101. package/src/types/rpc/modal/openSession.ts +25 -0
  102. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  103. package/src/types/rpc/modal/transaction.ts +33 -0
  104. package/src/types/rpc/productInformation.ts +59 -0
  105. package/src/types/rpc/sso.ts +80 -0
  106. package/src/types/rpc/walletStatus.ts +35 -0
  107. package/src/types/rpc.ts +158 -0
  108. package/src/types/transport.ts +34 -0
  109. package/src/utils/FrakContext.test.ts +407 -0
  110. package/src/utils/FrakContext.ts +158 -0
  111. package/src/utils/compression/b64.test.ts +181 -0
  112. package/src/utils/compression/b64.ts +29 -0
  113. package/src/utils/compression/compress.test.ts +123 -0
  114. package/src/utils/compression/compress.ts +11 -0
  115. package/src/utils/compression/decompress.test.ts +145 -0
  116. package/src/utils/compression/decompress.ts +11 -0
  117. package/src/utils/compression/index.ts +3 -0
  118. package/src/utils/computeProductId.test.ts +80 -0
  119. package/src/utils/computeProductId.ts +11 -0
  120. package/src/utils/constants.test.ts +23 -0
  121. package/src/utils/constants.ts +4 -0
  122. package/src/utils/formatAmount.test.ts +113 -0
  123. package/src/utils/formatAmount.ts +18 -0
  124. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  125. package/src/utils/getCurrencyAmountKey.ts +15 -0
  126. package/src/utils/getSupportedCurrency.test.ts +51 -0
  127. package/src/utils/getSupportedCurrency.ts +14 -0
  128. package/src/utils/getSupportedLocale.test.ts +64 -0
  129. package/src/utils/getSupportedLocale.ts +16 -0
  130. package/src/utils/iframeHelper.test.ts +450 -0
  131. package/src/utils/iframeHelper.ts +143 -0
  132. package/src/utils/index.ts +21 -0
  133. package/src/utils/sso.test.ts +361 -0
  134. package/src/utils/sso.ts +119 -0
  135. package/src/utils/ssoUrlListener.ts +60 -0
  136. package/src/utils/trackEvent.test.ts +162 -0
  137. package/src/utils/trackEvent.ts +26 -0
  138. package/cdn/bundle.js +0 -19
  139. package/cdn/bundle.js.LICENSE.txt +0 -10
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Tests for sendInteraction action
3
+ * Tests sending user interactions via RPC
4
+ */
5
+
6
+ import type { Hex } from "viem";
7
+ import { vi } from "vitest";
8
+
9
+ // Mock computeProductId - must be before imports
10
+ vi.mock("../utils/computeProductId", () => ({
11
+ computeProductId: vi.fn(
12
+ () =>
13
+ "0xcomputed1234567890123456789012345678901234567890123456789012" as Hex
14
+ ),
15
+ }));
16
+
17
+ import { describe, expect, it } from "../../tests/vitest-fixtures";
18
+ import type {
19
+ FrakClient,
20
+ PreparedInteraction,
21
+ SendInteractionParamsType,
22
+ SendInteractionReturnType,
23
+ } from "../types";
24
+ import { sendInteraction } from "./sendInteraction";
25
+
26
+ describe("sendInteraction", () => {
27
+ const mockInteraction: PreparedInteraction = {
28
+ interactionData: "0xdata" as Hex,
29
+ handlerTypeDenominator: "0x01" as Hex,
30
+ };
31
+
32
+ describe("with productId provided", () => {
33
+ it("should use provided productId", async () => {
34
+ const mockResponse: SendInteractionReturnType = {
35
+ delegationId: "delegation-123",
36
+ };
37
+
38
+ const mockClient = {
39
+ config: {
40
+ domain: "example.com",
41
+ },
42
+ request: vi.fn().mockResolvedValue(mockResponse),
43
+ } as unknown as FrakClient;
44
+
45
+ const params: SendInteractionParamsType = {
46
+ productId:
47
+ "0xprovidedid567890123456789012345678901234567890123456789012" as Hex,
48
+ interaction: mockInteraction,
49
+ };
50
+
51
+ await sendInteraction(mockClient, params);
52
+
53
+ expect(mockClient.request).toHaveBeenCalledWith({
54
+ method: "frak_sendInteraction",
55
+ params: [
56
+ "0xprovidedid567890123456789012345678901234567890123456789012",
57
+ mockInteraction,
58
+ undefined,
59
+ ],
60
+ });
61
+ });
62
+
63
+ it("should return delegationId", async () => {
64
+ const mockResponse: SendInteractionReturnType = {
65
+ delegationId: "delegation-456",
66
+ };
67
+
68
+ const mockClient = {
69
+ config: {
70
+ domain: "example.com",
71
+ },
72
+ request: vi.fn().mockResolvedValue(mockResponse),
73
+ } as unknown as FrakClient;
74
+
75
+ const params: SendInteractionParamsType = {
76
+ productId:
77
+ "0xprovidedid567890123456789012345678901234567890123456789012" as Hex,
78
+ interaction: mockInteraction,
79
+ };
80
+
81
+ const result = await sendInteraction(mockClient, params);
82
+
83
+ expect(result).toEqual(mockResponse);
84
+ expect(result.delegationId).toBe("delegation-456");
85
+ });
86
+
87
+ it("should include validation signature when provided", async () => {
88
+ const mockResponse: SendInteractionReturnType = {
89
+ delegationId: "delegation-789",
90
+ };
91
+
92
+ const mockClient = {
93
+ config: {
94
+ domain: "example.com",
95
+ },
96
+ request: vi.fn().mockResolvedValue(mockResponse),
97
+ } as unknown as FrakClient;
98
+
99
+ const params: SendInteractionParamsType = {
100
+ productId:
101
+ "0xprovidedid567890123456789012345678901234567890123456789012" as Hex,
102
+ interaction: mockInteraction,
103
+ validation: "0xsignature1234567890" as Hex,
104
+ };
105
+
106
+ await sendInteraction(mockClient, params);
107
+
108
+ expect(mockClient.request).toHaveBeenCalledWith({
109
+ method: "frak_sendInteraction",
110
+ params: [
111
+ "0xprovidedid567890123456789012345678901234567890123456789012",
112
+ mockInteraction,
113
+ "0xsignature1234567890",
114
+ ],
115
+ });
116
+ });
117
+ });
118
+
119
+ describe("without productId provided", () => {
120
+ it("should compute productId from client.config", async () => {
121
+ const { computeProductId } = await import(
122
+ "../utils/computeProductId"
123
+ );
124
+
125
+ const mockResponse: SendInteractionReturnType = {
126
+ delegationId: "delegation-computed",
127
+ };
128
+
129
+ const mockClient = {
130
+ config: {
131
+ domain: "example.com",
132
+ },
133
+ request: vi.fn().mockResolvedValue(mockResponse),
134
+ } as unknown as FrakClient;
135
+
136
+ const params: SendInteractionParamsType = {
137
+ interaction: mockInteraction,
138
+ };
139
+
140
+ await sendInteraction(mockClient, params);
141
+
142
+ expect(computeProductId).toHaveBeenCalledWith(mockClient.config);
143
+ expect(mockClient.request).toHaveBeenCalledWith({
144
+ method: "frak_sendInteraction",
145
+ params: [
146
+ "0xcomputed1234567890123456789012345678901234567890123456789012",
147
+ mockInteraction,
148
+ undefined,
149
+ ],
150
+ });
151
+ });
152
+
153
+ it("should work with different interaction types", async () => {
154
+ const mockResponse: SendInteractionReturnType = {
155
+ delegationId: "delegation-different",
156
+ };
157
+
158
+ const mockClient = {
159
+ config: {
160
+ domain: "example.com",
161
+ },
162
+ request: vi.fn().mockResolvedValue(mockResponse),
163
+ } as unknown as FrakClient;
164
+
165
+ const differentInteraction: PreparedInteraction = {
166
+ interactionData: "0xdifferentdata" as Hex,
167
+ handlerTypeDenominator: "0x02" as Hex,
168
+ };
169
+
170
+ const params: SendInteractionParamsType = {
171
+ interaction: differentInteraction,
172
+ };
173
+
174
+ const result = await sendInteraction(mockClient, params);
175
+
176
+ expect(result).toEqual(mockResponse);
177
+ });
178
+ });
179
+
180
+ describe("error handling", () => {
181
+ it("should propagate errors from client.request", async () => {
182
+ const error = new Error("Send interaction failed");
183
+ const mockClient = {
184
+ config: {
185
+ domain: "example.com",
186
+ },
187
+ request: vi.fn().mockRejectedValue(error),
188
+ } as unknown as FrakClient;
189
+
190
+ const params: SendInteractionParamsType = {
191
+ productId:
192
+ "0xprovidedid567890123456789012345678901234567890123456789012" as Hex,
193
+ interaction: mockInteraction,
194
+ };
195
+
196
+ await expect(sendInteraction(mockClient, params)).rejects.toThrow(
197
+ "Send interaction failed"
198
+ );
199
+ });
200
+
201
+ it("should handle network errors", async () => {
202
+ const error = new Error("Network timeout");
203
+ const mockClient = {
204
+ config: {
205
+ domain: "example.com",
206
+ },
207
+ request: vi.fn().mockRejectedValue(error),
208
+ } as unknown as FrakClient;
209
+
210
+ const params: SendInteractionParamsType = {
211
+ interaction: mockInteraction,
212
+ };
213
+
214
+ await expect(sendInteraction(mockClient, params)).rejects.toThrow(
215
+ "Network timeout"
216
+ );
217
+ });
218
+ });
219
+ });
@@ -0,0 +1,32 @@
1
+ import type {
2
+ FrakClient,
3
+ SendInteractionParamsType,
4
+ SendInteractionReturnType,
5
+ } from "../types";
6
+ import { computeProductId } from "../utils/computeProductId";
7
+
8
+ /**
9
+ * Function used to send an interaction
10
+ * @param client - The current Frak Client
11
+ * @param args
12
+ *
13
+ * @example
14
+ * const interaction = PressInteractionEncoder.openArticle({
15
+ * articleId: keccak256(toHex("article-slug")),
16
+ * });
17
+ * const { delegationId } = await sendInteraction(frakConfig, {
18
+ * interaction,
19
+ * });
20
+ * console.log("Delegated interaction id", delegationId);
21
+ */
22
+ export async function sendInteraction(
23
+ client: FrakClient,
24
+ { productId, interaction, validation }: SendInteractionParamsType
25
+ ): Promise<SendInteractionReturnType> {
26
+ const pId = productId ?? computeProductId(client.config);
27
+
28
+ return await client.request({
29
+ method: "frak_sendInteraction",
30
+ params: [pId, interaction, validation],
31
+ });
32
+ }
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Tests for trackPurchaseStatus action
3
+ * Tests webhook registration for purchase tracking
4
+ */
5
+
6
+ import {
7
+ afterEach,
8
+ beforeEach,
9
+ describe,
10
+ expect,
11
+ it,
12
+ vi,
13
+ } from "../../tests/vitest-fixtures";
14
+ import { trackPurchaseStatus } from "./trackPurchaseStatus";
15
+
16
+ describe("trackPurchaseStatus", () => {
17
+ let mockSessionStorage: {
18
+ getItem: ReturnType<typeof vi.fn>;
19
+ setItem: ReturnType<typeof vi.fn>;
20
+ removeItem: ReturnType<typeof vi.fn>;
21
+ };
22
+ let fetchSpy: any;
23
+ let consoleWarnSpy: any;
24
+
25
+ beforeEach(() => {
26
+ // Mock sessionStorage
27
+ mockSessionStorage = {
28
+ getItem: vi.fn(),
29
+ setItem: vi.fn(),
30
+ removeItem: vi.fn(),
31
+ };
32
+ Object.defineProperty(window, "sessionStorage", {
33
+ value: mockSessionStorage,
34
+ writable: true,
35
+ configurable: true,
36
+ });
37
+
38
+ // Mock fetch
39
+ fetchSpy = vi.fn().mockResolvedValue({
40
+ ok: true,
41
+ status: 200,
42
+ });
43
+ global.fetch = fetchSpy;
44
+
45
+ // Mock console.warn
46
+ consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
47
+ });
48
+
49
+ afterEach(() => {
50
+ vi.clearAllMocks();
51
+ consoleWarnSpy.mockRestore();
52
+ });
53
+
54
+ describe("successful tracking", () => {
55
+ it("should send POST request with correct parameters", async () => {
56
+ mockSessionStorage.getItem.mockReturnValue("token-123");
57
+
58
+ await trackPurchaseStatus({
59
+ customerId: "cust-456",
60
+ orderId: "order-789",
61
+ token: "purchase-token",
62
+ });
63
+
64
+ expect(fetchSpy).toHaveBeenCalledWith(
65
+ "https://backend.frak.id/interactions/listenForPurchase",
66
+ {
67
+ method: "POST",
68
+ headers: {
69
+ Accept: "application/json",
70
+ "Content-Type": "application/json",
71
+ "x-wallet-sdk-auth": "token-123",
72
+ },
73
+ body: JSON.stringify({
74
+ customerId: "cust-456",
75
+ orderId: "order-789",
76
+ token: "purchase-token",
77
+ }),
78
+ }
79
+ );
80
+ });
81
+
82
+ it("should read interaction token from sessionStorage", async () => {
83
+ mockSessionStorage.getItem.mockReturnValue("my-token");
84
+
85
+ await trackPurchaseStatus({
86
+ customerId: "cust-1",
87
+ orderId: "order-1",
88
+ token: "token-1",
89
+ });
90
+
91
+ expect(mockSessionStorage.getItem).toHaveBeenCalledWith(
92
+ "frak-wallet-interaction-token"
93
+ );
94
+ expect(fetchSpy).toHaveBeenCalled();
95
+ });
96
+
97
+ it("should handle numeric customerId", async () => {
98
+ mockSessionStorage.getItem.mockReturnValue("token-123");
99
+
100
+ await trackPurchaseStatus({
101
+ customerId: 12345,
102
+ orderId: "order-789",
103
+ token: "purchase-token",
104
+ });
105
+
106
+ expect(fetchSpy).toHaveBeenCalledWith(
107
+ expect.any(String),
108
+ expect.objectContaining({
109
+ body: JSON.stringify({
110
+ customerId: 12345,
111
+ orderId: "order-789",
112
+ token: "purchase-token",
113
+ }),
114
+ })
115
+ );
116
+ });
117
+
118
+ it("should handle numeric orderId", async () => {
119
+ mockSessionStorage.getItem.mockReturnValue("token-123");
120
+
121
+ await trackPurchaseStatus({
122
+ customerId: "cust-456",
123
+ orderId: 67890,
124
+ token: "purchase-token",
125
+ });
126
+
127
+ expect(fetchSpy).toHaveBeenCalledWith(
128
+ expect.any(String),
129
+ expect.objectContaining({
130
+ body: JSON.stringify({
131
+ customerId: "cust-456",
132
+ orderId: 67890,
133
+ token: "purchase-token",
134
+ }),
135
+ })
136
+ );
137
+ });
138
+
139
+ it("should handle both numeric customerId and orderId", async () => {
140
+ mockSessionStorage.getItem.mockReturnValue("token-123");
141
+
142
+ await trackPurchaseStatus({
143
+ customerId: 12345,
144
+ orderId: 67890,
145
+ token: "purchase-token",
146
+ });
147
+
148
+ expect(fetchSpy).toHaveBeenCalledWith(
149
+ expect.any(String),
150
+ expect.objectContaining({
151
+ body: JSON.stringify({
152
+ customerId: 12345,
153
+ orderId: 67890,
154
+ token: "purchase-token",
155
+ }),
156
+ })
157
+ );
158
+ });
159
+ });
160
+
161
+ describe("missing interaction token", () => {
162
+ it("should warn when no interaction token found", async () => {
163
+ mockSessionStorage.getItem.mockReturnValue(null);
164
+
165
+ await trackPurchaseStatus({
166
+ customerId: "cust-456",
167
+ orderId: "order-789",
168
+ token: "purchase-token",
169
+ });
170
+
171
+ expect(consoleWarnSpy).toHaveBeenCalledWith(
172
+ "[Frak] No frak session found, skipping purchase check"
173
+ );
174
+ });
175
+
176
+ it("should not send request when no interaction token", async () => {
177
+ mockSessionStorage.getItem.mockReturnValue(null);
178
+
179
+ await trackPurchaseStatus({
180
+ customerId: "cust-456",
181
+ orderId: "order-789",
182
+ token: "purchase-token",
183
+ });
184
+
185
+ expect(fetchSpy).not.toHaveBeenCalled();
186
+ });
187
+
188
+ it("should not send request when interaction token is empty string", async () => {
189
+ mockSessionStorage.getItem.mockReturnValue("");
190
+
191
+ await trackPurchaseStatus({
192
+ customerId: "cust-456",
193
+ orderId: "order-789",
194
+ token: "purchase-token",
195
+ });
196
+
197
+ expect(fetchSpy).not.toHaveBeenCalled();
198
+ });
199
+ });
200
+
201
+ describe("network errors", () => {
202
+ it("should handle fetch rejection", async () => {
203
+ mockSessionStorage.getItem.mockReturnValue("token-123");
204
+ fetchSpy.mockRejectedValue(new Error("Network error"));
205
+
206
+ await expect(
207
+ trackPurchaseStatus({
208
+ customerId: "cust-456",
209
+ orderId: "order-789",
210
+ token: "purchase-token",
211
+ })
212
+ ).rejects.toThrow("Network error");
213
+ });
214
+
215
+ it("should handle fetch with error response", async () => {
216
+ mockSessionStorage.getItem.mockReturnValue("token-123");
217
+ fetchSpy.mockResolvedValue({
218
+ ok: false,
219
+ status: 500,
220
+ });
221
+
222
+ // Function doesn't check response, so it should complete
223
+ await trackPurchaseStatus({
224
+ customerId: "cust-456",
225
+ orderId: "order-789",
226
+ token: "purchase-token",
227
+ });
228
+
229
+ expect(fetchSpy).toHaveBeenCalled();
230
+ });
231
+ });
232
+
233
+ describe("request format", () => {
234
+ it("should include correct headers", async () => {
235
+ mockSessionStorage.getItem.mockReturnValue("my-auth-token");
236
+
237
+ await trackPurchaseStatus({
238
+ customerId: "cust-1",
239
+ orderId: "order-1",
240
+ token: "token-1",
241
+ });
242
+
243
+ expect(fetchSpy).toHaveBeenCalledWith(
244
+ expect.any(String),
245
+ expect.objectContaining({
246
+ headers: {
247
+ Accept: "application/json",
248
+ "Content-Type": "application/json",
249
+ "x-wallet-sdk-auth": "my-auth-token",
250
+ },
251
+ })
252
+ );
253
+ });
254
+
255
+ it("should use POST method", async () => {
256
+ mockSessionStorage.getItem.mockReturnValue("token-123");
257
+
258
+ await trackPurchaseStatus({
259
+ customerId: "cust-1",
260
+ orderId: "order-1",
261
+ token: "token-1",
262
+ });
263
+
264
+ expect(fetchSpy).toHaveBeenCalledWith(
265
+ expect.any(String),
266
+ expect.objectContaining({
267
+ method: "POST",
268
+ })
269
+ );
270
+ });
271
+
272
+ it("should target correct backend URL", async () => {
273
+ mockSessionStorage.getItem.mockReturnValue("token-123");
274
+
275
+ await trackPurchaseStatus({
276
+ customerId: "cust-1",
277
+ orderId: "order-1",
278
+ token: "token-1",
279
+ });
280
+
281
+ expect(fetchSpy).toHaveBeenCalledWith(
282
+ "https://backend.frak.id/interactions/listenForPurchase",
283
+ expect.any(Object)
284
+ );
285
+ });
286
+ });
287
+ });
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Function used to track the status of a purchase
3
+ * when a purchase is tracked, the `purchaseCompleted` interactions will be automatically send for the user when we receive the purchase confirmation via webhook.
4
+ *
5
+ * @param args.customerId - The customer id that made the purchase (on your side)
6
+ * @param args.orderId - The order id of the purchase (on your side)
7
+ * @param args.token - The token of the purchase
8
+ *
9
+ * @description This function will send a request to the backend to listen for the purchase status.
10
+ *
11
+ * @example
12
+ * async function trackPurchase(checkout) {
13
+ * const payload = {
14
+ * customerId: checkout.order.customer.id,
15
+ * orderId: checkout.order.id,
16
+ * token: checkout.token,
17
+ * };
18
+ *
19
+ * await trackPurchaseStatus(payload);
20
+ * }
21
+ *
22
+ * @remarks
23
+ * - The `trackPurchaseStatus` function requires the `frak-wallet-interaction-token` stored in the session storage to authenticate the request.
24
+ * - This function will print a warning if used in a non-browser environment or if the wallet interaction token is not available.
25
+ */
26
+ export async function trackPurchaseStatus(args: {
27
+ customerId: string | number;
28
+ orderId: string | number;
29
+ token: string;
30
+ }) {
31
+ if (typeof window === "undefined") {
32
+ console.warn("[Frak] No window found, can't track purchase");
33
+ return;
34
+ }
35
+ const interactionToken = window.sessionStorage.getItem(
36
+ "frak-wallet-interaction-token"
37
+ );
38
+ if (!interactionToken) {
39
+ console.warn("[Frak] No frak session found, skipping purchase check");
40
+ return;
41
+ }
42
+
43
+ // Submit the listening request
44
+ await fetch("https://backend.frak.id/interactions/listenForPurchase", {
45
+ method: "POST",
46
+ headers: {
47
+ Accept: "application/json",
48
+ "Content-Type": "application/json",
49
+ "x-wallet-sdk-auth": interactionToken,
50
+ },
51
+ body: JSON.stringify(args),
52
+ });
53
+ }