2 # Copyright (C) 2014 by Clinton Ebadi <clinton@unknownlamer.org>
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
9 from contextlib
import contextmanager
11 def notify_payment (charge
, member
):
13 A member has paid us via Stripe. Please visit the portal money
14 page at your earliest convenience to process the payment.
19 """.format (member
, charge
.amount
, charge
.id)
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'
26 s
= smtplib
.SMTP ('mail.hcoop.net')
27 s
.sendmail ('payment@hcoop.net', ['payment@hcoop.net'], msg
.as_string ())
30 def notify_payment_rejected (charge
, reason
):
31 # TODO: notify member...
32 msg_text
= """We have rejected a payment from a member.
36 """.format (charge
.amount
, charge
.id, reason
)
38 msg
= MIMEText(msg_text
)
39 msg
['Subject'] = 'Stripe payment rejected'
40 msg
['From'] = 'payment@hcoop.net'
41 msg
['To'] = 'payment@hcoop.net'
43 s
= smtplib
.SMTP ('mail.hcoop.net')
44 s
.sendmail ('payment@hcoop.net', ['payment@hcoop.net'], msg
.as_string ())
48 keyfile
= open ("/afs/hcoop.net/user/h/hc/hcoop/.portal-private/stripe", "r")
49 keystring
= keyfile
.read ()
54 def stripe_error_handling ():
57 except stripe
.error
.CardError
, e
: # The card has been declined
58 print 'Status: 200 OK'
61 print '<head><title>Transaction Failed</title></head>'
63 print '<h1>Failed</h1><p>Reason: '
64 print e
.json_body
['error']['message']
69 except stripe
.error
.StripeError
, e
: # General stripe failure
70 print 'Status: 200 OK'
73 print '<head><title>Stripe Error</title></head>'
75 print '<h1>Failed</h1><p>Reason: '
76 print e
.json_body
['error']['message']
83 def stripe_refund_on_error (charge
):
87 print 'Status: 200 OK'
88 print 'Content-Type: text/html'
90 print '<h1>Something went wrong after accepting payment!</h1>'
92 print '<p>The charge should be refunded. Please contact payment@hcoop.net if it was not!</p>'
95 def stripe_success (redirect_to
):
96 print 'Status: 303 See Other'
97 print 'Location: {0}'.format(redirect_to
);
99 print '<a href="{0}">Go back to the portal</a>'.format(redirect_to
)
101 connstring
= 'dbname=hcoop_portal3 user=hcoop host=postgres port=5433'
103 def hcoop_stripe_init ():
105 stripe
.api_key
= stripe_key ()