#RGRTHAT - api connection

1 messages · Page 1 of 1 (latest)

indigo pineBOT
lean notch
#

Could you share more context here?

chilly oak
#

This is with the production keys

#

Im trying to register a user on my domain

#

But when i register, i get error from the request saying

#

«An error occurred with our connection to Stripe.»

#

So it seems like my nodejs in production mode struggle to connect to stripe

versed cliff
#

Hi 👋

I'm stepping in as @lean notch needs to go soon.

chilly oak
#

👌🏽

versed cliff
#

Where is this code being run from?

chilly oak
#

Kubernetes. Dockerized container

versed cliff
chilly oak
#

Sure

#

I want to note that i recieved an email a few days ago, about me having to update website to be able to do production. But i have 8 days until stripe disable prod keys it says in dashboard. But i got this email cuz i want updating the website so it was temporarily in maintanance.

#

This shouldnt impact the connectivity?

#

let me check the stripe reachability

#

here i same error message from github

#

@versed cliff im unsure how i am supposed to be able to run that script inside the container

versed cliff
#

It's a shell script. You should be able to run it inside any container with a bash, zsh, or sh shell

chilly oak
#

yeh but how do i access the bin from inside

#

oh git url?

versed cliff
#

No, you need to download the test.sh script and run it

#

inside your container

chilly oak
#

doesnt work

versed cliff
#

Is that where you stored it? Is there a reason it's hidden? I've run this script from inside a Docker container but I was ssh'ing into the running container.

chilly oak
#

i went into the container now

#

but ofcourse the test.sh script is not available from within the container?

#

as the paths is relative to the container root

#

not computer root

#

is it possible to run a script thats outside of the directory of running container

versed cliff
#

I wouldn't know. These aren't my containers

#

I usually map some external directory to the internal state of my containers so I can add scripts to them. However, i don't know how your containers are configured

chilly oak
#

@versed cliff im thinking about my cors

#

is the stripe url differnet in production mode?

#

maybe i need to whitelist the stripe request in cors for production

#

if stripe has a different url in prod

#

everything works perfectly in dev

versed cliff
chilly oak
#

all i can think of is cors

versed cliff
#

I'm still wondering if you can even reach the Stripe API from inside the containers. The test.sh script from that GH repo does a number of tests to figure out if you can reach Stripe resources

#

That's why I was hoping you could run it from where your Stripe code is running

chilly oak
#

i will create a new image with the script inside

#

then run it.

versed cliff
#

RGRTHAT - api connection

chilly oak
#
Checking os...
OK: os check
#
Checking route...
OK: route check
#
< 
* Connection #0 to host api.stripe.com left intact
OK: curl_https check
#

everything seems fine

#

wierd

#

I sent u a link above. They suggest to add retries parameter but idk

versed cliff
#

Okay great, that eliminates the Kubernetes network config as a potential issue

chilly oak
#

yeh

#

it must be that the connection times out or something

#

maybe the connection between node.js and stripe is too slow

#

idk

versed cliff
#

That would present as a different error, I think

#

So what line in your code throws the exception?

#

Is it initializing the NodeJS Stripe instance?

#
const Stripe = require('stripe');
const stripe = Stripe('sk_test_XXXXXXXXX');
chilly oak
#

the first request i have tried to do that uses stripe is when i register a account on my domain

#

In the /register request i do ```js
const customer = await stripe.customers.create({

#

thats the first stripe function used

#

I import stripe ontop of file like so```js
const stripe = require("stripe")(process.env.STRIPE_SECRET, {
apiVersion: "2022-08-01",
});

versed cliff
chilly oak
#

let me check

#

wtf the api havent seen request since 25 okt

#

i removed all filters

versed cliff
#

Well if you are getting a Cannot connect error then the requests aren't even making it to our servers. That's why I'm thinking it's a networking issue. But you ran the reachability script just fine.

chilly oak
versed cliff
#

I'm wondering if you could test something out for me. Can you ssh into a running container and run terminal commands?

chilly oak
#

it fails to connect

#

yup

versed cliff
chilly oak
#

the charge response is in terminal too

#

so the connection to stripe within the container is not a problem

#

something must be wrong with the api keys?

#

have stripe disabled my production keys?

#

I dont think stripe have disabled the prod api, cuz how else am I going to make a functioning website without the apis working

versed cliff
#

Stripe hasn't disabled the keys but you will want to provide some extra info there soon.

