Initial support for Stripe (And Improve Paypal)
[hcoop/portal.git] / stripe / stripe-payment.cgi
1 #!/usr/bin/env python
2 # -*- python -*-
3
4 import sys
5 sys.path.insert(0, '/afs/hcoop.net/user/h/hc/hcoop/portal3/stripe/stripe-pkg/lib/python2.6/site-packages/')
6
7 import stripe, cgi, psycopg2, cgitb, datetime, smtplib
8 from email.mime.text import MIMEText
9
10 cgitb.enable()
11
12 def notify_payment (charge, member):
13 msg_text = """
14 A member has paid us via Stripe. Please visit the portal money
15 page at your earliest convenience to process the payment.
16
17 Member : {0}
18 Amount (cents) : {1}
19 Stripe Charge ID: {2}
20 """.format (member, charge.amount, charge.id)
21
22 msg = MIMEText(msg_text)
23 msg['Subject'] = 'Stripe payment received from {0}'.format(member)
24 msg['From'] = 'payment@hcoop.net'
25 msg['To'] = 'payment@hcoop.net'
26
27 s = smtplib.SMTP ('mail.hcoop.net')
28 s.sendmail ('payment@hcoop.net', ['payment@hcoop.net'], msg.as_string ())
29 s.quit ()
30
31 def stripe_key ():
32 keyfile = open ("/afs/hcoop.net/user/h/hc/hcoop/.portal-private/stripe", "r")
33 keystring = keyfile.read ()
34 keyfile.close ()
35 return keystring
36
37 # Set your secret key: remember to change this to your live secret key in production
38 # See your keys here https://manage.stripe.com/account
39
40 stripe.api_key = stripe_key ()
41
42 # Get the credit card details submitted by the form
43
44 request_params = cgi.FieldStorage()
45
46 token = request_params.getvalue ('stripeToken')
47 webuser_id = request_params.getvalue('webuser_id')
48 member_name = request_params.getvalue('webuser_name')
49 amount = request_params.getvalue('stripeDues')
50
51 # Create the charge on Stripe's servers - this will charge the user's card
52
53 try:
54 charge = stripe.Charge.create( amount=amount,
55 currency="usd",
56 card=token,
57 description='Payment for member {0}'.format (member_name))
58 except stripe.error.CardError, e: # The card has been declined
59 print 'Status: 200 OK'
60 print
61 print '<html>'
62 print '<head><title>Transaction Failed</title></head>'
63 print '<body>'
64 print '<h1>Failed</h1><p>Reason: '
65 print e.json_body['error']['message']
66 print '</p>'
67 print '</body>'
68 print '</html>'
69 else:
70 try:
71 balance = stripe.BalanceTransaction.retrieve (charge.balance_transaction);
72 conn = psycopg2.connect ('dbname=hcoop_portal3test user=hcoop host=postgres port=5433')
73 cur = conn.cursor ()
74 cur.execute ('insert into stripe_payment (charge_id, card_name, webuser_id, paid_on, gross, fee) values (%s, %s, %s, %s, %s, %s)',
75 (charge.id, charge.card.name, webuser_id, datetime.date.today (), charge.amount, balance.fee))
76 except:
77 print 'Status: 200 OK'
78 print 'Content-Type: text/html'
79 print ''
80 print '<h1>Something went wrong after accepting payment!</h1>'
81 charge.refund ()
82 conn.rollback ()
83 print '<p>The charge should be refunded. Please contact payment@hcoop.net if it was not!</p>'
84 raise
85 else:
86 conn.commit ()
87 cur.close ()
88 conn.close ()
89 notify_payment (charge, member_name)
90 print 'Status: 303 See Other'
91 print 'Location: /portal/portal?cmd=stripeSuccess'
92 print ''
93 print '<a href="/portal/portal?cmd=stripeSuccess">Go back to the portal</a>'