This repository was archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (35 loc) · 1.33 KB
/
main.py
File metadata and controls
47 lines (35 loc) · 1.33 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
import braintree
from flask import Flask
from flask_restful import reqparse, Resource, Api
app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('payment_method_nonce', type=str, help='required')
parser.add_argument('amount', type=str, help='The amount to pay')
braintree.Configuration.configure(braintree.Environment.Sandbox,
merchant_id='zf39nyvdzr5y8666',
public_key='6thc6f5vhx7m5d3y',
private_key='62a678e1651db3435a4adb2220dd2251')
class Token(Resource):
def get(self):
try:
return {'token': braintree.ClientToken.generate()}, 200
except Exception:
return '', 500
class Pay(Resource):
def post(self):
args = parser.parse_args(strict=True)
result = braintree.Transaction.sale({
"amount": args['amount'],
"payment_method_nonce": args['payment_method_nonce'],
"options": {
"submit_for_settlement": True
}
})
if result.is_success:
return {'message': 'Payment done'}, 200
return {'message': 'Error'}, 400
api.add_resource(Token, '/api/payments/token')
api.add_resource(Pay, '/api/payments/pay')
if __name__ == '__main__':
app.run(debug=True)