diff --git a/.github/badges/branches.svg b/.github/badges/branches.svg index 097c3a25e..f4fae1e6d 100644 --- a/.github/badges/branches.svg +++ b/.github/badges/branches.svg @@ -1 +1 @@ -branches33.2% \ No newline at end of file +branches33.7% \ No newline at end of file diff --git a/.github/badges/jacoco.svg b/.github/badges/jacoco.svg index 13424a652..8b3291a4f 100644 --- a/.github/badges/jacoco.svg +++ b/.github/badges/jacoco.svg @@ -1 +1 @@ -coverage42.8% \ No newline at end of file +coverage43.3% \ No newline at end of file diff --git a/superwall/src/main/java/com/superwall/sdk/delegate/subscription_controller/PurchaseController.kt b/superwall/src/main/java/com/superwall/sdk/delegate/subscription_controller/PurchaseController.kt index 41f769372..86a2a5ad9 100644 --- a/superwall/src/main/java/com/superwall/sdk/delegate/subscription_controller/PurchaseController.kt +++ b/superwall/src/main/java/com/superwall/sdk/delegate/subscription_controller/PurchaseController.kt @@ -5,6 +5,7 @@ import androidx.annotation.MainThread import com.android.billingclient.api.ProductDetails import com.superwall.sdk.delegate.PurchaseResult import com.superwall.sdk.delegate.RestorationResult +import com.superwall.sdk.store.abstractions.product.StoreProduct /** * The interface that handles Superwall's subscription-related logic. @@ -51,4 +52,25 @@ interface PurchaseController { */ @MainThread suspend fun restorePurchases(): RestorationResult + + /** + * Called when the user initiates purchasing of a **custom** product — a product whose + * `store` is `CUSTOM` and whose metadata is sourced from the Superwall API rather than + * Google Play. Implement this to route the purchase through your own payment system + * (e.g. Stripe, web checkout). + * + * The default implementation returns `PurchaseResult.Failed` to signal that the + * controller does not handle custom products. Override it if you've configured any + * custom products in the Superwall dashboard. + * + * @param customProduct The Superwall [StoreProduct] (with `isCustomProduct == true`) the + * user would like to purchase. Its [StoreProduct.customTransactionId] is pre-generated + * by the SDK and used as the original transaction identifier in analytics. + */ + @MainThread + suspend fun purchase(customProduct: StoreProduct): PurchaseResult = + PurchaseResult.Failed( + "This PurchaseController does not implement purchase(customProduct:). " + + "Override it to handle custom (store == CUSTOM) products.", + ) } diff --git a/superwall/src/main/java/com/superwall/sdk/dependencies/DependencyContainer.kt b/superwall/src/main/java/com/superwall/sdk/dependencies/DependencyContainer.kt index 15ba4c86c..ab2e00f19 100644 --- a/superwall/src/main/java/com/superwall/sdk/dependencies/DependencyContainer.kt +++ b/superwall/src/main/java/com/superwall/sdk/dependencies/DependencyContainer.kt @@ -1032,6 +1032,19 @@ class DependencyContainer( appSessionId = appSessionManager.appSession.id, ) + override suspend fun makeStoreTransaction( + customTransactionId: String, + productIdentifier: String, + purchaseDate: java.util.Date, + ): StoreTransaction = + StoreTransaction( + customTransactionId = customTransactionId, + productIdentifier = productIdentifier, + purchaseDate = purchaseDate, + configRequestId = configManager.config?.requestId ?: "", + appSessionId = appSessionManager.appSession.id, + ) + override suspend fun activeProductIds(): List = storeManager.receiptManager.purchases.toList() diff --git a/superwall/src/main/java/com/superwall/sdk/dependencies/FactoryProtocols.kt b/superwall/src/main/java/com/superwall/sdk/dependencies/FactoryProtocols.kt index f27880c24..7a753f2e9 100644 --- a/superwall/src/main/java/com/superwall/sdk/dependencies/FactoryProtocols.kt +++ b/superwall/src/main/java/com/superwall/sdk/dependencies/FactoryProtocols.kt @@ -225,6 +225,17 @@ interface ConfigManagerFactory { interface StoreTransactionFactory { suspend fun makeStoreTransaction(transaction: Purchase): StoreTransaction + /** + * Builds a StoreTransaction for a custom-product purchase (no Google Play receipt). + * [customTransactionId] is the pre-generated UUID used as both original and + * store transaction identifier. + */ + suspend fun makeStoreTransaction( + customTransactionId: String, + productIdentifier: String, + purchaseDate: java.util.Date, + ): StoreTransaction + suspend fun activeProductIds(): List } diff --git a/superwall/src/main/java/com/superwall/sdk/models/paywall/Paywall.kt b/superwall/src/main/java/com/superwall/sdk/models/paywall/Paywall.kt index 4cb102c72..16b9a188c 100644 --- a/superwall/src/main/java/com/superwall/sdk/models/paywall/Paywall.kt +++ b/superwall/src/main/java/com/superwall/sdk/models/paywall/Paywall.kt @@ -168,6 +168,9 @@ data class Paywall( val paddleProducts: List get() = _productItemsV3.filter { it.storeProduct is CrossplatformProduct.StoreProduct.Paddle } + val customProducts: List + get() = _productItemsV3.filter { it.storeProduct is CrossplatformProduct.StoreProduct.Custom } + // Public getter for productItems var productItems: List get() = ( diff --git a/superwall/src/main/java/com/superwall/sdk/models/product/CrossplatformProduct.kt b/superwall/src/main/java/com/superwall/sdk/models/product/CrossplatformProduct.kt index ce5f3d36c..8edbd1ec4 100644 --- a/superwall/src/main/java/com/superwall/sdk/models/product/CrossplatformProduct.kt +++ b/superwall/src/main/java/com/superwall/sdk/models/product/CrossplatformProduct.kt @@ -122,6 +122,18 @@ data class CrossplatformProduct( ) } + @Serializable(with = CustomSerializer::class) + @SerialName("CUSTOM") + data class Custom( + @SerialName("product_identifier") + val productIdentifier: String, + ) : StoreProduct() { + override fun toStoreProductType(): ProductItem.StoreProductType = + ProductItem.StoreProductType.Custom( + CustomStoreProduct(productIdentifier = productIdentifier), + ) + } + @Serializable(with = OtherSerializer::class) @SerialName("OTHER") data class Other( @@ -150,6 +162,7 @@ data class CrossplatformProduct( is StoreProduct.AppStore -> storeProduct.productIdentifier is StoreProduct.Stripe -> storeProduct.productIdentifier is StoreProduct.Paddle -> storeProduct.productIdentifier + is StoreProduct.Custom -> storeProduct.productIdentifier is StoreProduct.Other -> "" } } @@ -331,6 +344,38 @@ object PaddleSerializer : KSerializer } } +object CustomSerializer : KSerializer { + override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Custom") + + override fun serialize( + encoder: Encoder, + value: CrossplatformProduct.StoreProduct.Custom, + ) { + val jsonEncoder = + encoder as? JsonEncoder + ?: throw SerializationException("This class can be saved only by Json") + val jsonObj = + buildJsonObject { + put("store", JsonPrimitive("CUSTOM")) + put("product_identifier", JsonPrimitive(value.productIdentifier)) + } + jsonEncoder.encodeJsonElement(jsonObj) + } + + override fun deserialize(decoder: Decoder): CrossplatformProduct.StoreProduct.Custom { + val jsonDecoder = + decoder as? JsonDecoder + ?: throw SerializationException("This class can be loaded only by Json") + val jsonObject = jsonDecoder.decodeJsonElement() as JsonObject + + val productId = + jsonObject["product_identifier"]?.jsonPrimitive?.content + ?: throw SerializationException("product_identifier is missing") + + return CrossplatformProduct.StoreProduct.Custom(productId) + } +} + object OtherSerializer : KSerializer { override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Other") @@ -407,6 +452,7 @@ object CrossplatformProductSerializer : KSerializer { "APP_STORE" -> decoder.json.decodeFromJsonElement(storeProductJsonObject) "STRIPE" -> decoder.json.decodeFromJsonElement(storeProductJsonObject) "PADDLE" -> decoder.json.decodeFromJsonElement(storeProductJsonObject) + "CUSTOM" -> decoder.json.decodeFromJsonElement(storeProductJsonObject) "OTHER" -> decoder.json.decodeFromJsonElement(storeProductJsonObject) else -> CrossplatformProduct.StoreProduct.Other( diff --git a/superwall/src/main/java/com/superwall/sdk/models/product/ProductItem.kt b/superwall/src/main/java/com/superwall/sdk/models/product/ProductItem.kt index bec6dd6f1..48a8164bc 100644 --- a/superwall/src/main/java/com/superwall/sdk/models/product/ProductItem.kt +++ b/superwall/src/main/java/com/superwall/sdk/models/product/ProductItem.kt @@ -44,6 +44,9 @@ enum class Store { @SerialName("SUPERWALL") SUPERWALL, + @SerialName("CUSTOM") + CUSTOM, + @SerialName("OTHER") OTHER, @@ -57,6 +60,7 @@ enum class Store { "STRIPE" -> STRIPE "PADDLE" -> PADDLE "SUPERWALL" -> SUPERWALL + "CUSTOM" -> CUSTOM else -> OTHER } } @@ -147,6 +151,17 @@ data class PaddleProduct( get() = productIdentifier } +@Serializable +data class CustomStoreProduct( + @SerialName("store") + val store: Store = Store.CUSTOM, + @SerialName("product_identifier") + val productIdentifier: String, +) { + val fullIdentifier: String + get() = productIdentifier +} + @Serializable data class UnknownStoreProduct( @SerialName("product_identifier") @@ -269,6 +284,9 @@ object StoreProductSerializer : KSerializer { is ProductItem.StoreProductType.Paddle -> jsonEncoder.json.encodeToJsonElement(PaddleProduct.serializer(), value.product) + is ProductItem.StoreProductType.Custom -> + jsonEncoder.json.encodeToJsonElement(CustomStoreProduct.serializer(), value.product) + is ProductItem.StoreProductType.Other -> jsonEncoder.json.encodeToJsonElement( UnknownStoreProduct.serializer(), @@ -319,6 +337,11 @@ object StoreProductSerializer : KSerializer { ProductItem.StoreProductType.Paddle(product) } + Store.CUSTOM -> { + val product = json.decodeFromJsonElement(CustomStoreProduct.serializer(), jsonObject) + ProductItem.StoreProductType.Custom(product) + } + Store.SUPERWALL, Store.OTHER, -> { @@ -366,6 +389,11 @@ data class ProductItem( val product: PaddleProduct, ) : StoreProductType() + @Serializable + data class Custom( + val product: CustomStoreProduct, + ) : StoreProductType() + @Serializable data class Other( val product: UnknownStoreProduct, @@ -379,6 +407,7 @@ data class ProductItem( is StoreProductType.AppStore -> type.product.fullIdentifier is StoreProductType.Stripe -> type.product.fullIdentifier is StoreProductType.Paddle -> type.product.fullIdentifier + is StoreProductType.Custom -> type.product.fullIdentifier is StoreProductType.Other -> type.product.productIdentifier } @@ -447,6 +476,7 @@ object ProductItemSerializer : KSerializer { is ProductItem.StoreProductType.AppStore -> storeProductType.product.fullIdentifier is ProductItem.StoreProductType.Stripe -> storeProductType.product.fullIdentifier is ProductItem.StoreProductType.Paddle -> storeProductType.product.fullIdentifier + is ProductItem.StoreProductType.Custom -> storeProductType.product.fullIdentifier is ProductItem.StoreProductType.Other -> storeProductType.product.productIdentifier } diff --git a/superwall/src/main/java/com/superwall/sdk/paywall/request/PaywallLogic.kt b/superwall/src/main/java/com/superwall/sdk/paywall/request/PaywallLogic.kt index bde46b80e..032b09d44 100644 --- a/superwall/src/main/java/com/superwall/sdk/paywall/request/PaywallLogic.kt +++ b/superwall/src/main/java/com/superwall/sdk/paywall/request/PaywallLogic.kt @@ -161,6 +161,20 @@ object PaywallLogic { customerInfo = customerInfo, introOfferEligibility = introOfferEligibility, ) + + is ProductItem.StoreProductType.Custom -> { + // Custom product trial info lives on the cached StoreProduct's + // subscription metadata (fetched from /products). Use the same + // entitlement-history check as web products. + val trialDays = productsByFullId[productItem.fullProductId]?.trialPeriodDays ?: 0 + isWebTrialAvailable( + name = productItem.name, + trialDays = trialDays, + entitlements = productItem.entitlements, + customerInfo = customerInfo, + introOfferEligibility = introOfferEligibility, + ) + } } } diff --git a/superwall/src/main/java/com/superwall/sdk/paywall/request/PaywallRequestManager.kt b/superwall/src/main/java/com/superwall/sdk/paywall/request/PaywallRequestManager.kt index ece937db7..63931dddd 100644 --- a/superwall/src/main/java/com/superwall/sdk/paywall/request/PaywallRequestManager.kt +++ b/superwall/src/main/java/com/superwall/sdk/paywall/request/PaywallRequestManager.kt @@ -14,14 +14,17 @@ import com.superwall.sdk.misc.IOScope import com.superwall.sdk.misc.map import com.superwall.sdk.misc.mapError import com.superwall.sdk.misc.onError +import com.superwall.sdk.misc.fold import com.superwall.sdk.misc.then import com.superwall.sdk.models.customer.CustomerInfo import com.superwall.sdk.models.events.EventData import com.superwall.sdk.models.paywall.Paywall +import com.superwall.sdk.models.product.ProductItem import com.superwall.sdk.network.Network import com.superwall.sdk.paywall.presentation.PaywallInfo import com.superwall.sdk.paywall.presentation.internal.request.ProductOverride import com.superwall.sdk.store.StoreManager +import com.superwall.sdk.store.abstractions.product.ApiStoreProduct import com.superwall.sdk.store.abstractions.product.StoreProduct import com.superwall.sdk.utilities.withErrorTracking import kotlinx.coroutines.CompletableDeferred @@ -297,6 +300,9 @@ class PaywallRequestManager( var paywall = paywall paywall = trackProductsLoadStart(paywall, request) + // Fetch and cache custom products (store == CUSTOM) before Google Play product fetch. + // These are sourced from the Superwall /products endpoint, not from Play Billing. + fetchAndCacheCustomProducts(paywall) paywall = try { getProducts(paywall, request) @@ -308,6 +314,53 @@ class PaywallRequestManager( return@withContext paywall } + /** + * Fetches custom products (ProductItem.StoreProductType.Custom) from the Superwall + * /products endpoint and caches them in StoreManager so the downstream getProducts + * flow finds them already loaded. + * + * Idempotent: skips entirely when no custom products need refreshing. + */ + private suspend fun fetchAndCacheCustomProducts(paywall: Paywall) { + val customIds = + paywall.productItems + .filter { it.type is ProductItem.StoreProductType.Custom } + .map { it.fullProductId } + .filterNot { it.isEmpty() } + .toSet() + if (customIds.isEmpty()) return + + val idsNeedingRefresh = customIds.filterNot { storeManager.hasCached(it) } + if (idsNeedingRefresh.isEmpty()) return + + network.getSuperwallProducts().fold( + onSuccess = { response -> + val matches = response.data.filter { it.identifier in idsNeedingRefresh } + val seenIds = mutableSetOf() + for (superwallProduct in matches) { + if (!seenIds.add(superwallProduct.identifier)) { + Logger.debug( + LogLevel.warn, + LogScope.productsManager, + "Duplicate custom product id from /products: ${superwallProduct.identifier}", + ) + continue + } + val apiProduct = ApiStoreProduct(superwallProduct) + val storeProduct = StoreProduct.custom(apiProduct) + storeManager.cacheProduct(superwallProduct.identifier, storeProduct) + } + }, + onFailure = { error -> + Logger.debug( + LogLevel.error, + LogScope.productsManager, + "Failed to fetch custom products: ${error.message}", + ) + }, + ) + } + private suspend fun getProducts( paywall: Paywall, request: PaywallRequest, diff --git a/superwall/src/main/java/com/superwall/sdk/store/InternalPurchaseController.kt b/superwall/src/main/java/com/superwall/sdk/store/InternalPurchaseController.kt index f74f12532..e1484a790 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/InternalPurchaseController.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/InternalPurchaseController.kt @@ -7,6 +7,7 @@ import com.superwall.sdk.delegate.PurchaseResult import com.superwall.sdk.delegate.RestorationResult import com.superwall.sdk.delegate.subscription_controller.PurchaseController import com.superwall.sdk.delegate.subscription_controller.PurchaseControllerJava +import com.superwall.sdk.store.abstractions.product.StoreProduct import kotlin.coroutines.resume import kotlin.coroutines.suspendCoroutine @@ -61,6 +62,16 @@ class InternalPurchaseController( // return PurchaseResult.Cancelled() } + override suspend fun purchase(customProduct: StoreProduct): PurchaseResult { + if (kotlinPurchaseController != null) { + return kotlinPurchaseController.purchase(customProduct) + } + // No PurchaseControllerJava overload for custom products yet — callers should + // be guarded by TransactionManager against this path when no external controller + // is configured. + return PurchaseResult.Failed("No PurchaseController configured to handle custom product purchase.") + } + override suspend fun restorePurchases(): RestorationResult { if (kotlinPurchaseController != null) { return kotlinPurchaseController.restorePurchases() diff --git a/superwall/src/main/java/com/superwall/sdk/store/testmode/TestStoreProduct.kt b/superwall/src/main/java/com/superwall/sdk/store/abstractions/product/ApiStoreProduct.kt similarity index 97% rename from superwall/src/main/java/com/superwall/sdk/store/testmode/TestStoreProduct.kt rename to superwall/src/main/java/com/superwall/sdk/store/abstractions/product/ApiStoreProduct.kt index 101dae537..53aa4f10c 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/testmode/TestStoreProduct.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/abstractions/product/ApiStoreProduct.kt @@ -1,8 +1,5 @@ -package com.superwall.sdk.store.testmode +package com.superwall.sdk.store.abstractions.product -import com.superwall.sdk.store.abstractions.product.PriceFormatterProvider -import com.superwall.sdk.store.abstractions.product.StoreProductType -import com.superwall.sdk.store.abstractions.product.SubscriptionPeriod import com.superwall.sdk.store.testmode.models.SuperwallProduct import com.superwall.sdk.store.testmode.models.SuperwallSubscriptionPeriod import com.superwall.sdk.utilities.DateUtils @@ -13,7 +10,12 @@ import java.util.Calendar import java.util.Date import java.util.Locale -class TestStoreProduct( +/** + * StoreProductType backed by a Superwall API product (from the /products endpoint). + * Shared between test mode products and custom (CUSTOM-store) products that are not + * fetched from Google Play. + */ +class ApiStoreProduct( private val superwallProduct: SuperwallProduct, ) : StoreProductType { private val priceFormatterProvider = PriceFormatterProvider() diff --git a/superwall/src/main/java/com/superwall/sdk/store/abstractions/product/StoreProduct.kt b/superwall/src/main/java/com/superwall/sdk/store/abstractions/product/StoreProduct.kt index 0cbb1c0e5..f86cdb689 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/abstractions/product/StoreProduct.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/abstractions/product/StoreProduct.kt @@ -78,8 +78,26 @@ sealed class OfferType { class StoreProduct private constructor( val rawStoreProduct: RawStoreProduct?, private val backingProduct: StoreProductType, + val isCustomProduct: Boolean = false, ) : StoreProductType by backingProduct { constructor(rawStoreProduct: RawStoreProduct) : this(rawStoreProduct, rawStoreProduct) constructor(storeProductType: StoreProductType) : this(null, storeProductType) + + /** + * Pre-generated transaction identifier used as the original transaction ID when + * a custom product (store == CUSTOM) is purchased through an external + * PurchaseController. Regenerated by TransactionManager on each purchase attempt. + */ + var customTransactionId: String? = null + + companion object { + /** Builds a StoreProduct flagged as a custom product, backed by an ApiStoreProduct. */ + fun custom(apiStoreProduct: ApiStoreProduct): StoreProduct = + StoreProduct( + rawStoreProduct = null, + backingProduct = apiStoreProduct, + isCustomProduct = true, + ) + } } diff --git a/superwall/src/main/java/com/superwall/sdk/store/abstractions/transactions/StoreTransaction.kt b/superwall/src/main/java/com/superwall/sdk/store/abstractions/transactions/StoreTransaction.kt index e8cf0fb75..138911898 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/abstractions/transactions/StoreTransaction.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/abstractions/transactions/StoreTransaction.kt @@ -17,6 +17,43 @@ class StoreTransaction( @SerialName("app_session_id") val appSessionId: String, ) : StoreTransactionType { + /** + * Builds a StoreTransaction representing a custom (non-Play-Billing) purchase + * completed by an external PurchaseController. The pre-generated + * [customTransactionId] is used as both [originalTransactionIdentifier] and + * [storeTransactionId], mirroring the iOS CustomStoreTransaction. + */ + constructor( + customTransactionId: String, + productIdentifier: String, + purchaseDate: Date, + configRequestId: String, + appSessionId: String, + ) : this( + transaction = + GoogleBillingPurchaseTransaction( + underlyingSK2Transaction = null, + transactionDate = purchaseDate, + originalTransactionIdentifier = customTransactionId, + state = StoreTransactionState.Purchased, + storeTransactionId = customTransactionId, + originalTransactionDate = purchaseDate, + webOrderLineItemID = null, + appBundleId = null, + subscriptionGroupId = null, + isUpgraded = null, + expirationDate = null, + offerId = null, + revocationDate = null, + appAccountToken = null, + purchaseToken = "", + payment = StorePayment(productIdentifier = productIdentifier, quantity = 1, discountIdentifier = null), + signature = null, + ), + configRequestId = configRequestId, + appSessionId = appSessionId, + ) + val id = UUID.randomUUID().toString() override val transactionDate: Date? get() = transaction.transactionDate diff --git a/superwall/src/main/java/com/superwall/sdk/store/testmode/TestMode.kt b/superwall/src/main/java/com/superwall/sdk/store/testmode/TestMode.kt index e3a4ed261..5d1771e3e 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/testmode/TestMode.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/testmode/TestMode.kt @@ -18,6 +18,7 @@ import com.superwall.sdk.storage.Storage import com.superwall.sdk.storage.StoredTestModeSettings import com.superwall.sdk.storage.TestModeSettings import com.superwall.sdk.store.Entitlements +import com.superwall.sdk.store.abstractions.product.ApiStoreProduct import com.superwall.sdk.store.abstractions.product.StoreProduct import com.superwall.sdk.store.testmode.models.SuperwallEntitlementRef import com.superwall.sdk.store.testmode.models.SuperwallProduct @@ -311,7 +312,7 @@ class TestMode( val productsByFullId = androidProducts.associate { superwallProduct -> - val testProduct = TestStoreProduct(superwallProduct) + val testProduct = ApiStoreProduct(superwallProduct) superwallProduct.identifier to StoreProduct(testProduct) } setTestProducts(productsByFullId) diff --git a/superwall/src/main/java/com/superwall/sdk/store/testmode/models/SuperwallProduct.kt b/superwall/src/main/java/com/superwall/sdk/store/testmode/models/SuperwallProduct.kt index 19acc928a..39628896a 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/testmode/models/SuperwallProduct.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/testmode/models/SuperwallProduct.kt @@ -72,4 +72,7 @@ enum class SuperwallProductPlatform { @SerialName("superwall") SUPERWALL, + + @SerialName("custom") + CUSTOM, } diff --git a/superwall/src/main/java/com/superwall/sdk/store/transactions/TransactionManager.kt b/superwall/src/main/java/com/superwall/sdk/store/transactions/TransactionManager.kt index 17fa423bf..904953f8c 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/transactions/TransactionManager.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/transactions/TransactionManager.kt @@ -348,6 +348,12 @@ class TransactionManager( return result } + // Custom product flow: store == CUSTOM products are handled by the external + // PurchaseController, not Google Play Billing. + if (product.isCustomProduct) { + return handleCustomProductPurchase(product, purchaseSource, shouldDismiss) + } + val rawStoreProduct = product.rawStoreProduct ?: return PurchaseResult.Failed("Missing raw store product for ${product.fullIdentifier}") @@ -429,6 +435,117 @@ class TransactionManager( return result } + /** + * Handles purchase of a custom (store == CUSTOM) product. Requires an external + * PurchaseController; fails fast with a clear error otherwise. Pre-generates a + * UUID transaction identifier on each attempt, routes the purchase through + * [PurchaseController.purchase(customProduct:)], and constructs a + * StoreTransaction without touching Google Play Billing / receipts. + */ + private suspend fun handleCustomProductPurchase( + product: StoreProduct, + purchaseSource: PurchaseSource, + shouldDismiss: Boolean, + ): PurchaseResult { + if (!factory.makeHasExternalPurchaseController()) { + val message = + "Custom products require an external PurchaseController. " + + "Configure Superwall with a PurchaseController that overrides " + + "purchase(customProduct:) to handle ${product.fullIdentifier}." + log(message = message, error = Error(message)) + trackFailure(message, product, purchaseSource) + if (purchaseSource is PurchaseSource.Internal) { + updateState( + purchaseSource.paywallInfo.cacheKey, + PaywallViewState.Updates.ToggleSpinner(hidden = true), + ) + } + return PurchaseResult.Failed(message) + } + + // Regenerate the transaction id on every attempt so cancel-and-retry + // doesn't reuse the same identifier in analytics. + product.customTransactionId = java.util.UUID.randomUUID().toString() + + prepareToPurchase(product, purchaseSource) + + val result = storeManager.purchaseController.purchase(customProduct = product) + + // If we only have an external PurchaseController, the dev's flow handles the + // rest of the transaction lifecycle (mirrors the existing ExternalPurchase + // early return below for Play products). + if (purchaseSource is PurchaseSource.ExternalPurchase && + factory.makeHasExternalPurchaseController() && + !factory.makeHasInternalPurchaseController() + ) { + if (result is PurchaseResult.Purchased) { + // Still build + track a transaction so analytics include the custom txn id. + val transaction = + factory.makeStoreTransaction( + customTransactionId = product.customTransactionId ?: java.util.UUID.randomUUID().toString(), + productIdentifier = product.fullIdentifier, + purchaseDate = java.util.Date(), + ) + trackTransactionDidSucceed(transaction, product, purchaseSource, product.hasFreeTrial) + } + return result + } + + when (result) { + is PurchaseResult.Purchased -> { + val transaction = + factory.makeStoreTransaction( + customTransactionId = product.customTransactionId ?: java.util.UUID.randomUUID().toString(), + productIdentifier = product.fullIdentifier, + purchaseDate = java.util.Date(), + ) + // Skip storeManager.loadPurchasedProducts — no Play receipt for custom products. + trackTransactionDidSucceed(transaction, product, purchaseSource, product.hasFreeTrial) + if (shouldDismiss && + purchaseSource is PurchaseSource.Internal && + factory.makeSuperwallOptions().paywalls.automaticallyDismiss + ) { + dismiss( + purchaseSource.paywallInfo.cacheKey, + PaywallResult.Purchased(product.fullIdentifier), + ) + } else if (purchaseSource is PurchaseSource.Internal) { + updateState( + purchaseSource.paywallInfo.cacheKey, + PaywallViewState.Updates.SetLoadingState(PaywallLoadingState.Ready), + ) + } + } + + is PurchaseResult.Failed -> { + trackFailure(result.errorMessage, product, purchaseSource) + if (purchaseSource is PurchaseSource.Internal) { + val options = factory.makeSuperwallOptions() + val triggers = factory.makeTriggers() + val transactionFailExists = + triggers.contains(SuperwallEvents.TransactionFail.rawName) + if (options.paywalls.shouldShowPurchaseFailureAlert && !transactionFailExists) { + presentAlert(Error(result.errorMessage), product, purchaseSource.state) + } else { + updateState( + purchaseSource.paywallInfo.cacheKey, + PaywallViewState.Updates.ToggleSpinner(hidden = true), + ) + } + } + } + + is PurchaseResult.Cancelled -> { + trackCancelled(product, purchaseSource) + } + + is PurchaseResult.Pending -> { + handlePendingTransaction(purchaseSource) + } + } + return result + } + private suspend fun didRestore( product: StoreProduct? = null, purchaseSource: PurchaseSource, diff --git a/superwall/src/test/java/com/superwall/sdk/models/product/CustomStoreProductTest.kt b/superwall/src/test/java/com/superwall/sdk/models/product/CustomStoreProductTest.kt new file mode 100644 index 000000000..f124bd5d7 --- /dev/null +++ b/superwall/src/test/java/com/superwall/sdk/models/product/CustomStoreProductTest.kt @@ -0,0 +1,88 @@ +@file:Suppress("ktlint:standard:function-naming") + +package com.superwall.sdk.models.product + +import com.superwall.sdk.Given +import com.superwall.sdk.Then +import com.superwall.sdk.When +import kotlinx.serialization.json.Json +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +class CustomStoreProductTest { + private val json = + Json { + ignoreUnknownKeys = true + explicitNulls = false + encodeDefaults = true + } + + @Test + fun `deserializes a CUSTOM store product item via the polymorphic serializer`() { + Given("a JSON store_product payload with store CUSTOM") { + val payload = + """ + { + "store": "CUSTOM", + "product_identifier": "stripe_pro_monthly" + } + """.trimIndent() + + When("decoded via StoreProductSerializer") { + val decoded = json.decodeFromString(StoreProductSerializer, payload) + + Then("the result is a Custom variant with the right identifier") { + assertTrue(decoded is ProductItem.StoreProductType.Custom) + val custom = (decoded as ProductItem.StoreProductType.Custom).product + assertEquals("stripe_pro_monthly", custom.productIdentifier) + assertEquals(Store.CUSTOM, custom.store) + assertEquals("stripe_pro_monthly", custom.fullIdentifier) + } + } + } + } + + @Test + fun `round-trips CustomStoreProduct through the serializer`() { + Given("a Custom store product type") { + val original = + ProductItem.StoreProductType.Custom( + CustomStoreProduct(productIdentifier = "stripe_pro_yearly"), + ) + + When("encoded then decoded") { + val encoded = json.encodeToString(StoreProductSerializer, original) + val decoded = json.decodeFromString(StoreProductSerializer, encoded) + + Then("the result equals the original") { + assertTrue(decoded is ProductItem.StoreProductType.Custom) + assertEquals( + original.product.productIdentifier, + (decoded as ProductItem.StoreProductType.Custom).product.productIdentifier, + ) + } + } + } + } + + @Test + fun `ProductItem fullProductId returns the identifier for Custom`() { + Given("a ProductItem wrapping a Custom store product") { + val item = + ProductItem( + compositeId = "stripe_pro_monthly", + name = "pro", + type = + ProductItem.StoreProductType.Custom( + CustomStoreProduct(productIdentifier = "stripe_pro_monthly"), + ), + entitlements = emptySet(), + ) + + Then("fullProductId is the underlying identifier") { + assertEquals("stripe_pro_monthly", item.fullProductId) + } + } + } +} diff --git a/superwall/src/test/java/com/superwall/sdk/paywall/request/PaywallLogicTest.kt b/superwall/src/test/java/com/superwall/sdk/paywall/request/PaywallLogicTest.kt index 531bff726..c6b7f4d8e 100644 --- a/superwall/src/test/java/com/superwall/sdk/paywall/request/PaywallLogicTest.kt +++ b/superwall/src/test/java/com/superwall/sdk/paywall/request/PaywallLogicTest.kt @@ -4,6 +4,7 @@ import com.superwall.sdk.models.customer.CustomerInfo import com.superwall.sdk.models.entitlements.Entitlement import com.superwall.sdk.models.events.EventData import com.superwall.sdk.models.paywall.IntroOfferEligibility +import com.superwall.sdk.models.product.CustomStoreProduct import com.superwall.sdk.models.product.PaddleProduct import com.superwall.sdk.models.product.ProductItem import com.superwall.sdk.models.product.Store @@ -598,6 +599,97 @@ class PaywallLogicTest { assertFalse(outcome.isFreeTrialAvailable) } + // ---- Custom (store == CUSTOM) product trial eligibility ---- + + private fun customItem( + entitlements: Set = setOf(proEntitlement), + productId: String = "custom_pro_1", + ): ProductItem { + val customType = + ProductItem.StoreProductType.Custom( + CustomStoreProduct(productIdentifier = productId), + ) + return mockk { + every { name } returns "primary" + every { fullProductId } returns productId + every { type } returns customType + every { this@mockk.entitlements } returns entitlements + } + } + + private fun storeProductWithTrialDays(trialDays: Int): StoreProduct = + mockk { + every { attributes } returns emptyMap() + every { trialPeriodDays } returns trialDays + } + + @Test + fun test_custom_trialAvailable_whenCustomerHasNoEntitlementHistory() { + val outcome = + PaywallLogic.getVariablesAndFreeTrial( + productItems = listOf(customItem()), + productsByFullId = mapOf("custom_pro_1" to storeProductWithTrialDays(7)), + isFreeTrialAvailableOverride = null, + customerInfo = customerInfoWithEntitlements(), + ) + + assertTrue(outcome.isFreeTrialAvailable) + } + + @Test + fun test_custom_trialBlocked_whenEntitlementAlreadyHeld() { + val consumed = proEntitlement.copy(latestProductId = "custom_pro_old", store = Store.CUSTOM) + + val outcome = + PaywallLogic.getVariablesAndFreeTrial( + productItems = listOf(customItem()), + productsByFullId = mapOf("custom_pro_1" to storeProductWithTrialDays(7)), + isFreeTrialAvailableOverride = null, + customerInfo = customerInfoWithEntitlements(consumed), + ) + + assertFalse(outcome.isFreeTrialAvailable) + } + + @Test + fun test_custom_noTrial_whenTrialDaysZero() { + val outcome = + PaywallLogic.getVariablesAndFreeTrial( + productItems = listOf(customItem()), + productsByFullId = mapOf("custom_pro_1" to storeProductWithTrialDays(0)), + isFreeTrialAvailableOverride = null, + customerInfo = customerInfoWithEntitlements(), + ) + + assertFalse(outcome.isFreeTrialAvailable) + } + + @Test + fun test_custom_noTrial_whenEntitlementsEmpty() { + val outcome = + PaywallLogic.getVariablesAndFreeTrial( + productItems = listOf(customItem(entitlements = emptySet())), + productsByFullId = mapOf("custom_pro_1" to storeProductWithTrialDays(7)), + isFreeTrialAvailableOverride = null, + customerInfo = customerInfoWithEntitlements(), + ) + + assertFalse(outcome.isFreeTrialAvailable) + } + + @Test + fun test_custom_trialBlocked_whenCustomerInfoIsPlaceholder() { + val outcome = + PaywallLogic.getVariablesAndFreeTrial( + productItems = listOf(customItem()), + productsByFullId = mapOf("custom_pro_1" to storeProductWithTrialDays(7)), + isFreeTrialAvailableOverride = null, + customerInfo = CustomerInfo.empty(), + ) + + assertFalse(outcome.isFreeTrialAvailable) + } + @Test fun test_override_winsOverIneligible() { val outcome = diff --git a/superwall/src/test/java/com/superwall/sdk/store/testmode/TestStoreProductTest.kt b/superwall/src/test/java/com/superwall/sdk/store/abstractions/product/ApiStoreProductTest.kt similarity index 78% rename from superwall/src/test/java/com/superwall/sdk/store/testmode/TestStoreProductTest.kt rename to superwall/src/test/java/com/superwall/sdk/store/abstractions/product/ApiStoreProductTest.kt index 686609ace..c746e059f 100644 --- a/superwall/src/test/java/com/superwall/sdk/store/testmode/TestStoreProductTest.kt +++ b/superwall/src/test/java/com/superwall/sdk/store/abstractions/product/ApiStoreProductTest.kt @@ -1,6 +1,6 @@ @file:Suppress("ktlint:standard:function-naming") -package com.superwall.sdk.store.testmode +package com.superwall.sdk.store.abstractions.product import com.superwall.sdk.Given import com.superwall.sdk.Then @@ -20,7 +20,7 @@ import org.junit.Assert.assertTrue import org.junit.Test import java.math.BigDecimal -class TestStoreProductTest { +class ApiStoreProductTest { private fun makeProduct( identifier: String = "com.test.product", amountCents: Int = 999, @@ -63,7 +63,7 @@ class TestStoreProductTest { @Test fun `price converts from cents correctly`() { Given("a product with price 999 cents") { - val testProduct = TestStoreProduct(makeProduct(amountCents = 999)) + val testProduct = ApiStoreProduct(makeProduct(amountCents = 999)) Then("price is 9.99") { assertEquals(0, testProduct.price.compareTo(BigDecimal("9.99"))) @@ -74,7 +74,7 @@ class TestStoreProductTest { @Test fun `price converts zero cents`() { Given("a product with price 0 cents") { - val testProduct = TestStoreProduct(makeProduct(amountCents = 0)) + val testProduct = ApiStoreProduct(makeProduct(amountCents = 0)) Then("price is 0.00") { assertEquals(0, testProduct.price.compareTo(BigDecimal.ZERO)) @@ -85,7 +85,7 @@ class TestStoreProductTest { @Test fun `price converts large amount`() { Given("a product with price 99999 cents") { - val testProduct = TestStoreProduct(makeProduct(amountCents = 99999)) + val testProduct = ApiStoreProduct(makeProduct(amountCents = 99999)) Then("price is 999.99") { assertEquals(0, testProduct.price.compareTo(BigDecimal("999.99"))) @@ -96,7 +96,7 @@ class TestStoreProductTest { @Test fun `price converts single digit cents`() { Given("a product with price 5 cents") { - val testProduct = TestStoreProduct(makeProduct(amountCents = 5)) + val testProduct = ApiStoreProduct(makeProduct(amountCents = 5)) Then("price is 0.05") { assertEquals(0, testProduct.price.compareTo(BigDecimal("0.05"))) @@ -111,7 +111,7 @@ class TestStoreProductTest { @Test fun `subscription period maps daily correctly`() { Given("a daily product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.DAY, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.DAY, periodCount = 1)) Then("subscription period is 1 day") { val period = testProduct.subscriptionPeriod @@ -125,7 +125,7 @@ class TestStoreProductTest { @Test fun `subscription period maps weekly correctly`() { Given("a weekly product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.WEEK, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.WEEK, periodCount = 1)) Then("subscription period is 1 week") { val period = testProduct.subscriptionPeriod @@ -139,7 +139,7 @@ class TestStoreProductTest { @Test fun `subscription period maps monthly correctly`() { Given("a monthly product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) Then("subscription period is 1 month") { val period = testProduct.subscriptionPeriod @@ -153,7 +153,7 @@ class TestStoreProductTest { @Test fun `subscription period maps yearly correctly`() { Given("a yearly product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) Then("subscription period is 1 year") { val period = testProduct.subscriptionPeriod @@ -167,7 +167,7 @@ class TestStoreProductTest { @Test fun `subscription period respects periodCount`() { Given("a product with 3 month period") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 3)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 3)) Then("subscription period has value 3") { val period = testProduct.subscriptionPeriod @@ -181,7 +181,7 @@ class TestStoreProductTest { @Test fun `subscription period is null for non-subscription product`() { Given("a non-subscription product") { - val testProduct = TestStoreProduct(makeNonSubscriptionProduct()) + val testProduct = ApiStoreProduct(makeNonSubscriptionProduct()) Then("subscriptionPeriod is null") { assertNull(testProduct.subscriptionPeriod) @@ -196,7 +196,7 @@ class TestStoreProductTest { @Test fun `free trial is detected correctly`() { Given("a product with a 7-day trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = 7)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = 7)) Then("hasFreeTrial is true") { assertTrue(testProduct.hasFreeTrial) @@ -207,7 +207,7 @@ class TestStoreProductTest { @Test fun `no free trial when trialPeriodDays is null`() { Given("a product with no trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = null)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = null)) Then("hasFreeTrial is false") { assertFalse(testProduct.hasFreeTrial) @@ -218,7 +218,7 @@ class TestStoreProductTest { @Test fun `no free trial when trialPeriodDays is zero`() { Given("a product with 0-day trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = 0)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = 0)) Then("hasFreeTrial is false") { assertFalse(testProduct.hasFreeTrial) @@ -229,7 +229,7 @@ class TestStoreProductTest { @Test fun `trialPeriodText formats correctly`() { Given("a product with a 7-day trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = 7)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = 7)) Then("trialPeriodText is '7-day'") { assertEquals("7-day", testProduct.trialPeriodText) @@ -240,7 +240,7 @@ class TestStoreProductTest { @Test fun `trialPeriodText is empty when no trial`() { Given("a product with no trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = null)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = null)) Then("trialPeriodText is empty") { assertEquals("", testProduct.trialPeriodText) @@ -251,7 +251,7 @@ class TestStoreProductTest { @Test fun `trialPeriodEndDate is set when trial exists`() { Given("a product with a 14-day trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = 14)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = 14)) Then("trialPeriodEndDate is not null and in the future") { assertNotNull(testProduct.trialPeriodEndDate) @@ -263,7 +263,7 @@ class TestStoreProductTest { @Test fun `trialPeriodEndDate is null when no trial`() { Given("a product with no trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = null)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = null)) Then("trialPeriodEndDate is null") { assertNull(testProduct.trialPeriodEndDate) @@ -274,7 +274,7 @@ class TestStoreProductTest { @Test fun `trialPeriodPrice is zero`() { Given("a product with a trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = 7)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = 7)) Then("trialPeriodPrice is zero") { assertEquals(0, testProduct.trialPeriodPrice.compareTo(BigDecimal.ZERO)) @@ -289,7 +289,7 @@ class TestStoreProductTest { @Test fun `trialPeriodDays returns correct value`() { Given("a product with a 30-day trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = 30)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = 30)) Then("trialPeriodDays is 30") { assertEquals(30, testProduct.trialPeriodDays) @@ -301,7 +301,7 @@ class TestStoreProductTest { @Test fun `trialPeriodWeeks calculates from days`() { Given("a product with a 14-day trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = 14)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = 14)) Then("trialPeriodWeeks is 2") { assertEquals(2, testProduct.trialPeriodWeeks) @@ -313,7 +313,7 @@ class TestStoreProductTest { @Test fun `trial period values are zero when no trial`() { Given("a product with no trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = null)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = null)) Then("all trial period values are zero") { assertEquals(0, testProduct.trialPeriodDays) @@ -331,7 +331,7 @@ class TestStoreProductTest { @Test fun `product identifiers are correct`() { Given("a product with identifier 'com.test.premium'") { - val testProduct = TestStoreProduct(makeProduct(identifier = "com.test.premium")) + val testProduct = ApiStoreProduct(makeProduct(identifier = "com.test.premium")) Then("fullIdentifier and productIdentifier are set") { assertEquals("com.test.premium", testProduct.fullIdentifier) @@ -347,7 +347,7 @@ class TestStoreProductTest { @Test fun `productType is subs for subscription products`() { Given("a subscription product") { - val testProduct = TestStoreProduct(makeProduct()) + val testProduct = ApiStoreProduct(makeProduct()) Then("productType is 'subs'") { assertEquals("subs", testProduct.productType) @@ -358,7 +358,7 @@ class TestStoreProductTest { @Test fun `productType is inapp for non-subscription products`() { Given("a non-subscription product") { - val testProduct = TestStoreProduct(makeNonSubscriptionProduct()) + val testProduct = ApiStoreProduct(makeNonSubscriptionProduct()) Then("productType is 'inapp'") { assertEquals("inapp", testProduct.productType) @@ -373,7 +373,7 @@ class TestStoreProductTest { @Test fun `period string formats correctly for monthly`() { Given("a monthly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) Then("period is 'month'") { assertEquals("month", testProduct.period) @@ -384,7 +384,7 @@ class TestStoreProductTest { @Test fun `period string formats correctly for yearly`() { Given("a yearly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) Then("period is 'year'") { assertEquals("year", testProduct.period) @@ -395,7 +395,7 @@ class TestStoreProductTest { @Test fun `period string formats correctly for weekly`() { Given("a weekly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.WEEK, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.WEEK, periodCount = 1)) Then("period is 'week'") { assertEquals("week", testProduct.period) @@ -406,7 +406,7 @@ class TestStoreProductTest { @Test fun `period string for quarterly shows quarter`() { Given("a 3-month subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 3)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 3)) Then("period is 'quarter'") { assertEquals("quarter", testProduct.period) @@ -417,7 +417,7 @@ class TestStoreProductTest { @Test fun `period string for semi-annual shows 6 months`() { Given("a 6-month subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 6)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 6)) Then("period is '6 months'") { assertEquals("6 months", testProduct.period) @@ -428,7 +428,7 @@ class TestStoreProductTest { @Test fun `period string for bi-monthly shows 2 months`() { Given("a 2-month subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 2)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 2)) Then("period is '2 months'") { assertEquals("2 months", testProduct.period) @@ -439,7 +439,7 @@ class TestStoreProductTest { @Test fun `period string for 7-day product shows week`() { Given("a 7-day subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.DAY, periodCount = 7)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.DAY, periodCount = 7)) Then("period is 'week'") { assertEquals("week", testProduct.period) @@ -450,7 +450,7 @@ class TestStoreProductTest { @Test fun `period is empty for non-subscription product`() { Given("a non-subscription product") { - val testProduct = TestStoreProduct(makeNonSubscriptionProduct()) + val testProduct = ApiStoreProduct(makeNonSubscriptionProduct()) Then("period is empty") { assertEquals("", testProduct.period) @@ -465,7 +465,7 @@ class TestStoreProductTest { @Test fun `periodly formats monthly correctly`() { Given("a monthly product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) Then("periodly is 'monthly'") { assertEquals("monthly", testProduct.periodly) @@ -476,7 +476,7 @@ class TestStoreProductTest { @Test fun `periodly formats yearly correctly`() { Given("a yearly product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) Then("periodly is 'yearly'") { assertEquals("yearly", testProduct.periodly) @@ -487,7 +487,7 @@ class TestStoreProductTest { @Test fun `periodly formats quarterly as every quarter`() { Given("a quarterly product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 3)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 3)) Then("periodly is 'quarterly'") { assertEquals("quarterly", testProduct.periodly) @@ -498,7 +498,7 @@ class TestStoreProductTest { @Test fun `periodly formats semi-annual as every 6 months`() { Given("a 6-month product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 6)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 6)) Then("periodly is 'every 6 months'") { assertEquals("every 6 months", testProduct.periodly) @@ -509,7 +509,7 @@ class TestStoreProductTest { @Test fun `periodly formats bi-monthly as every 2 months`() { Given("a 2-month product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 2)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 2)) Then("periodly is 'every 2 months'") { assertEquals("every 2 months", testProduct.periodly) @@ -524,7 +524,7 @@ class TestStoreProductTest { @Test fun `periodDays calculates correctly for yearly`() { Given("a yearly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) Then("periodDays is 365") { assertEquals(365, testProduct.periodDays) @@ -536,7 +536,7 @@ class TestStoreProductTest { @Test fun `periodDays calculates correctly for monthly`() { Given("a monthly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) Then("periodDays is 30") { assertEquals(30, testProduct.periodDays) @@ -547,7 +547,7 @@ class TestStoreProductTest { @Test fun `periodDays calculates correctly for weekly`() { Given("a weekly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.WEEK, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.WEEK, periodCount = 1)) Then("periodDays is 7") { assertEquals(7, testProduct.periodDays) @@ -558,7 +558,7 @@ class TestStoreProductTest { @Test fun `periodWeeks calculates correctly for yearly`() { Given("a yearly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) Then("periodWeeks is 52") { assertEquals(52, testProduct.periodWeeks) @@ -570,7 +570,7 @@ class TestStoreProductTest { @Test fun `periodMonths calculates correctly for yearly`() { Given("a yearly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) Then("periodMonths is 12") { assertEquals(12, testProduct.periodMonths) @@ -582,7 +582,7 @@ class TestStoreProductTest { @Test fun `periodYears is 1 for yearly product`() { Given("a yearly subscription product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) Then("periodYears is 1") { assertEquals(1, testProduct.periodYears) @@ -594,7 +594,7 @@ class TestStoreProductTest { @Test fun `period calculations are zero for non-subscription product`() { Given("a non-subscription product") { - val testProduct = TestStoreProduct(makeNonSubscriptionProduct()) + val testProduct = ApiStoreProduct(makeNonSubscriptionProduct()) Then("all period values are 0") { assertEquals(0, testProduct.periodDays) @@ -612,7 +612,7 @@ class TestStoreProductTest { @Test fun `localizedSubscriptionPeriod for monthly`() { Given("a monthly product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 1)) Then("localizedSubscriptionPeriod is '1 month'") { assertEquals("1 month", testProduct.localizedSubscriptionPeriod) @@ -623,7 +623,7 @@ class TestStoreProductTest { @Test fun `localizedSubscriptionPeriod for plural months`() { Given("a 3-month product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 3)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.MONTH, periodCount = 3)) Then("localizedSubscriptionPeriod is '3 months'") { assertEquals("3 months", testProduct.localizedSubscriptionPeriod) @@ -634,7 +634,7 @@ class TestStoreProductTest { @Test fun `localizedSubscriptionPeriod for yearly`() { Given("a yearly product") { - val testProduct = TestStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) + val testProduct = ApiStoreProduct(makeProduct(period = SuperwallSubscriptionPeriod.YEAR, periodCount = 1)) Then("localizedSubscriptionPeriod is '1 year'") { assertEquals("1 year", testProduct.localizedSubscriptionPeriod) @@ -645,7 +645,7 @@ class TestStoreProductTest { @Test fun `localizedSubscriptionPeriod is empty for non-subscription`() { Given("a non-subscription product") { - val testProduct = TestStoreProduct(makeNonSubscriptionProduct()) + val testProduct = ApiStoreProduct(makeNonSubscriptionProduct()) Then("localizedSubscriptionPeriod is empty") { assertEquals("", testProduct.localizedSubscriptionPeriod) @@ -660,7 +660,7 @@ class TestStoreProductTest { @Test fun `currencyCode is set from product`() { Given("a product with EUR currency") { - val testProduct = TestStoreProduct(makeProduct(currency = "EUR")) + val testProduct = ApiStoreProduct(makeProduct(currency = "EUR")) Then("currencyCode is EUR") { assertEquals("EUR", testProduct.currencyCode) @@ -671,7 +671,7 @@ class TestStoreProductTest { @Test fun `currencySymbol resolves for USD`() { Given("a product with USD currency") { - val testProduct = TestStoreProduct(makeProduct(currency = "USD")) + val testProduct = ApiStoreProduct(makeProduct(currency = "USD")) Then("currencySymbol is not null and contains dollar sign") { assertNotNull(testProduct.currencySymbol) @@ -683,7 +683,7 @@ class TestStoreProductTest { @Test fun `currencySymbol resolves for EUR`() { Given("a product with EUR currency") { - val testProduct = TestStoreProduct(makeProduct(currency = "EUR")) + val testProduct = ApiStoreProduct(makeProduct(currency = "EUR")) Then("currencySymbol is euro sign") { // The euro sign can vary by locale, just verify it's not null @@ -699,7 +699,7 @@ class TestStoreProductTest { @Test fun `attributes map contains all expected keys`() { Given("a subscription product with trial") { - val testProduct = TestStoreProduct(makeProduct(trialPeriodDays = 7)) + val testProduct = ApiStoreProduct(makeProduct(trialPeriodDays = 7)) When("attributes is accessed") { val attrs = testProduct.attributes @@ -727,7 +727,7 @@ class TestStoreProductTest { @Test fun `attributes identifier matches product`() { Given("a product with identifier 'com.test.pro'") { - val testProduct = TestStoreProduct(makeProduct(identifier = "com.test.pro")) + val testProduct = ApiStoreProduct(makeProduct(identifier = "com.test.pro")) Then("attributes identifier is correct") { assertEquals("com.test.pro", testProduct.attributes["identifier"]) diff --git a/superwall/src/test/java/com/superwall/sdk/store/abstractions/transactions/CustomStoreTransactionTest.kt b/superwall/src/test/java/com/superwall/sdk/store/abstractions/transactions/CustomStoreTransactionTest.kt new file mode 100644 index 000000000..5f29198a3 --- /dev/null +++ b/superwall/src/test/java/com/superwall/sdk/store/abstractions/transactions/CustomStoreTransactionTest.kt @@ -0,0 +1,69 @@ +@file:Suppress("ktlint:standard:function-naming") + +package com.superwall.sdk.store.abstractions.transactions + +import com.superwall.sdk.Given +import com.superwall.sdk.Then +import com.superwall.sdk.When +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test +import java.util.Date + +class CustomStoreTransactionTest { + @Test + fun `custom-transaction constructor sets ids and skips SK2 fields`() { + Given("a pre-generated transaction id, product id and purchase date") { + val txnId = "ABC-123-CUSTOM" + val productId = "stripe_pro_monthly" + val purchaseDate = Date(1_700_000_000_000L) + + When("a StoreTransaction is built via the custom constructor") { + val tx = + StoreTransaction( + customTransactionId = txnId, + productIdentifier = productId, + purchaseDate = purchaseDate, + configRequestId = "req-1", + appSessionId = "sess-1", + ) + + Then("originalTransactionIdentifier and storeTransactionId equal the custom id") { + assertEquals(txnId, tx.originalTransactionIdentifier) + assertEquals(txnId, tx.storeTransactionId) + } + + Then("transaction date and original date both equal the purchase date") { + assertEquals(purchaseDate, tx.transactionDate) + assertEquals(purchaseDate, tx.originalTransactionDate) + } + + Then("state is Purchased") { + assertEquals(StoreTransactionState.Purchased, tx.state) + } + + Then("payment carries the product identifier") { + assertEquals(productId, tx.payment?.productIdentifier) + } + + Then("SK2 / Play-specific fields are nullable empties") { + assertNull(tx.webOrderLineItemID) + assertNull(tx.appBundleId) + assertNull(tx.subscriptionGroupId) + assertNull(tx.isUpgraded) + assertNull(tx.expirationDate) + assertNull(tx.offerId) + assertNull(tx.revocationDate) + assertNull(tx.appAccountToken) + assertEquals("", tx.purchaseToken) + assertNull(tx.signature) + } + + Then("config and session ids are preserved") { + assertEquals("req-1", tx.configRequestId) + assertEquals("sess-1", tx.appSessionId) + } + } + } + } +} diff --git a/superwall/src/test/java/com/superwall/sdk/store/testmode/models/SuperwallProductSerializationTest.kt b/superwall/src/test/java/com/superwall/sdk/store/testmode/models/SuperwallProductSerializationTest.kt index 20d5107fd..809f4040f 100644 --- a/superwall/src/test/java/com/superwall/sdk/store/testmode/models/SuperwallProductSerializationTest.kt +++ b/superwall/src/test/java/com/superwall/sdk/store/testmode/models/SuperwallProductSerializationTest.kt @@ -58,6 +58,32 @@ class SuperwallProductSerializationTest { } } + @Test + fun `deserializes product with platform custom`() { + Given("a JSON product with platform custom") { + val jsonString = + """ + { + "identifier": "stripe_pro_monthly", + "platform": "custom", + "price": { "amount": 1499, "currency": "USD" }, + "subscription": { "period": "month", "period_count": 1, "trial_period_days": 14 } + } + """.trimIndent() + + When("the product is deserialized") { + val product = json.decodeFromString(jsonString) + + Then("platform is CUSTOM and fields are preserved") { + assertEquals("stripe_pro_monthly", product.identifier) + assertEquals(SuperwallProductPlatform.CUSTOM, product.platform) + assertEquals(1499, product.price!!.amount) + assertEquals(14, product.subscription!!.trialPeriodDays) + } + } + } + } + @Test fun `deserializes product without subscription`() { Given("a JSON product without subscription field") {