Skip to content
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

[NT-438] Create backing with free shipping #901

Merged
merged 11 commits into from
Oct 21, 2019
33 changes: 33 additions & 0 deletions Kickstarter-iOS/Library/SharedFunctionsTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@testable import Kickstarter_Framework
@testable import KsApi
@testable import Library
import Prelude
import XCTest

internal final class SharedFunctionsTests: XCTestCase {
Expand Down Expand Up @@ -55,4 +57,35 @@ internal final class SharedFunctionsTests: XCTestCase {
XCTAssertTrue(mockPushNotificationDialog.resetAllContextsWasCalled)
XCTAssertTrue(mockViewController.dismissAnimatedWasCalled)
}

func testFormattedPledgeParameters_WithShipping() {
let reward = Reward.template
let selectedShippingRule = ShippingRule.template
|> ShippingRule.lens.cost .~ 3
|> ShippingRule.lens.location .~ (Location.template |> Location.lens.id .~ 123)

let params = formattedPledgeParameters(
from: reward,
pledgeAmount: 10,
selectedShippingRule: selectedShippingRule
)

XCTAssertEqual(params.rewardId, "UmV3YXJkLTE=")
XCTAssertEqual(params.pledgeTotal, "13.00")
XCTAssertEqual(params.locationId, "123")
}

func testFormattedPledgeParameters_NoShipping_NoReward() {
let reward = Reward.noReward

let params = formattedPledgeParameters(
from: reward,
pledgeAmount: 10,
selectedShippingRule: nil
)

XCTAssertNil(params.rewardId)
XCTAssertEqual(params.pledgeTotal, "10.00")
XCTAssertNil(params.locationId)
}
}
25 changes: 9 additions & 16 deletions Library/CreateApplePayBackingInput+Constructor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,21 @@ extension CreateApplePayBackingInput {
stripeToken: String,
refTag: RefTag?
) -> CreateApplePayBackingInput {
let pledgeAmountDecimal = Decimal(pledgeAmount)
var shippingAmountDecimal: Decimal = Decimal()
var shippingLocationId: String?

if let shippingRule = selectedShippingRule, shippingRule.cost > 0 {
shippingAmountDecimal = Decimal(shippingRule.cost)
shippingLocationId = String(shippingRule.location.id)
}

let pledgeTotal = NSDecimalNumber(decimal: pledgeAmountDecimal + shippingAmountDecimal)
let formattedPledgeTotal = Format.decimalCurrency(for: pledgeTotal.doubleValue)

let rewardId = reward == Reward.noReward ? nil : reward.graphID
let pledgeParams
= formattedPledgeParameters(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to the same line as let pledgeParams

from: reward,
pledgeAmount: pledgeAmount,
selectedShippingRule: selectedShippingRule
)

return CreateApplePayBackingInput(
amount: formattedPledgeTotal,
locationId: shippingLocationId,
amount: pledgeParams.pledgeTotal,
locationId: pledgeParams.locationId,
paymentInstrumentName: pkPaymentData.displayName,
paymentNetwork: pkPaymentData.network,
projectId: project.graphID,
refParam: refTag?.description,
rewardId: rewardId,
rewardId: pledgeParams.rewardId,
stripeToken: stripeToken,
transactionIdentifier: pkPaymentData.transactionIdentifier
)
Expand Down
4 changes: 2 additions & 2 deletions Library/CreateApplePayBackingInputConstructorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Prelude
import XCTest

final class CreateApplePayBackingInputConstructorTests: XCTestCase {
func testCreateApplePayBackingInput_NoShipping() {
func testCreateApplePayBackingInput_NoShipping_NoReward() {
let project = Project.template
let reward = Reward.noReward

Expand Down Expand Up @@ -34,7 +34,7 @@ final class CreateApplePayBackingInputConstructorTests: XCTestCase {
XCTAssertEqual(input.refParam, "project_page")
}

func testCreateApplePayBackingInput_WithShipping() {
func testCreateApplePayBackingInput_WithShipping_WithReward() {
let project = Project.template
let reward = Reward.template
let shippingRule = ShippingRule.template
Expand Down
25 changes: 9 additions & 16 deletions Library/CreateBackingInput+Constructor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,19 @@ extension CreateBackingInput {
refTag: RefTag?,
paymentSourceId: String
) -> CreateBackingInput {
let pledgeAmountDecimal = Decimal(pledgeAmount)
var shippingAmountDecimal: Decimal = Decimal()
var shippingLocationId: String?

if let shippingRule = selectedShippingRule, shippingRule.cost > 0 {
shippingAmountDecimal = Decimal(shippingRule.cost)
shippingLocationId = String(shippingRule.location.id)
}

let pledgeTotal = NSDecimalNumber(decimal: pledgeAmountDecimal + shippingAmountDecimal)
let formattedPledgeTotal = Format.decimalCurrency(for: pledgeTotal.doubleValue)

let rewardId = reward == Reward.noReward ? nil : reward.graphID
let pledgeParams
= formattedPledgeParameters(
from: reward,
pledgeAmount: pledgeAmount,
selectedShippingRule: selectedShippingRule
)

return CreateBackingInput(
amount: formattedPledgeTotal,
locationId: shippingLocationId,
amount: pledgeParams.pledgeTotal,
locationId: pledgeParams.locationId,
paymentSourceId: paymentSourceId,
projectId: project.graphID,
rewardId: rewardId,
rewardId: pledgeParams.rewardId,
refParam: refTag?.description
)
}
Expand Down
2 changes: 1 addition & 1 deletion Library/PKPaymentRequest+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension PKPaymentRequest {
)
)

if let selectedShippingRule = selectedShippingRule, selectedShippingRule.cost != 0.0 {
if let selectedShippingRule = selectedShippingRule {
paymentSummaryItems.append(
PKPaymentSummaryItem(
label: Strings.Shipping(),
Expand Down
24 changes: 24 additions & 0 deletions Library/SharedFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,27 @@ internal func classNameWithoutModule(_ class: AnyClass) -> String {
.dropFirst()
.joined(separator: ".")
}

typealias FormattedPledgeParameters = (pledgeTotal: String, rewardId: String?, locationId: String?)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I read FormattedPledgeParameters I think about this going through a formatter, I'm wondering if SanitizedPledgeParameters or SanitizedPledgeParams describes what we're doing more accurately?


internal func formattedPledgeParameters(
from reward: Reward,
pledgeAmount: Double,
selectedShippingRule: ShippingRule?
) -> FormattedPledgeParameters {
let pledgeAmountDecimal = Decimal(pledgeAmount)
var shippingAmountDecimal: Decimal = Decimal()
var shippingLocationId: String?

if let shippingRule = selectedShippingRule {
shippingAmountDecimal = Decimal(shippingRule.cost)
shippingLocationId = String(shippingRule.location.id)
}

let pledgeTotal = NSDecimalNumber(decimal: pledgeAmountDecimal + shippingAmountDecimal)
let formattedPledgeTotal = Format.decimalCurrency(for: pledgeTotal.doubleValue)

let rewardId = reward == Reward.noReward ? nil : reward.graphID

return (formattedPledgeTotal, rewardId, shippingLocationId)
}
29 changes: 8 additions & 21 deletions Library/UpdateBackingInput+Constructor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ extension UpdateBackingInput {
from updateBackingData: UpdateBackingData,
isApplePay: Bool
) -> UpdateBackingInput {
let pledgeAmount: String? = updateBackingData.pledgeAmount.flatMap { pledgeAmount in
pledgeAmountString(withAmount: pledgeAmount, shippingRule: updateBackingData.shippingRule)
}
let backingId: String = updateBackingData.backing.graphID
let locationId: String? = updateBackingData.shippingRule.flatMap { "\($0.location.id)" }
let rewardId: String? = updateBackingData.reward.graphID
let backingId = updateBackingData.backing.graphID
let (pledgeTotal, rewardId, locationId) =
formattedPledgeParameters(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here?

from: updateBackingData.reward,
pledgeAmount: updateBackingData.pledgeAmount,
selectedShippingRule: updateBackingData.shippingRule
)

return UpdateBackingInput(
amount: pledgeAmount,
amount: pledgeTotal,
applePay: isApplePay ? updateBackingData.applePayParams : nil,
id: backingId,
locationId: locationId,
Expand All @@ -23,17 +24,3 @@ extension UpdateBackingInput {
)
}
}

private func pledgeAmountString(withAmount amount: Double, shippingRule: ShippingRule?) -> String {
let pledgeAmountDecimal = Decimal(amount)
var shippingAmountDecimal: Decimal = Decimal()

if let shippingRule = shippingRule, shippingRule.cost > 0 {
shippingAmountDecimal = Decimal(shippingRule.cost)
}

let pledgeTotal = NSDecimalNumber(decimal: pledgeAmountDecimal + shippingAmountDecimal)
let formattedPledgeTotal = Format.decimalCurrency(for: pledgeTotal.doubleValue)

return formattedPledgeTotal
}
5 changes: 5 additions & 0 deletions Library/ViewModels/DeprecatedRewardPledgeViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ internal final class DeprecatedRewardPledgeViewModelTests: TestCase {
"amount": NSDecimalNumber(value: reward.minimum),
"type": PKPaymentSummaryItemType.final.rawValue
],
[
"label": "Shipping",
"amount": NSDecimalNumber(value: shippingRule.cost),
"type": PKPaymentSummaryItemType.final.rawValue
],
[
"label": "Kickstarter (if funded)",
"amount": NSDecimalNumber(value: reward.minimum),
Expand Down
4 changes: 2 additions & 2 deletions Library/ViewModels/PledgeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public typealias CreateBackingData = (
public typealias UpdateBackingData = (
backing: Backing,
reward: Reward,
pledgeAmount: Double?,
pledgeAmount: Double,
shippingRule: ShippingRule?,
paymentSourceId: String?,
applePayParams: ApplePayParams?
Expand Down Expand Up @@ -353,7 +353,7 @@ public class PledgeViewModel: PledgeViewModelType, PledgeViewModelInputs, Pledge
let updateBackingData = Signal.combineLatest(
backing,
reward,
pledgeAmount.wrapInOptional(),
pledgeAmount,
selectedShippingRule,
selectedPaymentSourceId,
applePayParamsData
Expand Down