Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Merge pull request #4518 from gratipay/record-ref-in-masspay
Browse files Browse the repository at this point in the history
Start recording ref when recording an exchange in masspay
  • Loading branch information
rohitpaulk authored Jun 19, 2017
2 parents 4f84de5 + 72efab2 commit 3471f1a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
19 changes: 14 additions & 5 deletions bin/masspay.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,21 @@ def compute_output_csvs():
print("{:>64} {:>7} {:>7}".format(total_gross, total_fees, total_net))


def load_statuses():
def load_statuses_and_refs():
_status_map = { 'Completed': 'succeeded'
, 'Unclaimed': 'pending'
, 'Denied': 'failed'
} # PayPal -> Gratipay
statuses = {}
refs = {}
fp = open(REPORT_CSV)
for line in fp:
if line.startswith('Transaction ID,Recipient'):
break
for rec in csv.reader(fp):
statuses[rec[1]] = _status_map[rec[5]]
return statuses
refs[rec[1]] = rec[0]
return statuses, refs


def post_back_to_gratipay(force=False):
Expand All @@ -200,7 +202,7 @@ def post_back_to_gratipay(force=False):
"did, then rerun with -f.")
return

statuses = load_statuses()
statuses, refs = load_statuses_and_refs()

nposts = 0
for username, route_id, email, gross, fee, net, additional_note in csv.reader(open(GRATIPAY_CSV)):
Expand All @@ -210,8 +212,15 @@ def post_back_to_gratipay(force=False):
note += " " + additional_note
print(note)
status = statuses[email]

data = {'amount': '-' + net, 'fee': fee, 'note': note, 'status': status, 'route_id': route_id}
ref = refs[email]

data = {'amount': '-' + net
, 'fee': fee
, 'note': note
, 'status': status
, 'ref': ref
, 'route_id': route_id
}
try:
response = requests.post(url, auth=(gratipay_api_key, ''), data=data)
except IncompleteRead:
Expand Down
1 change: 1 addition & 0 deletions tests/py/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def post_masspays(self, n):
, 'fee': '0'
, 'note': 'Exchange!'
, 'status': 'succeeded'
, 'ref': 'transactionidref'
, 'route_id': unicode(self.homer_route.id)
}
, auth_as='admin'
Expand Down
18 changes: 16 additions & 2 deletions tests/py/test_record_an_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def record_an_exchange(self, data, make_participants=True):
if data['route_id'] is None:
del(data['route_id'])

if 'ref' not in data:
data['ref'] = 'N/A'

return self.client.PxST('/~bob/history/record-an-exchange', data, auth_as='alice')


Expand Down Expand Up @@ -93,21 +96,32 @@ def test_route_should_belong_to_user_else_400(self):
assert response.code == 400
assert response.body == "Route doesn't exist"

def test_no_ref_is_400(self):
response = self.record_an_exchange({'amount': '10', 'fee': '0', 'ref': ''})
assert response.code == 400
assert response.body == "Invalid Reference"

def test_whitespace_ref_is_400(self):
response = self.record_an_exchange({'amount': '10', 'fee': '0', 'ref': ' '})
assert response.code == 400
assert response.body == "Invalid Reference"

def test_dropping_balance_below_zero_is_allowed_in_this_context(self):
self.record_an_exchange({'amount': '-10', 'fee': '0'})
actual = self.db.one("SELECT balance FROM participants WHERE username='bob'")
assert actual == D('-10.00')

def test_success_records_exchange(self):
self.record_an_exchange({'amount': '10', 'fee': '0.50'})
self.record_an_exchange({'amount': '10', 'fee': '0.50', 'ref':"605BSOC6G855L15OO"})
expected = { "amount": D('10.00')
, "fee": D('0.50')
, "participant": "bob"
, "recorder": "alice"
, "note": "noted"
, "ref" : "605BSOC6G855L15OO"
, "route": ExchangeRoute.from_network(self.bob, 'paypal').id
}
SQL = "SELECT amount, fee, participant, recorder, note, route " \
SQL = "SELECT amount, fee, participant, recorder, note, route, ref " \
"FROM exchanges"
actual = self.db.one(SQL, back_as=dict)
assert actual == expected
Expand Down
11 changes: 8 additions & 3 deletions www/~/%username/history/record-an-exchange.spt
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ if request.method == 'POST':
route = ExchangeRoute.from_id(route_id)
if not route or route.participant.id != participant.id:
raise Response(400, "Route doesn't exist")

ref = request.body['ref'].strip()
if not ref:
raise Response(400, "Invalid Reference")

with website.db.get_cursor() as cursor:
cursor.run("""
INSERT INTO exchanges
(amount, fee, route, participant, recorder, note, status)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""", (amount, fee, route_id, participant.username, user.participant.username, note, status))
(amount, fee, route, participant, recorder, note, status, ref)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
""", (amount, fee, route_id, participant.username, user.participant.username, note, status, ref))

if amount < 0:
# For payouts, we need to take the fee out of their Gratipay balance.
Expand Down Expand Up @@ -96,6 +100,7 @@ if request.method == 'POST':
</select>
<br>
<input name="note" placeholder="note" style="width: 420px" />
<input name="ref" placeholder="ref" />
<h2>Route</h2>
{% for route in routes %}
<input type="radio" name="route_id" value="{{ route.id }}">
Expand Down

0 comments on commit 3471f1a

Please sign in to comment.