bitpay / nodejs-bitpay-client

NodeJs implementation for the BitPay Cryptographically Secure RESTful API
MIT License
48 stars 42 forks source link

Create config file #99

Closed omariosman closed 10 months ago

omariosman commented 10 months ago

When I run: node node_modules/bitpay-sdk/dist/setup/BitPaySetup.js I encounter this error:

Requesting tokens... 

Requesting Merchant token... 

invalid json response body at https://test.bitpay.com/tokens reason: Unexpected token < in JSON at position 0
TypeError: Cannot read properties of undefined (reading 'data')
    at requestMerchantToken (/home/omarosman23/Documents/Fuze/Fuze-System/functions/node_modules/bitpay-sdk/dist/setup/BitPaySetup.js:192:25)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async requestTokens (/home/omarosman23/Documents/Fuze/Fuze-System/functions/node_modules/bitpay-sdk/dist/setup/BitPaySetup.js:244:13)
    at async /home/omarosman23/Documents/Fuze/Fuze-System/functions/node_modules/bitpay-sdk/dist/setup/BitPaySetup.js:166:21

When I tried to debug the problem I found out that the result variable at the following code snippet does not have a "data" property So it throws an error

result = result.data[0];
merchantToken = result.token;
merchantPairCode = result.pairingCode;
await sleep(2000);

I tried to solve the error by shifting up these lines inside the .then block so the config file was generated with a private key but with empty Api tokens. The pairing codes are not printed to the console as well.

bobbrodie commented 10 months ago

@omariosman could you share the following?

I've been testing this morning with SDK v5.0.1 without issue generating tokens so those will help us look into it. Thanks!

omariosman commented 10 months ago
bobbrodie commented 10 months ago

Thanks, @omariosman, that's helpful.

I've been testing with Node 16 and 18 but have yet to run into an issue.

omariosman commented 10 months ago

Thanks @bobbrodie for your patience! I appreciate it

REDACTED

bobbrodie commented 10 months ago

Awesome, thanks again for stepping through this @omariosman. One last thing here. When we see Unexpected token <, that usually means the response was starting with HTML, and the opening character is < because it's the start of <!DOCTYPE html> or <html> which is definitely unexpected.

Are you able to log the response body out to your console and drop it here? That will help us understand a bit more about what you're receiving.

omariosman commented 10 months ago

Sure thing! @bobbrodie. Here are the values of the response and result variables respectively.

REDACTED

bobbrodie commented 10 months ago

Hey @omariosman thank you so much -- I've redacted the images just so nobody takes any of the info and spams with your tokens. It looks like the full response wasn't captured, but we can keep going with additional steps. We should be able to sort this out with you.

I'm wondering if there might be a network issue, and we'll need some info that would be best to keep off GitHub comments just so if we get a good response, nobody will claim the token.

If you can update your BitPaySetup.js file to log the full text of the response from both token generation functions (requestMerchantToken and requestPayoutToken), then reach out to integrations@bitpay.com, we can work with you on this. Please mention Node.js SDK GitHub issue #99 and that we've been talking and I'll help pick it up from there.

Thanks!

omariosman commented 10 months ago

We got the problem now Bitpay is not supported in our region so we had to open a VPN to get the correct response

The response that we were getting before was actually the html page of the Access Denied page, as you said:

Unexpected token <, that usually means the response was starting with HTML, and the opening character is < because it's the start of <!DOCTYPE html> or which is definitely unexpected.

Thank you for your time in debugging this issue!

bobbrodie commented 10 months ago

I was curious if that might be it, thank you so much for letting me know! If you need anything else please feel free to reach out :)