Node.js dev issue

All,

Another migration issue going from sandbox to production. I’ve used the quickstart tutorial to build a payment. After many issues I’m finally able to call my server with with the following url’s:

http://myServer:3000/card-payment
http3://myServer:3000/card-payment

When using http I’m getting ‘applicationId and/or locationId is incorrect’. I get that, it probably needs the https & it won’t work anyway (I assume). When I try https I’m getting SSL errors, on firefox it’s SSL_ERROR_RX_RECORD_TOO_LONG and on chrome ERROR_SSL_PROTOCOL_ERROR. I’m certain this has to do with the communication between my server & square. The question is, what and why is this not working? Has anyone, I mean anyone, successfully used node.js for web payments? This is really looking like it’s not possible.

Thanks in advance,

Jeff

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Troubleshoot In-App Payments SDK Problems
In-App Payments SDK Quickstart
In-App Payments SDK Quickstart

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

The error you’re encountering is likely due to the fact that your server is not properly configured to handle HTTPS requests. When you switch from HTTP to HTTPS, you need to ensure that your server is set up with a valid SSL certificate and is configured to handle HTTPS traffic.

In the Square In-App Payments SDK Quickstart guide, it is mentioned that you need to deploy your server and it provides instructions to deploy a Heroku server. Heroku automatically handles the SSL certificate for your server, which allows it to process HTTPS requests.

If you’re not using Heroku or a similar service, you’ll need to manually set up an SSL certificate for your server. This process can vary depending on your server setup, but generally involves obtaining a certificate from a Certificate Authority and configuring your server to use this certificate for HTTPS traffic.

Once your server is properly set up to handle HTTPS, you should be able to make requests to the ‘https://myServer:3000/card-payment’ endpoint without encountering SSL errors.

As for the ‘applicationId and/or locationId is incorrect’ error, make sure you’re using the correct application ID and location ID for your Square account. These can be found in your Square Developer Dashboard.

Lastly, yes, it is definitely possible to use Node.js for web payments with Square. The issues you’re encountering are related to server configuration and not specific to Node.js or Square’s APIs.

This answer was reviewed by @Bryan-Square.

What’s the application ID you have configured? :slightly_smiling_face:

Bryan,

It’s

EAAAlwzmth6K5-DQyEFy1E2ZGEVOeYVr6bdw7gx8j4BFqBdUOwHpax_1MUvZMVDK

Thanks,

Jeff

Bryan,

The web server does successfully handle https calls and has a valid certificate. I handle web payments through Quickbooks and it works with them so it’s definetly not a server issue here. I think, although I’m not a node guru it has something to do with how the https is communicating with the ‘http’ listener when we setup the node listener. The line ‘micro --listen tcp://0.0.0.0:3000’ listens on http & not https which might cause the issue. Again, I’m not sure but that might be the issue. I keep getting emails regarding in-app purchases and Heroku servers but that is not applicable to what I"m trying to do.

Thanks,

Jeff

Are you available to jump on a call to troubleshoot? :slightly_smiling_face:

Yes, I am available, do you want to schedule a time or just call me? 512.689.1586.

Thanks,

Jeff

Bryan,

You can reach me at the number I entered above. After much moaning & groaning I think I’m getting pretty close. The CC form shows & I can enter in my CC & the invalid CC shows up & it looks OK at that point. However, I’m getting and error on:

POST ‘https://pci-connect.squareup.com/payments/mtx/v2’ NS_BINDING_ABORTED

When migrating to production I’m using:

I notice version difference between but when I try to use v2 in the script it doesn’t work. I checked my code and I’m not importing the pci-connect so I’m assuming it’s something going on behind the scenes. Any insight into getting this resolved is greatly appreciated.

Thanks,

Jeff

Can you reach out in this form and ask for me. I’ll then be able to send you a link to the meet. :slightly_smiling_face:

Bryan,

I responded to your request. You had sent an email to Katie and she forwarded it to me and I just sent a forward to your request. I hope you got it.

Jeff

Okay, that’s happens with our CRM. Did you get the link to the meeting? :slightly_smiling_face:

