expo-iap 2.9.6 → 3.0.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.
Files changed (56) hide show
  1. package/.eslintrc.js +24 -0
  2. package/CHANGELOG.md +43 -0
  3. package/README.md +1 -1
  4. package/android/build.gradle +7 -2
  5. package/android/src/main/java/expo/modules/iap/ExpoIapModule.kt +195 -668
  6. package/android/src/main/java/expo/modules/iap/PromiseUtils.kt +85 -0
  7. package/build/ExpoIap.types.d.ts +0 -6
  8. package/build/ExpoIap.types.d.ts.map +1 -1
  9. package/build/ExpoIap.types.js.map +1 -1
  10. package/build/helpers/subscription.d.ts.map +1 -1
  11. package/build/helpers/subscription.js +14 -3
  12. package/build/helpers/subscription.js.map +1 -1
  13. package/build/index.d.ts +6 -73
  14. package/build/index.d.ts.map +1 -1
  15. package/build/index.js +21 -154
  16. package/build/index.js.map +1 -1
  17. package/build/modules/android.d.ts +2 -2
  18. package/build/modules/android.d.ts.map +1 -1
  19. package/build/modules/android.js +11 -1
  20. package/build/modules/android.js.map +1 -1
  21. package/build/modules/ios.d.ts +0 -60
  22. package/build/modules/ios.d.ts.map +1 -1
  23. package/build/modules/ios.js +2 -121
  24. package/build/modules/ios.js.map +1 -1
  25. package/build/types/ExpoIapAndroid.types.d.ts +0 -8
  26. package/build/types/ExpoIapAndroid.types.d.ts.map +1 -1
  27. package/build/types/ExpoIapAndroid.types.js +0 -1
  28. package/build/types/ExpoIapAndroid.types.js.map +1 -1
  29. package/build/types/ExpoIapIOS.types.d.ts +0 -5
  30. package/build/types/ExpoIapIOS.types.d.ts.map +1 -1
  31. package/build/types/ExpoIapIOS.types.js.map +1 -1
  32. package/build/useIAP.d.ts +0 -18
  33. package/build/useIAP.d.ts.map +1 -1
  34. package/build/useIAP.js +1 -18
  35. package/build/useIAP.js.map +1 -1
  36. package/bun.lock +340 -137
  37. package/codecov.yml +17 -21
  38. package/ios/ExpoIapModule.swift +50 -23
  39. package/jest.config.js +5 -9
  40. package/package.json +5 -3
  41. package/plugin/build/withIAP.d.ts +4 -1
  42. package/plugin/build/withIAP.js +38 -24
  43. package/plugin/build/withLocalOpenIAP.d.ts +6 -2
  44. package/plugin/build/withLocalOpenIAP.js +175 -20
  45. package/plugin/src/withIAP.ts +66 -30
  46. package/plugin/src/withLocalOpenIAP.ts +228 -24
  47. package/src/ExpoIap.types.ts +0 -8
  48. package/src/helpers/subscription.ts +14 -3
  49. package/src/index.ts +22 -230
  50. package/src/modules/android.ts +16 -6
  51. package/src/modules/ios.ts +2 -168
  52. package/src/types/ExpoIapAndroid.types.ts +0 -11
  53. package/src/types/ExpoIapIOS.types.ts +0 -5
  54. package/src/useIAP.ts +3 -55
  55. package/android/src/main/java/expo/modules/iap/PlayUtils.kt +0 -178
  56. package/android/src/main/java/expo/modules/iap/Types.kt +0 -98
@@ -33,51 +33,61 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- const config_plugins_1 = require("@expo/config-plugins");
36
+ const config_plugins_1 = require("expo/config-plugins");
37
37
  const fs = __importStar(require("fs"));
38
38
  const path = __importStar(require("path"));
