#wasko-swiftui-paymentsheet
1 messages · Page 1 of 1 (latest)
Hey @short anvil I'm happy to try but I'm going to need a lot more details like your exact code, the doc you are following and detailed logs about what you are doing and tried to debud
wasko-swiftui-paymentsheet
yes no problem can i share you my code ? its little bit long😭
so for the documentation its this one
you can but I would expect to see 15 lines at most, not hundreds of your whole code. Only the really relevant bits
yes of course
class CardPaymentIntent: ObservableObject {
@Published var paymentSheet: PaymentSheet?
@Published var paymentResult: PaymentSheetResult?
@EnvironmentObject var ScanServiceID : GetTransactionByScanIDAPI
func preparePaymentSheet() async {
// Configuration de Stripes -->
let ephemeralKeyString = ScanIDService.shared.dataTransaction.dataTransactionApi?.striephemeralKey
STPAPIClient.shared.publishableKey = ScanIDService.shared.dataTransaction.dataTransactionApi?.publishablekey
// MARK: Create a PaymentSheet instance
var configuration = PaymentSheet.Configuration()
configuration.merchantDisplayName = "Example, Inc."
configuration.customer = .init(id: ScanIDService.shared.dataTransaction.dataTransactionApi?.customerId ?? "pas de customer ID dans la configuration",
ephemeralKeySecret: ephemeralKeyString ?? "pas de clé ephemere pour la configuation du customer")
configuration.allowsDelayedPaymentMethods = false
self.paymentSheet = PaymentSheet(paymentIntentClientSecret: ScanIDService.shared.dataTransaction.dataTransactionApi?.paymentIntent ?? "pas de pi", configuration: configuration)
}
// function completion
func onPaymentCompletion(result: PaymentSheetResult) {
self.paymentResult = result
}
}
so here im doing the configuration of the paymenSheet
and it works because he turned to buy when im loading my view here ⬇️
struct CardPaymentView: View {
// @EnvironmentObject var modelAPI : GetTransactionByScanIDAPI
@ObservedObject var model = CardPaymentIntent()
@State var isConfirmingPayment = false
@EnvironmentObject var scanServiceID: GetTransactionByScanIDAPI
var body:some View {
VStack {
if let paymentSheet = model.paymentSheet {
PaymentSheet.PaymentButton(
paymentSheet: paymentSheet,
onCompletion: model.onPaymentCompletion
) {
Button("Buy"){
DispatchQueue.main.async {
isConfirmingPayment = true
print("sa buy")
}
}
}
} else {
Text("Loading…")
}
if let result = model.paymentResult {
switch result {
case .completed:
Text("Payment complete")
case .failed(let error):
Text("Payment failed: \(error.localizedDescription)")
case .canceled:
Text("Payment canceled.")
}
}
}.onAppear {
Task{
// await ScanIDService.shared.createTransactionPayment(parameters:"")
await model.preparePaymentSheet()
}
}
}
}
try to describe this as a developer please. You have code, your code does things, you can add logs and explain exactly what is or isn't working
I want to help you but you need to help me help you 🙂
like here you're clearly clicking many times but that doesn't prove much. I assume your code does a back end call when you click buy and it takes a while. If you add clear logs to those you will be able to figure out what part is taking a while no?
precisely in the logs the form is there.. thats why im here🥲
precisely in the logs the form is there
I'm sorry I don't follow what that means
i mean in the stripes log the form is there but when i click he doesnt show up
All good.
Sorry you do need to fully understand your code here. I am guessing that when you click on the button, your code will make an HTTP request to your local server to get an API key, an EphemeralKey, etc. This takes some time. Please add logs to that part like "Starting the request...." "request finished!" so that you see if this is what causes the delay
in the previous view I have already retrieved the elements thanks to an API call so when I arrive at this view I just do the configuration of stripes and the buy button just has to output me the paymentSheet
For example my own code does this first func preparePaymentSheet() { // Fetch the PaymentIntent and Customer information from the backend var request = URLRequest(url: backendCheckoutUrl) ... let task = URLSession.shared.dataTask(with: request, completionHandler: { [weak self] (data, response, error) in guard let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any], let paymentIntentClientSecret = json["payment_intent_client_secret"] as? String, let publishableKey = json["publishable_api_key"] as? String, let self = self else { print("[Error fetching initial data from server!") return } print("Success fetching data from server: ") print("client secret: " + paymentIntentClientSecret)
but he doesnt
I assume you have something similar
yeah sorry you say in the previous view but it's really tough for me to help with just a video and nothing else
I'm kinda a "rubber duck" here. You are the developer, you own all of the code. I point you towards possible paths and you carefully debug it all
So instead of clicking 20 times on that button, what exactly happens when you click it once?
when i click it once i did nothing only print what's inside the button
and that's not what he should do
if let paymentSheet = model.paymentSheet {
PaymentSheet.PaymentButton(
paymentSheet: paymentSheet,
onCompletion: model.onPaymentCompletion
) {
Button("Buy"){
isConfirmingPayment = true
print("sa buy")
}
}
} else {
Text("Loading…")
}
Gotcha! I'm looking, give me a few minutes
Can you add logs at the end of preparePaymentSheet()
Like after self.paymentSheet = PaymentSheet(paymentIntentClientSecret: ScanIDService.shared.dataTransaction.dataTransactionApi?.paymentIntent ?? "pas de pi", configuration: configuration) I would like to see a real log that says when that part of the code is done and confirm when it prints (whether it's before or after clicking on the button)
also add logs before and after await model.preparePaymentSheet() so that we can see the order of operations
Once you have, can you share the exact logs you get before you click that button and then after you click it once?
alright give 2 min
please
so here all the logs at my console when the view appear
and after when i tap once on the button they only print ("button buy tapped")
👋 hopping in here since koopajah had to head out
okay no problem thanks lot brother
and when you just tap the button once the payment sheet just compeltely doesn't show up, correct?
yes nothing show up
Quick question - what are you using isConfirmingPayment for?
Could you temporarily try replacing
Button("Buy"){
isConfirmingPayment = true
print("sa buy")
}
with just
Text("Buy")
is for nothing i was trying something but forget to erase it
yes let me try
it works😭
but it dont understand why because in the docs its button not text
and its problem because i want to custom my own button with an image..
In the docs we do use a Text() and not a button - https://stripe.com/docs/payments/accept-a-payment?platform=ios&uikit-swiftui=swiftui
okay im an idiot who don't know how to read...😅
but if i want to make a custom button for the buy text()? how i can do ?
If you want a custom button with your own image you should still be able to do that - you would just use Image() instead of Text()
it works thanks lot brother 7 days i was working on it and struggling
👍 glad we could help!
thanks 🙏