Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
feat: Compile store sku from enrollments and remote config data
Browse files Browse the repository at this point in the history
Fixes: LEARNER-9818
  • Loading branch information
HamzaIsrar12 committed Feb 20, 2024
1 parent 43f1dfb commit cc4deb8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ data class IAPConfig(
@SerializedName("experiment_enabled")
val isExperimentEnabled: Boolean = false,

@SerializedName("product_prefix")
val prodcut_prefix: String = "",

@SerializedName("android_disabled_versions")
val disableVersions: List<String> = listOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.edx.mobile.model.api

import com.google.gson.annotations.SerializedName
import java.io.Serializable
import kotlin.math.ceil

data class CourseMode(
@SerializedName("slug")
Expand All @@ -12,4 +13,20 @@ data class CourseMode(

@SerializedName("android_sku")
val androidSku: String?,
) : Serializable

@SerializedName("min_price")
val price: Double?,

var storeSku: String?,
) : Serializable {

fun setStoreProductSku(storeProductPrefix: String) {
val ceilPrice = price
?.let { ceil(it).toInt() }
?.takeIf { it > 0 }

if (storeProductPrefix.isNotBlank() && ceilPrice != null) {
storeSku = "$storeProductPrefix$ceilPrice"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,21 @@ data class EnrolledCoursesResponse(
}
}

fun setStoreSku(storeProductPrefix: String) {
courseModes?.forEach {
it.setStoreProductSku(storeProductPrefix)
}
}

private val courseSku: String?
get() = courseModes?.firstOrNull { item ->
EnrollmentMode.VERIFIED.name.equals(item.slug, ignoreCase = true)
}?.androidSku.takeUnless { it.isNullOrEmpty() }

private val storeSku: String?
get() = "org.edx.mobile.test_product1"
get() = courseModes?.firstOrNull { item ->
EnrollmentMode.VERIFIED.name.equals(item.slug, ignoreCase = true)
}?.storeSku

override fun isChapter(): Boolean {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.annotations.SerializedName
import com.google.gson.reflect.TypeToken
import org.edx.mobile.extenstion.isNotNullOrEmpty
import org.edx.mobile.logger.Logger
import org.edx.mobile.model.iap.IAPFlowData
import java.io.Serializable
Expand Down Expand Up @@ -59,6 +60,12 @@ data class EnrollmentResponse(
AppConfig::class.java
)

if (appConfig.iapConfig.prodcut_prefix.isNotNullOrEmpty()) {
enrolledCourses.forEach { courseData ->
courseData.setStoreSku(appConfig.iapConfig.prodcut_prefix)
}
}

EnrollmentResponse(appConfig, enrolledCourses)
}
} catch (ex: Exception) {
Expand All @@ -81,8 +88,7 @@ fun List<EnrolledCoursesResponse>.getAuditCourses(): List<IAPFlowData> {
course.productInfo?.let { productInfo ->
IAPFlowData(
courseId = course.courseId,
courseSku = productInfo.courseSku,
storeSku = productInfo.storeSku,
productInfo = productInfo,
isCourseSelfPaced = course.course.isSelfPaced
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.edx.mobile.model.iap

import org.edx.mobile.model.api.EnrolledCoursesResponse.ProductInfo
import java.io.Serializable

data class IAPFlowData(
var flowType: IAPFlowType = IAPFlowType.USER_INITIATED,
var courseId: String = "",
var isCourseSelfPaced: Boolean = false,
var courseSku: String = "",
var storeSku: String = "",
var productInfo: ProductInfo = ProductInfo("", ""),
var basketId: Long = 0,
var purchaseToken: String = "",
var price: Double = 0.0,
Expand All @@ -18,8 +18,7 @@ data class IAPFlowData(
fun clear() {
courseId = ""
isCourseSelfPaced = false
courseSku = ""
storeSku = ""
productInfo = ProductInfo("", "")
basketId = 0
price = 0.0
currencyCode = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object InAppPurchasesUtils {
): MutableList<IAPFlowData> {
purchases.forEach { purchase ->
auditCourses.find { course ->
purchase.getCourseSku() == course.courseSku
purchase.getCourseSku() == course.productInfo.courseSku
}?.apply {
this.purchaseToken = purchase.purchaseToken
this.flowType = flowType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class InAppPurchasesViewModel @Inject constructor(

override fun onPurchaseComplete(purchase: Purchase) {
super.onPurchaseComplete(purchase)
if (purchase.getCourseSku() == iapFlowData.courseSku) {
if (purchase.getCourseSku() == iapFlowData.productInfo.courseSku) {
iapFlowData.purchaseToken = purchase.purchaseToken
_productPurchased.postEvent(iapFlowData)
iapAnalytics.trackIAPEvent(eventName = Analytics.Events.IAP_PAYMENT_TIME)
Expand Down Expand Up @@ -119,8 +119,7 @@ class InAppPurchasesViewModel @Inject constructor(
}

fun startPurchaseFlow(productInfo: ProductInfo, price: Double, currencyCode: String) {
iapFlowData.courseSku = productInfo.courseSku
iapFlowData.storeSku = productInfo.storeSku
iapFlowData.productInfo = productInfo
iapFlowData.price = price
iapFlowData.currencyCode = currencyCode
iapFlowData.flowType = IAPFlowData.IAPFlowType.USER_INITIATED
Expand All @@ -131,7 +130,7 @@ class InAppPurchasesViewModel @Inject constructor(
private fun addProductToBasket() {
startLoading()
repository.addToBasket(
productId = iapFlowData.courseSku,
productId = iapFlowData.productInfo.courseSku,
callback = object : NetworkResponseCallback<AddToBasketResponse> {
override fun onSuccess(result: Result.Success<AddToBasketResponse>) {
result.data?.let {
Expand Down Expand Up @@ -189,7 +188,7 @@ class InAppPurchasesViewModel @Inject constructor(
this.iapFlowData = iapData
repository.executeOrder(
basketId = iapData.basketId,
productId = iapData.courseSku,
productId = iapData.productInfo.courseSku,
purchaseToken = iapData.purchaseToken,
price = iapData.price,
currencyCode = iapData.currencyCode,
Expand Down Expand Up @@ -305,9 +304,9 @@ class InAppPurchasesViewModel @Inject constructor(

//Start the purchase flow
viewModelScope.launch {
val response = billingProcessor.querySyncDetails(iapFlowData.storeSku)
val response = billingProcessor.querySyncDetails(iapFlowData.productInfo.storeSku)
val productDetail = response.productDetailsList?.firstOrNull()
if (productDetail?.productId == iapFlowData.storeSku) {
if (productDetail?.productId == iapFlowData.productInfo.storeSku) {
productDetail.oneTimePurchaseOfferDetails?.let {
iapFlowData.currencyCode = it.priceCurrencyCode
iapFlowData.price = it.getPriceAmount()
Expand Down

0 comments on commit cc4deb8

Please sign in to comment.