Integrate Apple Pay - Server Setup [Optional]

📘

Implementation Notes:

This example integration was successfully completed using a Nodejs/Express on an Apache/Ubuntu server.

This is not a recommendation, just an example for those that have never built a server before.

📘

Implementation Note 2:

Steps may differ for different companies and server setups. If you have a remote server already setup, skip Server Setup from scratch and proceed to Apple Pay implementation.

Server Setup from scratch (skip this if you already have a server):

  1. Create a DigitalOcean droplet (your remote server)

    1. Go to DigitalOcean – The developer cloud

    2. Sign up for an account

    3. Once your account has been created, click “New Project” on the lefthand side

    4. Name your project, add a description, and choose what it’s for

    5. Click Create Project

    6. In “Move Resources into your Project”, press Skip for Now

    7. Click Create → Droplets

    8. Choose a region and datacenter (it’s recommended you choose regions closest to you to reduce latency)

    9. For Choose Image, select OS → Ubuntu

    10. For Version, choose 22.04 x 64

    11. For Droplet Type and CPU Options, consider your use case to decide which options to choose. For example, if you are just doing a proof of concept, Basic and Regular with SSD ($4/mo) would suffice. Consult this page for more reference on choosing.

    12. For Choose Authentication Method, select either SSH Key or Password

      1. For the purposes of this demo, Password was selected

      2. Add any additional recommended options as needed for your use case

      3. Finalize Details of your droplet. It would help if you change the hostname to something unique that you can remember

      4. Click Create Droplet. Your droplet will take a few minutes to be created.

      5. Once created, you will see your droplet with its IP address on your dashboard

      6. On the right side of your droplet, click the … → Add a Domain

      7. Add the domain that you purchased from your domain vendor. You need to do this so that any changes you make on your remote server will be reflected on your website.

      8. Your droplet / remote server is now ready!

  2. Log into your droplet

    1. In order to log into your remote server securely, you need to use SSH (Secure Shell) protocol. This can be accomplished on terminal / command line by entering ssh root@

      1. You can get your IP address from your droplet’s dashboard
        The command to enter via terminal:

      2. Terminal will ask “Are you sure you want to continue connecting?”. Enter yes then click return

      3. Enter the password you entered for step 1L and click return

      4. You’ll know you logged in when you get the “welcome to Ubuntu” message.
        Additionally, your terminal should show you logged in as a root user on your instance

      5. You’re now logged into your droplet!

  3. Install Apache Web Server onto Ubuntu - reference

    1. Apache will enable us to deliver web content through the internet. Without Apache, we won’t be able to serve a web page with our Apple Pay button.

    2. Start by updating the local package index to reflect the latest upstream changes:
      sudo apt update

    3. Then, install the apache2 package:
      sudo apt install apache2

    4. It will ask if you want to continue. Type Y and click Return

    5. Now let’s adjust the firewall. By default, Ubuntu distribution comes with a firewall configuration tool called Uncomplicated Firewall (UFW). Enter the following command to see the UFW application profiles:
      ufw app list

    6. You’ll want to enable OpenSSH so that you can SSH into your server. You can do that by running:
      sudo ufw allow 'OpenSSH'
      Note: ENSURE YOU ENABLE THIS FIRST OR YOU WILL BE LOCKED OUT OF YOUR SERVER ONCE YOUR UFW TURNS ON

    7. Next, the 3 profiles available for Apache:

      1. Apache: This profile opens only port 80 (normal, unencrypted web traffic)
      2. Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
      3. Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
    8. We don’t have SSL configured yet, so we only need to allow traffic on port 80. Enable port 80 as follows:
      sudo ufw allow 'Apache'

    9. Verify the change by running:
      sudo ufw status

    10. At the end of the installation process, Ubuntu 20.04 starts Apache. The web server should already be up and running.
      Check with the systemd init system to make sure the service is running by typing:
      sudo systemctl status apache2

    11. As confirmed by this output, the service has started successfully. However, the best way to test this is to request a page from Apache.

      You can access the default Apache landing page to confirm that the software is running properly through your IP address. You can get your server’s IP from your droplet dashboard on DigitalOcean.
      However, you can also get it from the command line.

      Try typing this into your terminal:
      hostname -I

      You will get back a few addresses separated by spaces. You can try each in your web browser to determine if they work.

    12. When you have your server’s IP address, enter it into your browser’s address bar:
      <http://your_server_ip>
      You should see the default Ubuntu 20.04 Apache web page:

    13. Your server is now running and can serve a webpage via http://<your IP address> ! If you want to change the page, you need to modify the index.html file in the root of your website. You can access it by changing directory (cd) into the following folder:
      cd /var/www/html

    14. We can see the root index.html if we run ls:
      Keep note of this directory and index.html file. We will need both when we implement Apple Pay later.

  4. Install Node.js - reference

    1. Node.js is a JavaScript runtime environment that runs on a JavaScript Engine and executes JavaScript code outside a web browser. It will allow us to write and execute our backend in Javascript.
    2. We will follow option 3: installing Node using the Node Version Manager (nvm)
    3. Install the nvm script to your user (in our case, root) account by running:
      curl -o- <https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh> | bash
    4. Use the nvm script by sourcing your .bashrc file. Run:
      source ~/.bashrc
    5. Now you can ask which versions of Node are available with:
      nvm list-remote
    6. You can install a version of Node by typing any of the release versions you see. For instance, to get version v16.17.0, you can type:
      nvm install v16.17.0
    7. You can see the different versions you have installed by typing:
      nvm list
    8. You now have Node.js installed!
  5. Install npm

    1. npm is a package manager that allows you to add dependencies to your project. It should be included with your Node.js install. You can verify if npm is installed by running:
      npm -v
    2. A version should be returned like below:
  6. Install Express

    1. Express is a back end web application framework for Node.js and will help simplify writing our API.
    2. We need to install Express with npm. And this install needs to be in the root directory of our website. First, navigate to your website’s root by running:
      cd /var/www/html
    3. You need to setup a new or existing npm package. Do so by running:
      npm init
      You will be prompted on creating your package.json file. This will contain info such as package name, author, licenses, and dependencies.
    4. Enter your package name.
    5. Enter version number. You can hit Return and the default will be chosen.
    6. Define entry point. This will be the Javascript file that is run by default.
      1. For this guide, index.js will be the entry point
    7. Define your test command.
    8. Define the Git repository, if you have one.
    9. Define keywords
    10. Define author (yourself)
    11. Define license
    12. It will ask if the package.json looks OK? Enter yes and click Return
    13. Now that you have a package.json, you can install dependencies! We can install express.js by running:
      npm install express
    14. Verify express is installed by checking your npm local packages:
      npm list
    15. Express is now installed!
  7. Install Axios

    1. Axios is a Javascript library used to make HTTP requests from node. js or XMLHttpRequests from the browser and it supports the Promise API that is native to JS ES6. Axios will help us write the POST request when we request a merchant session object from Apple Pay servers.

    2. Install using npm:
      npm install axios

    3. Confirm using:
      npm list

    4. You can also see your dependencies by inspecting your package.json file. Run:
      vim package.json

  8. Setup your Start script

    1. You can define scripts for npm in your package.json. We want npm to execute our index.js file with nodejs every time we run npm start. Thus, under scripts, add the following line:
      "start": "node index.js"

    2. Save your changes by entering:
      :wq

    3. Click Return

    4. Enter npm start

    5. You’ll see you get an error. This is because we don’t have an index.js defined!

    6. Create a new index.js file. Start by executing:
      vim index.js

    7. Within your now empty file, place the following line of code:
      console.log('Hello world!');

    8. Save your changes by entering:
      :wq

    9. Click Return

    10. Now run npm start. You now should see an output.

    11. You now have your start script setup! We will add any Express server code to our index.js file.