#phiredrop

1 messages · Page 1 of 1 (latest)

storm plazaBOT
woeful ridge
#

hello! can you share your code?

fleet tapir
#

Sure, there's a lot tho! Which would you like to see first?

woeful ridge
#

you mentioned during the start of my app during the Stripe init, it fails to retrieve my publishable key. - this section

fleet tapir
#

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

woeful ridge
#

what's the error you're getting?

fleet tapir
#

The one in the code: Failed to retrieve publishableKey from /config

#

that gets printed in my console when starting the app

woeful ridge
#

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?

fleet tapir
#

Which part are you thinking?

woeful ridge
#

what's in data for example?

fleet tapir
#

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

woeful ridge
#

what do you mean by it won't let you print it?

fleet tapir
#

I guess I don't know how to answer your question "what's in data"

woeful ridge
#

can you log data or json?

fleet tapir
#

print("*** STRIPE DATA: (data) ***")

#

returns:

#

*** STRIPE DATA: Optional(147 bytes) ***

frosty sequoia
#

Hi @fleet tapir can you use debugPrint() ?

fleet tapir
#

Hello, yes it returns the same

frosty sequoia
#

Can you print the json object instead? so that we can see the unserialized data.

fleet tapir
#

Having trouble finding where I can put that one...

frosty sequoia
#

That means the json deserialziation is unsuccessful.

#

Can you do a debugPrint(response) after the line response.statusCode ==200 ?

fleet tapir
#
<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
    );
} }
frosty sequoia
#

The status code is 404, which means your app is unable to communicate with your server

fleet tapir
#

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

frosty sequoia
#

OK, I see the problem. You should make a POST request instead of GET.

#

request.httpMethod = "POST"

fleet tapir
#

Also, if I change to post I get:

frosty sequoia
#

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.

fleet tapir
#

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

frosty sequoia
#

Yes, and I see you changed the url from config to checkout, and therefore the error.

fleet tapir
#

Yes, this is retrieving the publishable key now!

#

Yeah, that config was from the sample app, but the Glitch server by default creates checkout

frosty sequoia
fleet tapir
#

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?

frosty sequoia
#

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

fleet tapir
#

Thank you - where would be the easiest/path of least resistance to set that up?

frosty sequoia