chilly oak
#

we have eliminated some possible causes for this, but i cant understand why it shouldnt work

versed cliff
#

I agree. Can you provide the exact text of the error you receive when running the NodeJS server?

chilly oak
#

I have the whole request inside a trycatch

#

and then i print the error.message to server

#

the error.messge is ```js
«An error occurred with our connection to Stripe.

#

I can confirm that im able to connect to stripe from "mount" from frontend

#

when i goto my domain, the stripe post request is 200

#

i dont do any stripe requests from "mount", i only have stripe request in certain pages like checkout, but it seems like stripe does a request globally

#

i have to use my Google chrome browser, cuz brave doesnt work

#

in brave i have adblocker and stripe doesnt work

versed cliff
#

That's a request to m.stripe.com. I'm not sure exactly what that is for but we do make some requests more widely to catch attempted fraud

#

Yeah, that probably is blocking some of those domains I shared earlier.

chilly oak
#

but anyways that request have nothing to do with the connection in nodejs

#

well this error must come from the customer.create though

#

cuz if stripe was not able to connect initially when i imported it, it would throw error wouldnt it and server would crash

#

this error only happens when i try to register user

livid bobcat
#

creating a customer happens server-side

#

m.stripe.com is used for analytics client-side and is irrelevant in that case

chilly oak
#

yeh

livid bobcat
#

We're going to need a lot more details than this unfortunately

chilly oak
#

What ya need

#

should i try to generate new api keys?

#

for prod

#

@livid bobcat

#

i ran that curl connectivity check earlier

#

so the connectivity issue is not issue

#

the issue must be that the connection is too slow?

#

idk

livid bobcat
#

I'm sorry you haven't really given much that would be actionable yet

#

and no, rolling API keys would have nothing to do with it as long as you use the ones you see in your account

#

Like you mention docker and such. Does it work just on your server? Can you make a direct API request to our servers in Test mode from there?

chilly oak
#

UPDATE!

#

I tried to run my nodejs in production mode so the server would use the stripe production key. I then tried to do the "/register" route which does the customer.create() function. AND YES. IT works

#

In other words. Its docker or kubernetes causing this.

#

must be

#

idk

livid bobcat
#

okay so that narrows things down. Something in that docker install/config is blocking outgoing requests to Stripe then

chilly oak
#

But when i ran script above that checks connectivity, everything was OK it says

livid bobcat
#

Maybe you didn't run it the way you thought? Sorry it's really hard to help with almost no information

chilly oak
#

i have the script in the directory inside the container

#

the image includes the stripe script

#

i then ran the script, and it prints out alot of info

livid bobcat
#

You should be able to run a node console anything similar and make a Stripe call in there and see what happens. And try another language like Python or PHP or even just curl and see what happens too though

chilly oak
#

i have tried using curl with a charge, like the other person suggested

#

with Dev key, that worked amazing

#

so the container is able to connect to stripe via curl

#

i did a successful charge

livid bobcat
#

So what happens when you do a simple Customer Create call with node in that docker image then?

chilly oak
#

what u mean with node? using the stripe npm package in node.js?

#

using development mode?

#

current nodejs docker image use node.js with production keys

#

i can try to build a new node.js docker image using dev keys

#

and then check if that connection works

livid bobcat
#

yes for example

#

can't you ssh or similar in your docker image and just run commands from there? Sorry I don't play with docker much myself but I assume it works like a normal remote server and you can do lots of things with the command line for example

livid bobcat
#

that's not really relevant unless many of the requests work

chilly oak
#

I created a brand new node.js server. I downloaded stripe api and used the development keys, and i create a customer on the server when it starts. I dockerize the image and run the docker image

#

it successfully creates a customer

#

so the issue is NOT docker

livid bobcat
#

I mean at this point it's still really all over the place unfortunately

#

You need to sit down and carefully debug this

#

I'm sorry, I'm just unable to help narrow things down with so little information other than it sometimes works and sometimes doesn't

chilly oak
#

@livid bobcat I dont understand what u mean, i have given ALOT of info. I have tried to exclude every possible thing that cause this connection problem

#

UPDATE: when using docker image with production keys, it manages to create customers

#

IT must be a kubernetes problem

#

maybe my cloud provider have issues trusting stripe?

#

idk

#

I use nginx ingress controller

livid bobcat
#

I mean you definitely wrote a lot of words since the start, but there's almost nothing about the real issue

chilly oak
#

I HAVE CONCLUDED WITH THE FOLLOWING

#

THE ISSUE IS KUBERNETES OR THE CLOUD PROVIDER THAT HOST MY SERVER

#

cuz its not docker

#

the docker image works perfectly fine

#

its the network connection between my server in FrankFurt Germany and stripe

livid bobcat
#

but if it was the cloud provider, you wouldn't be able to make requests in that environment, and you said it works totally fine in Test mode

chilly oak
#

im not in FrankFurt, im in a total different place in the world

chilly oak
#

how could i be that im able to send stripe curl requests within the container in kubernetes with development keys, but not use production keys

#

LET ME TRY TO USE THE CURL COMMAND WITH production keys

#

i will try to create a customer with the production key

#

inside container in kubernetes

#

it that succeeds

#

its the stripe npm library thats screwed

livid bobcat
#

I mean I can tell you it's not our "stripe npm library" that is "screwed"

#

nothing is screwed, right now this purely looks like a network issue on your end

chilly oak
#

ok so im able to curl requests within the container in kubernetes

#

so the only place im having connection issue is using stripe with the npm package

#

i dont get any errors when importing the stripe npm package and specifying key. I only get the error when trying to use the package when i run request "/register"

livid bobcat
#

I don't understand what "when I run the request /register" could mean

chilly oak
#

the error occurs when doing stripe.customer.create() in "/register" request

chilly oak
#

i call a request on frontend to that route

#

thats when the stripe error happens

livid bobcat
#

okay then you need to add logs to that code to understand what's going on

chilly oak
#

I will add a ```js
.catch((err) => {
console.log("err", err)
})

#

see if it details more errors.

livid bobcat
#

I would also clearly add logs before/after to make sure this is what's failing

chilly oak
#
console.log(
        "Hello this runs right before i run the stripe.customers.create()"
      );

      const customer = await stripe.customers
        .create({
#

i add a console.log right before

#

to see if that runs

livid bobcat
#

yeah add one right after too

chilly oak
#

Its that function that fails

#

❯ kubectl logs backend-deployment-69ff789d76-zl7cr

> backend@1.0.0 start
> node server.js

Example app listening on port 8888!
Hello this runs right before i run the stripe.customers.create()
err StripeConnectionError: An error occurred with our connection to Stripe.
    at /app/node_modules/stripe/lib/StripeResource.js:572:15
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  type: 'StripeConnectionError',
  raw: {
    message: 'An error occurred with our connection to Stripe.',
    detail: TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Authorization"]
        at ClientRequest.setHeader (node:_http_outgoing:579:3)
    (/app/node_modules/stripe/lib/net/NodeHttpClient.js:43:28)
        at makeRequest (/app/node_modules/stripe/lib/StripeResource.js:513:10)
        at /app/node_modules/stripe/lib/StripeResource.js:602:9
        at /app/node_modules/stripe/lib/stripe.js:503:7
        at /app/node_modules/stripe/lib/stripe.js:457:41
        at processTicksAndRejections (node:internal/process/task_queues:96:5) {
      code: 'ERR_INVALID_CHAR'
    }
  },
  rawType: undefined,
  code: undefined,
  doc_url: undefined,
  param: undefined,
  detail: TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Authorization"]
      at ClientRequest.setHeader (node:_http_outgoing:579:3)
      at new ClientRequest (node:_http_client:262:14)
      at processTicksAndRejections (node:internal/process/task_queues:96:5) {
    code: 'ERR_INVALID_CHAR'
  },
  
}
customers undefined
#

@livid bobcat the error occurs in the stripe.customer.create() function

livid bobcat
#

ah

chilly oak
#

I read the error and 2 things stick out to me

livid bobcat
#

see, we have the exact clear error now

chilly oak
#
  detail: TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Authorization"]
livid bobcat
#

Invalid character in header content ["Authorization"] this is all we needed from the start

chilly oak
#

yeh but i dont add any header

livid bobcat
#

sure, but that header is where the API key goes

#

so you must have some bad character in your API key value

#

so wherever you load the API key, from like an environment variable or something, that's bad/polluted

chilly oak
#

it should be correct

#
const stripe = require("stripe")(process.env.STRIPE_SECRET, {
  apiVersion: "2022-08-01",
});

const customer = await stripe.customers
        .create({
          name: username,
          email,
          metadata: {
            first_name,
            last_name,
            email,
          },
        })
        .then((res) => {
          console.log("res", res);
          return res;
        })
        .catch((err) => {
          console.log("err", err);
        });
#

let me console.log the enviroment variable inside server

#

see if the value is correct

livid bobcat
#

I mean I'm fairly certain that's your problem

#

try hardcoding the value and you'll see it works magically

#

you must have some bad characters in your variable

chilly oak
#

ok i can confirm that the private key is correct

#

i have console.log

#

and its the correct output

#

so the env variable is correct

#

@livid bobcat I checked the output of the env variable and compared it to the prod key i used to use the curl with production key

#

and its completely the same

#

the question is then... is the environemnt variable not a string?

#

is there a difference between a hardcoded string and a env variable?

#

cuz i have had issued with that before, where i have hardcoded and it works, but when i use env it doesnt work @livid bobcat

#

but its the same value, so i can see how its a difference

livid bobcat
#

I can't really tell you, this is purely something in your own system at this point

#

you think it's correct but it clearly isn't based on the crash. My guess is that you have some bad/invisible characters somewhere in the way it's being read off of the environment variable

chilly oak
#

dude I took the output from the console... i pasted the output into a notepad together with the real production key

#

then i used search to find exact match

#

and the output and the real production key is completely the same.

livid bobcat
#

Hey, I know this is frustrating, but I'm doing my best to help you here. We spent a long time doing a ton of back and forth about stripe-node being the problem when really it was right there in the error message from the start what the issue was.
I can tell you with near certainty that the problem is what you have in your variable. I know because if it wasn't that, hardcoding would not fix the problem.

#

What you need to do is log exactly the raw value in that variable because it's not what you think

chilly oak
#

@livid bobcat I do think hardcoding will solve the issue

livid bobcat
#

sure but you don't want to hardcode an API key in your code and then have it in version control and such. That's unsafe

chilly oak
#

but i want to know why a enviroment variable, when u console.log it, it gives me the completely same value as the production key

#

but its still not interpreted as the same

#

the environment variable must do something unique to the api key somehow

livid bobcat
#

I explained above twice

#

the environment variable does nothing. You think you have the right value, and you don't

#

something else, like an invisible character must be in wherever you stored/loaded that value from

#

that's what you need to debug

chilly oak
#

It worked when i hardcoded

#

@livid bobcat Idk if u are familiar with kubernetes

#

but u define my environment variables in a YML file

#

and i have to turn the ENV variables into base64 strings

#

turning the ENV variables into base64 must do something to the varible...

#

i know i have inputted correct... i have checked 3 times

#

i have inputed new key 3 times, so i know i have inputed correctly

#

so it must be the base64 encoder that does it

livid bobcat
#

ah yeah that's possible. I don't know much about kubernetes, but you should be able to debug this with some of the options I clearly outlined above to confirm the raw value is not what you expect

chilly oak
#

yeh

#

I will try the encodeURIComponent

#

md5? no clue what that is

#

hash generator

#

@livid bobcat I want to thank u ❤️

#

Sorry for being a terrible customer

#

Appreciate ur effort in trying to help

#

Im really going to check out why kubernetes does that to my variables.. cuz thats important. I need to trust that the inputs i give to my YML file will turn out correctly on the server

#

input is not always output LOL

#

especially when ur doing encoding and then decoding again

livid bobcat
#

you're not terrible or anything

#

those errors are so hard to debug 😦

chilly oak
#

i encode when i pass the ENV to YML file, then kubernetes automatically decode the variable for me

#

so it must be the decoder of kubernetes that fucks things up

livid bobcat
#

it's already hard enough on your own computer, when docker/kurbernetes or deploy steps are involved it's such a pain 😦

chilly oak
#

yeh

#

I want to thank u so much

#

❤️

#

really appreciate it

livid bobcat
#

Of course! I'm glad we were able to narrow it down

chilly oak
#

yes

#

i did a trycatch, but i only outputed the error.message

#

which is a mistake i see

#

i should output the whole error

#
try {
    const paymentIntent = await stripe.paymentIntents.create(args);
    console.log('No error.');
  } catch (e) {
    switch (e.type) {
      case 'StripeCardError':
        console.log(`A payment error occurred: ${e.message}`);
        break;
      case 'StripeInvalidRequestError':
        console.log('An invalid request occurred.');
        break;
      default:
        console.log('Another problem occurred, maybe unrelated to Stripe.');
        break;
    }
  }
```this looks like a good approach going forward