#wasko-swiftui-paymentsheet

1 messages · Page 1 of 1 (latest)

supple summitBOT
bronze totem
#

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

short anvil
#

yes no problem can i share you my code ? its little bit long😭

#

so for the documentation its this one

bronze totem
#

you can but I would expect to see 15 lines at most, not hundreds of your whole code. Only the really relevant bits

short anvil
#

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()
        }
       
         }
  }
}
bronze totem
#

okay so that's a lot of code

#

which part is not working exactly?

short anvil
#

sorry i thought it wasn't a lot

#

so its the button buy who doesnt work

bronze totem
#

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?

short anvil
#

precisely in the logs the form is there.. thats why im here🥲

bronze totem
#

precisely in the logs the form is there
I'm sorry I don't follow what that means

short anvil
#

i mean in the stripes log the form is there but when i click he doesnt show up

bronze totem
#

what form? what does "the form is there" mean?

#

do you mean PaymentSheet?

short anvil
#

the forme is the paymenSheet

#

yes sorry for the confusion

bronze totem
#

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

short anvil
#

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

bronze totem
#

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)

short anvil
#

but he doesnt

bronze totem
#

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?

short anvil
#

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…")

  }
bronze totem
#

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?

supple summitBOT
short anvil
#

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")

sonic badge
#

👋 hopping in here since koopajah had to head out

short anvil
#

okay no problem thanks lot brother

sonic badge
#

and when you just tap the button once the payment sheet just compeltely doesn't show up, correct?

short anvil
#

yes nothing show up

sonic badge
#

Quick question - what are you using isConfirmingPayment for?

#

Could you temporarily try replacing

            Button("Buy"){
                isConfirmingPayment = true
                print("sa buy")

            }

with just

    Text("Buy")
short anvil
#

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..

sonic badge
short anvil
#

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 ?

sonic badge
#

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()

short anvil
#

it works thanks lot brother 7 days i was working on it and struggling

sonic badge
#

👍 glad we could help!

short anvil
#

thanks 🙏