|
| 1 | +package com.superwallreactnative.models |
| 2 | + |
| 3 | +import com.facebook.react.bridge.Arguments |
| 4 | +import com.facebook.react.bridge.ReadableMap |
| 5 | +import com.superwall.sdk.models.internal.RedemptionResult |
| 6 | +import com.superwall.sdk.models.internal.RedemptionInfo |
| 7 | +import com.superwall.sdk.models.internal.PurchaserInfo |
| 8 | +import com.superwall.sdk.models.internal.StoreIdentifiers |
| 9 | +import com.superwall.sdk.models.internal.ErrorInfo |
| 10 | +import com.superwall.sdk.models.internal.ExpiredInfo |
| 11 | +import com.superwall.sdk.models.internal.RedemptionOwnership |
| 12 | +import com.superwall.sdk.models.entitlements.Entitlement |
| 13 | + |
| 14 | +fun RedemptionResult.toJson(): ReadableMap { |
| 15 | + val map = Arguments.createMap() |
| 16 | + map.putString("code", this.code) |
| 17 | + |
| 18 | + when (this) { |
| 19 | + is RedemptionResult.Success -> { |
| 20 | + map.putString("status", "SUCCESS") |
| 21 | + map.putMap("redemptionInfo", this.redemptionInfo.toJson()) |
| 22 | + } |
| 23 | + is RedemptionResult.Error -> { |
| 24 | + map.putString("status", "ERROR") |
| 25 | + map.putMap("error", this.error.toJson()) |
| 26 | + } |
| 27 | + is RedemptionResult.Expired -> { |
| 28 | + map.putString("status", "CODE_EXPIRED") |
| 29 | + map.putMap("expired", this.expired.toJson()) |
| 30 | + } |
| 31 | + is RedemptionResult.InvalidCode -> { |
| 32 | + map.putString("status", "INVALID_CODE") |
| 33 | + } |
| 34 | + is RedemptionResult.ExpiredSubscription -> { |
| 35 | + map.putString("status", "EXPIRED_SUBSCRIPTION") |
| 36 | + map.putMap("redemptionInfo", this.redemptionInfo.toJson()) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return map |
| 41 | +} |
| 42 | + |
| 43 | +private fun RedemptionInfo.toJson(): ReadableMap { |
| 44 | + val map = Arguments.createMap() |
| 45 | + map.putMap("ownership", this.ownership.toJson()) |
| 46 | + map.putMap("purchaserInfo", this.purchaserInfo.toJson()) |
| 47 | + this.paywallInfo?.let { map.putMap("paywallInfo", it.toJson()) } |
| 48 | + |
| 49 | + val entitlementsArray = Arguments.createArray() |
| 50 | + this.entitlements.forEach { entitlement -> |
| 51 | + val entitlementMap = Arguments.createMap() |
| 52 | + entitlementMap.putString("id", entitlement.id) |
| 53 | + entitlementsArray.pushMap(entitlementMap) |
| 54 | + } |
| 55 | + map.putArray("entitlements", entitlementsArray) |
| 56 | + |
| 57 | + return map |
| 58 | +} |
| 59 | + |
| 60 | +private fun PurchaserInfo.toJson(): ReadableMap { |
| 61 | + val map = Arguments.createMap() |
| 62 | + map.putString("appUserId", this.appUserId) |
| 63 | + this.email?.let { map.putString("email", it) } |
| 64 | + map.putMap("storeIdentifiers", this.storeIdentifiers.toJson()) |
| 65 | + return map |
| 66 | +} |
| 67 | + |
| 68 | +private fun StoreIdentifiers.toJson(): ReadableMap { |
| 69 | + val map = Arguments.createMap() |
| 70 | + when (this) { |
| 71 | + is StoreIdentifiers.Stripe -> { |
| 72 | + map.putString("store", "STRIPE") |
| 73 | + map.putString("stripeCustomerId", this.stripeCustomerId) |
| 74 | + val subscriptionIdsArray = Arguments.createArray() |
| 75 | + this.subscriptionIds.forEach { id -> |
| 76 | + subscriptionIdsArray.pushString(id) |
| 77 | + } |
| 78 | + map.putArray("stripeSubscriptionIds", subscriptionIdsArray) |
| 79 | + } |
| 80 | + is StoreIdentifiers.Unknown -> { |
| 81 | + map.putString("store", "UNKNOWN") |
| 82 | + } |
| 83 | + } |
| 84 | + return map |
| 85 | +} |
| 86 | + |
| 87 | +private fun ErrorInfo.toJson(): ReadableMap { |
| 88 | + val map = Arguments.createMap() |
| 89 | + map.putString("message", this.message) |
| 90 | + return map |
| 91 | +} |
| 92 | + |
| 93 | +private fun ExpiredInfo.toJson(): ReadableMap { |
| 94 | + val map = Arguments.createMap() |
| 95 | + map.putBoolean("resent", this.resent) |
| 96 | + this.obfuscatedEmail?.let { map.putString("obfuscatedEmail", it) } |
| 97 | + return map |
| 98 | +} |
| 99 | + |
| 100 | +private fun RedemptionOwnership.toJson(): ReadableMap { |
| 101 | + val map = Arguments.createMap() |
| 102 | + when (this) { |
| 103 | + is RedemptionOwnership.Device -> { |
| 104 | + map.putString("type", "DEVICE") |
| 105 | + map.putString("deviceId", this.deviceId) |
| 106 | + } |
| 107 | + is RedemptionOwnership.AppUser -> { |
| 108 | + map.putString("type", "APP_USER") |
| 109 | + map.putString("appUserId", this.appUserId) |
| 110 | + } |
| 111 | + } |
| 112 | + return map |
| 113 | +} |
| 114 | + |
| 115 | +private fun RedemptionResult.PaywallInfo.toJson(): ReadableMap { |
| 116 | + val map = Arguments.createMap() |
| 117 | + map.putString("identifier", this.identifier) |
| 118 | + map.putString("placementName", this.placementName) |
| 119 | + |
| 120 | + val placementParamsMap = Arguments.createMap() |
| 121 | + this.placementParams.forEach { (key, value) -> |
| 122 | + placementParamsMap.putString(key, value.toString()) |
| 123 | + } |
| 124 | + map.putMap("placementParams", placementParamsMap) |
| 125 | + |
| 126 | + map.putString("variantId", this.variantId) |
| 127 | + map.putString("experimentId", this.experimentId) |
| 128 | + return map |
| 129 | +} |
0 commit comments