-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.py
83 lines (69 loc) · 2.63 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
from flask import Flask, url_for, redirect, request
from config import config, interface
app = Flask(__name__)
@app.route("/")
def index():
return """
<a href="%s">
<img src="https://www.paypalobjects.com/en_US/i/btn/btn_xpressCheckout.gif">
</a>
""" % url_for('paypal_redirect')
@app.route("/paypal/redirect")
def paypal_redirect():
kw = {
'amt': '10.00',
'currencycode': 'USD',
'returnurl': url_for('paypal_confirm', _external=True),
'cancelurl': url_for('paypal_cancel', _external=True),
'paymentaction': 'Sale'
}
setexp_response = interface.set_express_checkout(**kw)
return redirect(interface.generate_express_checkout_redirect_url(setexp_response.token))
@app.route("/paypal/confirm")
def paypal_confirm():
getexp_response = interface.get_express_checkout_details(token=request.args.get('token', ''))
if getexp_response['ACK'] == 'Success':
return """
Everything looks good! <br />
<a href="%s">Click here to complete the payment.</a>
""" % url_for('paypal_do', token=getexp_response['TOKEN'])
else:
return """
Oh noes! PayPal returned an error code. <br />
<pre>
%s
</pre>
Click <a href="%s">here</a> to try again.
""" % (getexp_response['ACK'], url_for('index'))
@app.route("/paypal/do/<string:token>")
def paypal_do(token):
getexp_response = interface.get_express_checkout_details(token=token)
kw = {
'amt': getexp_response['AMT'],
'paymentaction': 'Sale',
'payerid': getexp_response['PAYERID'],
'token': token,
'currencycode': getexp_response['CURRENCYCODE']
}
interface.do_express_checkout_payment(**kw)
return redirect(url_for('paypal_status', token=kw['token']))
@app.route("/paypal/status/<string:token>")
def paypal_status(token):
checkout_response = interface.get_express_checkout_details(token=token)
if checkout_response['CHECKOUTSTATUS'] == 'PaymentActionCompleted':
# Here you would update a database record.
return """
Awesome! Thank you for your %s %s purchase.
""" % (checkout_response['AMT'], checkout_response['CURRENCYCODE'])
else:
return """
Oh no! PayPal doesn't acknowledge the transaction. Here's the status:
<pre>
%s
</pre>
""" % checkout_response['CHECKOUTSTATUS']
@app.route("/paypal/cancel")
def paypal_cancel():
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8338, debug=True)