-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrainTree.py
84 lines (75 loc) · 2.9 KB
/
BrainTree.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
import braintree
import stripe
from flask import Flask
from flask import request
from flask import json
braintree.Configuration.configure(
braintree.Environment.Sandbox,
'8vcr8hbq5yknmx7p',
'68wxydkzwpxms2vp',
'3ff43ddfcc2e2b97718917952d19c070'
)
app = Flask(__name__)
@app.route("/client_token", methods=["GET"])
def client_token():
return braintree.ClientToken.generate()
@app.route("/checkout", methods=["POST"])
def create_purchase():
# def f(nonce_from_the_client):
# nonce_from_the_client = request.form["sandbox_4mpg9vyr_8vcr8hbq5yknmx7p"]
# Use payment method nonce here...
json = request.get_json(force=True)
nonce = json["payment_method_nonce"]
ammount = json["amount"]
try:
result = braintree.Transaction.sale({
"amount": ammount,
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}
})
except result.errors, e:
pass
# if result.is_success:
# print("success!: " + result.transaction.id)
# elif result.transaction:
# print("Error processing transaction:")
# print(" code: " + result.transaction.processor_response_code)
# print(" text: " + result.transaction.processor_response_text)
# else:
# for error in result.errors.deep_errors:
# print("attribute: " + error.attribute)
# print(" code: " + error.code)
# print(" message: " + error.message)
return "success"
#1
@app.route('/pay', methods=['POST'])
def pay():
#2
# Set this to your Stripe secret key (use your test key!)
stripe.api_key = "sk_test_4HKKLEaGa6YweA50gknGDYEf"
#3
# Parse the request as JSON
json = request.get_json(force=True)
# Get the credit card details
token = json['stripeToken']
amount = json['amount']
description = json['description']
# Create the charge on Stripe's servers - this will charge the user's card
try:
#4
charge = stripe.Charge.create(
amount=amount,
currency="usd",
card=token,
description=description
)
except stripe.CardError, e:
# The card has been declined
pass
#5
return "Success!"
if __name__ == '__main__':
# Set as 0.0.0.0 to be accessible outside your local machine
app.run(debug=True, host= '0.0.0.0')