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:
- pay_order(amount): This function should send an order request with a given amount and return an order id if successful or false if unsuccessful.
- pay_capture(amount, cardinfo): This function should send a capture request with an amount and card information.
- fakepay_token() (a helper function): This helper function is designed to avoid unnecessary repeated authentication requests. It should first check whether a valid, unexpired token exists (checking the token file). If not, it should send an authentication request to Fakepay using your Fakepay username and password credentials (your issued Cpanel username and password). Whether the token is old or new, it should return a valid, unexpired token. The goal is that this function can be called whenever an order or capture request is made and will always return a valid, unexpired token.
Add Routes to app.py
Add the following POST routes to app.py in the sample code:
- POST /pay/order: Validates that username matches the logged in user. Sends an Order request to the Fakepay API using your model functions and returns an order id.
- POST /pay/capture/{order_id}: Validates that username matches the logged in user. Sends a Capture request to the Fakepay API using your model functions and returns confirmation or rejection information.
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:
- Take a donation amount from the user.
- On submission, send an AJAX request to your Flask app's POST /pay/order route.
- If there is an error from your Flask server, alert the user.
- Store the order id in a state variable.
- 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").
- On submission, send an AJAX request to your Flask app's POST /pay/capture route.
- Alert the user whether payment succeeded or failed.