react-native-nami-sdk 2.0.5 → 3.0.8

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/.github/workflows/app_stg.yaml +203 -0
  2. package/.pre-commit-config.yaml +1 -1
  3. package/android/build.gradle +24 -13
  4. package/android/gradle/wrapper/gradle-wrapper.properties +5 -1
  5. package/android/src/main/java/com/nami/reactlibrary/NamiBridgeModule.kt +6 -50
  6. package/android/src/main/java/com/nami/reactlibrary/NamiBridgePackage.java +1 -0
  7. package/android/src/main/java/com/nami/reactlibrary/NamiCampaignManagerBridge.kt +133 -0
  8. package/android/src/main/java/com/nami/reactlibrary/NamiCustomerManagerBridge.kt +88 -19
  9. package/android/src/main/java/com/nami/reactlibrary/NamiEmitter.kt +24 -24
  10. package/android/src/main/java/com/nami/reactlibrary/NamiEntitlementManagerBridgeModule.kt +30 -129
  11. package/android/src/main/java/com/nami/reactlibrary/NamiPaywallManagerBridgeModule.kt +36 -147
  12. package/android/src/main/java/com/nami/reactlibrary/NamiPurchaseManagerBridge.kt +36 -38
  13. package/android/src/main/java/com/nami/reactlibrary/NamiUtil.kt +50 -180
  14. package/build-utils/get_version_code.py +140 -0
  15. package/index.d.ts +20 -0
  16. package/index.js +7 -6
  17. package/ios/Nami.m +9 -63
  18. package/ios/NamiBridgeUtil.h +4 -6
  19. package/ios/NamiBridgeUtil.m +16 -78
  20. package/ios/NamiCampaignManagerBridge.m +26 -0
  21. package/ios/NamiCampaignManagerBridge.swift +107 -0
  22. package/ios/NamiCustomerManager.m +18 -23
  23. package/ios/NamiCustomerManager.swift +122 -0
  24. package/ios/NamiEmitter.m +55 -65
  25. package/ios/NamiEntitlementManagerBridge.m +7 -107
  26. package/ios/NamiEntitlementManagerBridge.swift +74 -0
  27. package/ios/NamiMLManagerBridge.m +2 -2
  28. package/ios/NamiPaywallManagerBridge.m +22 -94
  29. package/ios/NamiPaywallManagerBridge.swift +93 -0
  30. package/ios/NamiPurchaseManagerBridge.m +38 -69
  31. package/ios/NamiPurchaseManagerBridge.swift +174 -0
  32. package/ios/Podfile +2 -2
  33. package/ios/RNNami-Bridging-Header.h +5 -0
  34. package/ios/RNNami.h +0 -1
  35. package/ios/RNNami.m +1 -1
  36. package/ios/RNNami.xcodeproj/project.pbxproj +84 -8
  37. package/ios/RNNami.xcodeproj/xcshareddata/xcschemes/RNNami.xcscheme +67 -0
  38. package/package.json +1 -1
  39. package/react-native-nami-sdk.podspec +3 -3
  40. package/src/Nami.d.ts +112 -0
  41. package/src/Nami.js +10 -0
  42. package/src/NamiCampaignManager.d.ts +54 -0
  43. package/src/NamiCampaignManager.js +43 -0
  44. package/src/NamiCustomerManager.d.ts +39 -0
  45. package/src/NamiCustomerManager.js +43 -0
  46. package/src/NamiEntitlementManager.d.ts +24 -0
  47. package/src/NamiEntitlementManager.js +23 -0
  48. package/src/NamiMLManager.d.ts +5 -0
  49. package/src/NamiMLManager.js +7 -0
  50. package/src/NamiPaywallManager.d.ts +57 -0
  51. package/src/NamiPaywallManager.js +28 -0
  52. package/src/NamiPurchaseManager.d.ts +57 -0
  53. package/src/NamiPurchaseManager.js +37 -0
  54. package/src/types.ts +36 -0
  55. package/android/src/main/java/com/nami/reactlibrary/NamiAnalyticsEmitter.kt +0 -121
  56. package/ios/NamiAnalyticsEmitter.m +0 -146
@@ -33,31 +33,31 @@ class NamiEmitter(reactContext: ReactApplicationContext) :
33
33
  }
34
34
 
