-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
166 lines (124 loc) · 5.73 KB
/
main.py
File metadata and controls
166 lines (124 loc) · 5.73 KB
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
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import datetime
import os
import jinja2
from google.appengine.ext import ndb
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True
)
ALIAS = 'aub'
from e24PaymentPipe import Gateway
from models import Transaction
class MainHandler(webapp2.RequestHandler):
def get(self):
amount = self.request.GET.get('amount', 1)
# The first step, after a checkout request
# is to generate a database record.
# Doing this gives us a unique key (automatically
# generated by the database) that we will use
# when requesting a payment token from KNET
order = Transaction()
order.total_amount = amount
key = order.put() # Save the unique key
# Read the resource file, and generate a payment request
file_path = os.path.join(os.path.split(__file__)[0], 'resource.cgn')
pgw = Gateway(file_path, ALIAS)
pgw.error_url = webapp2.uri_for('error', _full=True)
pgw.response_url = webapp2.uri_for('gw', _full=True)
pgw.amount = amount
pgw.trackid = key.urlsafe() # You must supply a tracking ID
# which has to be unique per transaction
# the Python version of the integration library
# will automatically generate a key if you do not
# provide one, but its best to give a value
# explicity. This key is returned back as "trackid"
# This line contacts KNET to get the payment token
# and redirect URI. Any exception raised at this stage
# should stop the application going forward.
gw_info = pgw.get_payment_url()
payment_token = gw_info['paymentID']
payment_url = gw_info['paymentURL']
if payment_url[-1] == '&':
template_url = '{}PaymentID={}'
else:
template_url = '{}?PaymentID={}'
order.payment_id = payment_token # Save the payment token as part of the
# transaction record
order.put()
# Redirect the user to the payment page
return webapp2.redirect(template_url.format(payment_url, payment_token))
class GatewayHandler(webapp2.RequestHandler):
def post(self):
# First step, fetch the payment id
# and the result of the transaction,
# use the trackid that is sent as part of
# the initial request to get the object
# from the database
payment_id = self.request.POST.get('paymentid')
result = self.request.POST.get('result')
key = ndb.Key(urlsafe=self.request.POST.get('trackid'))
order = key.get()
error_url = webapp2.uri_for('error', _full=True, PaymentID=payment_id)
success_url = webapp2.uri_for('success', _full=True, PaymentID=payment_id)
if not order:
# This payment id does not exist
# return an error message
self.response.write('REDIRECT={}'.format(error_url))
return
# Save the return values and status
# and update the order
order.result = result
order.post_date = self.request.POST.get('postdate')
order.transaction_id = self.request.POST.get('tranid')
order.transaction_ref = self.request.POST.get('ref')
order.authorization_code = self.request.POST.get('auth')
order.put() # Update the order
# Depending on the transaction status
# we need to tell the KNET payment gateway
# to which URL to redirect to
if result in (u'CANCELLED', u'NOT CAPTURED'):
self.response.write('REDIRECT={}'.format(error_url))
self.response.write('REDIRECT={}'.format(success_url))
class ErrorHandler(webapp2.RequestHandler):
def get(self):
return self.response.write('There was an error processing you request # {}'.format(self.request.GET.get('PaymentID')))
class SuccessHandler(webapp2.RequestHandler):
def get(self):
pid = self.request.GET.get('PaymentID')
# Fetch the order details
order = Transaction.query(Transaction.payment_id==pid).get()
# You must display the following details
# to the customer
context = {'total': order.total_amount,
'auth': order.authorization_code,
'postdate': order.post_date,
'transaction_ref': order.transaction_ref,
'transaction_id': order.transaction_id,
'tracking_id': order.key.urlsafe(),
}
template = JINJA_ENVIRONMENT.get_template('receipt.html')
self.response.write(template.render(context))
app = webapp2.WSGIApplication([
webapp2.Route('/', handler=MainHandler, name='home'),
webapp2.Route('/gw', handler=GatewayHandler, name='gw'),
webapp2.Route('/error', handler=ErrorHandler, name='error'),
webapp2.Route('/thanks', handler=SuccessHandler, name='success'),
], debug=True)