Use a Server to Server API

Purpose: To teach you to use a server-to-server API with a common use case (e-commerce). Project Part 8 will require you to have your server send payment information to the Fakepay API, set up for this class. You will practice using this API with a sample page.

Using the Python HTTPX Library

The HTTPX library in Python sends server-to-server communications. To use it, install:

pip install httpx

and import httpx in your Python code.

How the Fakepay API Works

The Fakepay API is based roughly on an API from a common payment provider. Review the API documentation, which includes Python HTTPX instructions.

Practice API Request and Response

Based on the documentation, practice creating API authentication requests in CURL (command line). You may also use a GUI tool like Postman if desired.

Using the sample code in the documentation, practice making Python HTTPX requests. Generate authentication, order, and capture requests, using either Python console mode or small test scripts. Practice getting the information you need, such as bearer tokens and order ids, from the responses.

Incorporate functions into Model

After getting comfortable with HTTPX requests, create a file named model_pay.py. Create functions that make authentication, order, and capture requests to the Fakepay API. Since bearer tokens last 9 hours, prepare to use a token file to store bearer token and expiration information. Create the following functions at a minimum, along with any desired helper functions:

Add Routes to app.py

Add the following POST routes to app.py in the sample code:

Add a "Donate" Page to Your Sample React Code

Now, add a page named Donate.jsx that allows anyone to make a donation to Batman on your Batpage. The workflow should be as follows:

  1. Take a donation amount from the user.
  2. On submission, send an AJAX request to your Flask app's POST /pay/order route.
  3. If there is an error from your Flask server, alert the user.
  4. Store the order id in a state variable.
  5. Ask for the user's card information. Use a button and state variable to show or hide the user's card information is typing (this can be done by toggling the input type to "password" or "text").
  6. On submission, send an AJAX request to your Flask app's POST /pay/capture route.
  7. Alert the user whether payment succeeded or failed.