35
35
  override fun initialize() {
36
- NamiPaywallManager.registerSignInListener { _, _, developerPaywallID ->
37
- Log.i(LOG_TAG, "Sign in clicked with developerPaywallID $developerPaywallID")
38
-
39
- emitSignInActivated(developerPaywallID)
40
- }
36
+ // NamiPaywallManager.registerSignInListener { _, _, developerPaywallID ->
37
+ // Log.i(LOG_TAG, "Sign in clicked with developerPaywallID $developerPaywallID")
38
+ //
39
+ // emitSignInActivated(developerPaywallID)
40
+ // }
41
41
 
42
42
  Log.i(LOG_TAG, "In Emitter Initialize()")
43
43
 
44
- NamiPaywallManager.registerPaywallRaiseListener { _, paywallData, products, developerPaywallId ->
45
- Log.i(
46
- LOG_TAG,
47
- "Products from registerPaywallRaiseListener callback are $products"
48
- )
49
-
50
- val sendProducts: List<NamiSKU> = products ?: ArrayList<NamiSKU>()
51
- emitPaywallRaise(paywallData, sendProducts, developerPaywallId)
52
- }
53
-
54
- NamiPurchaseManager.registerPurchasesChangedListener { list, namiPurchaseState, s ->
55
- emitPurchaseMade(list, namiPurchaseState, s)
56
- }
57
-
58
- NamiCustomerManager.registerCustomerJourneyChangedListener {
59
- emitCustomerJourneyChanged(it)
60
- }
44
+ // NamiPaywallManager.registerPaywallRaiseListener { _, paywallData, products, developerPaywallId ->
45
+ // Log.i(
46
+ // LOG_TAG,
47
+ // "Products from registerPaywallRaiseListener callback are $products"
48
+ // )
49
+ //
50
+ // val sendProducts: List<NamiSKU> = products ?: ArrayList<NamiSKU>()
51
+ // emitPaywallRaise(paywallData, sendProducts, developerPaywallId)
52
+ // }
53
+
54
+ // NamiPurchaseManager.registerPurchasesChangedListener { list, namiPurchaseState, s ->
55
+ // emitPurchaseMade(list, namiPurchaseState, s)
56
+ // }
57
+ //
58
+ // NamiCustomerManager.registerCustomerJourneyChangedListener {
59
+ // emitCustomerJourneyChanged(it)
60
+ // }
61
61
  }
62
62
 
63
63
  private fun emitCustomerJourneyChanged(customerJourneyState: CustomerJourneyState) {
@@ -126,8 +126,8 @@ class NamiEmitter(reactContext: ReactApplicationContext) :
126
126
  }
127
127
 
128
128
  // Populate paywall metadata map
129
- val paywallMap: WritableMap = namiPaywall.toNamiPaywallDict()
130
- map.putMap("paywallMetadata", paywallMap)
129
+ // val paywallMap: WritableMap = namiPaywall.toNamiPaywallDict()
130
+ // map.putMap("paywallMetadata", paywallMap)
131
131
 
132
132
  // Populate SKU details
133
133
  val skusArray: WritableArray = Arguments.createArray()
@@ -1,167 +1,68 @@
1
1
  package com.nami.reactlibrary
2
2
 
3
3
  import android.util.Log
4
- import com.facebook.react.bridge.Callback
5
- import com.facebook.react.bridge.ReactApplicationContext
6
- import com.facebook.react.bridge.ReactContextBaseJavaModule
7
- import com.facebook.react.bridge.ReactMethod
8
- import com.facebook.react.bridge.ReadableArray
9
- import com.facebook.react.bridge.ReadableMap
10
- import com.facebook.react.bridge.WritableArray
11
- import com.facebook.react.bridge.WritableMap
12
- import com.facebook.react.bridge.WritableNativeArray
13
- import com.facebook.react.bridge.WritableNativeMap
4
+ import com.facebook.react.bridge.*
14
5
  import com.facebook.react.modules.core.DeviceEventManagerModule
15
6
  import com.namiml.entitlement.NamiEntitlement
16
7
  import com.namiml.entitlement.NamiEntitlementManager
17
- import com.namiml.entitlement.NamiEntitlementSetter
8
+ //import com.namiml.entitlement.NamiEntitlementSetter
18
9
  import com.namiml.entitlement.NamiPlatformType
