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

Link v3: Fix Billing Address Collection and a few other TODOs #4218

Merged
merged 8 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Created by Eduardo Urias on 2/27/23.
//

@_spi(STP) import StripeCore
@_spi(STP) import StripePayments
@testable import StripePaymentSheet
import XCTest

Expand Down Expand Up @@ -38,4 +40,23 @@ final class STPPaymentMethodBillingDetailsTests: XCTestCase {
XCTAssertNil(params["state"])
XCTAssertNil(params["country"])
}

func testCreateLinkBillingAddress() {
let billingAddress = BillingAddress(
name: "Name",
line1: "Line 1",
line2: "Line 2",
city: "City",
state: "State",
countryCode: "Country"
)
let billingDetails = STPPaymentMethodBillingDetails.init(billingAddress: billingAddress, email: "email@email.com")!
XCTAssertEqual(billingDetails.name, "Name")
XCTAssertEqual(billingDetails.address?.line1, "Line 1")
XCTAssertEqual(billingDetails.address?.line2, "Line 2")
XCTAssertEqual(billingDetails.address?.city, "City")
XCTAssertEqual(billingDetails.address?.state, "State")
XCTAssertEqual(billingDetails.address?.country, "Country")
XCTAssertEqual(billingDetails.email, "email@email.com")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import Foundation

@_spi(STP) public struct BillingAddress: Encodable {
let name: String?
let line1: String?
let line2: String?
let city: String?
let state: String?
let postalCode: String?
let countryCode: String?
@_spi(STP) public struct BillingAddress: Codable {
@_spi(STP) public let name: String?
@_spi(STP) public let line1: String?
@_spi(STP) public let line2: String?
@_spi(STP) public let city: String?
@_spi(STP) public let state: String?
@_spi(STP) public let postalCode: String?
@_spi(STP) public let countryCode: String?

@_spi(STP) public init(
name: String? = nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,12 @@ extension PaymentSheetLinkAccount {
/// - Returns: Payment method params for paying with Link.
func makePaymentMethodParams(from paymentDetails: ConsumerPaymentDetails, cvc: String?) -> STPPaymentMethodParams? {
guard let currentSession = currentSession else {
assertionFailure("Cannot make payment method params without an active session.")
stpAssertionFailure("Cannot make payment method params without an active session.")
return nil
}

let params = STPPaymentMethodParams(type: .link)
params.billingDetails = STPPaymentMethodBillingDetails(billingAddress: paymentDetails.billingAddress, email: paymentDetails.billingEmailAddress)
davidme-stripe marked this conversation as resolved.
Show resolved Hide resolved
params.link?.paymentDetailsID = paymentDetails.stripeID
params.link?.credentials = ["consumer_session_client_secret": currentSession.clientSecret]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,34 @@ typealias ConsumerSessionWithPaymentDetails = (session: ConsumerSession, payment
final class ConsumerPaymentDetails: Decodable {
let stripeID: String
let details: Details
let billingAddress: BillingAddress?
let billingEmailAddress: String?
var isDefault: Bool

// TODO(link) : Billing address

init(stripeID: String,
details: Details,
billingAddress: BillingAddress?,
billingEmailAddress: String?,
isDefault: Bool) {
self.stripeID = stripeID
self.details = details
self.billingAddress = billingAddress
self.billingEmailAddress = billingEmailAddress
self.isDefault = isDefault
}

private enum CodingKeys: String, CodingKey {
case stripeID = "id"
case billingAddress = "billing_address"
case billingEmailAddress = "billing_email_address"
case isDefault
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.stripeID = try container.decode(String.self, forKey: .stripeID)
self.billingAddress = try? container.decode(BillingAddress.self, forKey: .billingAddress)
self.billingEmailAddress = try? container.decode(String.self, forKey: .billingEmailAddress)
// The payment details are included in the dictionary, so we pass the whole dict to Details
self.details = try decoder.singleValueContainer().decode(Details.self)
self.isDefault = try container.decode(Bool.self, forKey: .isDefault)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2021 Stripe, Inc. All rights reserved.
//

@_spi(STP) import StripePayments
@_spi(STP) import StripePaymentsUI
@_spi(STP) import StripeUICore
import UIKit
Expand All @@ -22,10 +23,8 @@ extension LinkPaymentMethodPicker {
var paymentMethod: ConsumerPaymentDetails? {
didSet {
switch paymentMethod?.details {
case .card:
// TODO(link): Needs refactor
// case .card(let card):
// cardBrandView.cardBrand = card.stpBrand
case .card(let card):
cardBrandView.setCardBrand(STPCard.brand(from: card.brand))
bankIconView.isHidden = true
cardBrandView.isHidden = false
primaryLabel.text = paymentMethod?.paymentSheetLabel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,13 @@ extension PayWithLinkViewController {
var configuration = context.configuration
configuration.linkPaymentMethodsOnly = true
configuration.appearance = LinkUI.appearance
// TODO(link): Update elementsSession, formCache, analyticsHelper, and paymentMethodTypes
assertionFailure("Not yet implemented")
return AddPaymentMethodViewController(
intent: context.intent,
// TODO(link): Update elementsSession
elementsSession: .makeBackupElementsSession(allResponseFields: [:], paymentMethodTypes: []),
elementsSession: context.elementsSession,
configuration: configuration,
paymentMethodTypes: [.stripe(.card)],
formCache: .init(),
analyticsHelper: .init(integrationShape: .complete, configuration: configuration),
formCache: .init(), // We don't want to share a form cache with the containing PaymentSheet
analyticsHelper: context.analyticsHelper,
delegate: self
)
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,8 @@ extension PayWithLinkViewController {
case .success(let account):
self?.coordinator?.accountUpdated(account)
STPAnalyticsClient.sharedClient.logLinkSignupComplete()
case .failure: break
// TODO(link): Fix signup failure logging
// STPAnalyticsClient.sharedClient.logLinkSignupFailure()
case .failure(let error):
STPAnalyticsClient.sharedClient.logLinkSignupFailure(error: error)
}

self?.signUpButton.isLoading = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ final class PayWithLinkViewController: UINavigationController {
let shouldFinishOnClose: Bool
let callToAction: ConfirmButton.CallToActionType
var lastAddedPaymentDetails: ConsumerPaymentDetails?
var analyticsHelper: PaymentSheetAnalyticsHelper

/// Creates a new Context object.
/// - Parameters:
Expand All @@ -77,20 +78,23 @@ final class PayWithLinkViewController: UINavigationController {
/// - shouldOfferApplePay: Whether or not to show Apple Pay as a payment option.
/// - shouldFinishOnClose: Whether or not Link should finish with `.canceled` result instead of returning to Payment Sheet when the close button is tapped.
/// - callToAction: A custom CTA to display on the confirm button. If `nil`, will display `intent`'s default CTA.
/// - analyticsHelper: An instance of `AnalyticsHelper` to use for logging.
init(
intent: Intent,
elementsSession: STPElementsSession,
configuration: PaymentSheet.Configuration,
shouldOfferApplePay: Bool,
shouldFinishOnClose: Bool,
callToAction: ConfirmButton.CallToActionType?
callToAction: ConfirmButton.CallToActionType?,
analyticsHelper: PaymentSheetAnalyticsHelper
) {
self.intent = intent
self.elementsSession = elementsSession
self.configuration = configuration
self.shouldOfferApplePay = shouldOfferApplePay
self.shouldFinishOnClose = shouldFinishOnClose
self.callToAction = callToAction ?? intent.callToAction
self.analyticsHelper = analyticsHelper
}
}

Expand Down Expand Up @@ -118,7 +122,8 @@ final class PayWithLinkViewController: UINavigationController {
configuration: PaymentSheet.Configuration,
shouldOfferApplePay: Bool = false,
shouldFinishOnClose: Bool = false,
callToAction: ConfirmButton.CallToActionType? = nil
callToAction: ConfirmButton.CallToActionType? = nil,
analyticsHelper: PaymentSheetAnalyticsHelper
) {
self.init(
context: Context(
Expand All @@ -127,7 +132,8 @@ final class PayWithLinkViewController: UINavigationController {
configuration: configuration,
shouldOfferApplePay: shouldOfferApplePay,
shouldFinishOnClose: shouldFinishOnClose,
callToAction: callToAction
callToAction: callToAction,
analyticsHelper: analyticsHelper
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@_spi(STP) import StripeUICore
import UIKit

// TODO(link): Remove after migrating to modern bindings
fileprivate extension ConsumerPaymentDetails {
var cardDetails: Details.Card? {
switch details {
Expand Down Expand Up @@ -167,8 +166,10 @@ final class LinkCardEditElement: Element {
private lazy var billingAddressSection: AddressSectionElement? = {
guard configuration.billingDetailsCollectionConfiguration.address != .never else { return nil }

let defaultBillingAddress = AddressSectionElement.AddressDetails(billingAddress: paymentMethod.billingAddress ?? .init(), phone: nil)
return AddressSectionElement(
title: String.Localized.billing_address_lowercase,
defaults: defaultBillingAddress,
collectionMode: configuration.billingDetailsCollectionConfiguration.address == .full
? .all()
: .countryAndPostal(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ extension ConfirmButton {
didTap: didTap
)

// Override the background color of the `.succeeded` state. Make it match
// the background color of the `.enabled` state.
// TODO(link): Needs refactor
// button.succeededBackgroundColor = (
// LinkUI.appearance.primaryButton.backgroundColor ??
// LinkUI.appearance.colors.primary
// )

button.directionalLayoutMargins = compact
? LinkUI.compactButtonMargins
: LinkUI.buttonMargins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ extension LinkUI {
// Primary button
appearance.primaryButton.textColor = .linkPrimaryButtonForeground
appearance.primaryButton.backgroundColor = .linkBrand
appearance.primaryButton.successBackgroundColor = .linkBrand
appearance.primaryButton.borderWidth = 0
appearance.primaryButton.cornerRadius = LinkUI.cornerRadius
appearance.primaryButton.font = LinkUI.font(forTextStyle: .bodyEmphasized)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ extension PaymentSheet.LinkConfirmOption {
return intentConfirmParams.paymentMethodParams.billingDetails
case .withPaymentMethod(let paymentMethod):
return paymentMethod.billingDetails
case .withPaymentDetails:
// TODO(link): Implement .billingDetails
// return paymentDetails.billingDetails
return nil
case .withPaymentDetails(_, let paymentDetails):
return STPPaymentMethodBillingDetails(billingAddress: paymentDetails.billingAddress, email: paymentDetails.billingEmailAddress)
case .withPaymentMethodParams(_, let paymentMethodParams):
return paymentMethodParams.billingDetails
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ extension PaymentSheet {
case .withPaymentMethod(let paymentMethod):
confirmWithPaymentMethod(paymentMethod, nil, false)
case .withPaymentDetails(let linkAccount, let paymentDetails):
// TODO(link): Confirm the last two options should be "nil" and "false"
confirmWithPaymentDetails(linkAccount, paymentDetails, nil, false)
// shouldSave is false, as we don't show a save checkbox in the Link VC
confirmWithPaymentDetails(linkAccount, paymentDetails, paymentDetails.cvc, false)
case .withPaymentMethodParams(let linkAccount, let paymentMethodParams):
// TODO(link): Confirm the last two options should be "nil" and "false"
// shouldSave is false, as we don't show a save checkbox in the Link VC
createPaymentDetailsAndConfirm(linkAccount, paymentMethodParams, false)
}
case let .external(paymentMethod, billingDetails):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ extension PaymentSheet {
elementsSession: elementsSession,
configuration: configuration,
shouldOfferApplePay: shouldOfferApplePay,
shouldFinishOnClose: shouldFinishOnClose
shouldFinishOnClose: shouldFinishOnClose,
analyticsHelper: self.analyticsHelper
)

payWithLinkVC.payWithLinkDelegate = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ extension LinkCardEditElementSnapshotTests {
checks: nil
)
),
billingAddress: nil,
billingEmailAddress: nil,
isDefault: isDefault
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ extension PayWithLinkViewController_WalletViewModelTests {
configuration: .init(),
shouldOfferApplePay: false,
shouldFinishOnClose: false,
callToAction: nil
callToAction: nil,
analyticsHelper: ._testValue()
),
paymentMethods: LinkStubs.paymentMethods()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extension LinkStubs {
last4: "1234",
checks: nil)
),
billingAddress: nil,
billingEmailAddress: nil,
isDefault: true
),
ConsumerPaymentDetails(
Expand All @@ -49,11 +51,15 @@ extension LinkStubs {
last4: "4321",
checks: .init(cvcCheck: .fail))
),
billingAddress: nil,
billingEmailAddress: nil,
isDefault: false
),
ConsumerPaymentDetails(
stripeID: "3",
details: .bankAccount(bankAccount: .init(iconCode: nil, name: "test", last4: "1234")),
billingAddress: nil,
billingEmailAddress: nil,
isDefault: false
),
ConsumerPaymentDetails(
Expand All @@ -65,6 +71,8 @@ extension LinkStubs {
last4: "1111",
checks: nil)
),
billingAddress: nil,
billingEmailAddress: nil,
isDefault: false
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extension PaymentSheetLinkAccountTests {
return ConsumerPaymentDetails(
stripeID: "1",
details: .card(card: .init(expiryYear: 30, expiryMonth: 10, brand: "visa", last4: "1234", checks: nil)),
billingAddress: nil,
billingEmailAddress: nil,
isDefault: false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,22 @@ extension STPPaymentMethodBillingDetails {
address.country = countryCode
self.address = address
}

/// Convenience initializer for creating an `STPPaymentMethodBillingDetails` instance with a Link BillingDetails
@_spi(STP) public convenience init?(
billingAddress: BillingAddress?,
email: String?
) {
self.init()
let address = STPPaymentMethodAddress()
address.line1 = billingAddress?.line1
address.line2 = billingAddress?.line2
address.city = billingAddress?.city
address.state = billingAddress?.state
address.postalCode = billingAddress?.postalCode
address.country = billingAddress?.countryCode
self.address = address
self.name = billingAddress?.name
self.email = email
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ import UIKit
fatalError("init(coder:) has not been implemented")
}

@_spi(STP) public func setCardBrand(_ brand: STPCardBrand) {
setCardBrand(.brand(brand), animated: false)
}

/// Updates the card brand, optionally animating the transition.
/// - Parameters:
/// - newBrandState: New card brand state.
Expand Down
Loading
Loading