react-native-purchases-ui 11.0.1 → 11.0.2

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/Package.swift CHANGED
@@ -15,7 +15,7 @@ let package = Package(
15
15
  // Resolved from the autolinker's ios/build/generated/autolinking/libs/<SwiftPM name>
16
16
  // symlink: four levels up reaches ios/build, where RN generates xcframeworks.
17
17
  .package(name: "ReactNative", path: "../../../../xcframeworks"),
18
- .package(url: "https://github.com/RevenueCat/purchases-hybrid-common", exact: "20.4.0"),
18
+ .package(url: "https://github.com/RevenueCat/purchases-hybrid-common", exact: "20.7.0"),
19
19
  ],
20
20
  targets: [
21
21
  .target(
package/README.md CHANGED
@@ -173,3 +173,88 @@ when there was nothing to present, such as an unconfigured identifier or no matc
173
173
  Backing out, a blocking failure, or another checkpoint owning presentation does not invoke
174
174
  the callback. Treat a checkpoint as a callback rather than a pending operation to await.
175
175
  The SDK handles error presentation.
176
+
177
+ #### Customizing the error dialog
178
+
179
+ When something goes wrong during a checkpoint, the native SDK shows an error dialog. Pass an
180
+ `errorDialogFactory` to choose custom content, the SDK's default dialog, or no dialog. The factory
181
+ receives the actual error, including its kind, code, and message. The SDK still presents the dialog.
182
+
183
+ ```tsx
184
+ RevenueCatUI.checkpoint('upgrade', onPassed, {
185
+ errorDialogFactory: error => {
186
+ if (error.kind === 'flowResultSyncFailed') {
187
+ return {
188
+ type: 'show',
189
+ dialog: {
190
+ message: 'We could not confirm your purchase. Please try again.',
191
+ primaryButtonTitle: 'OK',
192
+ },
193
+ };
194
+ }
195
+ return { type: 'useDefault' };
196
+ },
197
+ });
198
+ ```
199
+
200
+ `CheckpointErrorDialogOutcome` has three cases:
201
+
202
+ - `{ type: 'show', dialog: { message, primaryButtonTitle } }` shows custom content. Both strings must be non-empty.
203
+ - `{ type: 'useDefault' }` shows the SDK's localized dialog.
204
+ - `{ type: 'suppress' }` skips the dialog and continues as if the user had acknowledged it.
205
+
206
+ `error.kind` is `flowCouldNotBeShown`, `flowActionFailed`, or `flowResultSyncFailed`.
207
+ `error.code` is numeric and `error.message` contains the error description.
208
+
209
+ The factory may return a promise, which the SDK waits to settle. If it never settles, error handling
210
+ stays pending. If it throws, rejects, or returns an invalid outcome, the SDK shows its default dialog.
211
+ Calling the function returned by `checkpoint` detaches the factory and resolves pending requests with
212
+ that dialog. Without a factory, error requests aren’t sent to JavaScript.
213
+
214
+ #### Custom checkpoint paywall presentation
215
+
216
+ Register a global presenter to show checkpoint-selected offerings with app-owned UI. A per-checkpoint
217
+ `paywallPresenter` overrides the global handler. The handler receives the resolved offering and must call its
218
+ completion when the presentation ends, using the same `CONTINUED`, `CLOSED`, or `NAVIGATED_BACK` outcomes as the
219
+ native SDKs.
220
+
221
+ Omit `paywallPresenter` or pass `undefined` to use the global default. Pass `null` to use the SDK's native
222
+ presenter for one checkpoint, even when a global custom presenter is set. Changes to the global presenter
223
+ affect future checkpoint calls; existing calls keep the presenter selected when they were created.
224
+
225
+ ```tsx
226
+ import RevenueCatUI, {
227
+ PAYWALL_PRESENTATION_RESULT,
228
+ type PaywallPresenter,
229
+ } from 'react-native-purchases-ui';
230
+
231
+ const presenter: PaywallPresenter = (params, complete) => {
232
+ // Present params.offering with app-owned UI.
233
+ // `CONTINUED` and `CLOSED` let the checkpoint continue after purchase sync.
234
+ complete(PAYWALL_PRESENTATION_RESULT.CONTINUED);
235
+ };
236
+
237
+ RevenueCatUI.setPaywallPresenter(presenter);
238
+
239
+ RevenueCatUI.checkpoint('upgrade', () => {}, {
240
+ paywallPresenter: presenter, // Optional per-checkpoint override.
241
+ });
242
+
243
+ RevenueCatUI.checkpoint('native-upgrade', () => {}, {
244
+ paywallPresenter: null, // Use the SDK's native presenter for this call.
245
+ });
246
+
247
+ // Restore the SDK's default presenter:
248
+ RevenueCatUI.setPaywallPresenter(null);
249
+ ```
250
+
251
+ `NAVIGATED_BACK` leaves the app at the point that launched the checkpoint and does not call `onPassed`.
252
+ The app-owned presenter is responsible for dismissing its UI and reporting its final outcome; RevenueCat
253
+ synchronizes purchases and updates customer information after `CONTINUED` or `CLOSED`.
254
+
255
+ Calling the cleanup function returned by `checkpoint` cancels a custom presentation whose handler has not
256
+ run yet. It leaves already shown app UI open; dismiss that UI and call its completion as usual.
257
+
258
+ A presenter that throws or rejects a returned promise reports `NAVIGATED_BACK`. Resolving a promise alone
259
+ does not finish presentation; the completion callback determines the outcome. `errorDialogFactory` also
260
+ applies with a custom presenter, including purchase synchronization errors after completion.
@@ -17,6 +17,6 @@ Pod::Spec.new do |spec|
17
17
  spec.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
18
18
 
19
19
  spec.dependency "React-Core"
20
- spec.dependency "PurchasesHybridCommonUI", '20.4.0'
20
+ spec.dependency "PurchasesHybridCommonUI", '20.7.0'
21
21
  spec.swift_version = '5.7'
22
22
  end
@@ -69,7 +69,7 @@ android {
69
69
  minSdkVersion getExtOrIntegerDefault("minSdkVersion")
70
70
  targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
71
71
  versionCode 1
72
- versionName '11.0.1'
72
+ versionName '11.0.2'
73
73
  }
74
74
 
75
75
  buildTypes {
@@ -101,8 +101,8 @@ dependencies {
101
101
  //noinspection GradleDynamicVersion
102
102
  implementation "com.facebook.react:react-native:+"
103
103
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
104
- implementation 'com.revenuecat.purchases:purchases-hybrid-common:20.4.0'
105
- implementation 'com.revenuecat.purchases:purchases-hybrid-common-ui:20.4.0'
104
+ implementation 'com.revenuecat.purchases:purchases-hybrid-common:20.7.0'
105
+ implementation 'com.revenuecat.purchases:purchases-hybrid-common-ui:20.7.0'
106
106
  implementation 'androidx.compose.ui:ui-android:1.5.4'
107
107
  implementation "androidx.appcompat:appcompat:1.6.1"
108
108
  }
@@ -7,7 +7,12 @@ import com.facebook.react.bridge.ReadableMap
7
7
  import com.facebook.react.bridge.UiThreadUtil
8
8
  import com.facebook.react.modules.core.DeviceEventManagerModule
9
9
  import com.revenuecat.purchases.hybridcommon.ui.CheckpointListener
10
+ import com.revenuecat.purchases.hybridcommon.ui.cancelErrorDialogs
11
+ import com.revenuecat.purchases.hybridcommon.ui.completePaywallPresentation as completeNativePaywallPresentation
12
+ import com.revenuecat.purchases.hybridcommon.ui.detachCheckpoint
13
+ import com.revenuecat.purchases.hybridcommon.ui.invalidateCheckpointPresentations
10
14
  import com.revenuecat.purchases.hybridcommon.ui.presentCheckpoint
15
+ import com.revenuecat.purchases.hybridcommon.ui.resolveErrorDialog as resolveNativeErrorDialog
11
16
 
12
17
  internal class RNCheckpointsModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
13
18
  @Volatile private var invalidated = false
@@ -15,27 +20,95 @@ internal class RNCheckpointsModule(reactContext: ReactApplicationContext) : Reac
15
20
  override fun getName(): String = "RNCheckpoints"
16
21
 
17
22
  @ReactMethod
18
- fun checkpoint(identifier: String, customVariables: ReadableMap?, presentationMode: String, callId: String) {
23
+ fun checkpoint(
24
+ identifier: String,
25
+ customVariables: ReadableMap?,
26
+ presentationMode: String,
27
+ callId: String,
28
+ hasErrorDialogFactory: Boolean,
29
+ useCustomPresenter: Boolean,
30
+ ) {
19
31
  UiThreadUtil.runOnUiThread {
20
32
  if (invalidated) return@runOnUiThread
21
- presentCheckpoint(
22
- identifier,
23
- customVariables?.toHashMap(),
24
- presentationMode,
25
- object : CheckpointListener {
26
- override fun onCheckpointPassed(result: Map<String, Any?>) {
27
- emit("Checkpoints-onPassed", mapOf("callId" to callId, "result" to result))
33
+ val listener = object : CheckpointListener {
34
+ override fun onCheckpointPassed(result: Map<String, Any?>) {
35
+ emit("Checkpoints-onPassed", mapOf("callId" to callId, "result" to result))
36
+ }
37
+
38
+ override fun onCheckpointError(code: String, message: String) {
39
+ emit("Checkpoints-onError", mapOf("callId" to callId))
40
+ }
41
+
42
+ override fun onPaywallPresentationRequested(requestId: String, params: Map<String, Any?>) {
43
+ if (invalidated) {
44
+ completeNativePaywallPresentation(requestId, "NAVIGATED_BACK")
45
+ return
28
46
  }
29
- },
47
+ emit(
48
+ "Checkpoints-onPaywallPresentation",
49
+ mapOf("callId" to callId, "requestId" to requestId, "params" to params),
50
+ )
51
+ }
52
+
53
+ override fun onErrorDialogRequested(requestId: String, error: Map<String, Any?>) {
54
+ if (invalidated) {
55
+ resolveNativeErrorDialog(requestId, mapOf("type" to "useDefault"))
56
+ return
57
+ }
58
+ emit(
59
+ "Checkpoints-onErrorDialogRequested",
60
+ mapOf("callId" to callId, "requestId" to requestId, "error" to error),
61
+ )
62
+ }
63
+ }
64
+ presentCheckpoint(
65
+ identifier = identifier,
66
+ callId = callId,
67
+ customVariables = customVariables?.toHashMap(),
68
+ presentationMode = presentationMode,
69
+ useCustomPaywallPresenter = useCustomPresenter,
70
+ hasErrorDialogFactory = hasErrorDialogFactory,
71
+ listener = listener,
30
72
  )
31
73
  }
32
74
  }
33
75
 
76
+ @ReactMethod
77
+ fun completePaywallPresentation(requestId: String, result: String) {
78
+ UiThreadUtil.runOnUiThread {
79
+ completeNativePaywallPresentation(requestId, result)
80
+ }
81
+ }
82
+
83
+ @ReactMethod
84
+ fun detachPaywallPresenter(callId: String, activeRequestId: String?) {
85
+ UiThreadUtil.runOnUiThread {
86
+ detachCheckpoint(callId, activeRequestId)
87
+ }
88
+ }
89
+
90
+ @ReactMethod
91
+ fun resolveErrorDialog(requestId: String, outcome: ReadableMap?) {
92
+ UiThreadUtil.runOnUiThread {
93
+ resolveNativeErrorDialog(requestId, outcome?.toHashMap())
94
+ }
95
+ }
96
+
97
+ @ReactMethod
98
+ fun detachErrorDialogFactory(callId: String) {
99
+ UiThreadUtil.runOnUiThread {
100
+ cancelErrorDialogs(callId)
101
+ }
102
+ }
103
+
34
104
  @ReactMethod fun addListener(eventName: String) { /* Required by NativeEventEmitter. */ }
35
105
  @ReactMethod fun removeListeners(count: Int) { /* Required by NativeEventEmitter. */ }
36
106
 
37
107
  override fun invalidate() {
38
108
  invalidated = true
109
+ UiThreadUtil.runOnUiThread {
110
+ invalidateCheckpointPresentations()
111
+ }
39
112
  super.invalidate()
40
113
  }
41
114
 
@@ -46,7 +46,7 @@ RCT_EXPORT_MODULE();
46
46
  }
47
47
 
48
48
  - (NSArray<NSString *> *)supportedEvents {
49
- return @[@"Checkpoints-onPassed"];
49
+ return @[@"Checkpoints-onPassed", @"Checkpoints-onError", @"Checkpoints-onErrorDialogRequested", @"Checkpoints-onPaywallPresentation"];
50
50
  }
51
51
 
52
52
  // Required for RN 0.79+ NativeEventEmitter (JavaScript class) support.
@@ -63,24 +63,88 @@ RCT_EXPORT_METHOD(removeListeners:(double)count) {
63
63
  RCT_EXPORT_METHOD(checkpoint:(NSString *)identifier
64
64
  customVariables:(NSDictionary *)customVariables
65
65
  presentationMode:(NSString *)presentationMode
66
- callId:(NSString *)callId) {
66
+ callId:(NSString *)callId
67
+ hasErrorDialogFactory:(BOOL)hasErrorDialogFactory
68
+ useCustomPresenter:(BOOL)useCustomPresenter) {
67
69
  if (self.invalidated) {
68
70
  return;
69
71
  }
70
72
  __weak RNCheckpoints *weakSelf = self;
71
- [CheckpointProxy presentWithIdentifier:identifier
72
- customVariables:customVariables
73
- presentationMode:presentationMode
74
- onPassed:^(NSDictionary<NSString *, id> *result) {
73
+ void (^onPassed)(NSDictionary<NSString *, id> *) = ^(NSDictionary<NSString *, id> *result) {
75
74
  RNCheckpoints *strongSelf = weakSelf;
76
75
  if (strongSelf == nil || strongSelf.invalidated) {
77
76
  return;
78
77
  }
79
78
  [strongSelf sendEventWithName:@"Checkpoints-onPassed" body:@{@"callId": callId, @"result": result}];
79
+ };
80
+ // PurchasesHybridCommonUI already logs why the checkpoint could not start.
81
+ void (^onError)(NSString *, NSString *) = ^(NSString *code, NSString *message) {
82
+ RNCheckpoints *strongSelf = weakSelf;
83
+ if (strongSelf == nil || strongSelf.invalidated) {
84
+ return;
85
+ }
86
+ [strongSelf sendEventWithName:@"Checkpoints-onError" body:@{@"callId": callId}];
87
+ };
88
+ void (^onPaywallPresentationRequested)(NSString *, NSDictionary<NSString *, id> *) = nil;
89
+ if (useCustomPresenter) {
90
+ onPaywallPresentationRequested = ^(NSString *requestId, NSDictionary<NSString *, id> *params) {
91
+ RNCheckpoints *strongSelf = weakSelf;
92
+ if (strongSelf == nil || strongSelf.invalidated) {
93
+ // Native checkpoint work can request presentation after a JS reload tears down this bridge.
94
+ // Back out because the old JS runtime can no longer present the paywall or answer the request.
95
+ if (@available(iOS 15.0, *)) {
96
+ [CheckpointProxy completePaywallPresentationWithRequestId:requestId result:@"NAVIGATED_BACK"];
97
+ }
98
+ return;
99
+ }
100
+ [strongSelf sendEventWithName:@"Checkpoints-onPaywallPresentation"
101
+ body:@{@"callId": callId, @"requestId": requestId, @"params": params}];
102
+ };
103
+ }
104
+ void (^onErrorDialogRequested)(NSString *, NSDictionary<NSString *, id> *) = nil;
105
+ if (hasErrorDialogFactory) {
106
+ onErrorDialogRequested = ^(NSString *requestId, NSDictionary<NSString *, id> *error) {
107
+ RNCheckpoints *strongSelf = weakSelf;
108
+ if (strongSelf == nil || strongSelf.invalidated) {
109
+ [CheckpointProxy resolveErrorDialogWithRequestId:requestId outcome:@{@"type": @"useDefault"}];
110
+ return;
111
+ }
112
+ [strongSelf sendEventWithName:@"Checkpoints-onErrorDialogRequested"
113
+ body:@{@"callId": callId, @"requestId": requestId, @"error": error}];
114
+ };
115
+ }
116
+ [CheckpointProxy presentWithIdentifier:identifier
117
+ callId:callId
118
+ customVariables:customVariables
119
+ presentationMode:presentationMode
120
+ onPassed:onPassed
121
+ onError:onError
122
+ onPaywallPresentationRequested:onPaywallPresentationRequested
123
+ onErrorDialogRequested:onErrorDialogRequested];
124
+ }
125
+
126
+ RCT_EXPORT_METHOD(resolveErrorDialog:(NSString *)requestId
127
+ outcome:(NSDictionary *)outcome) {
128
+ if (@available(iOS 15.0, *)) {
129
+ [CheckpointProxy resolveErrorDialogWithRequestId:requestId outcome:outcome];
130
+ }
131
+ }
132
+
133
+ RCT_EXPORT_METHOD(detachPaywallPresenter:(NSString *)callId activeRequestId:(NSString *)activeRequestId) {
134
+ if (@available(iOS 15.0, *)) {
135
+ [CheckpointProxy detachCheckpointWithCallId:callId activePaywallRequestId:activeRequestId];
136
+ }
137
+ }
138
+
139
+ RCT_EXPORT_METHOD(detachErrorDialogFactory:(NSString *)callId) {
140
+ [CheckpointProxy cancelErrorDialogsWithCallId:callId];
141
+ }
142
+
143
+ RCT_EXPORT_METHOD(completePaywallPresentation:(NSString *)requestId result:(NSString *)result) {
144
+ if (@available(iOS 15.0, *)) {
145
+ [CheckpointProxy completePaywallPresentationWithRequestId:requestId
146
+ result:self.invalidated ? @"NAVIGATED_BACK" : result];
80
147
  }
81
- onError:^(NSString *code, NSString *message) {
82
- // PurchasesHybridCommonUI already logs why the checkpoint could not start.
83
- }];
84
148
  }
85
149
 
86
150
  - (void)invalidate {
@@ -89,6 +153,14 @@ RCT_EXPORT_METHOD(checkpoint:(NSString *)identifier
89
153
  os_unfair_lock_lock(&_invalidationLock);
90
154
  _didInvalidate = YES;
91
155
  os_unfair_lock_unlock(&_invalidationLock);
156
+ // Nothing will answer pending presentation requests after a reload.
157
+ dispatch_async(dispatch_get_main_queue(), ^{
158
+ if (@available(iOS 15.0, *)) {
159
+ [CheckpointProxy invalidateCheckpointPresentations];
160
+ } else {
161
+ [CheckpointProxy cancelErrorDialogsWithCallId:nil];
162
+ }
163
+ });
92
164
  [super invalidate];
93
165
  }
94
166
 
@@ -3,8 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.FLOW_PRESENTATION_MODE = void 0;
6
+ exports.PAYWALL_PRESENTATION_RESULT = exports.FLOW_PRESENTATION_MODE = void 0;
7
7
  exports.checkpoint = checkpoint;
8
+ exports.setPaywallPresenter = setPaywallPresenter;
8
9
  var _reactNative = require("react-native");
9
10
  var _customVariables = require("./customVariables");
10
11
  var _environment = require("./utils/environment");
@@ -15,9 +16,31 @@ let FLOW_PRESENTATION_MODE = exports.FLOW_PRESENTATION_MODE = /*#__PURE__*/funct
15
16
  FLOW_PRESENTATION_MODE["MODAL_SHEET"] = "MODAL_SHEET";
16
17
  return FLOW_PRESENTATION_MODE;
17
18
  }({});
19
+ /** What kind of failure the native SDK needs an error dialog for. */
20
+ /** The content of the native error dialog. */
21
+ /** What to do with a checkpoint error. The SDK owns dialog presentation and acknowledgement. */
22
+ /** How an app-owned checkpoint paywall presentation ended. */
23
+ let PAYWALL_PRESENTATION_RESULT = exports.PAYWALL_PRESENTATION_RESULT = /*#__PURE__*/function (PAYWALL_PRESENTATION_RESULT) {
24
+ /** The user continued. Permit checkpoint continuation after purchase synchronization. */
25
+ PAYWALL_PRESENTATION_RESULT["CONTINUED"] = "CONTINUED";
26
+ /** The user closed the paywall. Permit checkpoint continuation after purchase synchronization. */
27
+ PAYWALL_PRESENTATION_RESULT["CLOSED"] = "CLOSED";
28
+ /** The user navigated back. Stop the checkpoint without calling `onPassed`. */
29
+ PAYWALL_PRESENTATION_RESULT["NAVIGATED_BACK"] = "NAVIGATED_BACK";
30
+ return PAYWALL_PRESENTATION_RESULT;
31
+ }({});
32
+ /**
33
+ * Presents a checkpoint-selected offering. Dismiss the app-owned UI, then call completion.
34
+ * Throwing or rejecting a returned promise reports NAVIGATED_BACK. Resolving a promise alone
35
+ * does not complete presentation; the completion callback determines the outcome.
36
+ */
18
37
  const PASSED_EVENT = 'Checkpoints-onPassed';
38
+ const ERROR_DIALOG_REQUESTED_EVENT = 'Checkpoints-onErrorDialogRequested';
39
+ const ERROR_EVENT = 'Checkpoints-onError';
40
+ const PRESENT_PAYWALL_EVENT = 'Checkpoints-onPaywallPresentation';
19
41
  let nextCallId = 0;
20
42
  const callIdPrefix = `${Date.now()}-${Math.random()}`;
43
+ let globalPaywallPresenter = null;
21
44
  function nativeCheckpoints() {
22
45
  if ((0, _environment.shouldUsePreviewAPIMode)() || _reactNative.Platform.OS !== 'ios' && _reactNative.Platform.OS !== 'android') {
23
46
  throw new Error('[RevenueCatUI] Checkpoints require a native iOS or Android development build.');
@@ -31,6 +54,44 @@ function nativeCheckpoints() {
31
54
  }
32
55
  return native;
33
56
  }
57
+ function toErrorDialogOutcome(value) {
58
+ if (typeof value === 'object' && value !== null) {
59
+ const outcome = value;
60
+ if (outcome.type === 'useDefault' || outcome.type === 'suppress') {
61
+ return {
62
+ type: outcome.type
63
+ };
64
+ }
65
+ if (outcome.type === 'show' && typeof outcome.dialog === 'object' && outcome.dialog !== null) {
66
+ const {
67
+ message,
68
+ primaryButtonTitle
69
+ } = outcome.dialog;
70
+ if (typeof message === 'string' && message.length > 0 && typeof primaryButtonTitle === 'string' && primaryButtonTitle.length > 0) {
71
+ return {
72
+ type: 'show',
73
+ dialog: {
74
+ message,
75
+ primaryButtonTitle
76
+ }
77
+ };
78
+ }
79
+ }
80
+ }
81
+ throw new Error('errorDialogFactory must return a show outcome with non-empty message and primaryButtonTitle, useDefault, or suppress.');
82
+ }
83
+ function toNativeErrorDialogOutcome(outcome) {
84
+ if (outcome.type !== 'show') {
85
+ return outcome;
86
+ }
87
+ return {
88
+ type: 'show',
89
+ dialog: {
90
+ message: outcome.dialog.message,
91
+ buttonTitle: outcome.dialog.primaryButtonTitle
92
+ }
93
+ };
94
+ }
34
95
 
35
96
  /**
36
97
  * Calls onPassed only when the app can continue past the checkpoint. It is not called when
@@ -38,25 +99,117 @@ function nativeCheckpoints() {
38
99
  * Requires iOS 15+ or Android. Checkpoints are an invite-only API.
39
100
  *
40
101
  * Returns a function that removes the callback. Call it when the owning screen unmounts;
41
- * removing the callback does not dismiss a flow already on screen.
102
+ * cleanup cancels custom presentation if its handler has not run yet. Already shown UI stays
103
+ * open; dismiss it and call its presentation completion as usual.
42
104
  */
43
105
  function checkpoint(identifier, onPassed, params = {}) {
44
106
  const native = nativeCheckpoints();
45
107
  const callId = `${callIdPrefix}-${++nextCallId}`;
108
+ const paywallPresenter = params.paywallPresenter === undefined ? globalPaywallPresenter : params.paywallPresenter;
46
109
  const emitter = new _reactNative.NativeEventEmitter(native);
47
- const subscription = emitter.addListener(PASSED_EVENT, event => {
110
+ const errorDialogFactory = params.errorDialogFactory;
111
+ let isCallbackActive = true;
112
+ let detached = false;
113
+ let activePaywallRequestId = null;
114
+ const subscriptions = [];
115
+ const removeSubscriptions = () => subscriptions.splice(0).forEach(subscription => subscription.remove());
116
+ subscriptions.push(emitter.addListener(PASSED_EVENT, event => {
48
117
  if (event.callId !== callId) {
49
118
  return;
50
119
  }
51
- subscription.remove();
52
- onPassed(event.result);
53
- });
120
+ removeSubscriptions();
121
+ if (isCallbackActive) {
122
+ onPassed(event.result);
123
+ }
124
+ }));
125
+ subscriptions.push(emitter.addListener(ERROR_EVENT, event => {
126
+ if (event.callId === callId) {
127
+ removeSubscriptions();
128
+ }
129
+ }));
130
+ if (errorDialogFactory) {
131
+ subscriptions.push(emitter.addListener(ERROR_DIALOG_REQUESTED_EVENT, async event => {
132
+ if (event.callId !== callId) {
133
+ return;
134
+ }
135
+ let outcome = {
136
+ type: 'useDefault'
137
+ };
138
+ try {
139
+ outcome = toErrorDialogOutcome(await errorDialogFactory(event.error));
140
+ } catch (error) {
141
+ // tslint:disable-next-line:no-console
142
+ console.warn('[RevenueCatUI] Using the default checkpoint error dialog because errorDialogFactory failed:', error);
143
+ }
144
+ if (!detached) {
145
+ native.resolveErrorDialog(event.requestId, toNativeErrorDialogOutcome(outcome));
146
+ }
147
+ }));
148
+ }
149
+ if (paywallPresenter) {
150
+ subscriptions.push(emitter.addListener(PRESENT_PAYWALL_EVENT, event => {
151
+ if (event.callId !== callId) {
152
+ return;
153
+ }
154
+ activePaywallRequestId = event.requestId;
155
+ let didComplete = false;
156
+ const customVariables = {};
157
+ Object.entries(event.params.customVariables ?? {}).forEach(([key, value]) => {
158
+ if (typeof value === 'string') {
159
+ customVariables[key] = _customVariables.CustomVariableValue.string(value);
160
+ } else if (typeof value === 'number') {
161
+ customVariables[key] = _customVariables.CustomVariableValue.number(value);
162
+ } else if (typeof value === 'boolean') {
163
+ customVariables[key] = _customVariables.CustomVariableValue.boolean(value);
164
+ }
165
+ });
166
+ const complete = result => {
167
+ if (didComplete) {
168
+ return;
169
+ }
170
+ didComplete = true;
171
+ activePaywallRequestId = null;
172
+ native.completePaywallPresentation(event.requestId, result);
173
+ if (result === PAYWALL_PRESENTATION_RESULT.NAVIGATED_BACK) {
174
+ removeSubscriptions();
175
+ }
176
+ };
177
+ try {
178
+ Promise.resolve(paywallPresenter({
179
+ ...event.params,
180
+ customVariables
181
+ }, complete)).catch(() => complete(PAYWALL_PRESENTATION_RESULT.NAVIGATED_BACK));
182
+ } catch {
183
+ complete(PAYWALL_PRESENTATION_RESULT.NAVIGATED_BACK);
184
+ }
185
+ }));
186
+ }
54
187
  try {
55
- native.checkpoint(identifier, (0, _customVariables.convertCustomVariablesToNativeMap)(params.customVariables), params.presentationMode ?? FLOW_PRESENTATION_MODE.DEFAULT, callId);
188
+ native.checkpoint(identifier, (0, _customVariables.convertCustomVariablesToNativeMap)(params.customVariables), params.presentationMode ?? FLOW_PRESENTATION_MODE.DEFAULT, callId, errorDialogFactory !== undefined, paywallPresenter != null);
56
189
  } catch (error) {
57
- subscription.remove();
190
+ removeSubscriptions();
58
191
  throw error;
59
192
  }
60
- return () => subscription.remove();
193
+ return () => {
194
+ isCallbackActive = false;
195
+ removeSubscriptions();
196
+ if (paywallPresenter && !detached) {
197
+ native.detachPaywallPresenter(callId, activePaywallRequestId);
198
+ }
199
+ if (errorDialogFactory && !detached) {
200
+ native.detachErrorDialogFactory(callId);
201
+ }
202
+ detached = true;
203
+ };
204
+ }
205
+
206
+ /**
207
+ * Sets the default app-owned presenter for checkpoint-selected offerings. A per-checkpoint
208
+ * `paywallPresenter` overrides this handler. Pass `null` to restore the SDK presenter.
209
+ * Changes affect future checkpoint calls. Each call captures its presenter when `checkpoint` is called,
210
+ * even if presentation starts after the global presenter changes.
211
+ */
212
+ function setPaywallPresenter(presenter) {
213
+ globalPaywallPresenter = presenter;
61
214
  }
62
215
  //# sourceMappingURL=checkpoints.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_reactNative","require","_customVariables","_environment","FLOW_PRESENTATION_MODE","exports","PASSED_EVENT","nextCallId","callIdPrefix","Date","now","Math","random","nativeCheckpoints","shouldUsePreviewAPIMode","Platform","OS","Error","parseInt","String","Version","native","NativeModules","RNCheckpoints","checkpoint","identifier","onPassed","params","callId","emitter","NativeEventEmitter","subscription","addListener","event","remove","result","convertCustomVariablesToNativeMap","customVariables","presentationMode","DEFAULT","error"],"sourceRoot":"../../src","sources":["checkpoints.ts"],"mappings":";;;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAMA,IAAAC,gBAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAEA;AAAA,IACYG,sBAAsB,GAAAC,OAAA,CAAAD,sBAAA,0BAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAA,OAAtBA,sBAAsB;AAAA;AA+BlC,MAAME,YAAY,GAAG,sBAAsB;AAC3C,IAAIC,UAAU,GAAG,CAAC;AAClB,MAAMC,YAAY,GAAG,GAAGC,IAAI,CAACC,GAAG,CAAC,CAAC,IAAIC,IAAI,CAACC,MAAM,CAAC,CAAC,EAAE;AAErD,SAASC,iBAAiBA,CAAA,EAAG;EAC3B,IAAI,IAAAC,oCAAuB,EAAC,CAAC,IAAKC,qBAAQ,CAACC,EAAE,KAAK,KAAK,IAAID,qBAAQ,CAACC,EAAE,KAAK,SAAU,EAAE;IACrF,MAAM,IAAIC,KAAK,CAAC,+EAA+E,CAAC;EAClG;EACA,IAAIF,qBAAQ,CAACC,EAAE,KAAK,KAAK,IAAIE,QAAQ,CAACC,MAAM,CAACJ,qBAAQ,CAACK,OAAO,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;IACxE,MAAM,IAAIH,KAAK,CAAC,qDAAqD,CAAC;EACxE;EACA,MAAMI,MAAM,GAAGC,0BAAa,CAACC,aAAa;EAC1C,IAAI,CAACF,MAAM,EAAE;IACX,MAAM,IAAIJ,KAAK,CAAC,iHAAiH,CAAC;EACpI;EACA,OAAOI,MAAM;AACf;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASG,UAAUA,CACxBC,UAAkB,EAClBC,QAA4C,EAC5CC,MAAwB,GAAG,CAAC,CAAC,EACjB;EACZ,MAAMN,MAAM,GAAGR,iBAAiB,CAAC,CAAC;EAClC,MAAMe,MAAM,GAAG,GAAGpB,YAAY,IAAI,EAAED,UAAU,EAAE;EAChD,MAAMsB,OAAO,GAAG,IAAIC,+BAAkB,CAACT,MAAM,CAAC;EAC9C,MAAMU,YAAY,GAAGF,OAAO,CAACG,WAAW,CAAC1B,YAAY,EAAE2B,KAAK,IAAI;IAC9D,IAAIA,KAAK,CAACL,MAAM,KAAKA,MAAM,EAAE;MAC3B;IACF;IACAG,YAAY,CAACG,MAAM,CAAC,CAAC;IACrBR,QAAQ,CAACO,KAAK,CAACE,MAAM,CAAC;EACxB,CAAC,CAAC;EACF,IAAI;IACFd,MAAM,CAACG,UAAU,CAACC,UAAU,EAAE,IAAAW,kDAAiC,EAACT,MAAM,CAACU,eAAe,CAAC,EACrFV,MAAM,CAACW,gBAAgB,IAAIlC,sBAAsB,CAACmC,OAAO,EAAEX,MAAM,CAAC;EACtE,CAAC,CAAC,OAAOY,KAAK,EAAE;IACdT,YAAY,CAACG,MAAM,CAAC,CAAC;IACrB,MAAMM,KAAK;EACb;EACA,OAAO,MAAMT,YAAY,CAACG,MAAM,CAAC,CAAC;AACpC","ignoreList":[]}
1
+ {"version":3,"names":["_reactNative","require","_customVariables","_environment","FLOW_PRESENTATION_MODE","exports","PAYWALL_PRESENTATION_RESULT","PASSED_EVENT","ERROR_DIALOG_REQUESTED_EVENT","ERROR_EVENT","PRESENT_PAYWALL_EVENT","nextCallId","callIdPrefix","Date","now","Math","random","globalPaywallPresenter","nativeCheckpoints","shouldUsePreviewAPIMode","Platform","OS","Error","parseInt","String","Version","native","NativeModules","RNCheckpoints","toErrorDialogOutcome","value","outcome","type","dialog","message","primaryButtonTitle","length","toNativeErrorDialogOutcome","buttonTitle","checkpoint","identifier","onPassed","params","callId","paywallPresenter","undefined","emitter","NativeEventEmitter","errorDialogFactory","isCallbackActive","detached","activePaywallRequestId","subscriptions","removeSubscriptions","splice","forEach","subscription","remove","push","addListener","event","result","error","console","warn","resolveErrorDialog","requestId","didComplete","customVariables","Object","entries","key","CustomVariableValue","string","number","boolean","complete","completePaywallPresentation","NAVIGATED_BACK","Promise","resolve","catch","convertCustomVariablesToNativeMap","presentationMode","DEFAULT","detachPaywallPresenter","detachErrorDialogFactory","setPaywallPresenter","presenter"],"sourceRoot":"../../src","sources":["checkpoints.ts"],"mappings":";;;;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAOA,IAAAC,gBAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAEA;AAAA,IACYG,sBAAsB,GAAAC,OAAA,CAAAD,sBAAA,0BAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAA,OAAtBA,sBAAsB;AAAA;AA0BlC;AASA;AAMA;AAMA;AAAA,IACYE,2BAA2B,GAAAD,OAAA,CAAAC,2BAAA,0BAA3BA,2BAA2B;EACrC;EADUA,2BAA2B;EAGrC;EAHUA,2BAA2B;EAKrC;EALUA,2BAA2B;EAAA,OAA3BA,2BAA2B;AAAA;AAgBvC;AACA;AACA;AACA;AACA;AAyBA,MAAMC,YAAY,GAAG,sBAAsB;AAC3C,MAAMC,4BAA4B,GAAG,oCAAoC;AACzE,MAAMC,WAAW,GAAG,qBAAqB;AACzC,MAAMC,qBAAqB,GAAG,mCAAmC;AACjE,IAAIC,UAAU,GAAG,CAAC;AAClB,MAAMC,YAAY,GAAG,GAAGC,IAAI,CAACC,GAAG,CAAC,CAAC,IAAIC,IAAI,CAACC,MAAM,CAAC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;MACtE,CAAC,CAAC,MAAM;QACNF,QAAQ,CAACtE,2BAA2B,CAACwE,cAAc,CAAC;MACtD;IACF,CAAC,CAAC,CAAC;EACL;EACA,IAAI;IACFpD,MAAM,CAACa,UAAU,CAACC,UAAU,EAAE,IAAA0C,kDAAiC,EAACxC,MAAM,CAAC0B,eAAe,CAAC,EACrF1B,MAAM,CAACyC,gBAAgB,IAAI/E,sBAAsB,CAACgF,OAAO,EAAEzC,MAAM,EACjEK,kBAAkB,KAAKH,SAAS,EAAED,gBAAgB,IAAI,IAAI,CAAC;EAC/D,CAAC,CAAC,OAAOkB,KAAK,EAAE;IACdT,mBAAmB,CAAC,CAAC;IACrB,MAAMS,KAAK;EACb;EACA,OAAO,MAAM;IACXb,gBAAgB,GAAG,KAAK;IACxBI,mBAAmB,CAAC,CAAC;IACrB,IAAIT,gBAAgB,IAAI,CAACM,QAAQ,EAAE;MACjCxB,MAAM,CAAC2D,sBAAsB,CAAC1C,MAAM,EAAEQ,sBAAsB,CAAC;IAC/D;IACA,IAAIH,kBAAkB,IAAI,CAACE,QAAQ,EAAE;MACnCxB,MAAM,CAAC4D,wBAAwB,CAAC3C,MAAM,CAAC;IACzC;IACAO,QAAQ,GAAG,IAAI;EACjB,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,SAASqC,mBAAmBA,CAACC,SAAkC,EAAQ;EAC5EvE,sBAAsB,GAAGuE,SAAS;AACpC","ignoreList":[]}
@@ -326,6 +326,7 @@ const InternalCustomerCenterView = !usingPreviewAPIMode && _reactNative.UIManage
326
326
 
327
327
  class RevenueCatUI {
328
328
  static checkpoint = _checkpoints.checkpoint;
329
+ static setPaywallPresenter = _checkpoints.setPaywallPresenter;
329
330
  static Defaults = {
330
331
  PRESENT_PAYWALL_DISPLAY_CLOSE_BUTTON: true
331
332
  };