19
10
  import java.util.ArrayList
20
11
  import java.util.Date
12
+ import com.facebook.react.bridge.Callback
21
13
 
22
14
  class NamiEntitlementManagerBridgeModule(reactContext: ReactApplicationContext) :
23
15
  ReactContextBaseJavaModule(reactContext) {
24
16
 
25
- init {
26
- NamiEntitlementManager.registerEntitlementChangeListener {
27
- emitEntitlementsChanged(it)
28
- }
17
+ override fun getName(): String {
18
+ return "RNNamiEntitlementManager"
29
19
  }
30
20
 
31
- private fun emitEntitlementsChanged(activeEntitlements: List<NamiEntitlement>) {
32
- val map: WritableMap = WritableNativeMap()
21
+ @ReactMethod
22
+ fun isEntitlementActive(referenceId: String, promise: Promise){
23
+ val isEntitlementActive = NamiEntitlementManager.isEntitlementActive(referenceId)
24
+ promise.resolve(isEntitlementActive)
25
+ }
33
26
 
27
+ @ReactMethod
28
+ fun active(promise: Promise){
29
+ val nativeEntitlements = NamiEntitlementManager.active()
34
30
  val resultArray: WritableArray = WritableNativeArray()
35
- for (entitlement in activeEntitlements) {
31
+ for (entitlement in nativeEntitlements) {
36
32
  entitlement.toEntitlementDict()?.let { entitlementDict ->
37
33
  resultArray.pushMap(entitlementDict)
38
- }
39
- }
40
-
41
- map.putArray("activeEntitlements", resultArray)
42
- try {
43
- reactApplicationContext
44
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
45
- .emit("EntitlementsChanged", map)
46
- } catch (e: Exception) {
47
- Log.e(LOG_TAG, "Caught Exception: " + e.message)
34
+ }
48
35
  }
49
- }
50
-
51
- override fun getName(): String {
52
- return "NamiEntitlementManagerBridge"
36
+ promise.resolve(resultArray)
53
37
  }
54
38
 
55
39
  @ReactMethod
56
- fun isEntitlementActive(entitlementRefID: String, resultsCallback: Callback) {
57
- reactApplicationContext.runOnUiQueueThread {
58
- val isActive = NamiEntitlementManager.isEntitlementActive(entitlementRefID)
59
-
60
- Log.i(
61
- LOG_TAG,
62
- "Checking for $entitlementRefID entitlement active, result was $isActive"
63
- )
64
- resultsCallback.invoke(isActive)
65
- }
66
- }
67
-
68
- @ReactMethod
69
- fun activeEntitlements(resultsCallback: Callback) {
70
-
71
- reactApplicationContext.runOnUiQueueThread {
72
- val nativeEntitlements = NamiEntitlementManager.activeEntitlements()
73
-
40
+ fun refresh(callback: Callback){
41
+ NamiEntitlementManager.refresh { activeNativeEntitlements ->
74
42
  val resultArray: WritableArray = WritableNativeArray()
75
- for (entitlement in nativeEntitlements) {
76
- entitlement.toEntitlementDict()?.let { entitlementDict ->
77
- resultArray.pushMap(entitlementDict)
43
+ if (activeNativeEntitlements != null) {
44
+ for (entitlement in activeNativeEntitlements) {
45
+ entitlement.toEntitlementDict()?.let { entitlementDict ->
46
+ resultArray.pushMap(entitlementDict)
47
+ }
78
48
  }
79
49
  }
80
- resultsCallback.invoke(resultArray)
50
+ callback.invoke(resultArray)
81
51
  }
82
52
  }
83
53
 
84
54
  @ReactMethod
85
- fun getEntitlements(resultsCallback: Callback) {
86
-
87
- reactApplicationContext.runOnUiQueueThread {
88
- val nativeEntitlements = NamiEntitlementManager.getEntitlements()
89
-
90
- Log.i(LOG_TAG, "getEntitlements result is $nativeEntitlements")
91
-
55
+ fun registerActiveEntitlementsHandler() {
56
+ NamiEntitlementManager.registerActiveEntitlementsHandler { activeNativeEntitlements ->
92
57
  val resultArray: WritableArray = WritableNativeArray()
93
- for (entitlement in nativeEntitlements) {
58
+ for (entitlement in activeNativeEntitlements) {
94
59
  entitlement.toEntitlementDict()?.let { entitlementDict ->
95
60
  resultArray.pushMap(entitlementDict)
96
61
  }
97
62
  }
98
- resultsCallback.invoke(resultArray)
99
- }
100
- }
101
-
102
- @ReactMethod
103
- fun setEntitlements(entitlements: ReadableArray) {
104
- val entitlementsToSet = ArrayList<NamiEntitlementSetter>()
105
-
106
- val size = entitlements.size()
107
- var index = 0
108
- while (index < size) {
109
- val setterMap: ReadableMap? = entitlements.getMap(index)
110
- setterMap?.let {
111
- val entitlementSetter = entitlementSetterFromSetterMap(setterMap)
112
- entitlementSetter?.let {
113
- entitlementsToSet.add(entitlementSetter)
114
- }
115
- }
116
- index += 1
117
- }
118
-
119
- reactApplicationContext.runOnUiQueueThread {
120
- NamiEntitlementManager.setEntitlements(entitlementsToSet)
121
- }
122
- }
123
-
124
- @ReactMethod
125
- fun clearAllEntitlements() {
126
- reactApplicationContext.runOnUiQueueThread {
127
- NamiEntitlementManager.clearAllEntitlements()
128
- }
129
- }
130
-
131
- private fun entitlementSetterFromSetterMap(entitlementSetterMap: ReadableMap): NamiEntitlementSetter? {
132
- if (entitlementSetterMap.hasKey("referenceID")) {
133
- val referenceID = entitlementSetterMap.getString("referenceID").orEmpty()
134
- if (referenceID.isNotEmpty()) {
135
- val expires: Date? = null
136
-
137
- var purchasedSKUid: String? = null
138
- if (entitlementSetterMap.hasKey("purchasedSKUID")) {
139
- purchasedSKUid = entitlementSetterMap.getString("purchasedSKUid")
140
- }
141
-
142
- var platform: NamiPlatformType = NamiPlatformType.OTHER
143
- if (entitlementSetterMap.hasKey("platform")) {
144
- platform = when (entitlementSetterMap.getString("platform")) {
145
- "other" -> NamiPlatformType.OTHER
146
-
147
- "android" -> NamiPlatformType.ANDROID
148
-
149
- "apple" -> NamiPlatformType.APPLE
150
-
151
- "roku" -> NamiPlatformType.ROKU
152
-
153
- "web" -> NamiPlatformType.WEB
154
-
155
- else -> NamiPlatformType.OTHER
156
- }
157
- }
158
-
159
- //referenceId: kotlin.String, purchasedSKUid: kotlin.String?, expires: java.util.Date?, platform: com.namiml.entitlement.NamiPlatformType
160
- return NamiEntitlementSetter(referenceID, platform, purchasedSKUid, expires)
161
- }
63
+ reactApplicationContext
64
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
65
+ .emit("EntitlementsChanged", resultArray)
162
66
  }
163
- Log.e(LOG_TAG, "Attempted to set entitlement with no referenceID $entitlementSetterMap")
164
-
165
- return null
166
67
  }
167
68
  }
@@ -3,13 +3,8 @@ package com.nami.reactlibrary
3
3
  import android.app.Activity
4
4
  import android.content.Intent
5
5
  import android.util.Log
6
- import com.facebook.react.bridge.ActivityEventListener
7
- import com.facebook.react.bridge.Arguments
8
- import com.facebook.react.bridge.Callback
9
- import com.facebook.react.bridge.ReactApplicationContext
10
- import com.facebook.react.bridge.ReactContextBaseJavaModule
11
- import com.facebook.react.bridge.ReactMethod
12
- import com.facebook.react.bridge.WritableNativeMap
6
+ import com.android.billingclient.api.Purchase
7
+ import com.facebook.react.bridge.*
13
8
  import com.facebook.react.modules.core.DeviceEventManagerModule
14
9
  import com.namiml.paywall.NamiPaywallManager
15
10
  import com.namiml.paywall.PreparePaywallResult
@@ -19,18 +14,41 @@ class NamiPaywallManagerBridgeModule(reactContext: ReactApplicationContext) :
19
14
 
20
15
  private var blockRaisePaywall: Boolean = false
21
16
 
22
- init {
23
- NamiPaywallManager.registerApplicationAutoRaisePaywallBlocker {
24
- Log.i(LOG_TAG, "Nami flag for blocking paywall raise is $blockRaisePaywall")
25
- blockRaisePaywall
17
+ override fun getName(): String {
18
+ return "RNNamiPaywallManager"
19
+ }
20
+
21
+ @ReactMethod
22
+ fun buySkuComplete(purchase: WritableMap, skuRefId: String) {
23
+ // NamiPaywallManager.buySkuComplete(currentActivity!!, purchase, skuRefId)
24
+ }
25
+
26
+
27
+ @ReactMethod
28
+ fun registerCloseHandler(blockDismiss: Boolean) {
29
+ NamiPaywallManager.registerCloseHandler { activity ->
30
+ val resultMap = Arguments.createMap()
31
+ resultMap.putBoolean("blockingPaywallClosed", true)
32
+ reactApplicationContext
33
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
34
+ .emit("BlockingPaywallClosed", resultMap)
35
+ if (!blockDismiss) {
36
+ activity.finish()
37
+ }
26
38
  }
27
- reactContext.addActivityEventListener(this)
28
39
  }
29
40
 
30
- override fun getName(): String {
31
- return "NamiPaywallManagerBridge"
41
+ @ReactMethod
42
+ fun registerBuySkuHandler() {
43
+ NamiPaywallManager.registerBuySkuHandler { activity, sku ->
44
+ val dictionary = sku.toSkuDict()
45
+ reactApplicationContext
46
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
47
+ .emit("RegisterBuySKU", dictionary)
48
+ }
32
49
  }
33
50
 
51
+
34
52
  override fun onActivityResult(
35
53
  activity: Activity?,
36
54
  requestCode: Int,
@@ -64,138 +82,9 @@ class NamiPaywallManagerBridgeModule(reactContext: ReactApplicationContext) :
64
82
  // do nothing
65
83
  }
66
84
 
67
- @ReactMethod
68
- fun raisePaywall() {
69
- reactApplicationContext.runOnUiQueueThread {
70
- raisePaywall(currentActivity, null)
71
- }
72
- }
73
-
74
- @ReactMethod
75
- fun raisePaywallByDeveloperPaywallId(developerPaywallID: String) {
76
- reactApplicationContext.runOnUiQueueThread {
77
- raisePaywall(currentActivity, developerPaywallID)
78
- }
79
- }
80
-
81
- private fun raisePaywall(activity: Activity?, developerPaywallID: String?) {
82
- if (NamiPaywallManager.canRaisePaywall()) {
83
- Log.d(LOG_TAG, "About to raise Paywall ")
84
- if (activity != null) {
85
- Log.i(LOG_TAG, "Nami Activity to raise paywall is $activity")
86
- if (developerPaywallID == null) {
87
- Log.i(LOG_TAG, "Raising Paywall: ")
88
- NamiPaywallManager.raisePaywall(activity)
89
- } else {
90
- Log.i(LOG_TAG, "Raising Paywall by Id: $developerPaywallID")
91
- NamiPaywallManager.raisePaywall(developerPaywallID, activity)
92
- }
93
- } else {
94
- Log.w(LOG_TAG, "Activity from react getCurrentActivity was null.")
95
- }
96
- } else {
97
- Log.w(LOG_TAG, "Paywall not raised, SDK says paywall cannot be raised at this time.")
98
- }
99
- }
100
-
101
- @ReactMethod
102
- fun canRaisePaywall(successCallback: Callback) {
103
- reactApplicationContext.runOnUiQueueThread {
104
- val canRaiseResult = NamiPaywallManager.canRaisePaywall()
105
- successCallback.invoke(canRaiseResult)
106
- }
107
- }
108
-
109
- @ReactMethod
110
- fun blockRaisePaywall(blockRaise: Boolean) {
111
- blockRaisePaywall = blockRaise
112
- }
113
-
114
- @ReactMethod
115
- fun fetchCustomMetadataForDeveloperID(paywallDeveloperID: String, successCallback: Callback) {
116
- val sendDict = WritableNativeMap()
117
- //TODO: Android SDK needs fetchCustomMetadataForDeveloperID
118
- successCallback(sendDict)
119
- }
120
-
121
- @ReactMethod
122
- fun paywallImpression(developerPaywallID: String) {
123
- // TODO: Android SDK paywall impression call.
124
- }
125
-
126
- @ReactMethod
127
- fun preparePaywallForDisplay(backgroundImageRequired: Boolean, imageFetchTimeout: Double) {
128
- val imageFetchTimeoutConvertedToLong: Long = imageFetchTimeout.toLong()
129
- reactApplicationContext.runOnUiQueueThread {
130
-
131
- NamiPaywallManager.preparePaywallForDisplay(
132
- backgroundImageRequired,
133
- imageFetchTimeoutConvertedToLong
134
- ) { result ->
135
- when (result) {
136
- is PreparePaywallResult.Success -> {
137
- emitPreparePaywallFinished(true, null, null)
138
- }
139
- is PreparePaywallResult.Failure -> {
140
- emitPreparePaywallFinished(false, null, result.error)
141
- }
142
- }
143
- }
144
- }
145
- }
146
-
147
- @ReactMethod
148
- fun preparePaywallForDisplayByDeveloperPaywallId(
149
- developerPaywallID: String,
150
- backgroundImageRequired: Boolean,
151
- imageFetchTimeout: Double
152
- ) {
153
- val imageFetchTimeoutConvertedToLong: Long = imageFetchTimeout.toLong()
154
- reactApplicationContext.runOnUiQueueThread {
155
- NamiPaywallManager.preparePaywallForDisplay(
156
- developerPaywallID,
157
- backgroundImageRequired,
158
- imageFetchTimeoutConvertedToLong
159
- ) { result ->
160
- when (result) {
161
- is PreparePaywallResult.Success -> {
162
- emitPreparePaywallFinished(true, developerPaywallID, null)
163
- }
164
- is PreparePaywallResult.Failure -> {
165
- emitPreparePaywallFinished(false, developerPaywallID, result.error)
166
- }
167
- }
168
- }
169
- }
170
- }
171
-
172
- private fun emitPreparePaywallFinished(
173
- success: Boolean,
174
- developerPaywallID: String?,
175
- error: com.namiml.paywall.PreparePaywallError?
176
- ) {
177
- val prepareContentMap = Arguments.createMap()
178
- prepareContentMap.putBoolean("success", success)
179
-
180
- if (developerPaywallID != null) {
181
- prepareContentMap.putString("developerPaywallID", developerPaywallID)
182
- }
183
-
184
- if (error != null) {
185
- prepareContentMap.putInt("errorCode", error.ordinal)
186
- prepareContentMap.putString("errorMessage", error.toString())
187
- }
85
+ // @ReactMethod
86
+ // fun blockRaisePaywall(blockRaise: Boolean) {
87
+ // blockRaisePaywall = blockRaise
88
+ // }
188
89
 
189
- Log.i(
190
- LOG_TAG,
191
- "Emitting preparePaywallForDisplay finished with result " + success + "error: " + error.toString()
192
- )
193
- try {
194
- reactApplicationContext
195
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
196
- .emit("PreparePaywallFinished", prepareContentMap)
197
- } catch (e: Exception) {
198
- Log.e(LOG_TAG, "Caught Exception: " + e.message)
199
- }
200
- }
201
90
  }
@@ -1,22 +1,16 @@
1
1
  package com.nami.reactlibrary
2
2
 
3
3
  import android.util.Log
4
- import com.facebook.react.bridge.Callback
5
- import com.facebook.react.bridge.ReactApplicationContext
6
- import com.facebook.react.bridge.ReactContextBaseJavaModule
7
- import com.facebook.react.bridge.ReactMethod
8
- import com.facebook.react.bridge.ReadableArray
9
- import com.facebook.react.bridge.ReadableType
10
- import com.facebook.react.bridge.WritableArray
11
- import com.facebook.react.bridge.WritableNativeArray
12
- import com.facebook.react.bridge.WritableNativeMap
4
+ import com.facebook.react.bridge.*
5
+ import com.facebook.react.modules.core.DeviceEventManagerModule
13
6
  import com.namiml.billing.NamiPurchaseManager
7
+ import com.namiml.paywall.NamiPaywallManager
14
8
 
15
9
  class NamiPurchaseManagerBridgeModule(reactContext: ReactApplicationContext) :
16
10
  ReactContextBaseJavaModule(reactContext) {
17
11
 
18
12
  override fun getName(): String {
19
- return "NamiPurchaseManagerBridge"
13
+ return "RNNamiPurchaseManager"
20
14
  }
21
15
 
22
16
  @ReactMethod
@@ -26,26 +20,6 @@ class NamiPurchaseManagerBridgeModule(reactContext: ReactApplicationContext) :
26
20
  }
27
21
  }
28
22
 
29
- @ReactMethod
30
- fun buySKU(skuPlatformID: String, developerPaywallID: String, resultsCallback: Callback) {
31
- reactApplicationContext.runOnUiQueueThread {
32
- currentActivity?.let {
33
- NamiPurchaseManager.buySKU(it, skuPlatformID) {
34
-
35
- val result: Boolean
36
- if (NamiPurchaseManager.isSKUIDPurchased(skuPlatformID)) {
37
- result = true
38
- Log.i(LOG_TAG, "Purchase complete, result is PURCHASED.")
39
- } else {
40
- result = false
41
- Log.i(LOG_TAG, "Purchase complete, product not purchased.")
42
- }
43
- resultsCallback.invoke(result)
44
- }
45
- }
46
- }
47
- }
48
-
49
23
  @ReactMethod
50
24
  fun purchases(resultsCallback: Callback) {
51
25
  reactApplicationContext.runOnUiQueueThread {
@@ -63,22 +37,20 @@ class NamiPurchaseManagerBridgeModule(reactContext: ReactApplicationContext) :
63
37
  }
64
38
 
65
39
  @ReactMethod
66
- fun isSKUIDPurchased(skuID: String, resultsCallback: Callback) {
67
- reactApplicationContext.runOnUiQueueThread {
68
- val isPurchased = NamiPurchaseManager.isSKUIDPurchased(skuID)
69
- resultsCallback.invoke(isPurchased)
70
- }
40
+ fun skuPurchased(skuID: String, promise: Promise) {
41
+ val isPurchased = NamiPurchaseManager.isSKUIDPurchased(skuID)
42
+ promise.resolve(isPurchased)
71
43
  }
72
44
 
73
45
  @ReactMethod
74
- fun consumePurchasedSKU(skuRefId: String) {
46
+ fun consumePurchasedSku(skuRefId: String) {
75
47
  reactApplicationContext.runOnUiQueueThread {
76
48
  NamiPurchaseManager.consumePurchasedSKU(skuRefId)
77
49
  }
78
50
  }
79
51
 
80
52
  @ReactMethod
81
- fun anySKUIDPurchased(skuIDs: ReadableArray, resultsCallback: Callback) {
53
+ fun anySkuPurchased(skuIDs: ReadableArray, promise: Promise) {
82
54
  reactApplicationContext.runOnUiQueueThread {
83
55
  val checkArray: MutableList<String> = mutableListOf()
84
56
  for (x in 0 until skuIDs.size()) {
@@ -92,7 +64,7 @@ class NamiPurchaseManagerBridgeModule(reactContext: ReactApplicationContext) :
92
64
 
93
65
  val anyPurchased = NamiPurchaseManager.anySKUIDPurchased(checkArray)
94
66
 
95
- resultsCallback.invoke(anyPurchased)
67
+ promise.resolve(anyPurchased)
96
68
  }
97
69
  }
98
70
 
@@ -109,4 +81,30 @@ class NamiPurchaseManagerBridgeModule(reactContext: ReactApplicationContext) :
109
81
  }
110
82
  resultsCallback.invoke(resultMap)
111
83
  }
84
+
85
+ @ReactMethod
86
+ fun registerPurchasesChangedHandler() {
87
+ NamiPurchaseManager.registerPurchasesChangedHandler{purchases, purchaseState, error ->
88
+ run {
89
+ val resultPurchases: WritableArray = WritableNativeArray()
90
+
91
+ for (purchase in purchases) {
92
+ resultPurchases.pushMap(purchase.toPurchaseDict())
93
+ }
94
+
95
+ val stateString = purchaseState.toString()
96
+
97
+ val payload = Arguments.createMap()
98
+ payload.putArray("purchases", resultPurchases)
99
+ payload.putString("purchaseState", stateString)
100
+ payload.putString("error", error)
101
+
102
+ reactApplicationContext
103
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
104
+ .emit("PurchasesChanged", payload)
105
+
106
+
107
+ }
108
+ }
109
+ }
112
110
  }