release portal3 into production
[hcoop/portal.git] / stripe / hcoopstripe.py
CommitLineData
e4964ef9
CE
1# -*- python -*-
2# Copyright (C) 2014 by Clinton Ebadi <clinton@unknownlamer.org>
3
4import sys
5sys.path.insert(0, '/afs/hcoop.net/user/h/hc/hcoop/portal3/stripe/stripe-pkg/lib/python2.6/site-packages/')
6
7import stripe, cgi, psycopg2, cgitb, datetime, smtplib
8from email.mime.text import MIMEText
9from contextlib import contextmanager
10
11def notify_payment (charge, member):
12 msg_text = """
13A member has paid us via Stripe. Please visit the portal money
14page at your earliest convenience to process the payment.
15
16 Member : {0}
17 Amount (cents) : {1}
18 Stripe Charge ID: {2}
19""".format (member, charge.amount, charge.id)
20
21 msg = MIMEText(msg_text)
22 msg['Subject'] = 'Stripe payment received from {0}'.format(member)
23 msg['From'] = 'payment@hcoop.net'
24 msg['To'] = 'payment@hcoop.net'
25
26 s = smtplib.SMTP ('mail.hcoop.net')
27 s.sendmail ('payment@hcoop.net', ['payment@hcoop.net'], msg.as_string ())
28 s.quit ()
29
30def notify_payment_rejected (charge, reason):
31 # TODO: notify member...
32 msg_text = """We have rejected a payment from a member.
33 Amount: {0}
34 Stripe Charge ID: {1}
35 Reason: {2}
36""".format (charge.amount, charge.id, reason)
37
38 msg = MIMEText(msg_text)
39 msg['Subject'] = 'Stripe payment rejected'
40 msg['From'] = 'payment@hcoop.net'
41 msg['To'] = 'payment@hcoop.net'
42
43 s = smtplib.SMTP ('mail.hcoop.net')
44 s.sendmail ('payment@hcoop.net', ['payment@hcoop.net'], msg.as_string ())
45 s.quit ()
46
47def stripe_key ():
48 keyfile = open ("/afs/hcoop.net/user/h/hc/hcoop/.portal-private/stripe", "r")
49 keystring = keyfile.read ()
50 keyfile.close ()
51 return keystring
52
53@contextmanager
54def stripe_error_handling ():
55 try:
56 yield
57 except stripe.error.CardError, e: # The card has been declined
58 print 'Status: 200 OK'
59 print
60 print '<html>'
61 print '<head><title>Transaction Failed</title></head>'
62 print '<body>'
63 print '<h1>Failed</h1><p>Reason: '
64 print e.json_body['error']['message']
65 print '</p>'
66 print '</body>'
67 print '</html>'
68 raise
69 except stripe.error.StripeError, e: # General stripe failure
70 print 'Status: 200 OK'
71 print
72 print '<html>'
73 print '<head><title>Stripe Error</title></head>'
74 print '<body>'
75 print '<h1>Failed</h1><p>Reason: '
76 print e.json_body['error']['message']
77 print '</p>'
78 print '</body>'
79 print '</html>'
80 raise
81
82@contextmanager
83def stripe_refund_on_error (charge):
84 try:
85 yield
86 except:
87 print 'Status: 200 OK'
88 print 'Content-Type: text/html'
89 print ''
90 print '<h1>Something went wrong after accepting payment!</h1>'
91 charge.refund ()
92 print '<p>The charge should be refunded. Please contact payment@hcoop.net if it was not!</p>'
93 raise
94
95def stripe_success (redirect_to):
96 print 'Status: 303 See Other'
97 print 'Location: {0}'.format(redirect_to);
98 print ''
99 print '<a href="{0}">Go back to the portal</a>'.format(redirect_to)
100
588a1662 101connstring = 'dbname=hcoop_portal3 user=hcoop host=postgres port=5433'
e4964ef9
CE
102
103def hcoop_stripe_init ():
104 cgitb.enable()
105 stripe.api_key = stripe_key ()