Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send initiation_source in post receipt requests #1212

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ internal class PostPendingTransactionsHelper(
transactionsToSync,
allowSharingPlayStoreAccount,
appUserID,
PostReceiptInitiationSource.QUEUE,
transactionPostSuccess = { _, customerInfo ->
results.add(Result.Success(customerInfo))
callCompletionFromResults(transactionsToSync, results, onError, onSuccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal class PostReceiptHelper(
isRestore: Boolean,
appUserID: String,
marketplace: String?,
initiationSource: PostReceiptInitiationSource,
onSuccess: (CustomerInfo) -> Unit,
onError: (PurchasesError) -> Unit,
) {
Expand All @@ -48,6 +49,7 @@ internal class PostReceiptHelper(
receiptInfo,
storeUserID,
marketplace,
initiationSource,
onSuccess = {
deviceCache.addSuccessfullyPostedToken(purchaseToken)
onSuccess(it)
Expand Down Expand Up @@ -79,6 +81,7 @@ internal class PostReceiptHelper(
storeProduct: StoreProduct?,
isRestore: Boolean,
appUserID: String,
initiationSource: PostReceiptInitiationSource,
onSuccess: (SuccessfulPurchaseCallback)? = null,
onError: (ErrorPurchaseCallback)? = null,
) {
Expand All @@ -96,6 +99,7 @@ internal class PostReceiptHelper(
receiptInfo = receiptInfo,
storeUserID = purchase.storeUserID,
marketplace = purchase.marketplace,
initiationSource = initiationSource,
onSuccess = { info ->
billing.consumeAndSave(finishTransactions, purchase)
onSuccess?.let { it(purchase, info) }
Expand Down Expand Up @@ -125,6 +129,7 @@ internal class PostReceiptHelper(
receiptInfo: ReceiptInfo,
storeUserID: String?,
marketplace: String?,
initiationSource: PostReceiptInitiationSource,
onSuccess: (CustomerInfo) -> Unit,
onError: PostReceiptDataErrorCallback,
) {
Expand All @@ -138,6 +143,7 @@ internal class PostReceiptHelper(
receiptInfo = receiptInfo,
storeAppUserID = storeUserID,
marketplace = marketplace,
initiationSource = initiationSource,
onSuccess = { customerInfo, responseBody ->
offlineEntitlementsManager.resetOfflineCustomerInfoCache()
subscriberAttributesManager.markAsSynced(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.revenuecat.purchases

internal enum class PostReceiptInitiationSource {
RESTORE,
PURCHASE,
QUEUE,
;

val postReceiptFieldValue: String
get() = when (this) {
RESTORE -> "restore"
PURCHASE -> "purchase"
QUEUE -> "queue"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ internal class PostTransactionWithProductDetailsHelper(
/**
* The callbacks in this method are called for each transaction in the list.
*/
@Suppress("LongParameterList")
fun postTransactions(
transactions: List<StoreTransaction>,
allowSharingPlayStoreAccount: Boolean,
appUserID: String,
initiationSource: PostReceiptInitiationSource,
transactionPostSuccess: (SuccessfulPurchaseCallback)? = null,
transactionPostError: (ErrorPurchaseCallback)? = null,
) {
Expand All @@ -46,6 +48,7 @@ internal class PostTransactionWithProductDetailsHelper(
storeProduct = purchasedStoreProduct,
isRestore = allowSharingPlayStoreAccount,
appUserID = appUserID,
initiationSource = initiationSource,
onSuccess = transactionPostSuccess,
onError = transactionPostError,
)
Expand All @@ -56,6 +59,7 @@ internal class PostTransactionWithProductDetailsHelper(
storeProduct = null,
isRestore = allowSharingPlayStoreAccount,
appUserID = appUserID,
initiationSource = initiationSource,
onSuccess = transactionPostSuccess,
onError = transactionPostError,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ internal class PurchasesOrchestrator constructor(
this.allowSharingPlayStoreAccount,
appUserID,
marketplace = null,
PostReceiptInitiationSource.RESTORE,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming RESTORE for syncing amazon here... which might depends on the user's implementation. Alternatively, we could add another source for this, but seemed unnecessary since it's still kinda a sync, even if it happens right after a purchase. Lmk if you think otherwise @aboedo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this is a tricky one.

For observer mode for any other store we'd do queue, right? Maybe we should do the same here? It'd mean we don't alias unless you specifically call logIn I suppose, which should be a good thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, for observer mode in android we would actually do purchase, since it's coming from the listener.

In Android, we only use queue when syncing unsynced purchases on app open/foreground. That doesn't actually use the listener.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for purchases outside the app, it would also be purchase

{
val logMessage = PurchaseStrings.PURCHASE_SYNCED_USER_ID.format(receiptID, amazonUserID)
log(LogIntent.PURCHASE, logMessage)
Expand Down Expand Up @@ -339,6 +340,7 @@ internal class PurchasesOrchestrator constructor(
storeProduct = null,
isRestore = true,
appUserID = appUserID,
initiationSource = PostReceiptInitiationSource.RESTORE,
onSuccess = { _, info ->
log(LogIntent.DEBUG, RestoreStrings.PURCHASE_RESTORED.format(purchase))
if (sortedByTime.last() == purchase) {
Expand Down Expand Up @@ -779,6 +781,7 @@ internal class PurchasesOrchestrator constructor(
purchases,
allowSharingPlayStoreAccount,
appUserID,
PostReceiptInitiationSource.PURCHASE,
transactionPostSuccess = callbackPair.first,
transactionPostError = callbackPair.second,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal class SyncPurchasesHelper(
isRestore,
appUserID,
purchase.marketplace,
PostReceiptInitiationSource.RESTORE,
{
log(LogIntent.PURCHASE, PurchaseStrings.PURCHASE_SYNCED.format(purchase))
handleLastPurchase(purchase, lastPurchase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.revenuecat.purchases.common

import com.revenuecat.purchases.CustomerInfo
import com.revenuecat.purchases.PostReceiptInitiationSource
import com.revenuecat.purchases.PurchasesError
import com.revenuecat.purchases.PurchasesErrorCode
import com.revenuecat.purchases.common.networking.Endpoint
Expand Down Expand Up @@ -176,6 +177,7 @@ internal class Backend(
storeAppUserID: String?,
@SuppressWarnings("UnusedPrivateMember")
marketplace: String? = null,
initiationSource: PostReceiptInitiationSource,
onSuccess: PostReceiptDataSuccessCallback,
onError: PostReceiptDataErrorCallback,
) {
Expand Down Expand Up @@ -204,6 +206,7 @@ internal class Backend(
"store_user_id" to storeAppUserID,
"pricing_phases" to receiptInfo.pricingPhases?.map { it.toMap() },
"proration_mode" to receiptInfo.prorationMode?.name,
"initiation_source" to initiationSource.postReceiptFieldValue,
).filterNotNullValues()

val postFieldsToSign = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ internal open class BasePurchasesTest {
private fun mockPostReceiptHelper() {
with(mockPostReceiptHelper) {
every {
postTransactionAndConsumeIfNeeded(any(), any(), any(), any(), captureLambda(), any())
postTransactionAndConsumeIfNeeded(any(), any(), any(), any(), any(), captureLambda(), any())
} answers {
lambda<SuccessfulPurchaseCallback>().captured.invoke(
firstArg(),
mockInfo
)
}
every {
postTokenWithoutConsuming(any(), any(), any(), any(), any(), any(), captureLambda(), any())
postTokenWithoutConsuming(any(), any(), any(), any(), any(), any(), any(), captureLambda(), any())
} answers {
lambda<(CustomerInfo) -> Unit>().captured.invoke(mockInfo)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PostPendingTransactionsHelperTest {

private val allowSharingPlayStoreAccount = true
private val appUserId = "test-app-user-id"
private val initiationSource = PostReceiptInitiationSource.QUEUE

private lateinit var appConfig: AppConfig
private lateinit var deviceCache: DeviceCache
Expand Down Expand Up @@ -73,7 +74,7 @@ class PostPendingTransactionsHelperTest {
billing.queryPurchases(any(), any(), any())
}
verify(exactly = 0) {
postTransactionWithProductDetailsHelper.postTransactions(any(), any(), any(), any(), any())
postTransactionWithProductDetailsHelper.postTransactions(any(), any(), any(), any(), any(), any())
}
}

Expand Down Expand Up @@ -121,6 +122,7 @@ class PostPendingTransactionsHelperTest {
listOf(activePurchase),
allowSharingPlayStoreAccount,
appUserId,
initiationSource,
any(),
any()
)
Expand Down Expand Up @@ -178,7 +180,7 @@ class PostPendingTransactionsHelperTest {
syncAndAssertSuccessful(null)

verify(exactly = 0) {
postTransactionWithProductDetailsHelper.postTransactions(any(), any(), any(), any(), any())
postTransactionWithProductDetailsHelper.postTransactions(any(), any(), any(), any(), any(), any())
}
}

Expand All @@ -195,7 +197,7 @@ class PostPendingTransactionsHelperTest {

verify(exactly = 0) { deviceCache.cleanPreviouslySentTokens(any()) }
verify(exactly = 0) {
postTransactionWithProductDetailsHelper.postTransactions(any(), any(), any(), any(), any())
postTransactionWithProductDetailsHelper.postTransactions(any(), any(), any(), any(), any(), any())
}
}

Expand Down Expand Up @@ -242,6 +244,7 @@ class PostPendingTransactionsHelperTest {
allPurchases,
allowSharingPlayStoreAccount,
appUserId,
initiationSource,
captureLambda(),
any()
)
Expand Down Expand Up @@ -291,6 +294,7 @@ class PostPendingTransactionsHelperTest {
allPurchases,
allowSharingPlayStoreAccount,
appUserId,
initiationSource,
capture(successSlot),
capture(errorSlot)
)
Expand All @@ -314,6 +318,7 @@ class PostPendingTransactionsHelperTest {
transactions ?: any(),
allowSharingPlayStoreAccount,
appUserId,
initiationSource,
captureLambda(),
any()
)
Expand Down
Loading