Skip to content

Commit

Permalink
Merge pull request #10732 from woocommerce/issue/10721-blaze-payment-…
Browse files Browse the repository at this point in the history
…summary

[Blaze] Payment summary screen
  • Loading branch information
hichamboushaba authored Feb 9, 2024
2 parents e31d0dc + 37d215f commit 90c9600
Show file tree
Hide file tree
Showing 21 changed files with 664 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ object AppUrls {
const val STORE_ONBOARDING_WCPAY_SETUP_GUIDE = "https://woo.com/document/woopayments/startup-guide/"
const val STORE_ONBOARDING_PAYMENTS_SETUP_GUIDE =
"https://woo.com/documentation/woocommerce/getting-started/sell-products/core-payment-options/"
const val ADVERTISING_POLICY = "https://automattic.com/advertising-policy/"
const val BLAZE_SUPPORT = "https://wordpress.com/support/promote-a-post/"

fun getScreenshotUrl(themeDemoUrl: String) =
"https://s0.wp.com/mshots/v1/$themeDemoUrl?demo=true/?w=1200&h=2400&vpw=400&vph=800"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.woocommerce.android.model

import androidx.annotation.DrawableRes
import com.woocommerce.android.R

enum class CreditCardType {
VISA,
MASTERCARD,
UNIONPAY,
JCB,
DISCOVER,
DINERS,
AMEX,
UNKNOWN;

val icon: Int
@DrawableRes
get() = when (this) {
VISA -> R.drawable.credit_card_visa
MASTERCARD -> R.drawable.credit_card_mastercard
UNIONPAY -> R.drawable.credit_card_unionpay
JCB -> R.drawable.credit_card_jcb
DISCOVER -> R.drawable.credit_card_discover
DINERS -> R.drawable.credit_card_diners
AMEX -> R.drawable.credit_card_amex
UNKNOWN -> R.drawable.credit_card_placeholder
}

companion object {
fun fromString(string: String): CreditCardType {
return when (string.lowercase()) {
"visa" -> VISA
"mastercard" -> MASTERCARD
"unionpay" -> UNIONPAY
"jcb" -> JCB
"discover" -> DISCOVER
"diners" -> DINERS
"amex" -> AMEX
else -> UNKNOWN
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.woocommerce.android.ui.blaze

import android.os.Parcelable
import com.woocommerce.android.OnChangedException
import com.woocommerce.android.model.CreditCardType
import com.woocommerce.android.tools.SelectedSite
import com.woocommerce.android.ui.products.ProductDetailRepository
import com.woocommerce.android.util.TimezoneProvider
import com.woocommerce.android.util.WooLog
import kotlinx.coroutines.flow.map
import kotlinx.parcelize.Parcelize
import org.wordpress.android.fluxc.model.blaze.BlazeAdSuggestion
import org.wordpress.android.fluxc.model.blaze.BlazePaymentMethod.PaymentMethodInfo
import org.wordpress.android.fluxc.store.blaze.BlazeCampaignsStore
import java.util.Date
import javax.inject.Inject
Expand Down Expand Up @@ -39,6 +41,7 @@ class BlazeRepository @Inject constructor(
WooLog.w(WooLog.T.BLAZE, "Failed to fetch languages: ${result.error}")
Result.failure(OnChangedException(result.error))
}

else -> Result.success(Unit)
}
}
Expand All @@ -54,6 +57,7 @@ class BlazeRepository @Inject constructor(
WooLog.w(WooLog.T.BLAZE, "Failed to fetch devices: ${result.error}")
Result.failure(OnChangedException(result.error))
}

else -> Result.success(Unit)
}
}
Expand All @@ -69,6 +73,7 @@ class BlazeRepository @Inject constructor(
WooLog.w(WooLog.T.BLAZE, "Failed to fetch interests: ${result.error}")
Result.failure(OnChangedException(result.error))
}

else -> Result.success(Unit)
}
}
Expand All @@ -84,6 +89,7 @@ class BlazeRepository @Inject constructor(
WooLog.w(WooLog.T.BLAZE, "Failed to fetch locations: ${result.error}")
Result.failure(OnChangedException(result.error))
}

