-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (69 loc) · 2.79 KB
/
main.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
# Copyright 2012 Google Inc. All Rights Reserved.
# pylint: disable-msg=C6409,C6203
"""In-App Payments - Online Store Python Sample"""
# standard library imports
from cgi import escape
import os
import time
# third-party imports
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import jwt
# application-specific imports
from sellerinfo import SELLER_ID
from sellerinfo import SELLER_SECRET
class MainHandler(webapp.RequestHandler):
"""Handles /"""
def get(self):
"""Handles get requests."""
curr_time = int(time.time())
exp_time = curr_time + 3600
request_info = {'currencyCode': 'USD',
'sellerData': 'Custom Data'}
jwt_info = {'iss': SELLER_ID,
'aud': 'Google',
'typ': 'google/payments/inapp/item/v1',
'iat': curr_time,
'exp': exp_time,
'request': request_info}
# create JWT for first item
request_info.update({'name': 'Drive In Aniversary Poster', 'price': '20.00'})
token_1 = jwt.encode(jwt_info, SELLER_SECRET)
# create JWT for second item
request_info.update({'name': 'Golden Gate Bridge Poster', 'price': '25.00'})
token_2 = jwt.encode(jwt_info, SELLER_SECRET)
# update store web page
template_vals = {'jwt_1': token_1,
'jwt_2': token_2}
path = os.path.join(os.path.dirname(__file__), 'templates', 'index.html')
self.response.out.write(template.render(path, template_vals))
class PostbackHandler(webapp.RequestHandler):
"""Handles server postback - received at /postback"""
def post(self):
"""Handles post request."""
encoded_jwt = self.request.get('jwt', None)
if encoded_jwt is not None:
# jwt.decode won't accept unicode, cast to str
# http://github.com/progrium/pyjwt/issues/4
decoded_jwt = jwt.decode(str(encoded_jwt), SELLER_SECRET)
# validate the payment request and respond back to Google
if decoded_jwt['iss'] == 'Google' and decoded_jwt['aud'] == SELLER_ID:
if ('response' in decoded_jwt and
'orderId' in decoded_jwt['response'] and
'request' in decoded_jwt):
order_id = decoded_jwt['response']['orderId']
request_info = decoded_jwt['request']
if ('currencyCode' in request_info and 'sellerData' in request_info
and 'name' in request_info and 'price' in request_info):
# optional - update local database
# respond back to complete payment
self.response.out.write(order_id)
application = webapp.WSGIApplication([
('/', MainHandler),
('/postback', PostbackHandler),
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()