#tarunmv30
1 messages · Page 1 of 1 (latest)
Hi there
Really really hard to do anything with screenshots of code
Let's pause for a second and first... what exactly are you trying to do?
What guide did you follow?
And what do you see on your end (do you see any errors?)
im trying to create a restaurant online ordering app that allows customers to order food from a restaurant
and then take a 10% application fee on it and transfer the rest of the balance to the restaurant
Im a frontend ios developer trying to learn server side code so i'm struggling lol
right now im just trying to simply process a hard coded transaction in my app
Okay so let's start with client-side then
Can you share your iOS PaymentSheet code (not using a screenshot)
So use three backticks: like this
import StripePaymentSheet
import SwiftUI
class MyBackendModel: ObservableObject {
//let backendCheckoutUrl = URL(string: "https://different-imaginary-tarn.glitch.me/checkout")! // Your Glitch backend endpoint
let backendCheckoutUrl = URL(string: "https://us-central1-neilsurbanoven.cloudfunctions.net/checkout")!
@Published var paymentSheet: PaymentSheet?
@Published var paymentResult: PaymentSheetResult?
private var publishableKey = "pk_test_MY_PUB_KEY"
func preparePaymentSheet() {
var request = URLRequest(url: backendCheckoutUrl)
request.httpMethod = "POST"
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 customerId = json["customer"] as? String,
let customerEphemeralKeySecret = json["ephemeralKey"] as? String,
let paymentIntentClientSecret = json["paymentIntent"] as? String,
let self = self else {
// Handle error
return
}
STPAPIClient.shared.publishableKey = self.publishableKey
// MARK: Create a PaymentSheet instance
var configuration = PaymentSheet.Configuration()
configuration.merchantDisplayName = "Example, Inc."
configuration.customer = .init(id: customerId, ephemeralKeySecret: customerEphemeralKeySecret)
// Set `allowsDelayedPaymentMethods` to true if your business can handle payment methods
configuration.allowsDelayedPaymentMethods = true
DispatchQueue.main.async {
self.paymentSheet = PaymentSheet(paymentIntentClientSecret: paymentIntentClientSecret, configuration: configuration)
}
})
task.resume()
}
func onPaymentCompletion(result: PaymentSheetResult) {
self.paymentResult = result
}
}```
oh you asked for payment sheet
one second
import SwiftUI
import StripePaymentSheet
struct CheckoutView: View {
@ObservedObject var model = MyBackendModel()
var body: some View {
VStack {
if let paymentSheet = model.paymentSheet {
PaymentSheet.PaymentButton(
paymentSheet: paymentSheet,
onCompletion: model.onPaymentCompletion
) {
Text("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 { model.preparePaymentSheet() }
}
}
struct CheckoutView_Previews: PreviewProvider {
static var previews: some View {
CheckoutView()
}
}```
Okay thanks. And have you hard-coded a Customer ID, Eph Secret, Client Secret and ensured that the frontend works just fine when you do that?
No they can't be made up. You need to create test values via your server and then hard code. You can just use cURL requests to do that though