else -> Result.success(
result.model?.map { location ->
Location(location.id, location.name, location.parent?.name, location.type)
Expand All @@ -106,6 +112,7 @@ class BlazeRepository @Inject constructor(
WooLog.w(WooLog.T.BLAZE, "Failed to fetch ad suggestions: ${result.error}")
Result.failure(OnChangedException(result.error))
}

else -> Result.success(result.model?.mapToUiModel() ?: emptyList())
}
}
Expand All @@ -123,6 +130,48 @@ class BlazeRepository @Inject constructor(
)
}

suspend fun fetchPaymentMethods(): Result<PaymentMethodsData> {
val result = blazeCampaignsStore.fetchBlazePaymentMethods(selectedSite.get())

return when {
result.isError -> {
WooLog.w(WooLog.T.BLAZE, "Failed to fetch payment methods: ${result.error}")
Result.failure(OnChangedException(result.error))
}

else -> result.model?.let { paymentMethods ->
Result.success(
PaymentMethodsData(
savedPaymentMethods = paymentMethods.savedPaymentMethods.map { paymentMethod ->
PaymentMethod(
id = paymentMethod.id,
name = paymentMethod.name,
info = when (paymentMethod.info) {
is PaymentMethodInfo.CreditCardInfo ->
(paymentMethod.info as PaymentMethodInfo.CreditCardInfo).let {
PaymentMethod.PaymentMethodInfo.CreditCard(
creditCardType = CreditCardType.fromString(it.type),
cardHolderName = it.cardHolderName
)
}

PaymentMethodInfo.Unknown -> {
PaymentMethod.PaymentMethodInfo.Unknown
}
}
)
},
addPaymentMethodUrls = PaymentMethodUrls(
formUrl = paymentMethods.addPaymentMethodUrls.formUrl,
successUrl = paymentMethods.addPaymentMethodUrls.successUrl,
idUrlParameter = paymentMethods.addPaymentMethodUrls.idUrlParameter
)
)
)
} ?: Result.failure(NullPointerException("API response is null"))
}
}

@Parcelize
data class CampaignPreview(
val productId: Long,
Expand All @@ -146,6 +195,37 @@ class BlazeRepository @Inject constructor(
val durationInDays: Int,
val startDate: Date,
) : Parcelable

@Parcelize
data class PaymentMethodsData(
val savedPaymentMethods: List<PaymentMethod>,
val addPaymentMethodUrls: PaymentMethodUrls
) : Parcelable

@Parcelize
data class PaymentMethod(
val id: String,
val name: String,
val info: PaymentMethodInfo
) : Parcelable {
sealed interface PaymentMethodInfo : Parcelable {
@Parcelize
data class CreditCard(
val creditCardType: CreditCardType,
val cardHolderName: String
) : PaymentMethodInfo

@Parcelize
data object Unknown : PaymentMethodInfo
}
}

@Parcelize
data class PaymentMethodUrls(
val formUrl: String,
val successUrl: String,
val idUrlParameter: String
) : Parcelable
}

@Parcelize
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.woocommerce.android.ui.blaze.creation.payment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.woocommerce.android.ui.base.BaseFragment
import com.woocommerce.android.ui.compose.composeView
import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground
import com.woocommerce.android.ui.main.AppBarStatus
import com.woocommerce.android.viewmodel.MultiLiveEvent
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class BlazeCampaignPaymentSummaryFragment : BaseFragment() {
override val activityAppBarStatus: AppBarStatus
get() = AppBarStatus.Hidden

private val viewModel: BlazeCampaignPaymentSummaryViewModel by viewModels()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return composeView {
WooThemeWithBackground {
BlazeCampaignPaymentSummaryScreen(viewModel)
}
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
handleEvents()
}

private fun handleEvents() {
viewModel.event.observe(viewLifecycleOwner) { event ->
when (event) {
MultiLiveEvent.Event.Exit -> findNavController().navigateUp()
}
}
}
}
Loading

0 comments on commit 90c9600

Please sign in to comment.