Commit | Line | Data |
---|---|---|
e4964ef9 CE |
1 | #!/usr/bin/env python |
2 | # -*- python -*- | |
3 | ||
4 | # Copyright (C) 2014 by Clinton Ebadi <clinton@unknownlamer.org> | |
5 | ||
6 | from hcoopstripe import * | |
7 | import stripe, cgi, psycopg2, cgitb, datetime | |
8 | ||
9 | hcoop_stripe_init () | |
10 | ||
11 | request_params = cgi.FieldStorage() | |
12 | request_command = request_params.getvalue ('cmd', 'none'); | |
13 | ||
14 | assert request_command != 'none', 'No command given.' | |
15 | ||
16 | hcoop_stripe_init () | |
17 | ||
18 | if request_command == 'reject_member_payment': | |
19 | charge_id = request_params.getvalue ('stripeChargeId'); | |
20 | reason = request_params.getvalue ('reason', 'none given'); | |
21 | with stripe_error_handling (): | |
22 | conn = psycopg2.connect (connstring) | |
23 | cur = conn.cursor () | |
24 | ||
25 | cur.execute ('SELECT charge_id FROM stripe_payment WHERE charge_id = %s', (charge_id,)) | |
26 | assert cur.fetchone() != None, 'Bad charge id' | |
27 | cur.execute ('SELECT stripe_charge_id FROM stripe_handled WHERE stripe_charge_id = %s', (charge_id,)) | |
28 | assert cur.fetchone() == None, 'Cannot rejected a previously handled payment' | |
29 | ||
30 | charge = stripe.Charge.retrieve (charge_id); | |
31 | charge.refund () | |
32 | cur.execute ('insert into stripe_rejected (stripe_charge_id, refunded_on, reason) values (%s, %s, %s)', | |
33 | (charge.id, datetime.date.today (), reason)) | |
34 | conn.commit () | |
35 | ||
36 | notify_payment_rejected (charge, reason) | |
37 | stripe_success ('/portal/money?cmd=stripeRejected') | |
38 | else: | |
39 | assert False, 'Invalid command.' |