Skip to content

Commit

Permalink
add checkout function
Browse files Browse the repository at this point in the history
  • Loading branch information
nganphan123 committed Apr 12, 2022
1 parent 8a2b792 commit 439ac75
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 248 deletions.
Empty file added app/checkout/README.md
Empty file.
1 change: 1 addition & 0 deletions app/checkout/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .handle_checkout import handle_checkout
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import json
import os
from urllib import response
from requests import request
from flask import url_for
import requests


def handle_checkout():

'''
Handles checkout process
parameters: none
returns: json object
'''
# Get access token
# client_id and secret are claimed from paypal sandbox website
oauth_response = requests.post("https://api-m.sandbox.paypal.com/v1/oauth2/token",
auth=(os.getenv('client_id'), os.getenv('secret')),
headers= {'Accept': 'application/json',
'Accept-Language': 'en_US'},
params={'grant_type': 'client_credentials'}).json()

token = oauth_response['access_token']

headers = {"Content-Type" : "application/json", "Authorization" : ("Bearer " + token)}
data = '''{
data = {
"intent": "CAPTURE",
"purchase_units": [
{
Expand All @@ -27,11 +33,14 @@ def handle_checkout():
}
],
"application_context": {
"return_url": "https://www.facebook.com/",
"cancel_url": "https://www.facebook.com/"
"return_url": url_for('order_success',_external=True)
}
}'''
result = requests.post("https://api-m.sandbox.paypal.com/v2/checkout/orders", headers = headers, data = data).json()
}

# Make request to paypal checkout sandbox to get retrieve approve_url
# Create button template in messenger that redirect to the approve_url for customer checkout experience
# After customer finishes checkout, paypal redirects to the return_url (pages/order-success.html)
result = requests.post("https://api-m.sandbox.paypal.com/v2/checkout/orders", headers = headers, data = json.dumps(data)).json()
redirect_link = result["links"][1]["href"]
response = {
"fulfillmentMessages": [
Expand Down Expand Up @@ -59,5 +68,5 @@ def handle_checkout():
]
}

# return {"fulfillmentMessages": [{"text": {"text": [str(redirect_link)]}}]}
return response
# return str(hhh)
10 changes: 10 additions & 0 deletions app/pages/confirm-payment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>
Confirmation
</title>
</head>
<body>
Your order has been placed. You should receive an email from us shorlty. Thank you for shopping with us.
</body>
</html>
4 changes: 0 additions & 4 deletions app/products/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from .product_info import (
# ProductInfoHandler,
handle_prod_info
)
from .store_info import (
handle_store_info,
)
24 changes: 0 additions & 24 deletions app/products/access_info.py

This file was deleted.

53 changes: 0 additions & 53 deletions app/products/base_handler.py

This file was deleted.

143 changes: 0 additions & 143 deletions app/response/bot.py

This file was deleted.

12 changes: 6 additions & 6 deletions app/response/handle_intent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# from app.products.access_info import AccessHandler
from app.concerns.other_concern import handle_exchange_request, handle_feedback, handle_refund_request
from app.products.delivery_time_estimate import delivery_time_estimate
from app.products.handle_checkout import handle_checkout
from app.products.product_info import handle_prod_info
from app.products.store_info import handle_store_info
from app.shop import *
from app.checkout import *
from products import *

def handle_intent(request):
"""
Expand Down Expand Up @@ -34,6 +32,8 @@ def handle_intent(request):
# if user asks about estimated delivery time
elif(intent == "delivery-time-estimate"):
response = delivery_time_estimate(req["queryResult"]["parameters"])
elif(intent == "payment-checkout"):
elif(intent == "showcase"):
response = show_prods()
elif(intent == "checkout"):
response = handle_checkout()
return response
File renamed without changes.
15 changes: 15 additions & 0 deletions app/shop/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .delivery_time_estimate import(
delivery_time_estimate
)

from .other_concern import(
other_concern
)

from .show_prods import(
show_prods
)

from .store_info import(
store_info
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@


def delivery_time_estimate(params):
"""
'''
Handles requests for estimated delivery time
parameters: dictionary or json object
returns: json object
"""
returns: json object (of reply message)
'''
# Parse the address
address = str(params["street-address"]["street-address"]) + " " + str(params["geo-city"]) + " " + str(params["geo-state"]) + " " + str(params["geo-country"])
address.replace(",", " ")
address = re.sub(r'\s+', '%20', address)

# TODO: Better way to store api key
params = {'key' : os.getenv('API_KEY'), 'origins' : '1555%20Banks%20Rd%20Kelowna%20BC', 'destinations' : address, 'departure_time': 'now'}
params = {'key' : os.getenv('API_KEY'), 'origins' : 'c', 'destinations' : address, 'departure_time': 'now'}
result = requests.get("https://maps.googleapis.com/maps/api/distancematrix/json?", params=params).json()
info = result["rows"][0]["elements"]
# If the info is not empty, the given addresses are valid
Expand Down
File renamed without changes.
Loading

0 comments on commit 439ac75

Please sign in to comment.