#willbond
1 messages ยท Page 1 of 1 (latest)
Hello
Reading
Huh seems like your VPS is doing some manipulation here
My first recommendation would be to confirm that the above is the case. I would log out subscription.customer.toString() and log out type of subscription.customer.toString()
Thanks for answering! I did not log typeof subscription.customer, however I did log subscription and I am receiving a valid string (example: customer: 'cus_N7B_________2J')
Right but for some reason our API thinks that is JSON
If it helps, the raw from the StripeAPIError says: exception: SyntaxError: Unexpected token s in JSON at position 58
Oh
I will try without the .toString() method in the VPS, but it doesn't make much sense since the local is working
Wait wait wait
When you log you get customer: 'cus_N7B_________2J'?
That is what the whole subscription.customer.toString() logs?
Or subscription.customer.toString() logs just the customer ID?
Just the customer ID haha I'm sorry
Haha okay just making sure
and its complete too, I just hid the example customer info with the underscores
Right right
Hmmm
It works when you hard code the Customer ID I assume?
Does it work if you do the stringify before passing it to the Customer retrieve method?
Like let cus = subscription.customer.toString() and then await stripe.customers.retrieve(cus)
I didn't try to hard code the ID in the VPS ๐ค
Thanks for the suggestions, I will try both and BRB with the results
Sounds good
Ok, bad news ๐ฆ
On my VPS, even with a hard-coded ID it returns the same Invalid JSON error.
I did also try the stringify first - retrieve after method and got the same results.
I don't know what to try next, suggestions are welcomed
Well, the stripe.webhooks.constructEvent( /*...*/ ) does work
I'm currently only using constructEvent() and customers.retrieve() methods from stripe
I use the "new Stripe" method if this changes anything:
const stripe = new Stripe(stripeKey, { apiVersion: "2022-11-15", });
Don't think that should change anything
Also the stripe.customers.retrieve() works locally so I don't think its a problem with the SDK
Yeah this sounds specifically like your VPS is doing something the Stripe API doesn't like
Do you think it has something to do with the apiVersion?
Maybe a newer or older wouldn't do it
No it shouldn't and you are using the newest
I'm thinking
But coming up empty at the moment
So am I, this is kind of crazy
I have to step away but my teammate is stepping in... they might have some ideas
Thanks for the help @wise furnace !
if you come to think at any ideas please message me, I will be happy to read it
Hello! I'm taking over and catching up...
That's really strange. I'd love to see the raw data your server is receiving, will need to figure out if there's a way to have the Node library spit that out somewhere. Does the error tell you which part of the Stripe Node library is throwing the error? Like which file?
It's probably StripeResource.ts...
The error object you get should have several properties you can log to find out more: headers, statusCode, and requestId. Can you log those and provide the output?
This is the complete error log
Undefined? What the heck...
Hahah, I know
It actually is
Yeah, I was thinking it might be a xml return instead of json, but generally on these cases the JSON.parse error is Unexpected token < in JSON at position 0
Yeah, exactly.
After this error is thrown is there anything in customer.lastResponse by chance?
If so can you log customer.lastResponse.headers and share the output here?
I might be wrong but I think customer isn't assigned anything, because the rest of my code under the stripe.customers.retrieve() doesnยดt run
So if I do log customer.lastResponse.headers, I guess the log won't run and the last thing to run is the retrieve()
Oh, so you're not catching the error? One sec, let me test something...
I do have a general try..catch around the webhook, so I think after this JSON.parse error it returns
return res.status(400).send("Webhook Error:", ${err.message});
for the stripe API
I donยดt have a try..catch just for the stripe.customers.retrieve() tho
Can you try this instead of the await approach you have now?
stripe.customers.retrieve(subscription.customer.toString(), function (customer) {
console.log(customer);
});
Thanks for the suggestions, I will try it and BRB
Hoping to at least get some response headers out of that. ๐
Isn't this lacking a .then()? I can't pass a function to params?: CustomerRetrieveParams
customers.retrieve(id: string, params?: Stripe.CustomerRetrieveParams, options?: Stripe.RequestOptions)
yeah, I'm getting a "No overload matches this call."
Hm. Okay, let me look through the library's code, hang on...
I might try with .then() just to see if we get some headers:
stripe.customers .retrieve(subscription.customer.toString()) .then((customer_: any) => { console.log(customer_); });
Hm. It may not be set at all due to the nature of the error. That's so strange though...
What you could do if all else fails is modify StripeResource.js in the Stripe library and add console.log(headers); after this line: https://github.com/stripe/stripe-node/blob/master/lib/StripeResource.js#L157
I think we might be focusing too much in the Stripe parse error and ignoring the fact that it does works on the local environment
If it was an error in the StripeResource.js, I donยดt think it would work on my end too
I don't think it's an error there, I'm trying to figure out where the mangled response is coming from.
I think something is intercepting the request.
I want to see the response headers to determine where that response is coming from.
Oooh I get it
Like if we see headers that indicate the response actually came from Stripe I will be very surprised. ๐ I'm hoping the headers will indicate something like a firewall, security appliance, egress control, or something along those lines is causing the problem.
Headers! And they do seem to be coming from Stripe! Which is baffling! ๐
That's strange. It's like they're incomplete...
๐ค
I'm also think that it might be something with firewalls or security appliance
I'm not sure what should be on these, what is what fields are missing?
Okay, let's try this. in NodeHttpClient.js, add a console.log(response); line under this line: https://github.com/stripe/stripe-node/blob/13e955edf64b44aa2f2e90b1228b65caf33cf1a2/lib/net/NodeHttpClient.js#L97
Let's see what that response looks like.
Ok, 10 mins
Wait!
That's the wrong line!
So it would look like this:
this._res.once('end', () => {
console.log(response);
try {
resolve(JSON.parse(response));
} catch (e) {
reject(e);
}
});
I had an off by one error with the line number, sorry! ๐
Any luck?
oh, interesting
{
"error": {
"message": "Invalid API Key provided: "sk_test************************************************************************************************Pckr",
"type": "invalid_request_error"
}
}
I'm honestly lost hahah
Wow, it's not.
the "message" line does lack a quote
Oh...
Well, the real issue is that the quotes around the redacted key aren't escaped.
Wow. Okay.
{
"error": {
"message": "Invalid API Key provided: 'sk_test************************************************************************************************Pckr'
",
"type": "invalid_request_error"
}
}
it should be something like this
I think it should actually be this:
{
"error": {
"message": "Invalid API Key provided: \"sk_test************************************************************************************************Pckr\"",
"type": "invalid_request_error"
}
}
yeah, you're right, I just noticed
Wild. Okay, we need to fix this on our end and I'll get that work started now. In the meantime, at least you know why the error is happening. Looks like you've got a...
Wait.
WAIT!
Does your key have the quotes included?
Like do you have "sk_test_..." as the value instead of sk_test_...?
Or just that first " actually.
I think we're maybe spitting back what we see coming in, and we're seeing a " come in as part of the key?
But we're not escaping it correctly.
well, it does have quotes on the .env, but I assume node does parse the quotes
I don't think it does. Try removing the quotes.
well, I do use quotes on my pc and its working
I guess the .env on the server is missing a quote
Ah, it only has the leading quote?
Damn, it was a quote all along. You found it!
That was a wild ride.
both my pc and my VPS have the KEY as STRIPE_KEY="sk_test_actualkey", except the VPS was without the finishing comman
Ultimately this is a problem with the API; we are indeed not doing the right thing. I can reproduce with a raw curl command and get back invalid JSON.
So we're going to get that fixed!
I guess in the end this is a problem with both of our APIs ๐
Nothing else, at least not for now. Thanks for your help @robust oar , I appreciate your concern and calm investigating this with me
Happy to help!
Also thanks @wise furnace too!