diff --git a/.github/badges/branches.svg b/.github/badges/branches.svg index cf88d0552..95275ca45 100644 --- a/.github/badges/branches.svg +++ b/.github/badges/branches.svg @@ -1 +1 @@ -branches35.2% \ No newline at end of file +branches36.2% \ No newline at end of file diff --git a/.github/badges/jacoco.svg b/.github/badges/jacoco.svg index b9dcd7843..0ab08157f 100644 --- a/.github/badges/jacoco.svg +++ b/.github/badges/jacoco.svg @@ -1 +1 @@ -coverage44.7% \ No newline at end of file +coverage45.2% \ 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 886f5eca7..20a27a866 100644 --- a/superwall/src/main/java/com/superwall/sdk/dependencies/DependencyContainer.kt +++ b/superwall/src/main/java/com/superwall/sdk/dependencies/DependencyContainer.kt @@ -314,7 +314,7 @@ class DependencyContainer( InternalPurchaseController( kotlinPurchaseController = purchaseController - ?: AutomaticPurchaseController(context, ioScope, entitlements), + ?: AutomaticPurchaseController(context, ioScope, { entitlements }), javaPurchaseController = null, context, ) @@ -330,6 +330,7 @@ class DependencyContainer( customerInfoManager = { customerInfoManager }, ) }, + getSuperwallProducts = { network.getSuperwallProducts() }, testMode = testMode, ) @@ -943,6 +944,9 @@ class DependencyContainer( override fun makeHasExternalPurchaseController(): Boolean = storeManager.purchaseController.hasExternalPurchaseController + override fun makeHasCustomProductPurchaseController(): Boolean = + storeManager.purchaseController.hasCustomProductPurchaseController + override fun makeHasInternalPurchaseController(): Boolean = storeManager.purchaseController.hasInternalPurchaseController @@ -1073,6 +1077,19 @@ class DependencyContainer( appSessionId = appSessionManager.appSession.id, ) + override suspend fun makeStoreTransaction( + customTransactionId: String, + productIdentifier: String, + purchaseDate: 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 27cc6a212..8a12f0300 100644 --- a/superwall/src/main/java/com/superwall/sdk/dependencies/FactoryProtocols.kt +++ b/superwall/src/main/java/com/superwall/sdk/dependencies/FactoryProtocols.kt @@ -49,6 +49,7 @@ import com.superwall.sdk.store.abstractions.transactions.StoreTransaction import com.superwall.sdk.store.testmode.TestMode import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.StateFlow +import java.util.Date interface ApiFactory : JsonFactory { // TODO: Think of an alternative way such that we don't need to do this: @@ -189,6 +190,15 @@ interface UserAttributesEventFactory { interface HasExternalPurchaseControllerFactory { fun makeHasExternalPurchaseController(): Boolean + + /** + * Whether the configured controller can fulfill custom (store == CUSTOM) product purchases. + * Defaults to [makeHasExternalPurchaseController] since a fully external controller may + * override purchase(customProduct:); a CustomProductPurchaseController reports true here + * while reporting false for makeHasExternalPurchaseController (Superwall still manages the + * standard Play lifecycle for it). + */ + fun makeHasCustomProductPurchaseController(): Boolean = makeHasExternalPurchaseController() } interface HasInternalPurchaseControllerFactory { @@ -238,6 +248,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: 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..12932335e 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,9 +161,47 @@ 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, + ) + } } } + /** + * Entitlement-history-based trial eligibility for a custom (store == CUSTOM) product, + * mirroring the check used for web products in [computeHasFreeTrial]. Used at purchase + * time so a repeat purchaser who already consumed their trial does not generate a + * spurious `freeTrialStart` event — the external payment system charges them immediately. + * + * Returns false when the product has no trial metadata, when eligibility can't be + * verified (e.g. no entitlements or customer info not yet loaded), or when the customer + * has ever held one of the product's entitlements. + */ + fun isFreeTrialEligibleForCustomProduct( + product: StoreProduct, + entitlements: Set, + customerInfo: CustomerInfo, + introOfferEligibility: IntroOfferEligibility = IntroOfferEligibility.AUTOMATIC, + ): Boolean = + isWebTrialAvailable( + name = product.fullIdentifier, + trialDays = product.trialPeriodDays, + entitlements = entitlements, + customerInfo = customerInfo, + introOfferEligibility = introOfferEligibility, + ) + private fun isWebTrialAvailable( name: String, trialDays: Int?, 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 8199fe731..0f099e806 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 @@ -18,6 +18,7 @@ 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 @@ -297,17 +298,47 @@ class PaywallRequestManager( var paywall = paywall paywall = trackProductsLoadStart(paywall, request) - paywall = - try { - getProducts(paywall, request) - } catch (error: Throwable) { - throw error - } + try { + // Custom products (store == CUSTOM) come from /products, not Play Billing. + // A /products failure is fatal — mirror BillingNotAvailable below. + fetchAndCacheCustomProducts(paywall) + } catch (error: Throwable) { + paywall.productsLoadingInfo.failAt = Date() + track( + InternalSuperwallEvent.PaywallProductsLoad( + state = InternalSuperwallEvent.PaywallProductsLoad.State.Fail(error.message), + paywallInfo = paywall.getInfo(request.eventData), + eventData = request.eventData, + ), + ) + throw error + } + paywall = getProducts(paywall, request) paywall = trackProductsLoadFinish(paywall, request.eventData) 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. A /products + * failure is propagated (required = true) so [addProducts] surfaces it as a tracked + * product-load failure instead of presenting a paywall with missing prices. + */ + private suspend fun fetchAndCacheCustomProducts(paywall: Paywall) { + val customIds = + paywall.productItems + .filter { it.type is ProductItem.StoreProductType.Custom } + .map { it.fullProductId } + .toSet() + if (customIds.isEmpty()) return + + storeManager.fetchAndCacheCustomProducts(customIds, required = true) + } + private suspend fun getProducts( paywall: Paywall, request: PaywallRequest, diff --git a/superwall/src/main/java/com/superwall/sdk/store/AutomaticPurchaseController.kt b/superwall/src/main/java/com/superwall/sdk/store/AutomaticPurchaseController.kt index a3afcb12b..83f08d3e9 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/AutomaticPurchaseController.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/AutomaticPurchaseController.kt @@ -29,6 +29,7 @@ import com.superwall.sdk.models.entitlements.SubscriptionStatus import com.superwall.sdk.store.abstractions.product.BasePlanType import com.superwall.sdk.store.abstractions.product.OfferType import com.superwall.sdk.store.abstractions.product.RawStoreProduct +import com.superwall.sdk.store.abstractions.product.StoreProduct import com.superwall.sdk.store.transactions.PlayBillingErrors import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope @@ -50,7 +51,7 @@ private val BILLING_INSANTIATION_ERROR = class AutomaticPurchaseController( var context: Context, val scope: IOScope, - val entitlementsInfo: Entitlements, + val entitlementsInfo: () -> Entitlements = { Superwall.instance.dependencyContainer.entitlements }, val getBilling: (Context, PurchasesUpdatedListener) -> BillingClient = { ctx, listener -> try { BillingClient @@ -110,7 +111,7 @@ class AutomaticPurchaseController( LogLevel.error, LogScope.nativePurchaseController, "ExternalNativePurchaseController billing client disconnected, " + - "retrying in $reconnectMilliseconds milliseconds", + "retrying in $reconnectMilliseconds milliseconds", ) CoroutineScope(Dispatchers.IO).launch { @@ -270,6 +271,26 @@ class AutomaticPurchaseController( return value } + /** + * The automatic controller only handles Google Play products. Custom (store == CUSTOM) + * products require a [CustomProductPurchaseController] (or a PurchaseController that + * overrides purchase(customProduct:)). TransactionManager guards against reaching this in + * the normal flow; this override logs and fails clearly as a safety net. + */ + override suspend fun purchase(customProduct: StoreProduct): PurchaseResult { + val message = + "AutomaticPurchaseController cannot purchase custom (store == CUSTOM) product " + + "'${customProduct.fullIdentifier}'. Configure Superwall with a " + + "CustomProductPurchaseController (or a PurchaseController that overrides " + + "purchase(customProduct:)) to handle custom products." + Logger.debug( + logLevel = LogLevel.error, + scope = LogScope.nativePurchaseController, + message = message, + ) + return PurchaseResult.Failed(message) + } + override suspend fun restorePurchases(): RestorationResult { syncSubscriptionStatusAndWait() return RestorationResult.Restored() @@ -349,7 +370,7 @@ class AutomaticPurchaseController( it.products }.toSet() .flatMap { - val res = entitlementsInfo.byProductId(it) + val res = entitlementsInfo().byProductId(it) res }.toSet() .let { entitlements -> @@ -359,7 +380,7 @@ class AutomaticPurchaseController( message = "Found entitlements: ${entitlements.joinToString { it.id }}", ) - entitlementsInfo.activeDeviceEntitlements = entitlements + entitlementsInfo().activeDeviceEntitlements = entitlements if (entitlements.isNotEmpty()) { SubscriptionStatus.Active( entitlements.map { it.copy(isActive = true) }.toSet(), diff --git a/superwall/src/main/java/com/superwall/sdk/store/CustomProductPurchaseController.kt b/superwall/src/main/java/com/superwall/sdk/store/CustomProductPurchaseController.kt new file mode 100644 index 000000000..f416e3185 --- /dev/null +++ b/superwall/src/main/java/com/superwall/sdk/store/CustomProductPurchaseController.kt @@ -0,0 +1,24 @@ +package com.superwall.sdk.store + +import android.content.Context +import com.superwall.sdk.delegate.PurchaseResult +import com.superwall.sdk.delegate.subscription_controller.PurchaseController +import com.superwall.sdk.misc.IOScope +import com.superwall.sdk.store.abstractions.product.StoreProduct + +/** + * Use this controller as a [PurchaseController] in [com.superwall.sdk.Superwall.configure] if you: + * - Want to use custom store products + * - Also want to keep other purchases going through superwall + */ +class CustomProductPurchaseController( + val appContext: Context, + val onPurchase: (customProduct: StoreProduct)-> PurchaseResult +) : PurchaseController by AutomaticPurchaseController( + appContext, + IOScope() +){ + override suspend fun purchase(customProduct: StoreProduct): PurchaseResult { + return onPurchase(customProduct) + } +} 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..ec91ded8b 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 @@ -19,7 +20,19 @@ class InternalPurchaseController( get() = !hasInternalPurchaseController val hasInternalPurchaseController: Boolean - get() = kotlinPurchaseController is AutomaticPurchaseController + get() = + kotlinPurchaseController is AutomaticPurchaseController || + // Delegates standard purchases to the automatic controller, so Superwall still + // manages the Play lifecycle; only custom products route to the dev's lambda. + kotlinPurchaseController is CustomProductPurchaseController + + /** + * Whether the configured controller can fulfill custom (store == CUSTOM) product purchases: + * a dedicated [CustomProductPurchaseController], or any fully external controller (which may + * override purchase(customProduct:)). + */ + val hasCustomProductPurchaseController: Boolean + get() = kotlinPurchaseController is CustomProductPurchaseController || hasExternalPurchaseController override suspend fun purchase( activity: Activity, @@ -61,6 +74,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/StoreManager.kt b/superwall/src/main/java/com/superwall/sdk/store/StoreManager.kt index 744171660..698e6bc4f 100644 --- a/superwall/src/main/java/com/superwall/sdk/store/StoreManager.kt +++ b/superwall/src/main/java/com/superwall/sdk/store/StoreManager.kt @@ -9,16 +9,21 @@ import com.superwall.sdk.billing.DecomposedProductIds import com.superwall.sdk.logger.LogLevel import com.superwall.sdk.logger.LogScope import com.superwall.sdk.logger.Logger +import com.superwall.sdk.misc.Either +import com.superwall.sdk.misc.fold import com.superwall.sdk.models.entitlements.Entitlement import com.superwall.sdk.models.paywall.Paywall import com.superwall.sdk.models.product.PlayStoreProduct import com.superwall.sdk.models.product.ProductItem import com.superwall.sdk.models.product.ProductVariable +import com.superwall.sdk.network.NetworkError import com.superwall.sdk.paywall.request.PaywallRequest +import com.superwall.sdk.store.abstractions.product.ApiStoreProduct import com.superwall.sdk.store.abstractions.product.StoreProduct import com.superwall.sdk.store.abstractions.product.receipt.ReceiptManager import com.superwall.sdk.store.coordinator.ProductsFetcher import com.superwall.sdk.store.testmode.TestMode +import com.superwall.sdk.store.testmode.models.SuperwallProductsResponse import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.awaitAll import java.util.Date @@ -31,6 +36,9 @@ class StoreManager( private val track: suspend (InternalSuperwallEvent) -> Unit = { Superwall.instance.track(it) }, + private val getSuperwallProducts: suspend () -> Either = { + Either.Success(SuperwallProductsResponse(emptyList())) + }, var testMode: TestMode? = null, ) : ProductsFetcher, StoreKit { @@ -71,8 +79,10 @@ class StoreManager( request = request, ) + // Iterate all product items (not just Play Store) so custom products surface variables + // too; only DebugView consumes this, but a custom product should still show there. val productAttributes = - paywall.playStoreProducts.mapNotNull { productItem -> + paywall.productItems.mapNotNull { productItem -> output.productsByFullId[productItem.fullProductId]?.let { storeProduct -> ProductVariable( name = productItem.name, @@ -101,15 +111,88 @@ class StoreManager( ) val productsById = processingResult.substituteProductsById.toMutableMap() - val fetchResult = fetchOrAwaitProducts(processingResult.fullProductIdsToLoad) - for ((id, product) in fetchResult) { - productsById[id] = product + // Try Play Billing first so Play-only lookups never pay for a /products round-trip. + val billingError = + try { + for ((id, product) in fetchOrAwaitProducts(processingResult.fullProductIdsToLoad)) { + productsById[id] = product + } + null + } catch (e: Throwable) { + e + } + + // Anything Play couldn't resolve may be a custom (store == CUSTOM) product, which + // doesn't need Play Billing — probe /products only for those. + val unresolved = processingResult.fullProductIdsToLoad - productsById.keys + if (unresolved.isNotEmpty()) { + fetchAndCacheCustomProducts(unresolved, required = false) + for (id in unresolved) { + getProductFromCache(id)?.let { productsById[id] = it } + } + } + + // Surface the billing error only if /products didn't cover the gap. + if (billingError != null && (processingResult.fullProductIdsToLoad - productsById.keys).isNotEmpty()) { + throw billingError } return productsById } + /** + * Fetches custom (store == CUSTOM) products from the Superwall /products endpoint and + * caches them so downstream lookups resolve them from cache instead of Google Play + * Billing. Ids that are already cached, empty, or absent from /products are left + * untouched (the latter fall through to billing). + * + * @param candidateIds The product identifiers that may be custom products. + * @param required When true — a paywall declared these ids as custom products — a + * /products failure is rethrown so the caller can surface a product-load failure. + * When false — a best-effort probe where the ids may well be Play products — the + * failure is logged and swallowed so the ids fall through to billing. + */ + suspend fun fetchAndCacheCustomProducts( + candidateIds: Set, + required: Boolean, + ) { + val idsNeedingRefresh = + candidateIds + .filterNot { it.isEmpty() } + .filterNot { hasCached(it) } + .toSet() + if (idsNeedingRefresh.isEmpty()) return + + getSuperwallProducts().fold( + onSuccess = { response -> + val seenIds = mutableSetOf() + response.data + .filter { it.identifier in idsNeedingRefresh } + .forEach { superwallProduct -> + if (!seenIds.add(superwallProduct.identifier)) { + Logger.debug( + LogLevel.warn, + LogScope.productsManager, + "Duplicate custom product id from /products: ${superwallProduct.identifier}", + ) + return@forEach + } + val apiProduct = ApiStoreProduct(superwallProduct) + cacheProduct(superwallProduct.identifier, StoreProduct.custom(apiProduct)) + } + }, + onFailure = { error -> + Logger.debug( + LogLevel.error, + LogScope.productsManager, + "Failed to fetch custom products: ${error.message}", + ) + if (required) throw error + }, + ) + } + override suspend fun getProducts( substituteProducts: Map?, paywall: Paywall, 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 a69e34aad..2bd51b1ca 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..b451bbf94 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,27 @@ 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 id for the current custom-product (store == CUSTOM) purchase, + * exposed so the developer can read it inside `purchase(customProduct:)`. Regenerated on + * each attempt. Because custom instances are cached and shared, this field is only reliable + * within a single non-overlapping flow; analytics use a local copy so they stay correct. + */ + 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 cef1689fa..2699e365f 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 @@ -326,7 +327,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 30f19d13e..5393faa25 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 @@ -32,9 +32,11 @@ import com.superwall.sdk.misc.ActivityProvider import com.superwall.sdk.misc.AlertControllerFactory.AlertProps import com.superwall.sdk.misc.IOScope import com.superwall.sdk.misc.launchWithTracking +import com.superwall.sdk.models.customer.CustomerInfo import com.superwall.sdk.models.entitlements.Entitlement import com.superwall.sdk.models.entitlements.SubscriptionStatus import com.superwall.sdk.paywall.presentation.PaywallInfo +import com.superwall.sdk.paywall.request.PaywallLogic import com.superwall.sdk.paywall.presentation.internal.state.PaywallResult import com.superwall.sdk.paywall.view.PaywallView import com.superwall.sdk.paywall.view.PaywallViewState @@ -56,6 +58,8 @@ import kotlinx.coroutines.flow.dropWhile import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeoutOrNull +import java.util.Date +import java.util.UUID import java.util.concurrent.ConcurrentHashMap class TransactionManager( @@ -79,6 +83,9 @@ class TransactionManager( private val webEntitlements: () -> Set = { Superwall.instance.entitlements.web }, + private val currentCustomerInfo: () -> CustomerInfo = { + Superwall.instance.getCustomerInfo() + }, private val showRestoreDialogForWeb: suspend () -> Unit, private val notifyBackendOfReceipts: suspend () -> Unit = {}, private val refreshReceipt: () -> Unit, @@ -354,6 +361,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}") @@ -435,6 +448,153 @@ 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.makeHasCustomProductPurchaseController()) { + val message = + "Custom products require a PurchaseController that handles them. Configure " + + "Superwall with a CustomProductPurchaseController (or 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) { + // Surface the misconfiguration on the paywall, mirroring other purchase + // failures, instead of silently hiding the spinner. + val options = factory.makeSuperwallOptions() + val transactionFailExists = + factory.makeTriggers().contains(SuperwallEvents.TransactionFail.rawName) + if (options.paywalls.shouldShowPurchaseFailureAlert && !transactionFailExists) { + presentAlert(Error(message), product, purchaseSource.state) + } else { + updateState( + purchaseSource.paywallInfo.cacheKey, + PaywallViewState.Updates.ToggleSpinner(hidden = true), + ) + } + } + return PurchaseResult.Failed(message) + } + + // Local copy keeps this flow consistent even if a concurrent purchase overwrites the + // shared product's field; product.customTransactionId is set for the dev-facing contract. + val txnId = UUID.randomUUID().toString() + product.customTransactionId = txnId + + // Entitlement-history-based eligibility, not raw trial metadata: a repeat purchaser who + // already used their trial is charged immediately and must not emit freeTrialStart. + val didStartFreeTrial = + PaywallLogic.isFreeTrialEligibleForCustomProduct( + product = product, + entitlements = entitlementsById(product.fullIdentifier), + customerInfo = currentCustomerInfo(), + ) + + prepareToPurchase(product, purchaseSource) + + // prepareToPurchase skips the Start event for an external-only controller, but a custom + // purchase is otherwise invisible to Superwall — emit Start so it pairs with the Complete + // tracked on success and transaction funnels stay balanced. + val isExternalOnly = + purchaseSource is PurchaseSource.ExternalPurchase && + factory.makeHasExternalPurchaseController() && + !factory.makeHasInternalPurchaseController() + if (isExternalOnly) { + track( + InternalSuperwallEvent.Transaction( + InternalSuperwallEvent.Transaction.State.Start(product), + PaywallInfo.empty(), + product, + null, + isObserved = false, + source = TransactionSource.EXTERNAL, + demandScore = factory.demandScore(), + demandTier = factory.demandTier(), + ), + ) + } + + val result = storeManager.purchaseController.purchase(customProduct = product) + + // For an external-only controller the dev's flow owns the rest of the lifecycle; still + // build + track the transaction so analytics include the custom txn id. + if (isExternalOnly) { + if (result is PurchaseResult.Purchased) { + val transaction = + factory.makeStoreTransaction( + customTransactionId = txnId, + productIdentifier = product.fullIdentifier, + purchaseDate = Date(), + ) + trackTransactionDidSucceed(transaction, product, purchaseSource, didStartFreeTrial) + } + return result + } + + when (result) { + is PurchaseResult.Purchased -> { + val transaction = + factory.makeStoreTransaction( + customTransactionId = txnId, + productIdentifier = product.fullIdentifier, + purchaseDate = Date(), + ) + // Skip storeManager.loadPurchasedProducts — no Play receipt for custom products. + trackTransactionDidSucceed(transaction, product, purchaseSource, didStartFreeTrial) + 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/billing/StorefrontTest.kt b/superwall/src/test/java/com/superwall/sdk/billing/StorefrontTest.kt index 37649d0ba..9fb4a61c4 100644 --- a/superwall/src/test/java/com/superwall/sdk/billing/StorefrontTest.kt +++ b/superwall/src/test/java/com/superwall/sdk/billing/StorefrontTest.kt @@ -14,7 +14,7 @@ import com.superwall.sdk.misc.AppLifecycleObserver import com.superwall.sdk.misc.IOScope import com.superwall.sdk.paywall.presentation.PaywallInfo import com.superwall.sdk.store.StoreManager -import com.superwall.sdk.store.testmode.TestStoreProduct +import com.superwall.sdk.store.abstractions.product.ApiStoreProduct import com.superwall.sdk.store.testmode.models.SuperwallProduct import com.superwall.sdk.store.testmode.models.SuperwallProductPlatform import io.mockk.Runs @@ -135,7 +135,7 @@ class StorefrontTest { } private val someProduct = - TestStoreProduct( + ApiStoreProduct( SuperwallProduct( identifier = "test_product", platform = SuperwallProductPlatform.ANDROID, @@ -193,7 +193,7 @@ class StorefrontTest { fun testStoreProduct_exposesBackendProvidedStorefront() { Given("a test mode product with a backend-provided storefront") { val product = - TestStoreProduct( + ApiStoreProduct( SuperwallProduct( identifier = "test_product", platform = SuperwallProductPlatform.ANDROID, @@ -212,7 +212,7 @@ class StorefrontTest { fun storeProduct_withoutStorefront_fallsBackToNa() { Given("a product without any storefront information") { val product = - TestStoreProduct( + ApiStoreProduct( SuperwallProduct( identifier = "test_product", platform = SuperwallProductPlatform.ANDROID, 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..7af458f39 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 = @@ -611,4 +703,83 @@ class PaywallLogicTest { assertTrue(outcome.isFreeTrialAvailable) } + + // ---- isFreeTrialEligibleForCustomProduct (purchase-time eligibility) ---- + + private fun customStoreProduct( + trialDays: Int, + id: String = "custom_pro_1", + ): StoreProduct = + mockk { + every { fullIdentifier } returns id + every { trialPeriodDays } returns trialDays + } + + @Test + fun test_customEligibility_true_whenTrialAndNoHistory() { + assertTrue( + PaywallLogic.isFreeTrialEligibleForCustomProduct( + product = customStoreProduct(trialDays = 7), + entitlements = setOf(proEntitlement), + customerInfo = customerInfoWithEntitlements(), + ), + ) + } + + @Test + fun test_customEligibility_false_whenTrialAlreadyConsumed() { + val consumed = proEntitlement.copy(latestProductId = "custom_pro_old", store = Store.CUSTOM) + assertFalse( + PaywallLogic.isFreeTrialEligibleForCustomProduct( + product = customStoreProduct(trialDays = 7), + entitlements = setOf(proEntitlement), + customerInfo = customerInfoWithEntitlements(consumed), + ), + ) + } + + @Test + fun test_customEligibility_false_whenNoTrialDays() { + assertFalse( + PaywallLogic.isFreeTrialEligibleForCustomProduct( + product = customStoreProduct(trialDays = 0), + entitlements = setOf(proEntitlement), + customerInfo = customerInfoWithEntitlements(), + ), + ) + } + + @Test + fun test_customEligibility_false_whenNoEntitlements() { + assertFalse( + PaywallLogic.isFreeTrialEligibleForCustomProduct( + product = customStoreProduct(trialDays = 7), + entitlements = emptySet(), + customerInfo = customerInfoWithEntitlements(), + ), + ) + } + + @Test + fun test_customEligibility_false_whenCustomerInfoIsPlaceholder() { + assertFalse( + PaywallLogic.isFreeTrialEligibleForCustomProduct( + product = customStoreProduct(trialDays = 7), + entitlements = setOf(proEntitlement), + customerInfo = CustomerInfo.empty(), + ), + ) + } + + @Test + fun test_customEligibility_false_whenIntroOfferIneligible() { + assertFalse( + PaywallLogic.isFreeTrialEligibleForCustomProduct( + product = customStoreProduct(trialDays = 7), + entitlements = setOf(proEntitlement), + customerInfo = customerInfoWithEntitlements(), + introOfferEligibility = IntroOfferEligibility.INELIGIBLE, + ), + ) + } } diff --git a/superwall/src/test/java/com/superwall/sdk/store/InternalPurchaseControllerTest.kt b/superwall/src/test/java/com/superwall/sdk/store/InternalPurchaseControllerTest.kt index f172ec1c0..64da611e0 100644 --- a/superwall/src/test/java/com/superwall/sdk/store/InternalPurchaseControllerTest.kt +++ b/superwall/src/test/java/com/superwall/sdk/store/InternalPurchaseControllerTest.kt @@ -104,6 +104,72 @@ class InternalPurchaseControllerTest { } } + @Test + fun `test CustomProductPurchaseController is treated as internal, not external`() = + runTest { + Given("an InternalPurchaseController wrapping a CustomProductPurchaseController") { + val customProductController = mockk() + val controller = + InternalPurchaseController( + kotlinPurchaseController = customProductController, + javaPurchaseController = null, + context = mockContext, + ) + + When("checking the controller flags") { + Then("it is internal — Superwall manages the standard Play lifecycle") { + assertTrue(controller.hasInternalPurchaseController) + assertFalse(controller.hasExternalPurchaseController) + } + + And("it can still fulfill custom product purchases") { + assertTrue(controller.hasCustomProductPurchaseController) + } + } + } + } + + @Test + fun `test a fully external controller can fulfill custom products`() = + runTest { + Given("an InternalPurchaseController with a generic external controller") { + val externalController = mockk() + val controller = + InternalPurchaseController( + kotlinPurchaseController = externalController, + javaPurchaseController = null, + context = mockContext, + ) + + When("checking hasCustomProductPurchaseController") { + Then("it is true — the controller may override purchase(customProduct:)") { + assertTrue(controller.hasExternalPurchaseController) + assertTrue(controller.hasCustomProductPurchaseController) + } + } + } + } + + @Test + fun `test automatic controller cannot fulfill custom products`() = + runTest { + Given("an InternalPurchaseController with an AutomaticPurchaseController") { + val automaticController = mockk() + val controller = + InternalPurchaseController( + kotlinPurchaseController = automaticController, + javaPurchaseController = null, + context = mockContext, + ) + + When("checking hasCustomProductPurchaseController") { + Then("it is false — custom purchases are rejected") { + assertFalse(controller.hasCustomProductPurchaseController) + } + } + } + } + @Test fun `test purchase delegates to Kotlin controller`() = runTest { diff --git a/superwall/src/test/java/com/superwall/sdk/store/StoreManagerTest.kt b/superwall/src/test/java/com/superwall/sdk/store/StoreManagerTest.kt index a411b611f..5e8403f16 100644 --- a/superwall/src/test/java/com/superwall/sdk/store/StoreManagerTest.kt +++ b/superwall/src/test/java/com/superwall/sdk/store/StoreManagerTest.kt @@ -7,14 +7,21 @@ import com.superwall.sdk.When import com.superwall.sdk.assertTrue import com.superwall.sdk.billing.Billing import com.superwall.sdk.billing.BillingError +import com.superwall.sdk.misc.Either import com.superwall.sdk.models.entitlements.Entitlement import com.superwall.sdk.models.paywall.Paywall import com.superwall.sdk.models.product.CrossplatformProduct import com.superwall.sdk.models.product.Offer +import com.superwall.sdk.network.NetworkError import com.superwall.sdk.paywall.request.PaywallRequest import com.superwall.sdk.store.abstractions.product.StoreProduct import com.superwall.sdk.store.testmode.TestMode import com.superwall.sdk.store.testmode.TestModeBehavior +import com.superwall.sdk.store.testmode.models.SuperwallProduct +import com.superwall.sdk.store.testmode.models.SuperwallProductPlatform +import com.superwall.sdk.store.testmode.models.SuperwallProductSubscription +import com.superwall.sdk.store.testmode.models.SuperwallProductsResponse +import com.superwall.sdk.store.testmode.models.SuperwallSubscriptionPeriod import io.mockk.coEvery import io.mockk.coVerify import io.mockk.every @@ -658,6 +665,60 @@ class StoreManagerTest { } } + // ---- Custom (store == CUSTOM) products ---- + + private fun customProductsResponse(vararg ids: String): SuperwallProductsResponse = + SuperwallProductsResponse( + data = + ids.map { + SuperwallProduct( + identifier = it, + platform = SuperwallProductPlatform.CUSTOM, + subscription = + SuperwallProductSubscription( + period = SuperwallSubscriptionPeriod.MONTH, + trialPeriodDays = 7, + ), + ) + }, + ) + + private fun storeManagerWith(getSuperwallProducts: suspend () -> Either): StoreManager = + StoreManager( + purchaseController = purchaseController, + billing = billing, + receiptManagerFactory = { mockk(relaxed = true) }, + track = {}, + getSuperwallProducts = getSuperwallProducts, + ) + + @Test + fun `getProductsWithoutPaywall resolves a custom product via products when billing misses`() = + runTest { + Given("billing that doesn't know the id and a /products response that does") { + var productsCalls = 0 + val manager = + storeManagerWith { + productsCalls++ + Either.Success(customProductsResponse("custom_1")) + } + coEvery { billing.awaitGetProducts(any()) } returns emptySet() + + When("getProductsWithoutPaywall is called for the custom id") { + val result = manager.getProductsWithoutPaywall(listOf("custom_1")) + + Then("it resolves from /products as a custom product") { + assertEquals("custom_1", result["custom_1"]?.fullIdentifier) + junitAssertTrue(result["custom_1"]?.isCustomProduct == true) + } + + And("/products was consulted exactly once") { + assertEquals(1, productsCalls) + } + } + } + } + @Test fun `test getProducts in test mode returns partial test catalog when billing is unavailable`() = runTest { @@ -686,6 +747,30 @@ class StoreManagerTest { } } + @Test + fun `getProductsWithoutPaywall does not consult products when billing resolves everything`() = + runTest { + Given("billing that resolves the Play product") { + var productsCalls = 0 + val manager = + storeManagerWith { + productsCalls++ + Either.Success(SuperwallProductsResponse(emptyList())) + } + val play = mockk { every { fullIdentifier } returns "play_1" } + coEvery { billing.awaitGetProducts(any()) } returns setOf(play) + + When("getProductsWithoutPaywall is called") { + val result = manager.getProductsWithoutPaywall(listOf("play_1")) + + Then("the Play product is returned without a /products round-trip") { + assertEquals(play, result["play_1"]) + assertEquals(0, productsCalls) + } + } + } + } + @Test fun `test getProducts waits for the test catalog instead of racing activation`() = runTest { @@ -725,4 +810,53 @@ class StoreManagerTest { } } } + + @Test + fun `getProductsWithoutPaywall resolves a custom product even when billing is unavailable`() = + runTest { + Given("billing that throws and /products that has the custom product") { + val manager = storeManagerWith { Either.Success(customProductsResponse("custom_1")) } + coEvery { billing.awaitGetProducts(any()) } throws BillingError.BillingNotAvailable("nope") + + When("getProductsWithoutPaywall is called for the custom id") { + val result = manager.getProductsWithoutPaywall(listOf("custom_1")) + + Then("it still resolves the custom product without throwing") { + junitAssertTrue(result["custom_1"]?.isCustomProduct == true) + } + } + } + } + + @Test + fun `fetchAndCacheCustomProducts rethrows on products failure when required`() = + runTest { + Given("a /products endpoint that fails") { + val manager = storeManagerWith { Either.Failure(NetworkError.Unknown(Exception("boom"))) } + + When("fetchAndCacheCustomProducts is called with required = true") { + Then("it rethrows so the paywall can surface a product-load failure") { + assertThrows(NetworkError.Unknown::class.java) { + runBlocking { manager.fetchAndCacheCustomProducts(setOf("custom_1"), required = true) } + } + } + } + } + } + + @Test + fun `fetchAndCacheCustomProducts swallows products failure when not required`() = + runTest { + Given("a /products endpoint that fails") { + val manager = storeManagerWith { Either.Failure(NetworkError.Unknown(Exception("boom"))) } + + When("fetchAndCacheCustomProducts is called with required = false") { + manager.fetchAndCacheCustomProducts(setOf("custom_1"), required = false) + + Then("it does not throw and caches nothing") { + assertNull(manager.getProductFromCache("custom_1")) + } + } + } + } } 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") {