-
Notifications
You must be signed in to change notification settings - Fork 0
/
python snapet
81 lines (75 loc) · 2.65 KB
/
python snapet
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
## create secret key and publishable key,source from https://stripe.com/docs/checkout/flask
stripe_keys = {
'secret_key': os.environ['SECRET_KEY'],
'publishable_key': os.environ['PUBLISHABLE_KEY']
}
stripe.api_key = stripe_keys['secret_key']
## Payment sample
@app.route('/buy', methods=['POST'])
def buy():
stripe_token = request.form['stripeToken']
email = request.form['stripeEmail']
product_id = request.form['product_id']
product = Product.query.get(product_id)
try:
charge = stripe.Charge.create(
amount=int(product.price * 100),
currency='usd',
card=stripe_token,
description=email)
except stripe.CardError, e:
return """<html><body><h1>Card Declined</h1><p>Your chard could not
be charged. Please check the number and/or contact your credit card
company.</p></body></html>"""
print charge
purchase = Purchase(uuid=str(uuid.uuid4()),
email=email,
product=product)
db.session.add(purchase)
db.session.commit()
message = Message(
subject='Thanks for your purchase!',
sender="[email protected]",
html="""<html><body><h1>Thanks for buying Writing Idiomatic Python!</h1>
<p>If you didn't already download your copy, you can visit
<a href="http://buy.jeffknupp.com/{}">your private link</a>. You'll be able to
download the file up to five times, at which point the link will
expire.""".format(purchase.uuid),
recipients=[email])
with mail.connect() as conn:
conn.send(message)
return redirect('/{}'.format(purchase.uuid))
#Test run
@app.route('/test')
def test():
return """<http><body><form action="buy" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_w3qNBkDR8A4jkKejBmsMdH34"
data-amount="999"
data-name="jeffknupp.com"
data-description="Writing Idiomatic Python 3 PDF ($9.99)">
</script>
<input type="hidden" name="product_id" value="2" />
</form>
</body>
</html>
"""
if __name__ == '__main__':
sys.exit(app.run(debug=True))
#Scrapy form data
frmdata = {"id": "com.supercell.boombeach", "reviewType": '0', "reviewSortOrder": '0', "pageNum":'0'}
url = "https://play.google.com/store/getreviews"
yield FormRequest(url, callback=self.parse, formdata=frmdata)
## Redis sample
from flask import Flask
from redis import Redis
import os
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello():
redis.incr('hits')
return 'Hello World! I have been seen %s times.' % redis.get('hits')
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)