39
- /**
40
- * Plugin to add local OpenIAP pod dependency for development
41
- * This is only for local development with openiap-apple library
42
- */
43
39
  const withLocalOpenIAP = (config, props) => {
44
- return (0, config_plugins_1.withDangerousMod)(config, [
40
+ // Helper to resolve Android module path
41
+ const resolveAndroidModulePath = (p) => {
42
+ if (!p)
43
+ return null;
44
+ // Prefer the module directory if it exists
45
+ const candidates = [
46
+ path.join(p, 'openiap-google'),
47
+ path.join(p, 'openiap'),
48
+ p,
49
+ ];
50
+ for (const c of candidates) {
51
+ if (fs.existsSync(path.join(c, 'build.gradle')) ||
52
+ fs.existsSync(path.join(c, 'build.gradle.kts'))) {
53
+ return c;
54
+ }
55
+ }
56
+ return null;
57
+ };
58
+ // iOS: inject local pod path
59
+ config = (0, config_plugins_1.withDangerousMod)(config, [
45
60
  'ios',
46
61
  async (config) => {
47
- const { platformProjectRoot } = config.modRequest;
62
+ const { platformProjectRoot, projectRoot } = config.modRequest;
63
+ const raw = props?.localPath;
64
+ const iosPath = (typeof raw === 'string' ? raw : raw?.ios) ||
65
+ path.resolve(projectRoot, 'openiap-apple');
48
66
  const podfilePath = path.join(platformProjectRoot, 'Podfile');
49
- // Default local path or use provided one
50
- const localOpenIapPath = props?.localPath ||
51
- path.resolve(config.modRequest.projectRoot, 'openiap-apple');
52
- // Check if local path exists
53
- if (!fs.existsSync(localOpenIapPath)) {
54
- console.warn(`⚠️ Local openiap-apple path not found: ${localOpenIapPath}`);
55
- console.warn(' Skipping local pod injection. Using default pod resolution.');
67
+ if (!fs.existsSync(iosPath)) {
68
+ console.warn(`⚠️ Local openiap-apple path not found: ${iosPath}`);
69
+ console.warn(' Skipping local pod injection.');
56
70
  return config;
57
71
  }
58
- // Read Podfile
59
72
  if (!fs.existsSync(podfilePath)) {
60
73
  console.warn(`⚠️ Podfile not found at ${podfilePath}. Skipping.`);
61
74
  return config;
62
75
  }
63
76
  let podfileContent = fs.readFileSync(podfilePath, 'utf8');
64
- // Check if already has the local pod reference
65
77
  if (podfileContent.includes("pod 'openiap',")) {
66
78
  console.log('✅ Local OpenIAP pod already configured');
67
79
  return config;
68
80
  }
69
- // Find the target block and inject the local pod
70
81
  const targetRegex = /target\s+['"][\w]+['"]\s+do\s*\n\s*use_expo_modules!/;
71
82
  if (targetRegex.test(podfileContent)) {
72
83
  podfileContent = podfileContent.replace(targetRegex, (match) => {
73
84
  return `${match}
74
85
 
75
86
  # Local OpenIAP pod for development (added by expo-iap plugin)
76
- pod 'openiap', :path => '${localOpenIapPath}'`;
87
+ pod 'openiap', :path => '${iosPath}'`;
77
88
  });
78
- // Write back to Podfile
79
89
  fs.writeFileSync(podfilePath, podfileContent);
80
- console.log(`✅ Added local OpenIAP pod at: ${localOpenIapPath}`);
90
+ console.log(`✅ Added local OpenIAP pod at: ${iosPath}`);
81
91
  }
82
92
  else {
83
93
  console.warn('⚠️ Could not find target block in Podfile');
@@ -85,5 +95,150 @@ const withLocalOpenIAP = (config, props) => {
85
95
  return config;
86
96
  },
87
97
  ]);
98
+ // Android: include local module and add dependency if available
99
+ config = (0, config_plugins_1.withSettingsGradle)(config, (config) => {
100
+ const raw = props?.localPath;
101
+ const projectRoot = config.modRequest.projectRoot;
102
+ const androidInput = typeof raw === 'string' ? undefined : raw?.android;
103
+ const androidModulePath = resolveAndroidModulePath(androidInput) ||
104
+ resolveAndroidModulePath(path.resolve(projectRoot, 'openiap-google')) ||
105
+ null;
106
+ if (!androidModulePath || !fs.existsSync(androidModulePath)) {
107
+ if (androidInput) {
108
+ console.warn(`⚠️ Could not resolve Android OpenIAP module at: ${androidInput}. Skipping local Android linkage.`);
109
+ }
110
+ return config;
111
+ }
112
+ // 1) settings.gradle: include and map projectDir
113
+ const settings = config.modResults;
114
+ const includeLine = "include ':openiap-google'";
115
+ const projectDirLine = `project(':openiap-google').projectDir = new File('${androidModulePath.replace(/\\/g, '/')}')`;
116
+ let contents = settings.contents ?? '';
117
+ // Ensure pluginManagement has plugin mappings required by the included module
118
+ const injectPluginManagement = () => {
119
+ const header = 'pluginManagement {';
120
+ const needsVannik = !/id\s*\(\s*["']com\.vanniktech\.maven\.publish["']/.test(contents);
121
+ const needsKotlinAndroid = !/id\s*\(\s*["']org\.jetbrains\.kotlin\.android["']/.test(contents);
122
+ const needsCompose = !/id\s*\(\s*["']org\.jetbrains\.kotlin\.plugin\.compose["']/.test(contents);
123
+ const needsRepos = !/pluginManagement[\s\S]*?repositories\s*\{/.test(contents);
124
+ const pluginLines = [];
125
+ if (needsVannik)
126
+ pluginLines.push(` id("com.vanniktech.maven.publish") version "0.29.0"`);
127
+ if (needsKotlinAndroid)
128
+ pluginLines.push(` id("org.jetbrains.kotlin.android") version "2.0.21"`);
129
+ if (needsCompose)
130
+ pluginLines.push(` id("org.jetbrains.kotlin.plugin.compose") version "2.0.21"`);
131
+ // If everything already present, skip
132
+ if (pluginLines.length === 0 && !needsRepos)
133
+ return;
134
+ const pluginsBlock = pluginLines.length
135
+ ? `plugins {\n${pluginLines.join('\n')}\n}`
136
+ : '';
137
+ const reposBlock = `repositories { gradlePluginPortal(); google(); mavenCentral() }`;
138
+ if (contents.includes(header)) {
139
+ contents = contents.replace(/pluginManagement\s*\{/, (m) => {
140
+ let injection = m + `\n // Added by expo-iap (local openiap-google)\n`;
141
+ if (pluginsBlock)
142
+ injection += ` ${pluginsBlock}\n`;
143
+ if (needsRepos)
144
+ injection += ` ${reposBlock}\n`;
145
+ return injection;
146
+ });
147
+ }
148
+ else {
149
+ contents =
150
+ `pluginManagement {\n // Added by expo-iap (local openiap-google)\n` +
151
+ (pluginsBlock ? ` ${pluginsBlock}\n` : '') +
152
+ ` ${reposBlock}\n}\n\n${contents}`;
153
+ }
154
+ };
155
+ if (!/com\.vanniktech\.maven\.publish/.test(contents) ||
156
+ !/org\.jetbrains\.kotlin\.android/.test(contents)) {
157
+ injectPluginManagement();
158
+ }
159
+ if (!contents.includes(includeLine))
160
+ contents += `\n${includeLine}\n`;
161
+ if (!contents.includes(projectDirLine))
162
+ contents += `${projectDirLine}\n`;
163
+ settings.contents = contents;
164
+ console.log(`✅ Linked local Android module at: ${androidModulePath}`);
165
+ return config;
166
+ });
167
+ // 2) app/build.gradle: add implementation project(':openiap-google')
168
+ config = (0, config_plugins_1.withAppBuildGradle)(config, (config) => {
169
+ const raw = props?.localPath;
170
+ const projectRoot = config.modRequest.projectRoot;
171
+ const androidInput = typeof raw === 'string' ? undefined : raw?.android;
172
+ const androidModulePath = resolveAndroidModulePath(androidInput) ||
173
+ resolveAndroidModulePath(path.resolve(projectRoot, 'openiap-google')) ||
174
+ null;
175
+ if (!androidModulePath || !fs.existsSync(androidModulePath)) {
176
+ return config;
177
+ }
178
+ const gradle = config.modResults;
179
+ const dependencyLine = ` implementation project(':openiap-google')`;
180
+ // Remove any previously injected external deps that can conflict with the local module
181
+ const removalPatterns = [
182
+ /\n\s*implementation\s+"com\.android\.billingclient:billing-ktx:[^"]+"\s*\n/g,
183
+ /\n\s*implementation\s+"com\.google\.android\.gms:play-services-base:[^"]+"\s*\n/g,
184
+ ];
185
+ let contents = gradle.contents;
186
+ let removedAny = false;
187
+ for (const pattern of removalPatterns) {
188
+ if (pattern.test(contents)) {
189
+ contents = contents.replace(pattern, '\n');
190
+ removedAny = true;
191
+ }
192
+ }
193
+ if (removedAny) {
194
+ gradle.contents = contents;
195
+ console.log('🧹 Removed external Play Billing/GMS deps to use local :openiap-google');
196
+ }
197
+ if (!gradle.contents.includes(dependencyLine)) {
198
+ const anchor = /dependencies\s*\{/m;
199
+ if (anchor.test(gradle.contents)) {
200
+ gradle.contents = gradle.contents.replace(anchor, (m) => `${m}\n${dependencyLine}`);
201
+ }
202
+ else {
203
+ gradle.contents += `\n\ndependencies {\n${dependencyLine}\n}\n`;
204
+ }
205
+ console.log('🛠️ Added dependency on local :openiap-google project');
206
+ }
207
+ return config;
208
+ });
209
+ // 3) Ensure final cleanup in app/build.gradle after all mods are applied
210
+ config = (0, config_plugins_1.withDangerousMod)(config, [
211
+ 'android',
212
+ async (config) => {
213
+ try {
214
+ const { platformProjectRoot } = config.modRequest;
215
+ const appBuildGradle = path.join(platformProjectRoot, 'app', 'build.gradle');
216
+ if (fs.existsSync(appBuildGradle)) {
217
+ let contents = fs.readFileSync(appBuildGradle, 'utf8');
218
+ const patterns = [
219
+ /\n\s*implementation\s+"com\.android\.billingclient:billing-ktx:[^"]+"\s*\n/g,
220
+ /\n\s*implementation\s+"com\.google\.android\.gms:play-services-base:[^"]+"\s*\n/g,
221
+ ];
222
+ let changed = false;
223
+ for (const p of patterns) {
224
+ if (p.test(contents)) {
225
+ contents = contents.replace(p, '\n');
226
+ changed = true;
227
+ }
228
+ }
229
+ if (changed) {
230
+ fs.writeFileSync(appBuildGradle, contents);
231
+ console.log('🧹 expo-iap: Cleaned app/build.gradle billing/gms deps for local :openiap-google');
232
+ }
233
+ }
234
+ }
235
+ catch (e) {
236
+ console.warn('expo-iap: cleanup step failed:', e);
237
+ }
238
+ return config;
239
+ },
240
+ ]);
241
+ // (removed) Avoid global root build.gradle mutations; included module should manage its plugins
242
+ return config;
88
243
  };
89
244
  exports.default = withLocalOpenIAP;
@@ -32,49 +32,67 @@ const addLineToGradle = (
32
32
  const lines = content.split('\n');
33
33
  const index = lines.findIndex((line) => line.match(anchor));
34
34
  if (index === -1) {
35
- console.warn(
36
- `Anchor "${anchor}" not found in build.gradle. Appending to end.`,
35
+ WarningAggregator.addWarningAndroid(
36
+ 'expo-iap',
37
+ `dependencies { ... } block not found; skipping injection: ${lineToAdd.trim()}`,
37
38
  );
38
- lines.push(lineToAdd);
39
+ return content;
39
40
  } else {
40
41
  lines.splice(index + offset, 0, lineToAdd);
41
42
  }
42
43
  return lines.join('\n');
43
44
  };
44
45
 
45
- const modifyAppBuildGradle = (gradle: string): string => {
46
+ const modifyAppBuildGradle = (
47
+ gradle: string,
48
+ language: 'groovy' | 'kotlin',
49
+ ): string => {
46
50
  let modified = gradle;
47
51
 
48
- // Add billing library dependencies to app-level build.gradle
49
- const billingDep = ` implementation "com.android.billingclient:billing-ktx:8.0.0"`;
50
- const gmsDep = ` implementation "com.google.android.gms:play-services-base:18.1.0"`;
52
+ // Add OpenIAP dependency to app-level build.gradle(.kts)
53
+ const impl = (ga: string, v: string) =>
54
+ language === 'kotlin'
55
+ ? ` implementation("${ga}:${v}")`
56
+ : ` implementation "${ga}:${v}"`;
57
+ // Pin OpenIAP Google library to 1.1.0
58
+ const openiapDep = impl('io.github.hyochan.openiap:openiap-google', '1.1.0');
59
+
60
+ const hasGA = (ga: string) =>
61
+ new RegExp(String.raw`\b(?:implementation|api)\s*\(?["']${ga}:`, 'm').test(
62
+ modified,
63
+ );
51
64
 
52
65
  let hasAddedDependency = false;
53
66
 
54
- if (!modified.includes(billingDep)) {
55
- modified = addLineToGradle(modified, /dependencies\s*{/, billingDep);
56
- hasAddedDependency = true;
57
- }
58
- if (!modified.includes(gmsDep)) {
59
- modified = addLineToGradle(modified, /dependencies\s*{/, gmsDep, 1);
67
+ if (!hasGA('io.github.hyochan.openiap:openiap-google')) {
68
+ modified = addLineToGradle(modified, /dependencies\s*{/, openiapDep, 0);
60
69
  hasAddedDependency = true;
61
70
  }
62
71
 
63
72
  // Log only once and only if we actually added dependencies
64
73
  if (hasAddedDependency)
65
- logOnce('🛠️ expo-iap: Added billing dependencies to build.gradle');
74
+ logOnce('🛠️ expo-iap: Added OpenIAP dependency to build.gradle');
66
75
 
67
76
  return modified;
68
77
  };
69
78
 
70
- const withIapAndroid: ConfigPlugin = (config) => {
71
- // Add IAP dependencies to app build.gradle
72
- config = withAppBuildGradle(config, (config) => {
73
- config.modResults.contents = modifyAppBuildGradle(
74
- config.modResults.contents,
75
- );
76
- return config;
77
- });
79
+ const withIapAndroid: ConfigPlugin<{addDeps?: boolean} | void> = (
80
+ config,
81
+ props,
82
+ ) => {
83
+ const addDeps = props?.addDeps ?? true;
84
+
85
+ if (addDeps) {
86
+ config = withAppBuildGradle(config, (config) => {
87
+ // language provided by config-plugins: 'groovy' | 'kotlin'
88
+ const language = (config.modResults as any).language || 'groovy';
89
+ config.modResults.contents = modifyAppBuildGradle(
90
+ config.modResults.contents,
91
+ language,
92
+ );
93
+ return config;
94
+ });
95
+ }
78
96
 
79
97
  config = withAndroidManifest(config, (config) => {
80
98
  const manifest = config.modResults;
@@ -140,7 +158,12 @@ const withIapIOS: ConfigPlugin = (config) => {
140
158
 
141
159
  export interface ExpoIapPluginOptions {
142
160
  /** Local development path for OpenIAP library */
143
- localPath?: string;
161
+ localPath?:
162
+ | string
163
+ | {
164
+ ios?: string;
165
+ android?: string;
166
+ };
144
167
  /** Enable local development mode */
145
168
  enableLocalDev?: boolean;
146
169
  }
@@ -150,8 +173,9 @@ const withIap: ConfigPlugin<ExpoIapPluginOptions | void> = (
150
173
  options,
151
174
  ) => {
152
175
  try {
153
- // Apply Android modifications
154
- let result = withIapAndroid(config);
176
+ const isLocalDev = !!(options?.enableLocalDev || options?.localPath);
177
+ // Apply Android modifications (skip adding deps when linking local module)
178
+ let result = withIapAndroid(config, {addDeps: !isLocalDev});
155
179
 
156
180
  // iOS: choose one path to avoid overlap
157
181
  if (options?.enableLocalDev || options?.localPath) {
@@ -161,11 +185,23 @@ const withIap: ConfigPlugin<ExpoIapPluginOptions | void> = (
161
185
  'enableLocalDev is true but no localPath provided. Skipping local OpenIAP integration.',
162
186
  );
163
187
  } else {
164
- const localPath = path.resolve(options.localPath);
165
- logOnce(
166
- `🔧 [expo-iap] Enabling local OpenIAP development at: ${localPath}`,
167
- );
168
- result = withLocalOpenIAP(result, {localPath});
188
+ const raw = options.localPath;
189
+ const resolved =
190
+ typeof raw === 'string'
191
+ ? path.resolve(raw)
192
+ : {
193
+ ios: raw.ios ? path.resolve(raw.ios) : undefined,
194
+ android: raw.android ? path.resolve(raw.android) : undefined,
195
+ };
196
+
197
+ const preview =
198
+ typeof resolved === 'string'
199
+ ? resolved
200
+ : `ios=${resolved.ios ?? 'auto'}, android=${
201
+ resolved.android ?? 'auto'
202
+ }`;
203
+ logOnce(`🔧 [expo-iap] Enabling local OpenIAP: ${preview}`);
204
+ result = withLocalOpenIAP(result, {localPath: resolved});
169
205
  }
170
206
  } else {
171
207
  // Ensure iOS Podfile is set up to resolve public CocoaPods specs
@@ -1,4 +1,9 @@
1
- import {ConfigPlugin, withDangerousMod} from '@expo/config-plugins';
1
+ import {
2
+ ConfigPlugin,
3
+ withDangerousMod,
4
+ withSettingsGradle,
5
+ withAppBuildGradle,
6
+ } from 'expo/config-plugins';
2
7
  import * as fs from 'fs';
3
8
  import * as path from 'path';
4
9
 
@@ -6,46 +11,60 @@ import * as path from 'path';
6
11
  * Plugin to add local OpenIAP pod dependency for development
7
12
  * This is only for local development with openiap-apple library
8
13
  */
9
- const withLocalOpenIAP: ConfigPlugin<{localPath?: string} | void> = (
14
+ type LocalPathOption = string | {ios?: string; android?: string};
15
+
16
+ const withLocalOpenIAP: ConfigPlugin<{localPath?: LocalPathOption} | void> = (
10
17
  config,
11
18
  props,
12
19
  ) => {
13
- return withDangerousMod(config, [
20
+ // Helper to resolve Android module path
21
+ const resolveAndroidModulePath = (p?: string): string | null => {
22
+ if (!p) return null;
23
+ // Prefer the module directory if it exists
24
+ const candidates = [
25
+ path.join(p, 'openiap-google'),
26
+ path.join(p, 'openiap'),
27
+ p,
28
+ ];
29
+ for (const c of candidates) {
30
+ if (
31
+ fs.existsSync(path.join(c, 'build.gradle')) ||
32
+ fs.existsSync(path.join(c, 'build.gradle.kts'))
33
+ ) {
34
+ return c;
35
+ }
36
+ }
37
+ return null;
38
+ };
39
+
40
+ // iOS: inject local pod path
41
+ config = withDangerousMod(config, [
14
42
  'ios',
15
43
  async (config) => {
16
- const {platformProjectRoot} = config.modRequest;
44
+ const {platformProjectRoot, projectRoot} = config.modRequest as any;
45
+ const raw = props?.localPath;
46
+ const iosPath =
47
+ (typeof raw === 'string' ? raw : raw?.ios) ||
48
+ path.resolve(projectRoot, 'openiap-apple');
17
49
  const podfilePath = path.join(platformProjectRoot, 'Podfile');
18
50
 
19
- // Default local path or use provided one
20
- const localOpenIapPath =
21
- props?.localPath ||
22
- path.resolve(config.modRequest.projectRoot, 'openiap-apple');
23
-
24
- // Check if local path exists
25
- if (!fs.existsSync(localOpenIapPath)) {
26
- console.warn(
27
- `⚠️ Local openiap-apple path not found: ${localOpenIapPath}`,
28
- );
29
- console.warn(
30
- ' Skipping local pod injection. Using default pod resolution.',
31
- );
51
+ if (!fs.existsSync(iosPath)) {
52
+ console.warn(`⚠️ Local openiap-apple path not found: ${iosPath}`);
53
+ console.warn(' Skipping local pod injection.');
32
54
  return config;
33
55
  }
34
56
 
35
- // Read Podfile
36
57
  if (!fs.existsSync(podfilePath)) {
37
58
  console.warn(`⚠️ Podfile not found at ${podfilePath}. Skipping.`);
38
59
  return config;
39
60
  }
40
61
  let podfileContent = fs.readFileSync(podfilePath, 'utf8');
41
62
 
42
- // Check if already has the local pod reference
43
63
  if (podfileContent.includes("pod 'openiap',")) {
44
64
  console.log('✅ Local OpenIAP pod already configured');
45
65
  return config;
46
66
  }
47
67
 
48
- // Find the target block and inject the local pod
49
68
  const targetRegex =
50
69
  /target\s+['"][\w]+['"]\s+do\s*\n\s*use_expo_modules!/;
51
70
 
@@ -54,12 +73,10 @@ const withLocalOpenIAP: ConfigPlugin<{localPath?: string} | void> = (
54
73
  return `${match}
55
74
 
56
75
  # Local OpenIAP pod for development (added by expo-iap plugin)
57
- pod 'openiap', :path => '${localOpenIapPath}'`;
76
+ pod 'openiap', :path => '${iosPath}'`;
58
77
  });
59
-
60
- // Write back to Podfile
61
78
  fs.writeFileSync(podfilePath, podfileContent);
62
- console.log(`✅ Added local OpenIAP pod at: ${localOpenIapPath}`);
79
+ console.log(`✅ Added local OpenIAP pod at: ${iosPath}`);
63
80
  } else {
64
81
  console.warn('⚠️ Could not find target block in Podfile');
65
82
  }
@@ -67,6 +84,193 @@ const withLocalOpenIAP: ConfigPlugin<{localPath?: string} | void> = (
67
84
  return config;
68
85
  },
69
86
  ]);
87
+
88
+ // Android: include local module and add dependency if available
89
+ config = withSettingsGradle(config, (config) => {
90
+ const raw = props?.localPath;
91
+ const projectRoot = (config.modRequest as any).projectRoot as string;
92
+ const androidInput = typeof raw === 'string' ? undefined : raw?.android;
93
+ const androidModulePath =
94
+ resolveAndroidModulePath(androidInput) ||
95
+ resolveAndroidModulePath(path.resolve(projectRoot, 'openiap-google')) ||
96
+ null;
97
+
98
+ if (!androidModulePath || !fs.existsSync(androidModulePath)) {
99
+ if (androidInput) {
100
+ console.warn(
101
+ `⚠️ Could not resolve Android OpenIAP module at: ${androidInput}. Skipping local Android linkage.`,
102
+ );
103
+ }
104
+ return config;
105
+ }
106
+
107
+ // 1) settings.gradle: include and map projectDir
108
+ const settings = config.modResults;
109
+ const includeLine = "include ':openiap-google'";
110
+ const projectDirLine = `project(':openiap-google').projectDir = new File('${androidModulePath.replace(
111
+ /\\/g,
112
+ '/',
113
+ )}')`;
114
+ let contents = settings.contents ?? '';
115
+
116
+ // Ensure pluginManagement has plugin mappings required by the included module
117
+ const injectPluginManagement = () => {
118
+ const header = 'pluginManagement {';
119
+ const needsVannik =
120
+ !/id\s*\(\s*["']com\.vanniktech\.maven\.publish["']/.test(contents);
121
+ const needsKotlinAndroid =
122
+ !/id\s*\(\s*["']org\.jetbrains\.kotlin\.android["']/.test(contents);
123
+ const needsCompose =
124
+ !/id\s*\(\s*["']org\.jetbrains\.kotlin\.plugin\.compose["']/.test(
125
+ contents,
126
+ );
127
+ const needsRepos = !/pluginManagement[\s\S]*?repositories\s*\{/.test(
128
+ contents,
129
+ );
130
+
131
+ const pluginLines: string[] = [];
132
+ if (needsVannik)
133
+ pluginLines.push(
134
+ ` id("com.vanniktech.maven.publish") version "0.29.0"`,
135
+ );
136
+ if (needsKotlinAndroid)
137
+ pluginLines.push(
138
+ ` id("org.jetbrains.kotlin.android") version "2.0.21"`,
139
+ );
140
+ if (needsCompose)
141
+ pluginLines.push(
142
+ ` id("org.jetbrains.kotlin.plugin.compose") version "2.0.21"`,
143
+ );
144
+
145
+ // If everything already present, skip
146
+ if (pluginLines.length === 0 && !needsRepos) return;
147
+
148
+ const pluginsBlock = pluginLines.length
149
+ ? `plugins {\n${pluginLines.join('\n')}\n}`
150
+ : '';
151
+ const reposBlock = `repositories { gradlePluginPortal(); google(); mavenCentral() }`;
152
+
153
+ if (contents.includes(header)) {
154
+ contents = contents.replace(/pluginManagement\s*\{/, (m) => {
155
+ let injection =
156
+ m + `\n // Added by expo-iap (local openiap-google)\n`;
157
+ if (pluginsBlock) injection += ` ${pluginsBlock}\n`;
158
+ if (needsRepos) injection += ` ${reposBlock}\n`;
159
+ return injection;
160
+ });
161
+ } else {
162
+ contents =
163
+ `pluginManagement {\n // Added by expo-iap (local openiap-google)\n` +
164
+ (pluginsBlock ? ` ${pluginsBlock}\n` : '') +
165
+ ` ${reposBlock}\n}\n\n${contents}`;
166
+ }
167
+ };
168
+
169
+ if (
170
+ !/com\.vanniktech\.maven\.publish/.test(contents) ||
171
+ !/org\.jetbrains\.kotlin\.android/.test(contents)
172
+ ) {
173
+ injectPluginManagement();
174
+ }
175
+ if (!contents.includes(includeLine)) contents += `\n${includeLine}\n`;
176
+ if (!contents.includes(projectDirLine)) contents += `${projectDirLine}\n`;
177
+ settings.contents = contents;
178
+ console.log(`✅ Linked local Android module at: ${androidModulePath}`);
179
+ return config;
180
+ });
181
+
182
+ // 2) app/build.gradle: add implementation project(':openiap-google')
183
+ config = withAppBuildGradle(config, (config) => {
184
+ const raw = props?.localPath;
185
+ const projectRoot = (config.modRequest as any).projectRoot as string;
186
+ const androidInput = typeof raw === 'string' ? undefined : raw?.android;
187
+ const androidModulePath =
188
+ resolveAndroidModulePath(androidInput) ||
189
+ resolveAndroidModulePath(path.resolve(projectRoot, 'openiap-google')) ||
190
+ null;
191
+
192
+ if (!androidModulePath || !fs.existsSync(androidModulePath)) {
193
+ return config;
194
+ }
195
+
196
+ const gradle = config.modResults;
197
+ const dependencyLine = ` implementation project(':openiap-google')`;
198
+
199
+ // Remove any previously injected external deps that can conflict with the local module
200
+ const removalPatterns = [
201
+ /\n\s*implementation\s+"com\.android\.billingclient:billing-ktx:[^"]+"\s*\n/g,
202
+ /\n\s*implementation\s+"com\.google\.android\.gms:play-services-base:[^"]+"\s*\n/g,
203
+ ];
204
+ let contents = gradle.contents;
205
+ let removedAny = false;
206
+ for (const pattern of removalPatterns) {
207
+ if (pattern.test(contents)) {
208
+ contents = contents.replace(pattern, '\n');
209
+ removedAny = true;
210
+ }
211
+ }
212
+ if (removedAny) {
213
+ gradle.contents = contents;
214
+ console.log(
215
+ '🧹 Removed external Play Billing/GMS deps to use local :openiap-google',
216
+ );
217
+ }
218
+ if (!gradle.contents.includes(dependencyLine)) {
219
+ const anchor = /dependencies\s*\{/m;
220
+ if (anchor.test(gradle.contents)) {
221
+ gradle.contents = gradle.contents.replace(
222
+ anchor,
223
+ (m) => `${m}\n${dependencyLine}`,
224
+ );
225
+ } else {
226
+ gradle.contents += `\n\ndependencies {\n${dependencyLine}\n}\n`;
227
+ }
228
+ console.log('🛠️ Added dependency on local :openiap-google project');
229
+ }
230
+ return config;
231
+ });
232
+
233
+ // 3) Ensure final cleanup in app/build.gradle after all mods are applied
234
+ config = withDangerousMod(config, [
235
+ 'android',
236
+ async (config) => {
237
+ try {
238
+ const {platformProjectRoot} = config.modRequest as any;
239
+ const appBuildGradle = path.join(
240
+ platformProjectRoot,
241
+ 'app',
242
+ 'build.gradle',
243
+ );
244
+ if (fs.existsSync(appBuildGradle)) {
245
+ let contents = fs.readFileSync(appBuildGradle, 'utf8');
246
+ const patterns = [
247
+ /\n\s*implementation\s+"com\.android\.billingclient:billing-ktx:[^"]+"\s*\n/g,
248
+ /\n\s*implementation\s+"com\.google\.android\.gms:play-services-base:[^"]+"\s*\n/g,
249
+ ];
250
+ let changed = false;
251
+ for (const p of patterns) {
252
+ if (p.test(contents)) {
253
+ contents = contents.replace(p, '\n');
254
+ changed = true;
255
+ }
256
+ }
257
+ if (changed) {
258
+ fs.writeFileSync(appBuildGradle, contents);
259
+ console.log(
260
+ '🧹 expo-iap: Cleaned app/build.gradle billing/gms deps for local :openiap-google',
261
+ );
262
+ }
263
+ }
264
+ } catch (e) {
265
+ console.warn('expo-iap: cleanup step failed:', e);
266
+ }
267
+ return config;
268
+ },
269
+ ]);
270
+
271
+ // (removed) Avoid global root build.gradle mutations; included module should manage its plugins
272
+
273
+ return config;
70
274
  };
71
275
 
72
276
  export default withLocalOpenIAP;