-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
223 lines (169 loc) · 8.11 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from __future__ import print_function
from datetime import datetime
from random import randint
from flask import redirect, render_template, url_for, request
from paypalcheckoutsdk.core import PayPalHttpClient, SandboxEnvironment, LiveEnvironment
from paypalcheckoutsdk.orders import OrdersCaptureRequest, OrdersCreateRequest, OrdersGetRequest
from paypalhttp import HttpError
from googleapiclient.errors import HttpError
'''-------------------------------------------------------------
MY MODULES'''
from config import app, db, get_domain, lease_price, PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET, PAYPAL_LIVE
from models import Videos
from youtube import add_uploads_to_database
from mail import send_confirmation_email
from drive import fetch_beat_files, download_all_files
from docs import export_lease, create_lease
'''-------------------------------------------------------------
INSTANTIATING DATABASE IF IT ISN'T ALREADY'''
db.create_all()
'''----------------------------------------------------------------------------------------------------
ROUTING'''
@app.route('/') # START PAGE
def home():
return redirect(url_for('beat_library'))
@app.route('/beats')
def beat_library():
videos = Videos.query.order_by(Videos.video_publishedAt.desc()).all()
return render_template('beat-library.html', videos=videos)
@app.route('/beats/<video_id>')
def beat(video_id):
video = Videos.query.get(video_id)
return render_template('beat.html', video=video)
@app.route('/fetchvideos', methods=['GET', 'POST'])
def update_database():
if request.method == 'POST':
add_uploads_to_database()
videos = Videos.query.order_by(Videos.video_publishedAt.desc()).all()
return render_template('update_database.html', videos=videos)
'''-------------------------------------------------------------------------------------------------------------------
PAYPAL API - ACCEPTING PAYMENTS'''
if PAYPAL_LIVE == 'true':
environment = LiveEnvironment(
client_id=PAYPAL_CLIENT_ID,
client_secret=PAYPAL_CLIENT_SECRET
)
elif PAYPAL_LIVE == 'false':
environment = SandboxEnvironment(
client_id=PAYPAL_CLIENT_ID,
client_secret=PAYPAL_CLIENT_SECRET
)
client = PayPalHttpClient(environment)
@app.route('/beats/<video_id>/<video_title>/purchase')
def create_order(video_id, video_title):
reference_id = str(datetime.utcnow()) + str(randint(0,1000))
request = OrdersCreateRequest()
request.prefer('return=representation')
request.request_body (
{
'intent' : 'CAPTURE',
'purchase_units': [
{
'description': video_title, # CHANGE THIS TO YOUTUBE VIDEO TITLE
'custom_id': video_id, # CHANGE THIS TO YOUTUBE VIDEO ID
'reference_id': reference_id,
'soft_descriptor': 'Vince Maina', # THIS IS WHAT SHOWS IN BANK STATEMENT: 'PAYPAL *Vince Maina'
'amount': {
'currency_code': 'GBP',
'value': lease_price
}
}
],
'application_context': {
'brand_name': 'Vince Maina',
'cancel_url': get_domain() + url_for('beat', video_id=video_id),
'return_url': get_domain() + url_for('capture_order'),
'user_action': 'PAY_NOW'
}
}
)
# Executes request
try:
response = client.execute(request)
order_id = response.result.id
print('\nOrder ID:', order_id, '\n')
# Fetches approval link. Have used this approach in case the approval link is not the second link returned.
if response.result.links[1]['rel'] == 'approve':
approve_link = response.result.links[1]['href']
else:
print('False') # Add for loop here to iterate through all links until it finds the approve link.
# CUSTOMER APPROVES TRANSACTION
return redirect(approve_link)
except IOError as ioe:
print('EXCEPTION\n', ioe)
if isinstance(ioe, HttpError):
# Something went wrong server-side
print(ioe.status_code)
@app.route('/confirming', methods=['GET', 'POST'])
def capture_order():
if request.method == 'POST':
artists_legal_name = request.form['artists_legal_name']
artists_professional_name = request.form['artists_professional_name']
url = request.url
order_id = [i.split('=') for i in url[url.find('?') + 1:].split('&')][0][1]
try:
capture_request = OrdersCaptureRequest(order_id) # This should be an Approved Order ID
response = client.execute(capture_request)
amount_paid = response.result.purchase_units[0]['payments']['captures'][0]['amount']['value']
currency_code = response.result.purchase_units[0]['payments']['captures'][0]['amount']['currency_code']
if currency_code == 'GBP':
currency_code = '£'
else:
currency_code += ' '
video_id = response.result.purchase_units[0]['payments']['captures'][0]['custom_id']
video = Videos.query.get(video_id)
lease_id = create_lease(
producers_legal_name='Vincent Chapman-Andrews', # Update Model so that I get added by default, and then if any featured artists are specified they get added too.
producers_professional_name='Vince Maina',
artists_legal_name=artists_legal_name,
artists_professional_name=artists_professional_name,
beat_name=video.video_beat_name,
youtube_link=f'https://youtu.be/{video_id}', # FIX
composer_legal_name='Vincent Chapman-Andrews',
beat_price= currency_code + amount_paid, # FIX - SHOULD MATCH WHAT IS IN THE PAYPAL ORDER RECIEPT.
order_id=order_id
)
send_confirmation_email(
order_id = order_id,
beat_name = video.video_beat_name,
video_title = video.video_title,
recipient_address = 'vince@elevatecopy.com', # UPDATE THIS TO USE ACTUAL ADDRESS FROM PAYPAL ORDER
lease_id = lease_id
)
return redirect(url_for('receipt', order_id=order_id, lease_id=lease_id))
except IOError as ioe:
if isinstance(ioe, HttpError):
# Something went wrong server-side i.e. Paypal's end
print('Something went wrong on Paypal\'s end.')
print(ioe.status_code)
print(ioe.headers)
print(ioe)
return 'Error 001: Payment error. Please contact site owner.'
else:
# Something went wrong client side
print('Something went wrong client side')
print(ioe)
return "Form already completed. The link to get back to the receipt page is included in the purchase confirmation email."
return render_template('lease_form.html')
@app.route('/<order_id>/<lease_id>/receipt', methods=['GET', 'POST'])
def receipt(order_id, lease_id):
request = OrdersGetRequest(order_id)
response = client.execute(request)
video_id = response.result.purchase_units[0]['custom_id']
video = Videos.query.get(video_id)
video_beat_name = video.video_beat_name
stems, mixdowns = fetch_beat_files(video_beat_name)
from flask import request
if request.method == 'POST':
if request.form['submit'] == 'mixdowns':
return download_all_files(video_beat_name, mixdowns, 'mixdowns')
elif request.form['submit'] == 'stems':
return download_all_files(video_beat_name, stems, 'stems')
elif request.form['submit'] == 'lease':
return export_lease(lease_id, f'Licence - {video_beat_name} ({order_id}).pdf')
else:
return render_template('receipt.html', order_id=order_id, video=video, stems=stems, mixdowns=mixdowns)
'''----------------------------------------------------------------------------------------------------
RUNS THE APP ON A LOCAL SERVER'''
if __name__ == '__main__':
app.run(debug=True)