#RGRTHAT - api connection
1 messages · Page 1 of 1 (latest)
Could you share more context here?
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
Hi 👋
I'm stepping in as @lean notch needs to go soon.
👌🏽
Where is this code being run from?
Kubernetes. Dockerized container
Can you try running this script and letting me know if/where it fails?
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
It's a shell script. You should be able to run it inside any container with a bash, zsh, or sh shell
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.
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
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
@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
Nope, still https://api.stripe.com
But if you have firewall considerations you can see all the potential Stripe domains and IP addresses here:
https://stripe.com/docs/ips
all i can think of is cors
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
RGRTHAT - api connection
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
Okay great, that eliminates the Kubernetes network config as a potential issue
yeh
it must be that the connection times out or something
maybe the connection between node.js and stripe is too slow
idk
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');
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",
});
And that throws the error? Do you see any request in your Stripe Account logs?
Here's how you can find a request ID: https://support.stripe.com/questions/finding-the-id-for-an-api-request
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.
yeh
I'm wondering if you could test something out for me. Can you ssh into a running container and run terminal commands?
yes
Okay, then could you try running the curl command on the right hand side of this doc? Make sure to use your test API key.
https://stripe.com/docs/api/charges/create?lang=curl
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
That worked!
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
Stripe hasn't disabled the keys but you will want to provide some extra info there soon.
we have eliminated some possible causes for this, but i cant understand why it shouldnt work
I agree. Can you provide the exact text of the error you receive when running the NodeJS server?
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
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.
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
creating a customer happens server-side
m.stripe.com is used for analytics client-side and is irrelevant in that case
yeh
We're going to need a lot more details than this unfortunately
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
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?
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
okay so that narrows things down. Something in that docker install/config is blocking outgoing requests to Stripe then
But when i ran script above that checks connectivity, everything was OK it says
Maybe you didn't run it the way you thought? Sorry it's really hard to help with almost no information
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
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
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
So what happens when you do a simple Customer Create call with node in that docker image then?
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
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
that's not really relevant unless many of the requests work
UPDATE
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
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
@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
I mean you definitely wrote a lot of words since the start, but there's almost nothing about the real issue
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
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
im not in FrankFurt, im in a total different place in the world
yeh thats wierd
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
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
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"
I don't understand what "when I run the request /register" could mean
the error occurs when doing stripe.customer.create() in "/register" request
I have a route called "/register"
i call a request on frontend to that route
thats when the stripe error happens
okay then you need to add logs to that code to understand what's going on
I will add a ```js
.catch((err) => {
console.log("err", err)
})
see if it details more errors.
I would also clearly add logs before/after to make sure this is what's failing
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
yeah add one right after too
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
ah
I read the error and 2 things stick out to me
see, we have the exact clear error now
detail: TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Authorization"]
Invalid character in header content ["Authorization"] this is all we needed from the start
yeh but i dont add any header
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
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
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
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
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
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.
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
try https://stackoverflow.com/a/71803539/1606729
Or try md5 on the variable/value compared to a hardcoded one you'll see it is different
@livid bobcat I do think hardcoding will solve the issue
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
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
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
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
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
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
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
it's already hard enough on your own computer, when docker/kurbernetes or deploy steps are involved it's such a pain 😦
Of course! I'm glad we were able to narrow it down
make sure to improve your code to always log every API error: https://stripe.com/docs/error-handling
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