stripe-ios: [BUG] PaymentSheet not presenting in SwiftUI
Summary
I have a list view in SwiftUI with many line items and I want to show a “Pay” button for each row that, when clicked, would generate and present a PaymentSheet bottom view. However, when I create the payment sheet and set it to isPresented, the modal doesn’t appear.
I also don’t want to generate a paymentIntent when the view loads because I don’t know which item the client is purchasing. All the examples I’ve found through Stripe have the paymentIntent being generated when the view loads.
I’ve created an example with the basic functionality I’m looking for - the use case in my actual app is slightly different. I have a button that generates a paymentIntent from my server. However, the PaymentSheet isn’t being presented.
Code to reproduce
import SwiftUI
import Stripe
import FirebaseFunctions
class ItemCheckoutViewModel: ObservableObject {
@Published var paymentSheet: PaymentSheet?
@Published var paymentResult: PaymentSheetResult?
@Published var showPaymentSheet = false
var stripePublishableKey = "[Redacted]"
lazy var functions = Functions.functions()
@MainActor
func preparePaymentSheet(for item: ProductItem) async {
do {
let result = try await functions.httpsCallable("getStripeIntent").call([
"itemId": item.id
])
if let data = result.data as? [String: Any] {
guard
let customerId = data["customer"] as? String,
let customerEphemeralKeySecret = data["ephemeralKey"] as? String,
let paymentIntentClientSecret = data["paymentIntent"] as? String
else {
return
}
STPAPIClient.shared.publishableKey = stripePublishableKey
var configuration = PaymentSheet.Configuration()
configuration.merchantDisplayName = "Test"
configuration.customer = .init(id: customerId, ephemeralKeySecret: customerEphemeralKeySecret)
configuration.allowsDelayedPaymentMethods = true
self.paymentSheet = PaymentSheet(paymentIntentClientSecret: paymentIntentClientSecret, configuration: configuration)
self.showPaymentSheet = true
}
} catch {
print(error)
}
}
func handlePaymentCompletion(result: PaymentSheetResult) {
//
}
}
struct ProductItem: Identifiable, Codable {
var id: String
var title: String
var amount: Int
}
struct PaymentSheetView: View {
@StateObject var viewModel = ItemCheckoutViewModel()
var items: [ProductItem]
@ViewBuilder func itemRowView(item: ProductItem) -> some View {
HStack {
Text(item.title)
Spacer()
Button {
Task {
await viewModel.preparePaymentSheet(for: item)
}
} label: {
Text("Pay \(String(format: "$%.2f", item.amount/100))")
}
}
}
var body: some View {
List(items) { item in
itemRowView(item: item)
}
if let paymentSheet = viewModel.paymentSheet {
EmptyView()
.paymentSheet(isPresented: $viewModel.showPaymentSheet,
paymentSheet: paymentSheet,
onCompletion: viewModel.handlePaymentCompletion)
}
}
}
iOS version
16.1
Installation method
SPM
SDK version
22.8.4
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 2
- Comments: 15
I was getting the same error until I updated to the newest version (23.31.0). Seems to be resolved now. Thank you. 🙏
the problem was gone when I updated to the lateset version 23.29.2
@JUSTINMKAUFMAN Thanks for this, I will give a try.
This issue persists for me on the latest versions of the Stripe SDK (23.9.0). The hack I’m using is to delay toggling the PaymentSheet’s
isPresentedbinding by a fraction of a second as seen in this screenshot:If I remove the delay, the payment sheet never appears and Xcode says:
PaymentSheet+SwiftUI.swift:242 Modifying state during view update, this will cause undefined behavior.Screenshot:It’d be great to get this sorted out since I worry that my hack may stop working for reasons I don’t understand. Happy to provide any additional information that might help.
@DevonAllary were you able to resolve this while maintaining the approach ?