- Node.js working installation.
- A working Mailgun.com account, required for sending email receipts to customers.
- A working Stripe.com account, required for stripe payment platform platform integration.
- curl, Postman or any other application with similar functionalities at your disposal.
- Download and extract this repository to a local directory.
- Navigate to
folder-containing-the-extracted-repository/app/lib/config/
directory. There should be files namedstripeConfig.js
andmailgunConfig.js
. These files act as containers for data required from your end. - Open the
stripeConfig.js
file in the editor of your choice. - Find the
stripe
object in the file. You can see that this project has a few properties initializaed by an empty string. YOU NEED TO REPLACE THE EMPTY STRINGS WITH VALID ONES.
- Save the file and close it.
- Open the
mailgunConfig.js
file in the editor of your choice, and update the necessary information under themailgun
object.
- Save the file and close it.
Now the API setup is complete.
- Open a terminal/command-line window and navigate into the
app/
directory. - Run
node index.js
. - If everything's O.K. then the terminal is now displaying relevant port information.
- Check the next section to know more about API Interactions.
The default environment is "staging" and currently is the only environment defined in the configuration file.
To understand this, lets assume that we have a new customer who wants to order something on the Pizza Store. We are to assume that the Pizza Delivery Online Service already has a frontend configured to work with this API.
Details about the request formats are discussed later in "Interacting with API" section of this document.
Now, the primary requirement to access any of the functionalities provided by the online service is for a customer to have an account. To create an account, the customer needs to provide
- First name
- Last name
- Street address
- Password
This part is handled with a POST request from the frontend with all the above mentioned fields as request body.
Once the server validates the data, modifies and saves it, it sends a O.K. response to the frontend. This concludes the Registration Process. That's not all.
The next requirement for the customer to access the API is a valid session. For that the customer needs to share its email and password with the API. This also happens with a POST request with the fields in the request body (JSON). If the authentication is successful, the API sends another O.K. response to the frontend, this time with a TOKEN. That's essentially the Login Process.
The server assigns expiration to the tokens at the time of creation. This ensures that if no token modifications occur, each session is limited.
Now the customer can access other features, namely
- Edit their Credentials
- Delete their account
- Access the menu
- Add items from the menu to the cart
- Place orders
I want to refactor the API to be a message driven API without using external modules and dependencies. Any suggestions on how i can improve on that?
All the necessary information required to perform valid test transaction is stored in config file. For testing purposes, a module with all dummy information has been added to the API. When the app launches in staging environment, this module is used. This module adds payment information to each new user whenever a valid create user request is received.
The configuration folder in the API directory contains stripe and mailgun containers. Once the API is supplied with necessary keys and tokens, it uses them to perform HTTP basic authentication in case of a stripe and mailgun transaction.
The API accepts HTTP and HTTPS requests. Assuming that the API is running under test configuration with all the necessary bells and whistles (referring to MAILGUN and STRIPE credentials here), the following sections explain behavior that can be expected from the API.
request_type : POST
path : users
payload_type : JSON
fields :
- firstName
- lastName
- streetAddress
example : POST request at localhost:3000/users
with following payload
{
"firstName":"John",
"lastName":"Doe",
"email":"john@doe.com",
"streetAddress":"111, xyz street"
}
request_type : PATCH
path : users
payload_type : JSON
fields : token and atleast one of the following field to update
- firstName
- lastName
- streetAddress
example : UPDATE request at localhost:3000/users
with token placed in header and following payload
{
"lastName":"zzz"
}
request_type : DELETE
path : users
payload_type : JSON
fields : email
example : DELETE request at localhost:3000/users?email=john@doe.com
with a valid token in the header
request_type : POST
path : session
payload_type : JSON
fields : email
example : POST request at localhost:3000/login
with following payload
{
"email":"john@doe.com",
"password":"abcd1234"
}
request_type : DELETE
path : session
payload_type : JSON
fields : email
example : POST request at localhost:3000/logout
with a valid token in the header
request_type : GET
path : menu
payload_type : JSON
fields : token
example : GET request at localhost:3000/menu?token=akh279sbn09mn2gh89xSJ2
with a valid token in the header
request_type : PATCH
path : cart
payload_type : JSON
fields :
- token
- items
example : UPDATE request at localhost:3000/cart
with a valid token in the header and following payload
{
"items": [
{
"id":"1A",
"quantity":1
},
{
"id":"12B",
"quantity":4
}
]
}
request_type : POST
path : orders
payload_type : JSON
fields :
- token
example : POST request at localhost:3000/orders
with a valid token in the header and following payload
{
"token":"akh279sbn09mn2gh89xSJ2"
}