forked from openedx/openedx-app-android
-
Notifications
You must be signed in to change notification settings - Fork 0
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
chore: add missing course properties in IAP restore & unfulfilled events #82
Open
farhan-arshad-dev
wants to merge
2
commits into
2U/develop
Choose a base branch
from
2U/farhan_ar/LEARNER-10309
base: 2U/develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,6 +172,7 @@ val screenModule = module { | |
get(), | ||
get(), | ||
get(), | ||
get(), | ||
windowSize, | ||
get(), | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,12 @@ import org.openedx.core.R | |
import org.openedx.core.config.Config | ||
import org.openedx.core.data.repository.iap.IAPRepository | ||
import org.openedx.core.data.storage.CorePreferences | ||
import org.openedx.core.domain.model.EnrolledCourse | ||
import org.openedx.core.domain.model.iap.ProductInfo | ||
import org.openedx.core.domain.model.iap.PurchaseFlowData | ||
import org.openedx.core.exception.iap.IAPException | ||
import org.openedx.core.extension.decodeToLong | ||
import org.openedx.core.extension.decodeToString | ||
import org.openedx.core.module.billing.BillingProcessor | ||
import org.openedx.core.module.billing.getCourseSku | ||
import org.openedx.core.module.billing.getPriceAmount | ||
|
@@ -109,12 +112,42 @@ class IAPInteractor( | |
} | ||
} | ||
|
||
suspend fun processUnfulfilledPurchase(userId: Long): Boolean { | ||
suspend fun processUnfulfilledPurchase( | ||
userId: Long, | ||
enrolledCourses: List<EnrolledCourse>, | ||
purchaseVerified: (PurchaseFlowData) -> Unit = {}, | ||
): Boolean { | ||
val purchases = billingProcessor.queryPurchases() | ||
val userPurchases = | ||
purchases.filter { it.accountIdentifiers?.obfuscatedAccountId?.decodeToLong() == userId } | ||
val userPurchases = purchases.filter { purchase -> | ||
val userAccountId = purchase.accountIdentifiers?.obfuscatedAccountId?.decodeToLong() | ||
val storeSku = purchase.accountIdentifiers?.obfuscatedProfileId?.decodeToString() | ||
|
||
userAccountId == userId && enrolledCourses.any { enrolledCourse -> | ||
storeSku == enrolledCourse.productInfo?.courseSku | ||
} | ||
} | ||
if (userPurchases.isNotEmpty()) { | ||
startUnfulfilledVerification(userPurchases) | ||
userPurchases.forEach { purchase -> | ||
val courseVerified = enrolledCourses.find { enrolledCourse -> | ||
enrolledCourse.productInfo?.courseSku == purchase.getCourseSku() | ||
} | ||
Comment on lines
+131
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is same as above. |
||
courseVerified?.let { | ||
val productDetails = | ||
billingProcessor.querySyncDetails(purchase.products[0]).productDetailsList?.firstOrNull() | ||
val purchaseProductFlow = PurchaseFlowData( | ||
courseId = courseVerified.course.id, | ||
isSelfPaced = courseVerified.course.isSelfPaced, | ||
productInfo = courseVerified.productInfo | ||
).apply { | ||
productDetails?.oneTimePurchaseOfferDetails?.let { | ||
this.price = it.getPriceAmount() | ||
this.currencyCode = it.priceCurrencyCode | ||
} | ||
} | ||
startUnfulfilledVerification(purchase) | ||
purchaseVerified(purchaseProductFlow) | ||
} | ||
} | ||
return true | ||
} else { | ||
purchases.forEach { | ||
|
@@ -124,34 +157,34 @@ class IAPInteractor( | |
return false | ||
} | ||
|
||
private suspend fun startUnfulfilledVerification(userPurchases: List<Purchase>) { | ||
userPurchases.forEach { purchase -> | ||
val productDetail = | ||
billingProcessor.querySyncDetails(purchase.products.first()).productDetailsList?.firstOrNull() | ||
productDetail?.oneTimePurchaseOfferDetails?.takeIf { | ||
purchase.getCourseSku().isNullOrEmpty().not() | ||
}?.let { oneTimeProductDetails -> | ||
val courseSku = purchase.getCourseSku() ?: return@let | ||
val basketId = addToBasket(courseSku) | ||
executeOrder( | ||
basketId = basketId, | ||
purchaseToken = purchase.purchaseToken, | ||
price = oneTimeProductDetails.getPriceAmount(), | ||
currencyCode = oneTimeProductDetails.priceCurrencyCode, | ||
) | ||
consumePurchase(purchase.purchaseToken) | ||
} | ||
private suspend fun startUnfulfilledVerification(userPurchase: Purchase) { | ||
val productDetail = | ||
billingProcessor.querySyncDetails(userPurchase.products.first()).productDetailsList?.firstOrNull() | ||
productDetail?.oneTimePurchaseOfferDetails?.takeIf { | ||
userPurchase.getCourseSku().isNullOrEmpty().not() | ||
}?.let { oneTimeProductDetails -> | ||
val courseSku = userPurchase.getCourseSku() ?: return@let | ||
val basketId = addToBasket(courseSku) | ||
executeOrder( | ||
basketId = basketId, | ||
purchaseToken = userPurchase.purchaseToken, | ||
price = oneTimeProductDetails.getPriceAmount(), | ||
currencyCode = oneTimeProductDetails.priceCurrencyCode, | ||
) | ||
consumePurchase(userPurchase.purchaseToken) | ||
} | ||
} | ||
|
||
suspend fun detectUnfulfilledPurchase( | ||
enrolledCourses: List<EnrolledCourse>, | ||
purchaseVerified: (PurchaseFlowData) -> Unit, | ||
onSuccess: () -> Unit, | ||
onFailure: (IAPException) -> Unit, | ||
) { | ||
if (isIAPEnabled) { | ||
preferencesManager.user?.id?.let { userId -> | ||
runCatching { | ||
processUnfulfilledPurchase(userId) | ||
processUnfulfilledPurchase(userId, enrolledCourses, purchaseVerified) | ||
}.onSuccess { | ||
if (it) { | ||
onSuccess() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
package org.openedx.core.system.notifier.app | ||
|
||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
|
||
class AppNotifier { | ||
|
||
private val channel = MutableSharedFlow<AppEvent>(replay = 0, extraBufferCapacity = 0) | ||
private val channel = MutableSharedFlow<AppEvent>( | ||
replay = 0, | ||
extraBufferCapacity = 1, | ||
onBufferOverflow = BufferOverflow.DROP_OLDEST, | ||
) | ||
|
||
val notifier: Flow<AppEvent> = channel.asSharedFlow() | ||
|
||
|
@@ -16,4 +21,7 @@ class AppNotifier { | |
|
||
suspend fun send(event: AppUpgradeEvent) = channel.emit(event) | ||
|
||
suspend fun send(event: EnrolledCourseEvent) = channel.emit(event) | ||
|
||
suspend fun send(event: RequestEnrolledCourseEvent) = channel.emit(event) | ||
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should rename them and place them in |
||
} |
5 changes: 5 additions & 0 deletions
5
core/src/main/java/org/openedx/core/system/notifier/app/EnrolledCourseEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.openedx.core.system.notifier.app | ||
|
||
import org.openedx.core.domain.model.EnrolledCourse | ||
|
||
class EnrolledCourseEvent(val enrolledCourses: List<EnrolledCourse>,) : AppEvent |
3 changes: 3 additions & 0 deletions
3
core/src/main/java/org/openedx/core/system/notifier/app/RequestEnrolledCourseEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.openedx.core.system.notifier.app | ||
|
||
object RequestEnrolledCourseEvent : AppEvent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use
Purchase.getCourseSku()
extension function here.