Skip to content

Commit

Permalink
[MBL-1498] Fix updating payment method/fixing payment method for late…
Browse files Browse the repository at this point in the history
… pledge campaign (#2042)

* add new graph call to update payment method, update views to use correct values

* lint

* adjust implementation to not send amount, update observable to not be combined

* update factories and tests to have amounts for all scenarios

* add a couple new tests, remove unneeded graph mutation

* remove unneeded mock extension, add safety to add-ons display

---------

Co-authored-by: Isabel Martin <[email protected]>
  • Loading branch information
mtgriego and Arkariang authored May 30, 2024
1 parent 67307f6 commit cffa21a
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 14 deletions.
7 changes: 7 additions & 0 deletions app/src/main/graphql/fragments.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ fragment backing on Backing {
cancelable
pledgedOn
backerCompleted
isPostCampaign
project {
... project
}
Expand Down Expand Up @@ -290,6 +291,12 @@ fragment reward on Reward {
amount {
... amount
}
pledgeAmount {
... amount
}
latePledgeAmount {
... amount
}
convertedAmount{
... amount
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/kickstarter/mock/factories/RewardFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ object RewardFactory {
.description(description)
.estimatedDeliveryOn(ESTIMATED_DELIVERY)
.minimum(20.0)
.pledgeAmount(20.0)
.latePledgeAmount(30.0)
.shippingPreference("unrestricted")
.shippingType(Reward.SHIPPING_TYPE_NO_SHIPPING)
.title("Digital Bundle")
Expand Down Expand Up @@ -96,6 +98,8 @@ object RewardFactory {
return reward().toBuilder()
.id(rewardId)
.minimum(10.0)
.pledgeAmount(10.0)
.latePledgeAmount(20.0)
.isAddOn(true)
.addOnsItems(
listOf(
Expand Down Expand Up @@ -124,6 +128,8 @@ object RewardFactory {
fun maxReward(country: Country): Reward {
return reward().toBuilder()
.minimum(country.maxPledge.toDouble())
.pledgeAmount(country.maxPledge.toDouble())
.latePledgeAmount(country.maxPledge.toDouble() + 10.0)
.backersCount(0)
.build()
}
Expand All @@ -137,6 +143,8 @@ object RewardFactory {
.description("A digital download of the album and documentary.")
.limit(50)
.minimum(20.0)
.pledgeAmount(20.0)
.latePledgeAmount(30.0)
.remaining(0)
.title("Digital Bundle")
.build()
Expand Down Expand Up @@ -188,6 +196,8 @@ object RewardFactory {
.estimatedDeliveryOn(null)
.description("No reward")
.minimum(1.0)
.pledgeAmount(1.0)
.latePledgeAmount(1.0)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ open class MockApolloClientV2 : ApolloClientTypeV2 {
override fun updateBacking(updateBackingData: UpdateBackingData): io.reactivex.Observable<Checkout> {
return io.reactivex.Observable.empty()
}

override fun createBacking(createBackingData: CreateBackingData): io.reactivex.Observable<Checkout> {
return io.reactivex.Observable.empty()
}
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/com/kickstarter/models/Backing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Backing private constructor(
private val status: String,
private val addOns: List<Reward>?,
private val bonusAmount: Double,
private val isPostCampaign: Boolean
) : Parcelable, Relay {
fun amount() = this.amount
fun backer() = this.backer
Expand Down Expand Up @@ -61,6 +62,7 @@ class Backing private constructor(
fun status() = this.status
fun addOns() = this.addOns
fun bonusAmount() = this.bonusAmount
fun isPostCampaign() = this.isPostCampaign

@Parcelize
data class Builder(
Expand Down Expand Up @@ -90,6 +92,7 @@ class Backing private constructor(
private var status: String = "",
private var addOns: List<Reward>? = null,
private var bonusAmount: Double = 0.0,
private var isPostCampaign: Boolean = false
) : Parcelable {
fun amount(amount: Double?) = apply { this.amount = amount ?: 0.0 }
fun backer(backer: User?) = apply { this.backer = backer }
Expand All @@ -116,6 +119,7 @@ class Backing private constructor(
fun status(status: String?) = apply { this.status = status ?: "" }
fun addOns(addOns: List<Reward>?) = apply { this.addOns = addOns ?: emptyList() }
fun bonusAmount(bonusAmount: Double?) = apply { this.bonusAmount = bonusAmount ?: 0.0 }
fun isPostCampaign(isPostCampaign: Boolean) = apply { this.isPostCampaign = isPostCampaign }
fun build() = Backing(
amount = amount,
backer = backer,
Expand All @@ -141,7 +145,8 @@ class Backing private constructor(
shippingAmount = shippingAmount,
status = status,
addOns = addOns,
bonusAmount = bonusAmount
bonusAmount = bonusAmount,
isPostCampaign = isPostCampaign
)
}

Expand Down Expand Up @@ -170,7 +175,8 @@ class Backing private constructor(
shippingAmount = shippingAmount,
status = status,
addOns = addOns,
bonusAmount = bonusAmount
bonusAmount = bonusAmount,
isPostCampaign = isPostCampaign
)

override fun equals(other: Any?): Boolean {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/kickstarter/models/Reward.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Reward private constructor(
private val id: Long,
private val limit: Int?,
private val minimum: Double,
private val pledgeAmount: Double,
private val latePledgeAmount: Double,
private val estimatedDeliveryOn: DateTime?,
private val remaining: Int?,
private val rewardsItems: List<RewardsItem>?,
Expand Down Expand Up @@ -57,6 +59,8 @@ class Reward private constructor(
override fun id() = this.id
fun limit() = this.limit
fun minimum() = this.minimum
fun pledgeAmount() = this.pledgeAmount
fun latePledgeAmount() = this.latePledgeAmount
fun estimatedDeliveryOn() = this.estimatedDeliveryOn
fun remaining() = this.remaining
fun rewardsItems() = this.rewardsItems
Expand Down Expand Up @@ -85,6 +89,8 @@ class Reward private constructor(
private var id: Long = 0L,
private var limit: Int? = null,
private var minimum: Double = 0.0,
private var pledgeAmount: Double = 0.0,
private var latePledgeAmount: Double = 0.0,
private var estimatedDeliveryOn: DateTime? = null,
private var remaining: Int? = null,
private var rewardsItems: List<RewardsItem>? = emptyList(),
Expand All @@ -110,6 +116,8 @@ class Reward private constructor(
fun id(id: Long?) = apply { this.id = id ?: -1L }
fun limit(limit: Int?) = apply { this.limit = limit }
fun minimum(minimum: Double?) = apply { this.minimum = minimum ?: 0.0 }
fun pledgeAmount(pledgeAmount: Double?) = apply { this.pledgeAmount = pledgeAmount ?: 0.0 }
fun latePledgeAmount(latePledgeAmount: Double?) = apply { this.latePledgeAmount = latePledgeAmount ?: 0.0 }
fun estimatedDeliveryOn(estimatedDeliveryOn: DateTime?) = apply {
this.estimatedDeliveryOn = estimatedDeliveryOn
}
Expand All @@ -136,6 +144,8 @@ class Reward private constructor(
id = id,
limit = limit,
minimum = minimum,
pledgeAmount = pledgeAmount,
latePledgeAmount = latePledgeAmount,
estimatedDeliveryOn = estimatedDeliveryOn,
remaining = remaining,
rewardsItems = rewardsItems,
Expand Down Expand Up @@ -173,6 +183,8 @@ class Reward private constructor(
id = id,
limit = limit,
minimum = minimum,
pledgeAmount = pledgeAmount,
latePledgeAmount = latePledgeAmount,
estimatedDeliveryOn = estimatedDeliveryOn,
remaining = remaining,
rewardsItems = rewardsItems,
Expand Down Expand Up @@ -201,6 +213,8 @@ class Reward private constructor(
id() == other.id() &&
limit() == other.limit() &&
minimum() == other.minimum() &&
pledgeAmount() == other.pledgeAmount() &&
latePledgeAmount() == other.latePledgeAmount() &&
estimatedDeliveryOn() == other.estimatedDeliveryOn() &&
remaining() == other.remaining() &&
rewardsItems() == other.rewardsItems() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ class KSApolloClientV2(val service: ApolloClient, val gson: Gson) : ApolloClient
return Observable.defer {
val updateBackingMutation = UpdateBackingMutation.builder()
.backingId(encodeRelayId(updateBackingData.backing))
.amount(updateBackingData.amount.toString())
.amount(updateBackingData.amount)
.locationId(updateBackingData.locationId)
.rewardIds(updateBackingData.rewardsIds?.let { list -> list.map { encodeRelayId(it) } })
.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ fun rewardTransformer(
addOnItems: List<RewardsItem> = emptyList()
): Reward {
val amount = rewardGr.amount().fragments().amount().amount()?.toDouble() ?: 0.0
val latePledgeAmount = rewardGr.latePledgeAmount().fragments().amount().amount()?.toDouble() ?: 0.0
val pledgeAmount = rewardGr.pledgeAmount().fragments().amount().amount()?.toDouble() ?: 0.0
val convertedAmount =
rewardGr.convertedAmount().fragments().amount().amount()?.toDouble() ?: 0.0
val desc = rewardGr.description()
Expand Down Expand Up @@ -156,6 +158,8 @@ fun rewardTransformer(
.title(title)
.convertedMinimum(convertedAmount)
.minimum(amount)
.pledgeAmount(pledgeAmount)
.latePledgeAmount(latePledgeAmount)
.limit(limit)
.remaining(remaining)
.endsAt(endsAt)
Expand Down Expand Up @@ -712,6 +716,8 @@ fun backingTransformer(backingGr: fragment.Backing?): Backing {
.build()
val status = backingGr?.status()?.rawValue() ?: ""

val isPostCampaign = backingGr?.isPostCampaign ?: false

return Backing.builder()
.amount(backingGr?.amount()?.fragments()?.amount()?.amount()?.toDouble() ?: 0.0)
.bonusAmount(backingGr?.bonusAmount()?.fragments()?.amount()?.amount()?.toDouble() ?: 0.0)
Expand All @@ -733,6 +739,7 @@ fun backingTransformer(backingGr: fragment.Backing?): Backing {
.status(status)
.cancelable(backingGr?.cancelable() ?: false)
.completedByBacker(completedByBacker)
.isPostCampaign(isPostCampaign)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,20 @@ interface AddOnViewHolderViewModel {
this.ksCurrency.format(it.second.convertedMinimum(), it.first, true, RoundingMode.HALF_UP, true)

private fun buildCurrency(project: Project, reward: Reward): String {
val completeCurrency = ksCurrency.format(reward.minimum(), project, RoundingMode.HALF_UP)
val completeCurrency = if (project.backing()?.isPostCampaign() == true) {
if (reward.latePledgeAmount() > 0) {
ksCurrency.format(reward.latePledgeAmount(), project, RoundingMode.HALF_UP)
} else {
ksCurrency.format(reward.minimum(), project, RoundingMode.HALF_UP)
}
} else {
if (reward.pledgeAmount() > 0) {
ksCurrency.format(reward.pledgeAmount(), project, RoundingMode.HALF_UP)
} else {
ksCurrency.format(reward.minimum(), project, RoundingMode.HALF_UP)
}
}

val country = Country.findByCurrencyCode(project.currency()) ?: ""

return completeCurrency.removePrefix(country.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ interface PledgeFragmentViewModel {

val pledgeAmountHeader = this.rewardAndAddOns
.filter { !RewardUtils.isNoReward(it.first()) }
.map { getPledgeAmount(it) }
.map { getPledgeAmount(it, backing.blockingLast(Backing.builder().build()).isPostCampaign()) }

pledgeAmountHeader
.compose<Pair<Double, Project>>(combineLatestPair(project))
Expand Down Expand Up @@ -1363,13 +1363,18 @@ interface PledgeFragmentViewModel {
totalString,
locationId,
extendedListForCheckOut,
paymentMethod
) { b, a, l, r, pMethod ->
this.getUpdateBackingData(b, a, l, r, pMethod)
paymentMethod,
project
) { b, a, l, r, pMethod, pro ->
if (pro.isBacking() && pro.backing()?.amount().toString() == a) {
Pair(this.getUpdateBackingData(b, null, l, r, pMethod), pro)
} else {
Pair(this.getUpdateBackingData(b, a, l, r, pMethod), pro)
}
}
.compose<UpdateBackingData>(takeWhenV2(Observable.merge(updatePledgeClick, updatePaymentClick, fixPaymentClick)))
.compose<Pair<UpdateBackingData, Project>>(takeWhenV2(Observable.merge(updatePledgeClick, updatePaymentClick, fixPaymentClick)))
.switchMap {
this.apolloClient.updateBacking(it)
this.apolloClient.updateBacking(it.first)
.doOnSubscribe {
this.pledgeProgressIsGone.onNext(false)
this.pledgeButtonIsEnabled.onNext(false)
Expand Down Expand Up @@ -1690,11 +1695,29 @@ interface PledgeFragmentViewModel {
/**
* Calculate the pledge amount for the selected reward + addOns
*/
private fun getPledgeAmount(rewards: List<Reward>): Double {
private fun getPledgeAmount(rewards: List<Reward>, isLatePledge: Boolean): Double {
var totalPledgeAmount = 0.0
rewards.forEach {
totalPledgeAmount += if (RewardUtils.isNoReward(it) && !it.isAddOn()) it.minimum() // - Cost of the selected Reward
else it.quantity()?.let { q -> (q * it.minimum()) } ?: it.minimum() // - Cost of each addOn
totalPledgeAmount += if (isLatePledge) {
if (it.latePledgeAmount() > 0) {
if (RewardUtils.isNoReward(it) && !it.isAddOn()) it.latePledgeAmount() // - Cost of the selected Reward
else it.quantity()?.let { q -> (q * it.latePledgeAmount()) } ?: it.latePledgeAmount() // - Cost of each addOn
} else {
// We don't have a late pledge amount to work with, use the default minimum
if (RewardUtils.isNoReward(it) && !it.isAddOn()) it.minimum() // - Default cost of the selected Reward
else it.quantity()?.let { q -> (q * it.minimum()) } ?: it.minimum() // - Default cost of each addOn
}
} else {
// We have a pledge amount to work with, use it
if (it.pledgeAmount() > 0.0) {
if (RewardUtils.isNoReward(it) && !it.isAddOn()) it.pledgeAmount() // - Cost of the selected Reward during the campaign
else it.quantity()?.let { q -> (q * it.pledgeAmount()) } ?: it.pledgeAmount() // - Cost of each addOn during the campaign
} else {
// We don't have a pledge amount to work with, use the default minimum
if (RewardUtils.isNoReward(it) && !it.isAddOn()) it.minimum() // - Default cost of the selected Reward
else it.quantity()?.let { q -> (q * it.minimum()) } ?: it.minimum() // - Default cost of each addOn
}
}
}
return totalPledgeAmount
}
Expand Down Expand Up @@ -2074,7 +2097,7 @@ fun PledgeFragmentViewModel.PledgeFragmentViewModel.getUpdateBackingData(
backing: Backing,
amount: String? = null,
locationId: String? = null,
rewardsList: List<Reward>,
rewardsList: List<Reward> = listOf(),
pMethod: StoredCard? = null
): UpdateBackingData {
return pMethod?.let { card ->
Expand Down
Loading

0 comments on commit cffa21a

Please sign in to comment.