5 sys
.path
.insert
(0, '/afs/hcoop.net/user/h/hc/hcoop/portal3/stripe/stripe-pkg/lib/python2.6/site-packages/')
7 import stripe
, cgi
, psycopg2
, cgitb
, datetime
, smtplib
8 from email
.mime
.text import MIMEText
12 def notify_payment
(charge
, member
):
14 A member has paid us via Stripe. Please visit the portal money
15 page at your earliest convenience to process the payment.
20 """.format
(member
, charge
.amount
, charge
.id
)
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'
27 s
= smtplib
.SMTP
('mail.hcoop.net')
28 s
.sendmail
('payment@hcoop.net', ['payment@hcoop.net'], msg
.as_string
())
32 keyfile
= open ("/afs/hcoop.net/user/h/hc/hcoop/.portal-private/stripe", "r")
33 keystring
= keyfile
.read ()
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
40 stripe
.api_key
= stripe_key
()
42 # Get the credit card details submitted by the form
44 request_params
= cgi
.FieldStorage
()
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')
51 # Create the charge on Stripe's servers - this will charge the user's card
54 charge
= stripe
.Charge
.create
( amount
=amount
,
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'
62 print '<head><title>Transaction Failed</title></head>'
64 print '<h1>Failed</h1><p>Reason: '
65 print e
.json_body
['error']['message']
71 balance
= stripe
.BalanceTransaction
.retrieve
(charge
.balance_transaction
);
72 conn
= psycopg2
.connect ('dbname=hcoop_portal3test user=hcoop host=postgres port=5433')
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
))
77 print 'Status: 200 OK'
78 print 'Content-Type: text/html'
80 print '<h1>Something went wrong after accepting payment!</h1>'
83 print '<p>The charge should be refunded. Please contact payment@hcoop.net if it was not!</p>'
89 notify_payment
(charge
, member_name
)
90 print 'Status: 303 See Other'
91 print 'Location: /portal/portal?cmd=stripeSuccess'
93 print '<a href="/portal/portal?cmd=stripeSuccess">Go back to the portal</a>'