Skip to content

Commit cb1ec70

Browse files
committed
initialize sae
1 parent 7ae9a9c commit cb1ec70

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Checkout/Checkout.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public final class Checkout: ObservableObject {
5959
/// The CurrencySelectorElement for this Checkout instance, when Adaptive Pricing is available.
6060
private var currencySelectorElement: CurrencySelectorElement?
6161

62+
/// The ShippingAddressElement for this Checkout instance.
63+
private var shippingAddressElement: ShippingAddressElement!
64+
6265
// TODO(gbirch) TODO(porter) remove this nonisolatedSession
6366
// once MPE is properly MainActor isolated
6467
/// A snapshot of the current ``session`` accessible from non-MainActor contexts.
@@ -133,6 +136,7 @@ public final class Checkout: ObservableObject {
133136
try await applyDefaults()
134137

135138
// Load elements
139+
self.shippingAddressElement = ShippingAddressElement(checkout: self)
136140
self.paymentElement = try await PaymentElement(checkout: self)
137141
let sessionSource = CheckoutSessionSource(initialSession: session, sessionPublisher: $session)
138142
self.expressCheckoutElement = ExpressCheckoutElement(
@@ -287,6 +291,11 @@ public final class Checkout: ObservableObject {
287291
return currencySelectorElement
288292
}
289293

294+
/// Returns the ShippingAddressElement for this Checkout instance.
295+
public func getShippingAddressElement() -> ShippingAddressElement {
296+
return shippingAddressElement
297+
}
298+
290299
// MARK: - Confirm
291300

292301
/// Use this method to confirm the Checkout Session.

StripePaymentSheet/StripePaymentSheet/Source/PaymentSheet/Checkout/Shipping Address Element/ShippingAddressElement.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,27 @@
88
@MainActor
99
@_spi(STP)
1010
@_spi(ReactNativeSDK)
11-
public final class ShippingAddressElement {}
11+
public final class ShippingAddressElement {
12+
13+
private(set) var addressViewController: AddressViewController!
14+
15+
init(checkout: Checkout) {
16+
let configuration = checkout.configuration.shippingAddressElement.makeAddressViewControllerConfiguration(
17+
shippingAddress: checkout.session.shippingAddress,
18+
allowedCountries: checkout.session.allowedShippingCountries,
19+
apiClient: checkout.apiClient,
20+
useAutocompleteEndpoints: checkout.session.elementsSession.shouldUseAutocompleteProxyEndpoints
21+
)
22+
addressViewController = AddressViewController(
23+
configuration: configuration,
24+
delegate: self
25+
)
26+
}
27+
}
28+
29+
extension ShippingAddressElement: AddressViewControllerDelegate {
30+
public func addressViewControllerDidFinish(
31+
_ addressViewController: AddressViewController,
32+
with address: AddressViewController.AddressDetails?
33+
) {}
34+
}

StripePaymentSheet/StripePaymentSheetTests/PaymentSheet/Checkout/ShippingAddressElementConfigurationTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,55 @@ import XCTest
1111

1212
@MainActor
1313
final class ShippingAddressElementConfigurationTests: XCTestCase {
14+
func testCheckoutInitializesShippingAddressElement() async throws {
15+
// Given
16+
var sessionJSON = CheckoutTestHelpers.openSessionJSON
17+
sessionJSON["shipping_address_collection"] = ["allowed_countries": ["US", "CA"]]
18+
var elementsSessionJSON = CheckoutTestHelpers.minimalElementsSessionJSON
19+
elementsSessionJSON["flags"] = ["ocs_mobile_should_use_autocomplete_proxy_endpoints": true]
20+
sessionJSON["elements_session"] = elementsSessionJSON
21+
let apiResponse = try XCTUnwrap(PaymentPagesAPIResponse.decodedObject(fromAPIResponse: sessionJSON))
22+
23+
var configuration = Checkout.Configuration(
24+
clientSecret: "cs_test_123_secret_abc",
25+
returnURL: "stripe-ios-test://checkout-return"
26+
)
27+
configuration.shippingAddressElement.title = "Delivery address"
28+
configuration.shippingAddressElement.buttonTitle = "Save delivery address"
29+
configuration.shippingAddressElement.appearance.colors.primary = .purple
30+
var shippingDetails = Checkout.Configuration.Defaults.ShippingDetails()
31+
shippingDetails.name = "Jenny Rosen"
32+
shippingDetails.address = .init(
33+
country: "US",
34+
line1: "510 Townsend St.",
35+
city: "San Francisco",
36+
state: "CA",
37+
postalCode: "94103"
38+
)
39+
configuration.defaults.shippingDetails = shippingDetails
40+
configuration = CheckoutTestHelpers.makeConfiguration(
41+
apiResponse: apiResponse,
42+
configuration: configuration
43+
)
44+
45+
// When
46+
let checkout = try await Checkout(configuration: configuration)
47+
let shippingAddressElement = checkout.getShippingAddressElement()
48+
let addressConfiguration = shippingAddressElement.addressViewController.configuration
49+
50+
// Then
51+
XCTAssertTrue(shippingAddressElement === checkout.getShippingAddressElement())
52+
XCTAssertEqual(addressConfiguration.title, "Delivery address")
53+
XCTAssertEqual(addressConfiguration.buttonTitle, "Save delivery address")
54+
XCTAssertEqual(addressConfiguration.appearance.colors.primary, .purple)
55+
XCTAssertEqual(addressConfiguration.allowedCountries, ["US", "CA"])
56+
XCTAssertEqual(addressConfiguration.defaultValues.name, "Jenny Rosen")
57+
XCTAssertEqual(addressConfiguration.defaultValues.address.country, "US")
58+
XCTAssertEqual(addressConfiguration.defaultValues.address.line1, "510 Townsend St.")
59+
XCTAssertTrue(addressConfiguration.apiClient === configuration.apiClient)
60+
XCTAssertTrue(addressConfiguration.useAutocompleteEndpoints)
61+
}
62+
1463
func testMakeAddressViewControllerConfiguration() {
1564
// Given
1665
let apiClient = STPAPIClient(publishableKey: "pk_test_123")

0 commit comments

Comments
 (0)