#tarunmv30
1 messages · Page 1 of 1 (latest)
Hi, can you share more? What are you seeing? How are you sending this data? What does your code cool like? Are you seeing any errors?
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
};
app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "usd",
automatic_payment_methods: {
enabled: true,
},
});
// Generate an ephemeral key for the customer
const customer = await stripe.customers.create();
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2020-08-27' }
);
// Return the necessary information to the client
res.send({
customer: customer.id,
ephemeralKey: ephemeralKey.secret,
paymentIntent: paymentIntent.client_secret,
publishableKey: 'pk_test_51MSCbfJMYkCh6QmLjYSQLK5gPtmLvaUMn0KrXsbFVnqVROCRere3kLrAQnKOT2Uepb5fRqv34vjgNAArl8wzPwWd00OoOZlDFj' // replace with your own publishable key
});
});```
server code^
let backendCheckoutUrl = URL(string: "https://us-central1-neilsurbanoven.cloudfunctions.net/stripe/create-payment-intent")! // An example backend endpoint
@Published var paymentSheet: PaymentSheet?
@Published var paymentResult: PaymentSheetResult?
func preparePaymentSheet() {
// MARK: Fetch the PaymentIntent and Customer information from the backend
var request = URLRequest(url: backendCheckoutUrl)
request.httpMethod = "POST"
//request.body data needs to be attached here
let task = URLSession.shared.dataTask(
with: request,
completionHandler: { (data, _, _) 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 publishableKey = json["publishableKey"] as? String
else {
print("There was an errir with the data")
return
}
//rest of my functioning code goes here
}```
This is the function that works however takes in a hard coded payment amount value
@Published var selectedTab: Tab = .home
@Published var myCart: [MenuItem] = []
@Published var totalAmount: Double = 0
@Published var selectedLargePizza: Bool = true
var appetizers = [MenuItem]()
var sandwiches = [MenuItem]()
var calzones = [MenuItem]()
var smallPizzas = [MenuItem]()
var largePizzas = [MenuItem]()
var specs = [MenuItem]()
var wings = [MenuItem]()
var sweets = [MenuItem]()
var brunch = [MenuItem]()
var salad = [MenuItem]()
init() {
getSweets()
getAppetizers()
getSandwiches()
getCalzones()
getWings()
getSpecialties()
getBrunch()
getSalad()
getLargePizzas()
getSmallPizzas()
}
//cart save/delete item functions
func saveOrder(newCartItem: MenuItem) {
myCart.append(newCartItem)
}
func delete(item: MenuItem) {
delete(item.id)
}
func delete(_ id: MenuItem.ID) {
if let index = index(for: id) {
myCart.remove(at: index)
}
}
func index(for id: MenuItem.ID) -> Int? {
myCart.firstIndex(where: { $0.id == id })
}
``` this is the separate class that stores everything related to the order including items / total /etc.
im getting stuck trying to pass in that data to my request
I'm sorry, I was just looking at the specific line of code where you were trying to send this to your server side. That is a lot of text.
Where in the code are you stuck exactly?
Which line?
in the function called preparePaymentSheet() in my backend model
since its in a different class i dont know how to access myCart published property of my foodViewModel in the function so that I can pass up the data being stored in the cart
I'm sorry about sending so much code lol
Ok, from looking at this, it does not appear that the question is Stripe Specific. Rather, a general swiftui question.. I do not have much expertise on this but looking