NO, can you forward it to jeff.kimmel68@gmail?

Thanks,

Jeff

Bryan,

I sent an email but I’m not sure if you received it or not as I’m not sure how they link. I tried to simplify things by creating a new site but I’m still getting the same error on /paymnet. Do you have any other insights into what might be happening?

Thaks,

Jeff

Are you able to create an independent POST request to a custom CreatePayment function that you create? The problem we saw yesterday is that the POST to CreatePayment doesn’t have the correct path. That path needs to be configured so you can make the server side request to Square. :slightly_smiling_face:

Bryan,

The real problem is the CreatePayment is not available at all from a secure site so no I cannot. Nobody can. CreatePayment is on the node.js server running/listening on tcp port 3000. Node cannot listen/run on 443 because 443 is being used by the server. I can navigate to the site using ‘httpmysite:3000’ but the appID/locationId displays. This is beyond frustrating because I got the sandbox working and assumed the migration was as easy as the directions but the migration directions are incorrect.

Thanks,

Jeff

Are you able to remove that aspect of the Quickstart and create your own custom POST to your server side that calls CreatePayment? :slightly_smiling_face:

Bryan,

I was thinking about doing that but I’m a little gun shy at the moment. In server.js we have this:

async function createPayment(req, res) {

What are the contents of req & res. In looking at the code, I think req is the object but I don’t really see where we’re using res or what it is. Is it fairly safe to say I can just copy/paste that function into my js code?

Thanks,

Jeff

The req and res objects in a Node.js/Express server context are part of the Express framework and are used to handle HTTP requests and responses.

Understanding req and res

  • req (Request Object):
    • This object represents the HTTP request and contains data sent by the client to the server. It includes properties like req.body (for POST request data), req.query (for query string parameters), req.params (for URL parameters), and many others.
    • In the context of a payment function, req.body is typically used to access the payment data sent from the client, such as the sourceId, amount, and currency.
  • res (Response Object):
    • This object represents the HTTP response that the server sends back to the client. It includes methods like res.send(), res.json(), res.status(), and others to send data back to the client and set the HTTP status code.
    • In a payment function, res is used to send success or error responses back to the client, often in JSON format.

Example Usage in createPayment

Here’s an example of how req and res might be used in a createPayment function:

async function createPayment(req, res) {
  const { sourceId, amount, currency } = req.body;

  try {
    const response = await client.paymentsApi.createPayment({
      sourceId: sourceId,
      idempotencyKey: new Date().getTime().toString(), // Unique key for each payment
      amountMoney: {
        amount: amount,
        currency: currency,
      },
    });

    // Sending a success response
    res.status(200).json({ success: true, payment: response.result.payment });
  } catch (error) {
    console.error('Error creating payment:', error);

    // Sending an error response
    res.status(500).json({ success: false, error: error.message });
  }
}

Can You Copy/Paste the Function?

  • Server-Side Context: The createPayment function is designed to be used in a server context, where req and res are provided by the Express framework. You cannot directly copy/paste this function into client-side JavaScript because the client-side does not have access to these objects.
  • Client-Side Context: On the client side, you would typically gather data (like sourceId, amount, and currency) and then make a network request (e.g., using fetch) to your server, which would then handle the payment logic with the createPayment function.

Summary

  • Server-side: Keep the createPayment function on the server, where it can handle requests and send responses using req and res.
  • Client-side: Use JavaScript to collect payment information and send it to the server using an HTTP request (like fetch).

If you need to integrate this functionality into a new server or adjust it, ensure that the server environment is set up to handle HTTP requests and responses, and that you have the necessary access to Square’s API. :slightly_smiling_face:

Bryan,

This line:

Client-side: Use JavaScript to collect payment information and send it to the server using an HTTP request (like fetch).

Is simply impossible. The client side context javascript (in https) cannot call the server side context (http). It just cannot be done. Either the server needs to change to SSL (impossible as far as I can tell) or the javascript side needs to be able to handle http (which is also not possible, as far as I can tell). Or we somehow disable the CORS errors that you get when calling http from https. What you’re telling me to do is simply not doable.

Thanks,

Jeff