#willbond

1 messages ยท Page 1 of 1 (latest)

dense lotusBOT
wise furnace
#

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

dawn stratus
#

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

wise furnace
#

Right but for some reason our API thinks that is JSON

dawn stratus
#

If it helps, the raw from the StripeAPIError says: exception: SyntaxError: Unexpected token s in JSON at position 58

wise furnace
#

Oh

dawn stratus
#

I will try without the .toString() method in the VPS, but it doesn't make much sense since the local is working

wise furnace
#

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?

dawn stratus
#

Just the customer ID haha I'm sorry

wise furnace
#

Haha okay just making sure

dawn stratus
#

and its complete too, I just hid the example customer info with the underscores

wise furnace
#

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)

dawn stratus
#

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

wise furnace
#

Sounds good

dawn stratus
#

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

wise furnace
#

Woof

#

Do other methods work?

#

Other Stripe methods that is

dawn stratus
#

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", });

wise furnace
#

Don't think that should change anything

dawn stratus
#

Also the stripe.customers.retrieve() works locally so I don't think its a problem with the SDK

wise furnace
#

Yeah this sounds specifically like your VPS is doing something the Stripe API doesn't like

dawn stratus
#

Do you think it has something to do with the apiVersion?

#

Maybe a newer or older wouldn't do it

wise furnace
#

No it shouldn't and you are using the newest

#

I'm thinking

#

But coming up empty at the moment

dawn stratus
#

So am I, this is kind of crazy

wise furnace
#

I have to step away but my teammate is stepping in... they might have some ideas

dawn stratus
#

Thanks for the help @wise furnace !
if you come to think at any ideas please message me, I will be happy to read it

robust oar
#

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?

dawn stratus
robust oar
#

Undefined? What the heck...

dawn stratus
#

Hahah, I know

robust oar
#

And position 58...

#

Wow. Let me think about this for a bit...

dawn stratus
dawn stratus
# robust oar And position 58...

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

robust oar
#

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?

dawn stratus
#

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

robust oar
#

Oh, so you're not catching the error? One sec, let me test something...

dawn stratus
#

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

robust oar
#

Can you try this instead of the await approach you have now?

stripe.customers.retrieve(subscription.customer.toString(), function (customer) {
    console.log(customer);
});
dawn stratus
#

Thanks for the suggestions, I will try it and BRB

robust oar
#

Hoping to at least get some response headers out of that. ๐Ÿ˜…

dawn stratus
#

customers.retrieve(id: string, params?: Stripe.CustomerRetrieveParams, options?: Stripe.RequestOptions)

robust oar
#

This is using the older callback approach.

#

It may not be compatible with TypeScript.

dawn stratus
#

yeah, I'm getting a "No overload matches this call."

robust oar
#

Hm. Okay, let me look through the library's code, hang on...

dawn stratus
#

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_); });

robust oar
#

Hm. It may not be set at all due to the nature of the error. That's so strange though...

dawn stratus
#

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

robust oar
#

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.

dawn stratus
#

Oooh I get it

robust oar
#

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.

dawn stratus
robust oar
#

Headers! And they do seem to be coming from Stripe! Which is baffling! ๐Ÿ˜…

#

That's strange. It's like they're incomplete...

#

๐Ÿค”

dawn stratus
#

I'm also think that it might be something with firewalls or security appliance

dawn stratus
robust oar
#

Let's see what that response looks like.

dawn stratus
#

Ok, 10 mins

robust oar
#

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! ๐Ÿ˜…

robust oar
#

Any luck?

dawn stratus
#

oh, interesting

 {
   "error": {
     "message": "Invalid API Key provided: "sk_test************************************************************************************************Pckr",
     "type": "invalid_request_error"
   }
 }

robust oar
#

๐Ÿ˜ฎ

#

... That's not valid JSON? ๐Ÿ˜…

dawn stratus
#

I'm honestly lost hahah

robust oar
#

Wow, it's not.

dawn stratus
#

the "message" line does lack a quote

robust oar
#

Oh...

#

Well, the real issue is that the quotes around the redacted key aren't escaped.

#

Wow. Okay.

dawn stratus
#
 {
   "error": {
     "message": "Invalid API Key provided: 'sk_test************************************************************************************************Pckr'
",
     "type": "invalid_request_error"
   }
 }
#

it should be something like this

robust oar
#

I think it should actually be this:

{
    "error": {
        "message": "Invalid API Key provided: \"sk_test************************************************************************************************Pckr\"",
        "type": "invalid_request_error"
    }
}
dawn stratus
#

yeah, you're right, I just noticed

robust oar
#

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.

dawn stratus
#

well, it does have quotes on the .env, but I assume node does parse the quotes

robust oar
#

I don't think it does. Try removing the quotes.

dawn stratus
#

well, I do use quotes on my pc and its working

#

I guess the .env on the server is missing a quote

robust oar
#

Ah, it only has the leading quote?

dawn stratus
#

Damn, it was a quote all along. You found it!

robust oar
#

That was a wild ride.

dawn stratus
#

both my pc and my VPS have the KEY as STRIPE_KEY="sk_test_actualkey", except the VPS was without the finishing comman

robust oar
#

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!

dawn stratus
#

I guess in the end this is a problem with both of our APIs ๐Ÿ˜…

robust oar
#

At least we're not bored!

#

Anything else I can help you with?

dawn stratus
#

Nothing else, at least not for now. Thanks for your help @robust oar , I appreciate your concern and calm investigating this with me

robust oar
#

Happy to help!

dawn stratus
#

Also thanks @wise furnace too!

robust oar
#

I'll let them know!

#

Oh, don't forget to undo the changes you made to the Stripe library (or download a fresh unaltered copy). ๐Ÿ™‚