-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Async init for embedded and initial selection (#4068)
## Summary - Updates the embedded playground to take in the configuration from the PlaygroundController, and use the actual embedded APIs. - Implements the `create` for the EmbeddedPaymentElement. This now uses the PaymentSheetLoader to load the intent and elements/session. This function also determines with the load result if we should show Link or Apple Pay, and the first selection in the embedded view. - Updates `EmbeddedPaymentMethodsView` to take in an initial selection and select it if present - Adds an `IntegrationShape` to the PaymentSheetLoader to determine if we can default to Apple Pay/Link and when to start checkout timer - Some updated snapshot tests ## Motivation Embedded ## Testing - Manual ## Changelog N/A
- Loading branch information
1 parent
c8cf5d2
commit c147b50
Showing
16 changed files
with
459 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ import Contacts | |
import PassKit | ||
@_spi(STP) import StripeCore | ||
@_spi(STP) import StripePayments | ||
@_spi(CustomerSessionBetaAccess) @_spi(STP) @_spi(PaymentSheetSkipConfirmation) @_spi(ExperimentalAllowsRemovalOfLastSavedPaymentMethodAPI) @_spi(ExperimentalPaymentMethodLayoutAPI) import StripePaymentSheet | ||
@_spi(CustomerSessionBetaAccess) @_spi(STP) @_spi(PaymentSheetSkipConfirmation) @_spi(ExperimentalAllowsRemovalOfLastSavedPaymentMethodAPI) @_spi(ExperimentalPaymentMethodLayoutAPI) @_spi(EmbeddedPaymentElementPrivateBeta) import StripePaymentSheet | ||
import SwiftUI | ||
import UIKit | ||
|
||
|
@@ -177,6 +177,75 @@ class PlaygroundController: ObservableObject { | |
return configuration | ||
} | ||
|
||
var embeddedConfiguration: EmbeddedPaymentElement.Configuration { | ||
var configuration = EmbeddedPaymentElement.Configuration(formSheetAction: .confirm(completion: { [weak self] result in | ||
// TODO(porter) Handle two step confirm | ||
self?.lastPaymentResult = result | ||
})) | ||
configuration.externalPaymentMethodConfiguration = externalPaymentMethodConfiguration | ||
switch settings.externalPaymentMethods { | ||
case .paypal: | ||
configuration.paymentMethodOrder = ["card", "external_paypal"] | ||
case .off, .all: // When using all EPMs, alphabetize the order by not setting `paymentMethodOrder`. | ||
break | ||
} | ||
configuration.merchantDisplayName = "Example, Inc." | ||
configuration.applePay = applePayConfiguration | ||
configuration.customer = customerConfiguration | ||
configuration.appearance = appearance | ||
if settings.userOverrideCountry != .off { | ||
configuration.userOverrideCountry = settings.userOverrideCountry.rawValue | ||
} | ||
configuration.returnURL = "payments-example://stripe-redirect" | ||
|
||
if settings.defaultBillingAddress != .off { | ||
configuration.defaultBillingDetails.name = "Jane Doe" | ||
configuration.defaultBillingDetails.address = .init( | ||
city: "San Francisco", | ||
country: "US", | ||
line1: "510 Townsend St.", | ||
postalCode: "94102", | ||
state: "California" | ||
) | ||
} | ||
switch settings.defaultBillingAddress { | ||
case .on: | ||
configuration.defaultBillingDetails.email = "[email protected]" | ||
configuration.defaultBillingDetails.phone = "+13105551234" | ||
case .randomEmail: | ||
configuration.defaultBillingDetails.email = "test-\(UUID().uuidString)@stripe.com" | ||
configuration.defaultBillingDetails.phone = "+13105551234" | ||
case .randomEmailNoPhone: | ||
configuration.defaultBillingDetails.email = "test-\(UUID().uuidString)@stripe.com" | ||
case .customEmail: | ||
configuration.defaultBillingDetails.email = settings.customEmail | ||
case .off: | ||
break | ||
} | ||
|
||
if settings.allowsDelayedPMs == .on { | ||
configuration.allowsDelayedPaymentMethods = true | ||
} | ||
|
||
if settings.shippingInfo != .off { | ||
configuration.allowsPaymentMethodsRequiringShippingAddress = true | ||
configuration.shippingDetails = { [weak self] in | ||
return self?.addressDetails | ||
} | ||
} | ||
configuration.primaryButtonLabel = settings.customCtaLabel | ||
|
||
configuration.billingDetailsCollectionConfiguration.name = .init(rawValue: settings.collectName.rawValue)! | ||
configuration.billingDetailsCollectionConfiguration.phone = .init(rawValue: settings.collectPhone.rawValue)! | ||
configuration.billingDetailsCollectionConfiguration.email = .init(rawValue: settings.collectEmail.rawValue)! | ||
configuration.billingDetailsCollectionConfiguration.address = .init(rawValue: settings.collectAddress.rawValue)! | ||
configuration.billingDetailsCollectionConfiguration.attachDefaultsToPaymentMethod = settings.attachDefaults == .on | ||
configuration.preferredNetworks = settings.preferredNetworksEnabled == .on ? [.visa, .cartesBancaires] : nil | ||
configuration.allowsRemovalOfLastSavedPaymentMethod = settings.allowsRemovalOfLastSavedPaymentMethod == .on | ||
|
||
return configuration | ||
} | ||
|
||
var addressConfiguration: AddressViewController.Configuration { | ||
var configuration = AddressViewController.Configuration(additionalFields: .init(phone: .optional), appearance: configuration.appearance) | ||
if case .onWithDefaults = settings.shippingInfo { | ||
|
@@ -599,7 +668,7 @@ extension PlaygroundController { | |
) | ||
} | ||
} else if self.settings.uiStyle == .embedded { | ||
self.embeddedPaymentElement() | ||
self.makeEmbeddedPaymentElement() | ||
self.isLoading = false | ||
self.currentlyRenderedSettings = self.settings | ||
} | ||
|
@@ -806,14 +875,14 @@ class AnalyticsLogObserver: ObservableObject { | |
@Published var analyticsLog: [[String: Any]] = [] | ||
} | ||
|
||
|
||
// MARK: Embedded helpers | ||
extension PlaygroundController: EmbeddedPlaygroundViewControllerDelegate { | ||
func embeddedPaymentElement() { | ||
embeddedPlaygroundController = EmbeddedPlaygroundViewController(settings: settings, appearance: appearance) | ||
embeddedPlaygroundController?.delegate = self | ||
extension PlaygroundController { | ||
func makeEmbeddedPaymentElement() { | ||
embeddedPlaygroundController = EmbeddedPlaygroundViewController(configuration: embeddedConfiguration, | ||
intentConfig: intentConfig, | ||
appearance: appearance) | ||
} | ||
|
||
func presentEmbedded() { | ||
guard let embeddedPlaygroundController else { return } | ||
let closeButton = UIBarButtonItem(barButtonSystemItem: .close, target: self, action: #selector(dismissEmbedded)) | ||
|
@@ -826,8 +895,4 @@ extension PlaygroundController: EmbeddedPlaygroundViewControllerDelegate { | |
@objc func dismissEmbedded() { | ||
embeddedPlaygroundController?.dismiss(animated: true, completion: nil) | ||
} | ||
|
||
func didComplete(with result: StripePaymentSheet.PaymentSheetResult) { | ||
lastPaymentResult = result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.