1
1
package com.reclaimprotocol.inapp_rn_sdk
2
2
3
- import java.util.UUID
4
3
import android.util.Log
5
4
import com.facebook.react.bridge.Arguments
6
5
import com.facebook.react.bridge.Promise
@@ -12,6 +11,7 @@ import com.facebook.react.module.annotations.ReactModule
12
11
import org.reclaimprotocol.inapp_sdk.ReclaimOverrides
13
12
import org.reclaimprotocol.inapp_sdk.ReclaimSessionStatus
14
13
import org.reclaimprotocol.inapp_sdk.ReclaimVerification
14
+ import java.util.UUID
15
15
16
16
@ReactModule(name = InappRnSdkModule .NAME )
17
17
class InappRnSdkModule (private val reactContext : ReactApplicationContext ) :
@@ -39,7 +39,10 @@ class InappRnSdkModule(private val reactContext: ReactApplicationContext) :
39
39
userInfoMap.putString(" errorType" , errorType)
40
40
userInfoMap.putString(" sessionId" , exception.sessionId)
41
41
userInfoMap.putBoolean(" didSubmitManualVerification" , exception.didSubmitManualVerification)
42
- userInfoMap.putString(" reason" , if (exception is ReclaimVerification .ReclaimVerificationException .Failed ) exception.reason else null )
42
+ userInfoMap.putString(
43
+ " reason" ,
44
+ if (exception is ReclaimVerification .ReclaimVerificationException .Failed ) exception.reason else null
45
+ )
43
46
promise?.reject(" VERIFICATION_ERROR" , " Verification Error" , exception, userInfoMap)
44
47
}
45
48
@@ -68,12 +71,12 @@ class InappRnSdkModule(private val reactContext: ReactApplicationContext) :
68
71
}
69
72
val handler = ReclaimVerificationResultHandlerImpl (promise)
70
73
reactContext.runOnUiQueueThread {
71
- val appId = getString(request," appId" )
72
- val secret = getString(request," secret" )
74
+ val appId = getString(request, " appId" )
75
+ val secret = getString(request, " secret" )
73
76
val verificationRequest: ReclaimVerification .Request
74
77
val session = request.getMap(" session" )
75
78
val parametersRN = request.getMap(" parameters" )?.toHashMap()
76
- val parameters = mutableMapOf<String , String >()
79
+ val parameters = mutableMapOf<String , String >()
77
80
if (parametersRN != null ) {
78
81
for (key in parametersRN.keys) {
79
82
val value = parametersRN[key]
@@ -82,7 +85,6 @@ class InappRnSdkModule(private val reactContext: ReactApplicationContext) :
82
85
}
83
86
}
84
87
}
85
- val hideLanding = getBoolean(request, " hideLanding" )
86
88
val autoSubmit = getBoolean(request, " autoSubmit" )
87
89
val acceptAiProviders = getBoolean(request, " acceptAiProviders" )
88
90
val webhookUrl = getString(request, " webhookUrl" )
@@ -97,7 +99,6 @@ class InappRnSdkModule(private val reactContext: ReactApplicationContext) :
97
99
signature = getString(session, " signature" ) ? : " " ,
98
100
),
99
101
parameters = parameters,
100
- hideLanding = hideLanding ? : true ,
101
102
autoSubmit = autoSubmit ? : false ,
102
103
acceptAiProviders = acceptAiProviders ? : false ,
103
104
webhookUrl = webhookUrl,
@@ -114,16 +115,13 @@ class InappRnSdkModule(private val reactContext: ReactApplicationContext) :
114
115
signature = getString(session, " signature" ) ? : " " ,
115
116
),
116
117
parameters = parameters,
117
- hideLanding = hideLanding ? : true ,
118
118
autoSubmit = autoSubmit ? : false ,
119
119
acceptAiProviders = acceptAiProviders ? : false ,
120
120
webhookUrl = webhookUrl,
121
121
)
122
122
}
123
123
ReclaimVerification .startVerification(
124
- context = reactContext.applicationContext,
125
- request = verificationRequest,
126
- handler = handler
124
+ context = reactContext.applicationContext, request = verificationRequest, handler = handler
127
125
)
128
126
}
129
127
}
@@ -151,36 +149,74 @@ class InappRnSdkModule(private val reactContext: ReactApplicationContext) :
151
149
logConsumer = getMap(overrides, " logConsumer" ),
152
150
sessionManagement = getMap(overrides, " sessionManagement" ),
153
151
appInfo = getMap(overrides, " appInfo" ),
152
+ capabilityAccessToken = getString(overrides, " capabilityAccessToken" ),
154
153
promise,
155
154
)
156
155
}
157
156
158
- private val replyHandlers: MutableMap <String , (Result <Boolean >) -> Unit > = mutableMapOf ()
157
+ override fun clearAllOverrides (promise : Promise ? ) {
158
+ reactContext.runOnUiQueueThread {
159
+ ReclaimVerification .clearAllOverrides(
160
+ context = reactContext.applicationContext,
161
+ ) { result ->
162
+ result.onSuccess {
163
+ promise?.resolve(null )
164
+ }.onFailure { error ->
165
+ onPlatformException(promise, error)
166
+ }
167
+ }
168
+ }
169
+ }
159
170
160
171
private fun setOverrides (
161
172
provider : ReadableMap ? ,
162
173
featureOptions : ReadableMap ? ,
163
174
logConsumer : ReadableMap ? ,
164
175
sessionManagement : ReadableMap ? ,
165
176
appInfo : ReadableMap ? ,
177
+ capabilityAccessToken : String? ,
166
178
promise : Promise ?
167
179
) {
168
180
reactContext.runOnUiQueueThread {
169
181
ReclaimVerification .setOverrides(
170
182
context = reactContext.applicationContext,
171
183
provider = if (provider == null ) null else (
172
- if (! hasValue(provider, " url " ))
184
+ if (hasValue(provider, " jsonString " ))
173
185
ReclaimOverrides .ProviderInformation .FromJsonString (
174
186
requireString(
175
187
provider, " jsonString"
176
188
)
177
189
)
178
- else
190
+ else if (hasValue(provider, " url " ))
179
191
ReclaimOverrides .ProviderInformation .FromUrl (
180
192
requireString(
181
193
provider, " url"
182
194
)
183
195
)
196
+ else if (getBoolean(provider, " canFetchProviderInformationFromHost" ) == true )
197
+ ReclaimOverrides .ProviderInformation .FromCallback (object : ReclaimOverrides .ProviderInformation .FromCallbackHandler {
198
+ override fun fetchProviderInformation (
199
+ appId : String ,
200
+ providerId : String ,
201
+ sessionId : String ,
202
+ signature : String ,
203
+ timestamp : String ,
204
+ callback : (Result <String >) -> Unit
205
+ ) {
206
+ val args = Arguments .createMap()
207
+ args.putString(" appId" , appId)
208
+ args.putString(" providerId" , providerId)
209
+ args.putString(" sessionId" , sessionId)
210
+ args.putString(" signature" , signature)
211
+ args.putString(" timestamp" , timestamp)
212
+ val replyId = UUID .randomUUID().toString()
213
+ args.putString(" replyId" , replyId)
214
+ replyWithProviderInformationHandlers[replyId] = callback
215
+ emitOnProviderInformationRequest(args)
216
+ }
217
+ })
218
+ else
219
+ (throw IllegalStateException (" Invalid provider information. canFetchProviderInformationFromHost was not true and jsonString, url were also not provided." ))
184
220
),
185
221
featureOptions = if (featureOptions == null ) null else ReclaimOverrides .FeatureOptions (
186
222
cookiePersist = getBoolean(featureOptions, " cookiePersist" ),
@@ -257,31 +293,73 @@ class InappRnSdkModule(private val reactContext: ReactApplicationContext) :
257
293
appImageUrl = requireString(appInfo, " appImageUrl" ),
258
294
isRecurring = getBoolean(appInfo, " isRecurring" ) ? : false ,
259
295
),
296
+ capabilityAccessToken = capabilityAccessToken
260
297
) { result ->
261
298
result.onSuccess {
262
- promise?.resolve(true )
263
- }.onFailure { error ->
264
- promise?.reject(error)
299
+ try {
300
+ Log .d(NAME , " (setOverrides) Success" )
301
+ promise?.resolve(null )
302
+ } catch (e: Throwable ) {
303
+ Log .e(NAME , " (setOverrides) Error resolving promise" )
304
+ }
305
+
306
+ }.onFailure { error ->
307
+ try {
308
+ Log .d(NAME , " (setOverrides) Failure" )
309
+ onPlatformException(promise, error)
310
+ } catch (e: Throwable ) {
311
+ Log .e(NAME , " (setOverrides) Error rejecting promise" , e)
265
312
}
313
+ }
266
314
}
267
315
}
268
316
}
269
317
318
+ private val replyHandlers: MutableMap <String , (Result <Boolean >) -> Unit > = mutableMapOf ()
270
319
override fun reply (replyId : String? , reply : Boolean ) {
271
320
if (replyId == null ) {
272
- Log .w(NAME , " Missing arg replyId" )
321
+ Log .w(NAME , " (reply) Missing arg replyId" )
273
322
return
274
323
}
275
324
reactContext.runOnUiQueueThread {
276
325
val callback = replyHandlers[replyId]
277
326
if (callback != null ) {
278
327
callback(Result .success(reply))
279
328
} else {
280
- Log .w(NAME , " Missing reply handler for id: $replyId " )
329
+ Log .w(NAME , " (reply) Missing reply handler for id: $replyId " )
281
330
}
282
331
}
283
332
}
284
333
334
+ private val replyWithProviderInformationHandlers: MutableMap <String , (Result <String >) -> Unit > =
335
+ mutableMapOf ()
336
+
337
+ override fun replyWithProviderInformation (replyId : String? , providerInformation : String? ) {
338
+ if (replyId == null ) {
339
+ Log .w(NAME , " (replyWithProviderInformation) Missing arg replyId" )
340
+ return
341
+ }
342
+ reactContext.runOnUiQueueThread {
343
+ val callback = replyWithProviderInformationHandlers[replyId]
344
+ if (callback != null ) {
345
+ callback(Result .success(providerInformation ? : " " ))
346
+ } else {
347
+ Log .w(NAME , " (replyWithProviderInformation) Missing reply handler for id: $replyId " )
348
+ }
349
+ }
350
+ }
351
+
352
+ private fun onPlatformException (promise : Promise ? , exception : Throwable ) {
353
+ if (exception is ReclaimVerification .ReclaimPlatformException ) {
354
+ val userInfoMap = Arguments .createMap()
355
+ userInfoMap.putString(" message" , exception.internalErrorMessage)
356
+ userInfoMap.putString(" errorCode" , exception.errorCode)
357
+ promise?.reject(" PLATFORM_ERROR" , exception.message, exception, userInfoMap)
358
+ } else {
359
+ promise?.reject(" PLATFORM_ERROR" , " Unexpected Error" , exception)
360
+ }
361
+ }
362
+
285
363
@Suppress(" SameParameterValue" )
286
364
private fun hasValue (map : ReadableMap , key : String ): Boolean {
287
365
return map.hasKey(key) && ! map.isNull(key)
@@ -315,7 +393,8 @@ class InappRnSdkModule(private val reactContext: ReactApplicationContext) :
315
393
throw TypeCastException (" Value for key $key is not a Number" )
316
394
}
317
395
318
- private fun getString (map : ReadableMap , key : String ): String? {
396
+ private fun getString (map : ReadableMap ? , key : String ): String? {
397
+ if (map == null ) return null
319
398
return if (! map.hasKey(key) || map.isNull(key)) {
320
399
null
321
400
} else {
0 commit comments