#phiredrop
1 messages · Page 1 of 1 (latest)
hello! can you share your code?
Sure, there's a lot tho! Which would you like to see first?
you mentioned during the start of my app during the Stripe init, it fails to retrieve my publishable key. - this section
Here's the init where the failure comes from:
let url = URL(string: BackendUrl + "checkout")!
print("*** Stripe BackendUrl: \(url) ***")
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
guard let response = response as? HTTPURLResponse,
response.statusCode == 200,
let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any],
let publishableKey = json["publishableKey"] as? String else {
print("Failed to retrieve publishableKey from /config")
return
}
print("Fetched publishable key \(publishableKey)")
StripeAPI.defaultPublishableKey = publishableKey
})
task.resume()
}```
the BackendUrl is the Glitch url, so the full url string is the GlitchURL/checkout
what's the error you're getting?
The one in the code: Failed to retrieve publishableKey from /config
that gets printed in my console when starting the app
have you tried stepping through the lines of code before that also? e.g. have you validated what data you're receiving in the response?
Which part are you thinking?
what's in data for example?
Hmm, how can I test that? It won't let me print it
This code is taken straight from the sample stripe ios app. The only thing different is the backend url itself
what do you mean by it won't let you print it?
I guess I don't know how to answer your question "what's in data"
can you log data or json?
print("*** STRIPE DATA: (data) ***")
returns:
*** STRIPE DATA: Optional(147 bytes) ***
Hi @fleet tapir can you use debugPrint() ?
Hello, yes it returns the same
Can you print the json object instead? so that we can see the unserialized data.
Having trouble finding where I can put that one...
That means the json deserialziation is unsuccessful.
Can you do a debugPrint(response) after the line response.statusCode ==200 ?
<NSHTTPURLResponse: 0x60000138c060> { URL: https://snow-unique-fire.glitch.me/checkout } { Status Code: 404, Headers {
"Content-Length" = (
147
);
"Content-Type" = (
"text/html; charset=utf-8"
);
Date = (
"Wed, 25 Jan 2023 04:50:31 GMT"
);
"content-security-policy" = (
"default-src 'none'"
);
"x-content-type-options" = (
nosniff
);
"x-powered-by" = (
Express
);
} }
The status code is 404, which means your app is unable to communicate with your server
Right. Is the url formatted incorrectly? I'm not sure what it should be, the Stripe doc I linked above doesn't specify for testing the Glitch part
If I remove the /checkout part, it returns:
<NSHTTPURLResponse: 0x6000023abb00> { URL: https://snow-unique-fire.glitch.me/ } { Status Code: 200, Headers {
"Accept-Ranges" = (
bytes
);
"Cache-Control" = (
"public, max-age=0"
);
"Content-Length" = (
755
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
Date = (
"Wed, 25 Jan 2023 04:53:07 GMT"
);
Etag = (
"W/\"2f3-183258bbb50\""
);
"Last-Modified" = (
"Sat, 10 Sep 2022 03:57:38 GMT"
);
"x-powered-by" = (
Express
);
} }
But still prints Failed to retrieve publishableKey
OK, I see the problem. You should make a POST request instead of GET.
request.httpMethod = "POST"
Hmm, the sample app uses GET : https://github.com/stripe-samples/accept-a-payment/blob/main/custom-payment-flow/client/ios-swiftui/AcceptAPayment/AcceptAPaymentApp.swift
Also, if I change to post I get:
But your glithe server accepts POST request. I just sent a POST request to https://snow-unique-fire.glitch.me/checkout and I can see the response successfully.
Isn't the init trying to retrieve the publishable key from the server though? Rather than post?
Ah, that might have worked... I put checkout back into the URL...
Yes, and I see you changed the url from config to checkout, and therefore the error.
btw your glitch server doesn't accept a GET config request (https://snow-unique-fire.glitch.me/config)
Yes, this is retrieving the publishable key now!
Yeah, that config was from the sample app, but the Glitch server by default creates checkout
I'm not sure how you setup the glitch server, but you should follow the instructions here and use the correct server https://github.com/stripe-samples/accept-a-payment/tree/main/custom-payment-flow
There wasn't any setup to it. I just clicked the link from the Stripe doc I linked above
I don't know anything about Glitch servers, I assumed it was just temporary testing space. Can I actually use this as my backend?
That could an example server for a different integration.
I'd suggest just follow the information in the same repo (i.e., the link that I sent you earlier).
Thank you - where would be the easiest/path of least resistance to set that up?
https://github.com/stripe-samples/accept-a-payment/ you are already looking at it. This repo contains all the code (backend + frontend) that you need to build your first Stripe integration.