expo-iap 3.0.3 → 3.0.5

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 (70) hide show
  1. package/.eslintignore +1 -1
  2. package/.eslintrc.js +1 -0
  3. package/.prettierignore +1 -0
  4. package/CHANGELOG.md +13 -0
  5. package/CLAUDE.md +2 -0
  6. package/CONTRIBUTING.md +10 -0
  7. package/android/build.gradle +1 -1
  8. package/android/src/main/java/expo/modules/iap/ExpoIapModule.kt +12 -12
  9. package/android/src/main/java/expo/modules/iap/PromiseUtils.kt +2 -2
  10. package/build/helpers/subscription.d.ts +1 -12
  11. package/build/helpers/subscription.d.ts.map +1 -1
  12. package/build/helpers/subscription.js +12 -7
  13. package/build/helpers/subscription.js.map +1 -1
  14. package/build/index.d.ts +34 -18
  15. package/build/index.d.ts.map +1 -1
  16. package/build/index.js +40 -17
  17. package/build/index.js.map +1 -1
  18. package/build/modules/android.d.ts +5 -5
  19. package/build/modules/android.d.ts.map +1 -1
  20. package/build/modules/android.js +17 -4
  21. package/build/modules/android.js.map +1 -1
  22. package/build/modules/ios.d.ts +4 -8
  23. package/build/modules/ios.d.ts.map +1 -1
  24. package/build/modules/ios.js.map +1 -1
  25. package/build/purchase-error.d.ts +67 -0
  26. package/build/purchase-error.d.ts.map +1 -0
  27. package/build/purchase-error.js +166 -0
  28. package/build/purchase-error.js.map +1 -0
  29. package/build/types.d.ts +604 -0
  30. package/build/types.d.ts.map +1 -0
  31. package/build/types.js +42 -0
  32. package/build/types.js.map +1 -0
  33. package/build/useIAP.d.ts +8 -10
  34. package/build/useIAP.d.ts.map +1 -1
  35. package/build/useIAP.js +1 -1
  36. package/build/useIAP.js.map +1 -1
  37. package/build/utils/errorMapping.d.ts +1 -1
  38. package/build/utils/errorMapping.d.ts.map +1 -1
  39. package/build/utils/errorMapping.js +19 -3
  40. package/build/utils/errorMapping.js.map +1 -1
  41. package/ios/ExpoIap.podspec +1 -1
  42. package/ios/ExpoIapModule.swift +103 -89
  43. package/jest.config.js +1 -1
  44. package/package.json +2 -1
  45. package/plugin/build/withIAP.js +4 -5
  46. package/plugin/src/withIAP.ts +4 -5
  47. package/scripts/update-types.mjs +61 -0
  48. package/src/helpers/subscription.ts +12 -20
  49. package/src/index.ts +89 -41
  50. package/src/modules/android.ts +24 -8
  51. package/src/modules/ios.ts +7 -11
  52. package/src/purchase-error.ts +265 -0
  53. package/src/types.ts +705 -0
  54. package/src/useIAP.ts +16 -16
  55. package/src/utils/errorMapping.ts +24 -3
  56. package/build/ExpoIap.types.d.ts +0 -307
  57. package/build/ExpoIap.types.d.ts.map +0 -1
  58. package/build/ExpoIap.types.js +0 -235
  59. package/build/ExpoIap.types.js.map +0 -1
  60. package/build/types/ExpoIapAndroid.types.d.ts +0 -114
  61. package/build/types/ExpoIapAndroid.types.d.ts.map +0 -1
  62. package/build/types/ExpoIapAndroid.types.js +0 -29
  63. package/build/types/ExpoIapAndroid.types.js.map +0 -1
  64. package/build/types/ExpoIapIOS.types.d.ts +0 -149
  65. package/build/types/ExpoIapIOS.types.d.ts.map +0 -1
  66. package/build/types/ExpoIapIOS.types.js +0 -8
  67. package/build/types/ExpoIapIOS.types.js.map +0 -1
  68. package/src/ExpoIap.types.ts +0 -444
  69. package/src/types/ExpoIapAndroid.types.ts +0 -133
  70. package/src/types/ExpoIapIOS.types.ts +0 -172
