Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
idatg2204-project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Steffen Martinsen
idatg2204-project
Commits
2ef727fa
Commit
2ef727fa
authored
May 8, 2024
by
Knut Fineid
Browse files
Options
Downloads
Patches
Plain Diff
add payment functionality
parent
7a0fa9ab
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Backend/main.py
+13
-7
13 additions, 7 deletions
Backend/main.py
Backend/routes/order.py
+80
-3
80 additions, 3 deletions
Backend/routes/order.py
with
93 additions
and
10 deletions
Backend/main.py
+
13
−
7
View file @
2ef727fa
from
utils.application
import
app
from
utils.application
import
app
from
routes.category
import
get_category
from
routes.category
import
get_category
from
routes.home
import
get_home
from
routes.home
import
get_home
#
from routes.order import post_
pay_products
from
routes.order
import
post_
create_order
,
pay_order
#from routes.cart import get_cart
#from routes.cart import get_cart
from
routes.login
import
post_login
,
post_logout
,
post_register
from
routes.login
import
post_login
,
post_logout
,
post_register
from
routes.product
import
get_product_by_id
,
get_product_all
,
get_products_by_search
from
routes.product
import
get_product_by_id
,
get_product_all
,
get_products_by_search
...
@@ -13,28 +13,33 @@ from routes.product import get_product_by_id, get_product_all, get_products_by_s
...
@@ -13,28 +13,33 @@ from routes.product import get_product_by_id, get_product_all, get_products_by_s
def
home
():
def
home
():
return
get_home
()
return
get_home
()
#@app.route('/order/<int:order_id>', methods=['GET'])
#def order(order_id):
# return get_order(order_id)
# Routes for getting the different categories
# Routes for getting the different categories
@app.route
(
'
/category/
'
,
methods
=
[
'
GET
'
])
@app.route
(
'
/category/
'
,
methods
=
[
'
GET
'
])
@app.route
(
'
/category/<string:category_name>
'
,
methods
=
[
'
GET
'
])
@app.route
(
'
/category/<string:category_name>
'
,
methods
=
[
'
GET
'
])
def
category
(
category_name
=
None
):
def
category
(
category_name
=
None
):
return
get_category
(
category_name
)
return
get_category
(
category_name
)
# Route for creating the order
@app.route
(
'
/order/
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/order/
'
,
methods
=
[
'
POST
'
])
def
pay_products
():
def
create_order
():
return
post_pay_products
()
return
post_create_order
()
# Route for paying an order
@app.route
(
'
/order/payment/
'
,
methods
=
[
'
POST
'
])
def
pay
():
return
pay_order
()
# Route for logging out of the app
@app.route
(
'
/logout/
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/logout/
'
,
methods
=
[
'
POST
'
])
def
logout
():
def
logout
():
return
post_logout
()
return
post_logout
()
# Route for logging in
@app.route
(
'
/login/
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/login/
'
,
methods
=
[
'
POST
'
])
def
login
():
def
login
():
return
post_login
()
return
post_login
()
# Route for registering a new user
@app.route
(
'
/register/
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/register/
'
,
methods
=
[
'
POST
'
])
def
register
():
def
register
():
return
post_register
()
return
post_register
()
...
@@ -49,6 +54,7 @@ def get_product(product_id):
...
@@ -49,6 +54,7 @@ def get_product(product_id):
def
get_products
():
def
get_products
():
return
get_product_all
()
return
get_product_all
()
# Route for searching for a new product
@app.route
(
'
/product/search/<string:search>
'
,
methods
=
[
'
GET
'
])
@app.route
(
'
/product/search/<string:search>
'
,
methods
=
[
'
GET
'
])
def
search_products
(
search
):
def
search_products
(
search
):
return
get_products_by_search
(
search
)
return
get_products_by_search
(
search
)
...
...
This diff is collapsed.
Click to expand it.
Backend/routes/order.py
+
80
−
3
View file @
2ef727fa
...
@@ -12,15 +12,26 @@ def post_create_order():
...
@@ -12,15 +12,26 @@ def post_create_order():
return
jsonify
({
"
message
"
:
"
You are not logged in
"
}),
401
return
jsonify
({
"
message
"
:
"
You are not logged in
"
}),
401
data
=
request
.
json
data
=
request
.
json
# Products is array of {product_id, quantity}
# Products is array of {product_id, quantity}
# Example body:
# "products": [
# {
# "productid": 1,
# "quantity": 2
# },
# ...
# ],
products
=
data
[
"
products
"
]
products
=
data
[
"
products
"
]
payment_method
=
data
[
"
payment_method
"
]
productIds
=
[]
productIds
=
[]
for
product
in
products
:
for
product
in
products
:
productIds
.
append
(
product
[
"
product_id
"
])
productIds
.
append
(
product
[
"
product_id
"
])
if
(
len
(
products
)
=
=
0
):
if
(
len
(
products
)
<
=
0
):
return
jsonify
({
"
message
"
:
"
No products in order
"
}),
400
return
jsonify
({
"
message
"
:
"
No products in order
"
}),
400
# Acquire the mutex lock
# Acquire the mutex lock
...
@@ -76,3 +87,69 @@ def post_create_order():
...
@@ -76,3 +87,69 @@ def post_create_order():
mutex_lock
.
release
()
mutex_lock
.
release
()
return
jsonify
({
"
message
"
:
"
Order placed
"
}),
201
return
jsonify
({
"
message
"
:
"
Order placed
"
}),
201
def
pay_order
():
# Get user from logged in cookie
userId
=
request
.
cookies
.
get
(
'
logged_in
'
)
if
userId
is
None
:
return
jsonify
({
"
message
"
:
"
You are not logged in
"
}),
401
# Example body:
# {
# "order_id": 6,
# "payment_method": "Vipps"
# }
data
=
request
.
json
order_id
=
data
[
'
order_id
'
]
payment_method
=
data
[
'
payment_method
'
]
# Get the user_id of the user with the email from the user table
# Check that the user_id logged in is the same as the user on the order
# Get the order status to make sure the order is not already paid
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT user_order.status, user_order.total_amount, user.user_id
FROM user_order INNER JOIN user ON user_order.user_id = user.user_id
WHERE order_id = %s
'''
,
(
order_id
,))
order_info
=
cur
.
fetchall
()
cur
.
close
()
# Check we have one and only one order
if
len
(
order_info
)
!=
1
:
return
jsonify
({
"
message
"
:
"
Not a valid order id
"
}),
400
# Destructure the query into variables
status
,
amount
,
user_id
=
order_info
[
0
]
# Check email is the same as logged in email
if
userId
!=
str
(
user_id
):
return
jsonify
({
"
message
"
:
"
This order is not for the logged in user
"
}),
400
# Check order is not already paid
if
status
!=
"
Pending
"
:
return
jsonify
({
"
message
"
:
"
Order is already paid
"
}),
400
# Make sure correct payment method
if
payment_method
!=
"
Vipps
"
:
return
jsonify
({
"
message
"
:
"
Invalid payment method
"
}),
400
# Pay the order
payment
=
vipps
(
amount
,
order_id
)
if
not
payment
:
return
jsonify
({
"
message
"
:
"
Payment failed
"
}),
400
# Update the status of the order to paid
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
UPDATE user_order SET status =
'
Paid
'
WHERE user_order.order_id = %s
'''
,
(
order_id
,))
mysql
.
connection
.
commit
()
cur
.
close
()
return
jsonify
({
"
message
"
:
"
Order paid
"
}),
200
# Dummy payment method
def
vipps
(
amount
,
order_id
):
return
True
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment