Skip to content

Commit

Permalink
fix: Address PR comments by hamza
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan-arshad-dev committed Jul 8, 2024
1 parent a985f18 commit f2cc82e
Show file tree
Hide file tree
Showing 25 changed files with 531 additions and 243 deletions.
8 changes: 3 additions & 5 deletions app/src/main/java/org/openedx/app/di/ScreenModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.qualifier.named
import org.koin.dsl.module
import org.openedx.app.AppViewModel
import org.openedx.app.BuildConfig
import org.openedx.app.MainViewModel
import org.openedx.auth.data.repository.AuthRepository
import org.openedx.auth.domain.interactor.AuthInteractor
Expand Down Expand Up @@ -140,7 +139,7 @@ val screenModule = module {
factory { DashboardInteractor(get()) }
viewModel {
DashboardListViewModel(
versionName = BuildConfig.VERSION_NAME,
get(),
get(),
get(),
get(),
Expand Down Expand Up @@ -202,7 +201,6 @@ val screenModule = module {
viewModel { (username: String) -> AnothersProfileViewModel(get(), get(), username) }
viewModel {
SettingsViewModel(
versionName = BuildConfig.VERSION_NAME,
get(),
get(),
get(),
Expand Down Expand Up @@ -254,7 +252,7 @@ val screenModule = module {
courseTitle,
resumeBlockId,
enrollmentMode,
versionName = BuildConfig.VERSION_NAME,
get(),
get(),
get(),
get(),
Expand Down Expand Up @@ -447,7 +445,7 @@ val screenModule = module {
IAPViewModel(
iapFlow = iapFlow,
purchaseFlowData = purchaseFlowData,
versionName = BuildConfig.VERSION_NAME,
get(),
get(),
get(),
get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class IAPRepository(private val api: InAppPurchasesApi) {
return mapToDomain()
}
}
throw IAPException(IAPRequestType.ADD_TO_BASKET_CODE, response.code(), response.getMessage())
throw IAPException(
requestType = IAPRequestType.ADD_TO_BASKET_CODE,
httpErrorCode = response.code(),
errorMessage = response.getMessage()
)
}

suspend fun proceedCheckout(basketId: Long): CheckoutResponse {
Expand All @@ -31,7 +35,11 @@ class IAPRepository(private val api: InAppPurchasesApi) {
return mapToDomain()
}
}
throw IAPException(IAPRequestType.CHECKOUT_CODE, response.code(), response.getMessage())
throw IAPException(
requestType = IAPRequestType.CHECKOUT_CODE,
httpErrorCode = response.code(),
errorMessage = response.getMessage()
)
}

suspend fun executeOrder(
Expand All @@ -53,6 +61,10 @@ class IAPRepository(private val api: InAppPurchasesApi) {
return mapToDomain()
}
}
throw IAPException(IAPRequestType.EXECUTE_ORDER_CODE, response.code(), response.getMessage())
throw IAPException(
requestType = IAPRequestType.EXECUTE_ORDER_CODE,
httpErrorCode = response.code(),
errorMessage = response.getMessage()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.openedx.core.domain.interactor

import android.text.TextUtils
import androidx.fragment.app.FragmentActivity
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.BillingClient.BillingResponseCode
import com.android.billingclient.api.ProductDetails
import com.android.billingclient.api.Purchase
import org.openedx.core.ApiConstants
Expand All @@ -20,12 +20,20 @@ class IAPInteractor(
private val repository: IAPRepository,
) {
suspend fun loadPrice(productId: String): ProductDetails.OneTimePurchaseOfferDetails {
val response =
billingProcessor.querySyncDetails(productId)
val productDetail = response.productDetailsList?.firstOrNull()
val response = billingProcessor.querySyncDetails(productId)
val productDetails = response.productDetailsList?.firstOrNull()?.oneTimePurchaseOfferDetails
val billingResult = response.billingResult
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && productDetail?.oneTimePurchaseOfferDetails != null) {
return productDetail.oneTimePurchaseOfferDetails!!

if (billingResult.responseCode == BillingResponseCode.OK) {
if (productDetails != null) {
return productDetails
} else {
throw IAPException(
requestType = IAPRequestType.NO_SKU_CODE,
httpErrorCode = billingResult.responseCode,
errorMessage = billingResult.debugMessage
)
}
} else {
throw IAPException(
requestType = IAPRequestType.PRICE_CODE,
Expand Down Expand Up @@ -71,8 +79,12 @@ class IAPInteractor(

suspend fun consumePurchase(purchaseToken: String) {
val result = billingProcessor.consumePurchase(purchaseToken)
if (result.responseCode != BillingClient.BillingResponseCode.OK) {
throw IAPException(IAPRequestType.CONSUME_CODE, result.responseCode, result.debugMessage)
if (result.responseCode != BillingResponseCode.OK) {
throw IAPException(
requestType = IAPRequestType.CONSUME_CODE,
httpErrorCode = result.responseCode,
errorMessage = result.debugMessage
)
}
}

Expand All @@ -98,7 +110,8 @@ class IAPInteractor(
productDetail?.oneTimePurchaseOfferDetails?.takeIf {
TextUtils.isEmpty(purchase.getCourseSku()).not()
}?.let { oneTimeProductDetails ->
val basketId = addToBasket(purchase.getCourseSku()!!)
val courseSku = purchase.getCourseSku() ?: return@let
val basketId = addToBasket(courseSku)
processCheckout(basketId)
executeOrder(
basketId = basketId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data class CourseStructure(
) {
private val isStarted: Boolean
get() = TimeUtils.isDatePassed(Date(), start)

val isUpgradeable: Boolean
get() = enrollmentDetails.isAuditMode &&
isStarted &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@ data class EnrolledCourse(

private val isAuditMode: Boolean
get() = EnrollmentMode.AUDIT.toString().equals(mode, ignoreCase = true)

val isUpgradeable: Boolean
get() = isAuditMode &&
course.isStarted &&
course.isUpgradeDeadlinePassed.not() &&
productInfo != null
}

/**
* Method to filter the audit courses from the given enrolled course list.
*
* @return the list of all audit courses with non-null Skus.
*/
fun List<EnrolledCourse>.getAuditCourses(): List<EnrolledCourse> {
return this.filter { it.isUpgradeable }.toList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package org.openedx.core.domain.model
* Course Enrollment modes
*/
enum class EnrollmentMode(private val mode: String) {
AUDIT("audit"), VERIFIED("verified"), HONOR("honor"),
NO_ID_PROFESSIONAL("no-id-professional"), PROFESSIONAL("professional"),
CREDIT("credit"), MASTERS("masters"), NONE("none");
AUDIT("audit"),
VERIFIED("verified"),
NONE("none");

override fun toString(): String {
return mode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
package org.openedx.core.domain.model.iap

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import org.openedx.core.domain.ProductInfo

@Parcelize
data class PurchaseFlowData(
val screenName: String? = null,
val courseId: String? = null,
val courseName: String? = null,
val isSelfPaced: Boolean? = null,
val componentId: String? = null,
val productInfo: ProductInfo? = null,
) {
var screenName: String? = null,
var courseId: String? = null,
var courseName: String? = null,
var isSelfPaced: Boolean? = null,
var componentId: String? = null,
var productInfo: ProductInfo? = null,
) : Parcelable {

var currencyCode: String = ""
var price: Double = 0.0
var formattedPrice: String? = null
var purchaseToken: String? = null
var basketId: Long = -1

var flowStartTime: Long = 0

fun reset() {
screenName = null
courseId = null
courseName = null
isSelfPaced = null
componentId = null
productInfo = null
currencyCode = ""
price = 0.0
formattedPrice = null
purchaseToken = null
basketId = -1
flowStartTime = 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.util.Locale
* */
class IAPException(
val requestType: IAPRequestType = IAPRequestType.UNKNOWN,
val httpErrorCode: Int = -1,
val httpErrorCode: Int = DEFAULT_HTTP_ERROR_CODE,
val errorMessage: String
) : Exception(errorMessage) {

Expand All @@ -36,13 +36,17 @@ class IAPException(
}
body.append(String.format("%s", requestType.request))
// change the default value to -1 cuz in case of BillingClient return errorCode 0 for price load.
if (httpErrorCode == -1) {
if (httpErrorCode == DEFAULT_HTTP_ERROR_CODE) {
return body.toString()
}
body.append(String.format(Locale.ENGLISH, "-%d", httpErrorCode))
if (!TextUtils.isEmpty(errorMessage)) body.append(String.format("-%s", errorMessage))
return body.toString()
}

companion object {
private const val DEFAULT_HTTP_ERROR_CODE = -1
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,6 @@ class BillingProcessor(
return result.billingResult
}

fun release() {
if (billingClient.isReady) {
billingClient.endConnection()
}
}

/**
* Method to query the Purchases async and returns purchases for currently owned items
* bought within the app.
Expand Down
15 changes: 12 additions & 3 deletions core/src/main/java/org/openedx/core/presentation/IAPAnalytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ enum class IAPAnalyticsEvent(val eventName: String, val biValue: String) {
"Payments: Course Upgrade Success",
"edx.bi.app.payments.course_upgrade_success"
),
IAP_PAYMENT_ERROR("Payments: Payment Error", "edx.bi.app.payments.payment_error"),
IAP_PAYMENT_CANCELED("Payments: Canceled by User", "edx.bi.app.payments.canceled_by_user"),
IAP_PAYMENT_ERROR(
"Payments: Payment Error",
"edx.bi.app.payments.payment_error"
),
IAP_PAYMENT_CANCELED(
"Payments: Canceled by User",
"edx.bi.app.payments.canceled_by_user"
),
IAP_COURSE_UPGRADE_ERROR(
"Payments: Course Upgrade Error",
"edx.bi.app.payments.course_upgrade_error"
),
IAP_PRICE_LOAD_ERROR("Payments: Price Load Error", "edx.bi.app.payments.price_load_error"),
IAP_PRICE_LOAD_ERROR(
"Payments: Price Load Error",
"edx.bi.app.payments.price_load_error"
),
IAP_ERROR_ALERT_ACTION(
"Payments: Error Alert Action",
"edx.bi.app.payments.error_alert_action"
Expand Down
Loading

0 comments on commit f2cc82e

Please sign in to comment.