@@ -0,0 +1,265 @@
1
+ import {NATIVE_ERROR_CODES} from './ExpoIapModule';
2
+ import {ErrorCode, IapPlatform} from './types';
3
+
4
+ /** Properties used to construct a {@link PurchaseError}. */
5
+ export interface PurchaseErrorProps {
6
+ message: string;
7
+ responseCode?: number;
8
+ debugMessage?: string;
9
+ code?: ErrorCode;
10
+ productId?: string;
11
+ platform?: IapPlatform;
12
+ }
13
+
14
+ /** Shape of raw platform error objects coming from native modules. */
15
+ type PlatformErrorData = {
16
+ code?: string | number;
17
+ message?: string;
18
+ responseCode?: number;
19
+ debugMessage?: string;
20
+ productId?: string;
21
+ };
22
+
23
+ const toStandardizedCode = (errorCode: ErrorCode): string =>
24
+ errorCode.startsWith('E_') ? errorCode : `E_${errorCode}`;
25
+
26
+ const normalizePlatform = (platform: IapPlatform): 'ios' | 'android' =>
27
+ typeof platform === 'string' && platform.toLowerCase() === 'ios'
28
+ ? 'ios'
29
+ : 'android';
30
+
31
+ const OPENIAP_ERROR_CODE_SET: Set<string> = new Set(
32
+ Object.values(ErrorCode).map((code) => toStandardizedCode(code)),
33
+ );
34
+
35
+ const COMMON_ERROR_CODE_MAP: Record<ErrorCode, string> = {
36
+ [ErrorCode.Unknown]: toStandardizedCode(ErrorCode.Unknown),
37
+ [ErrorCode.UserCancelled]: toStandardizedCode(ErrorCode.UserCancelled),
38
+ [ErrorCode.UserError]: toStandardizedCode(ErrorCode.UserError),
39
+ [ErrorCode.ItemUnavailable]: toStandardizedCode(ErrorCode.ItemUnavailable),
40
+ [ErrorCode.RemoteError]: toStandardizedCode(ErrorCode.RemoteError),
41
+ [ErrorCode.NetworkError]: toStandardizedCode(ErrorCode.NetworkError),
42
+ [ErrorCode.ServiceError]: toStandardizedCode(ErrorCode.ServiceError),
43
+ [ErrorCode.ReceiptFailed]: toStandardizedCode(ErrorCode.ReceiptFailed),
44
+ [ErrorCode.ReceiptFinished]: toStandardizedCode(ErrorCode.ReceiptFinished),
45
+ [ErrorCode.ReceiptFinishedFailed]: toStandardizedCode(
46
+ ErrorCode.ReceiptFinishedFailed,
47
+ ),
48
+ [ErrorCode.NotPrepared]: toStandardizedCode(ErrorCode.NotPrepared),
49
+ [ErrorCode.NotEnded]: toStandardizedCode(ErrorCode.NotEnded),
50
+ [ErrorCode.AlreadyOwned]: toStandardizedCode(ErrorCode.AlreadyOwned),
51
+ [ErrorCode.DeveloperError]: toStandardizedCode(ErrorCode.DeveloperError),
52
+ [ErrorCode.BillingResponseJsonParseError]: toStandardizedCode(
53
+ ErrorCode.BillingResponseJsonParseError,
54
+ ),
55
+ [ErrorCode.DeferredPayment]: toStandardizedCode(ErrorCode.DeferredPayment),
56
+ [ErrorCode.Interrupted]: toStandardizedCode(ErrorCode.Interrupted),
57
+ [ErrorCode.IapNotAvailable]: toStandardizedCode(ErrorCode.IapNotAvailable),
58
+ [ErrorCode.PurchaseError]: toStandardizedCode(ErrorCode.PurchaseError),
59
+ [ErrorCode.SyncError]: toStandardizedCode(ErrorCode.SyncError),
60
+ [ErrorCode.TransactionValidationFailed]: toStandardizedCode(
61
+ ErrorCode.TransactionValidationFailed,
62
+ ),
63
+ [ErrorCode.ActivityUnavailable]: toStandardizedCode(
64
+ ErrorCode.ActivityUnavailable,
65
+ ),
66
+ [ErrorCode.AlreadyPrepared]: toStandardizedCode(ErrorCode.AlreadyPrepared),
67
+ [ErrorCode.Pending]: toStandardizedCode(ErrorCode.Pending),
68
+ [ErrorCode.ConnectionClosed]: toStandardizedCode(ErrorCode.ConnectionClosed),
69
+ [ErrorCode.InitConnection]: toStandardizedCode(ErrorCode.InitConnection),
70
+ [ErrorCode.ServiceDisconnected]: toStandardizedCode(
71
+ ErrorCode.ServiceDisconnected,
72
+ ),
73
+ [ErrorCode.QueryProduct]: toStandardizedCode(ErrorCode.QueryProduct),
74
+ [ErrorCode.SkuNotFound]: toStandardizedCode(ErrorCode.SkuNotFound),
75
+ [ErrorCode.SkuOfferMismatch]: toStandardizedCode(ErrorCode.SkuOfferMismatch),
76
+ [ErrorCode.ItemNotOwned]: toStandardizedCode(ErrorCode.ItemNotOwned),
77
+ [ErrorCode.BillingUnavailable]: toStandardizedCode(
78
+ ErrorCode.BillingUnavailable,
79
+ ),
80
+ [ErrorCode.FeatureNotSupported]: toStandardizedCode(
81
+ ErrorCode.FeatureNotSupported,
82
+ ),
83
+ [ErrorCode.EmptySkuList]: toStandardizedCode(ErrorCode.EmptySkuList),
84
+ };
85
+
86
+ /**
87
+ * Mapping between platforms and their canonical error codes.
88
+ * Values are platform-native string identifiers.
89
+ */
90
+ export const ErrorCodeMapping = {
91
+ ios: COMMON_ERROR_CODE_MAP,
92
+ android: COMMON_ERROR_CODE_MAP,
93
+ } as const;
94
+
95
+ /**
96
+ * Error thrown by expo-iap when purchases fail.
97
+ */
98
+ export class PurchaseError extends Error {
99
+ public responseCode?: number;
100
+ public debugMessage?: string;
101
+ public code?: ErrorCode;
102
+ public productId?: string;
103
+ public platform?: IapPlatform;
104
+
105
+ constructor(
106
+ message: string,
107
+ responseCode?: number,
108
+ debugMessage?: string,
109
+ code?: ErrorCode,
110
+ productId?: string,
111
+ platform?: IapPlatform,
112
+ );
113
+ constructor(props: PurchaseErrorProps);
114
+ constructor(
115
+ messageOrProps: string | PurchaseErrorProps,
116
+ responseCode?: number,
117
+ debugMessage?: string,
118
+ code?: ErrorCode,
119
+ productId?: string,
120
+ platform?: IapPlatform,
121
+ ) {
122
+ super(
123
+ typeof messageOrProps === 'string'
124
+ ? messageOrProps
125
+ : messageOrProps.message,
126
+ );
127
+ this.name = '[expo-iap]: PurchaseError';
128
+ Object.setPrototypeOf(this, new.target.prototype);
129
+
130
+ if (typeof messageOrProps === 'string') {
131
+ this.responseCode = responseCode;
132
+ this.debugMessage = debugMessage;
133
+ this.code = code;
134
+ this.productId = productId;
135
+ this.platform = platform;
136
+ } else {
137
+ this.responseCode = messageOrProps.responseCode;
138
+ this.debugMessage = messageOrProps.debugMessage;
139
+ this.code = messageOrProps.code;
140
+ this.productId = messageOrProps.productId;
141
+ this.platform = messageOrProps.platform;
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Create a {@link PurchaseError} from raw platform error data.
147
+ */
148
+ static fromPlatformError(
149
+ errorData: PlatformErrorData,
150
+ platform: IapPlatform,
151
+ ): PurchaseError {
152
+ const normalizedPlatform = normalizePlatform(platform);
153
+
154
+ const errorCode = errorData.code
155
+ ? ErrorCodeUtils.fromPlatformCode(errorData.code, normalizedPlatform)
156
+ : ErrorCode.Unknown;
157
+
158
+ return new PurchaseError({
159
+ message: errorData.message ?? 'Unknown error occurred',
160
+ responseCode: errorData.responseCode,
161
+ debugMessage: errorData.debugMessage,
162
+ code: errorCode,
163
+ productId: errorData.productId,
164
+ platform,
165
+ });
166
+ }
167
+
168
+ /**
169
+ * Returns the platform specific error code for this instance.
170
+ */
171
+ getPlatformCode(): string | number | undefined {
172
+ if (!this.code || !this.platform) {
173
+ return undefined;
174
+ }
175
+ return ErrorCodeUtils.toPlatformCode(this.code, this.platform);
176
+ }
177
+ }
178
+
179
+ /** Utility helpers for translating error codes between platforms. */
180
+ export const ErrorCodeUtils = {
181
+ /**
182
+ * Returns the native error code for the provided {@link ErrorCode}.
183
+ */
184
+ getNativeErrorCode: (errorCode: ErrorCode): string => {
185
+ const standardized = toStandardizedCode(errorCode);
186
+ return (
187
+ (NATIVE_ERROR_CODES as Record<string, string | undefined>)[
188
+ standardized
189
+ ] ?? standardized
190
+ );
191
+ },
192
+ /**
193
+ * Converts a platform-specific error code into a standardized {@link ErrorCode}.
194
+ */
195
+ fromPlatformCode: (
196
+ platformCode: string | number,
197
+ _platform: IapPlatform,
198
+ ): ErrorCode => {
199
+ if (typeof platformCode === 'string' && platformCode.startsWith('E_')) {
200
+ if (OPENIAP_ERROR_CODE_SET.has(platformCode)) {
201
+ const match = Object.entries(COMMON_ERROR_CODE_MAP).find(
202
+ ([, value]) => value === platformCode,
203
+ );
204
+ if (match) {
205
+ return match[0] as ErrorCode;
206
+ }
207
+ }
208
+ }
209
+
210
+ for (const [standardized, nativeCode] of Object.entries(
211
+ (NATIVE_ERROR_CODES || {}) as Record<string, string | number>,
212
+ )) {
213
+ if (
214
+ nativeCode === platformCode &&
215
+ OPENIAP_ERROR_CODE_SET.has(standardized)
216
+ ) {
217
+ const match = Object.entries(COMMON_ERROR_CODE_MAP).find(
218
+ ([, mappedCode]) => mappedCode === standardized,
219
+ );
220
+ if (match) {
221
+ return match[0] as ErrorCode;
222
+ }
223
+ }
224
+ }
225
+
226
+ for (const [errorCode, mappedCode] of Object.entries(
227
+ COMMON_ERROR_CODE_MAP,
228
+ )) {
229
+ if (mappedCode === platformCode) {
230
+ return errorCode as ErrorCode;
231
+ }
232
+ }
233
+
234
+ return ErrorCode.Unknown;
235
+ },
236
+ /**
237
+ * Converts a standardized {@link ErrorCode} into its platform-specific value.
238
+ */
239
+ toPlatformCode: (
240
+ errorCode: ErrorCode,
241
+ _platform: IapPlatform,
242
+ ): string | number => {
243
+ const standardized = toStandardizedCode(errorCode);
244
+ const native = (NATIVE_ERROR_CODES as Record<string, string | number>)[
245
+ standardized
246
+ ];
247
+ return native ?? COMMON_ERROR_CODE_MAP[errorCode] ?? 'E_UNKNOWN';
248
+ },
249
+ /**
250
+ * Determines whether the error code is supported on the given platform.
251
+ */
252
+ isValidForPlatform: (
253
+ errorCode: ErrorCode,
254
+ platform: IapPlatform,
255
+ ): boolean => {
256
+ const standardized = toStandardizedCode(errorCode);
257
+ if (
258
+ (NATIVE_ERROR_CODES as Record<string, unknown>)[standardized] !==
259
+ undefined
260
+ ) {
261
+ return true;
262
+ }
263
+ return standardized in ErrorCodeMapping[normalizePlatform(platform)];
264
+ },
265
+ };