-except stripe.error.CardError, e: # The card has been declined
- print 'Status: 200 OK'
- print
- print '<html>'
- print '<head><title>Transaction Failed</title></head>'
- print '<body>'
- print '<h1>Failed</h1><p>Reason: '
- print e.json_body['error']['message']
- print '</p>'
- print '</body>'
- print '</html>'
-else:
- try:
- balance = stripe.BalanceTransaction.retrieve (charge.balance_transaction);
- conn = psycopg2.connect ('dbname=hcoop_portal3test user=hcoop host=postgres port=5433')
- cur = conn.cursor ()
- cur.execute ('insert into stripe_payment (charge_id, card_name, webuser_id, paid_on, gross, fee) values (%s, %s, %s, %s, %s, %s)',
- (charge.id, charge.card.name, webuser_id, datetime.date.today (), charge.amount, balance.fee))
- except:
- print 'Status: 200 OK'
- print 'Content-Type: text/html'
- print ''
- print '<h1>Something went wrong after accepting payment!</h1>'
- charge.refund ()
- conn.rollback ()
- print '<p>The charge should be refunded. Please contact payment@hcoop.net if it was not!</p>'
- raise
- else:
- conn.commit ()
- cur.close ()
- conn.close ()
- notify_payment (charge, member_name)
- print 'Status: 303 See Other'
- print 'Location: /portal/portal?cmd=stripeSuccess'
- print ''
- print '<a href="/portal/portal?cmd=stripeSuccess">Go back to the portal</a>'
+
+with stripe_refund_on_error (charge):
+ with psycopg2.connect ('dbname=hcoop_portal3test user=hcoop host=postgres port=5433') as conn:
+ with conn.cursor () as cur:
+ balance = stripe.BalanceTransaction.retrieve (charge.balance_transaction);
+ cur.execute ('insert into stripe_payment (charge_id, card_name, webuser_id, paid_on, gross, fee) values (%s, %s, %s, %s, %s, %s)',
+ (charge.id, charge.card.name, webuser_id, datetime.date.today (), charge.amount, balance.fee))
+
+notify_payment (charge, member_name)
+print 'Status: 303 See Other'
+print 'Location: /portal/portal?cmd=stripeSuccess'
+print ''
+print '<a href="/portal/portal?cmd=stripeSuccess">Go back to the portal</a>'
+