Payment API backend implementation

Hello, I am very new here, trying to embed Square payments to my web site. I use html, php, and understand the general idea. I started to implement a test from this example:

" "

It works until when CreatePayment returns an error. I am also not clear about the back-end implementation (I prefer php, ) and I am not sure how the

"const paymentResponse = await fetch(‘/payment’, "

should be implemented. It seems “/payment” refers to a file that should implement the back-end API, but I don’t see what should be in it, or what it should do.

Please advise or direct to the document which could help.
Thanks and best

The /payment is the function in our example. That function is in the server.js file of our Quickstart. Also we do have a PHP payment example. :slightly_smiling_face:

Great thanks for the info!

Hello, since our last conversation, I purchased a VPS package, installed node.js, npm and tried the setup again. Still having the same problem, though I think node.js us running. The app fails right after const paymentResponse = await fetch(‘/payment’,
so the backend is not responding right. Is the directory structure correct? Could you please check the attached screenshots? I think I am pretty close, and any help would be very much appreciated. Thanks! (node - v: 20.9.0, npm -v 10.2.4)

Also, the last screenshot shows the token etc right inside “body” (document.write to debug) before it is passed to the backend, so the front end seems to be working. Thanks

Is the CreatePayment function in the same directory? The error your getting means the function that process the payment is found. :slightly_smiling_face:

OK, I think this is conflicting with the web site that I already have. I erased all the node modules then put everything in web-payments-quickstart as if that is a stand alone web site, then both development and sandbox works if I run it from a browser on the VPS. If I go to which is IP address where the actual web site resides, it fails with "application ID and/or location ID incorrect. It seems one of the links in web-payments-quickstart ends up in the root of the web site, and this way doesn’t find the .env files.

I think all is related to the directory structure and how it should be integrated with an actual web site etc. Could you give some ideas about that please? Should I update the apache configurations so that the root of the web site is inside the web-payments-quickstart for example? How should I arrange the structure of the square portion with the web site that is already there?

One more question: At the final implementation with the web site, payments etc, npm should always be running? After the npm start command, it should never stop and keep running to catch any payments that might come from a customer. Is my understanding correct?

Thanks a lot!

Hi again, I also implemented the php-chekout example as a separate project, on a separate web site, again it works if I run it on the server, with php -S localhost:8888
but if I go to the web site and run it there (same folder structure) the index.php appears and checkout.php fails with 500 error. I actually like the php structure better in a way, but please advise as to how to make this setup work for a real life web site. Thanks and best

At this time the Quickstart on local. Running it on your network isn’t currently available. Deploying a Node.js application involves several steps, and the exact process can vary depending on your hosting environment and deployment preferences. Here’s a general guide to deploying a Node.js server:

1. Prepare Your Node.js Application:

Ensure your Node.js application is ready for deployment. This includes:

  • Dependencies: Make sure your package.json file lists all the necessary dependencies, and run npm install to install them.
  • Environment Variables: If your application relies on environment variables, make sure they are properly configured for your production environment.

2. Choose a Hosting Provider:

Choose a hosting provider that supports Node.js. Popular choices include:

  • Heroku: Easy to use and deploy. Supports Node.js out of the box.
  • AWS (Amazon Web Services): Provides a wide range of services, including EC2 for virtual servers and AWS Lambda for serverless applications.
  • DigitalOcean: Offers virtual private servers (Droplets) that can be configured for Node.js applications.
  • Google Cloud Platform (GCP): Provides services like Compute Engine and App Engine for deploying Node.js applications.

3. Set Up Hosting Environment:

Depending on your hosting provider, set up the environment for your Node.js application. This may include configuring:

  • Environment Variables: Set any environment variables needed for your application.
  • Ports: Ensure that your application is configured to listen on the correct port (often port 80 for HTTP or 443 for HTTPS).

4. Choose a Process Manager:

Choose a process manager to keep your Node.js application running. Popular choices include:

  • PM2: A production process manager that makes it easy to run Node.js applications in a production environment.

5. Configure Deployment:

Configure your deployment based on your hosting provider. This might involve:

  • Creating Deployment Scripts: Write scripts or configuration files that specify how your application should be deployed.
  • Setting Up Continuous Integration (Optional): Consider setting up continuous integration tools (e.g., Jenkins, Travis CI) to automate the deployment process.

6. Deploy Your Application:

Deploy your Node.js application using the chosen deployment method. This might involve:

  • Uploading Your Code: Push your code to a repository connected to your hosting provider.
  • Running Deployment Scripts: Execute scripts or commands to deploy your application.

7. Monitor and Scale:

Set up monitoring to track the performance of your application. Depending on your needs, consider scaling options to handle increased traffic.

Remember that the specifics of deployment can vary based on your hosting provider and deployment strategy, so refer to the documentation provided by your chosen platform for detailed instructions. :slightly_smiling_face:

It works, yay! :smile: :smile: I use Hostinger, and after setting up the apache2 server with reverse proxy, and starting the app in the web-payments-quickstart folder with pm2, payment goes through from the “website”! It is cool when it works. Thanks for the information!

Awesome! Glad to hear its working! :slightly_smiling_face: