-
Notifications
You must be signed in to change notification settings - Fork 308
Start recording ref in exchanges table #4361
Changes from 3 commits
f10e1c7
cdaf7a2
4bafcf9
54f70d1
9445dda
b14e1e3
6e8ed29
c604b2a
f3cf8af
7abbf37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,7 @@ def create_card_hold(db, participant, amount): | |
|
||
hold = None | ||
error = "" | ||
ref = "" | ||
try: | ||
result = braintree.Transaction.sale({ | ||
'amount': str(cents/100.0), | ||
|
@@ -82,6 +83,7 @@ def create_card_hold(db, participant, amount): | |
'options': { 'submit_for_settlement': False }, | ||
'custom_fields': {'participant_id': participant.id} | ||
}) | ||
ref = result.transaction.id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A general question: can ref here and below be null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes it can be according to the current state of the db table. For recording and accounting purposes we don't want it not to be null but because of the current issue with the missing back data we need to allow nulls until we can back fill all the refs (if that is at all possible). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make sure, after this PR, all the newly inserted records afterwards will have ref field as non-null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do they give us a URL and not just a transaction id? Eventually we're going to want to cross-link from Gratipay (admin) UI over to Braintree (etc.). Transaction id is a start but if we can know a URL at this point then let's just store that, ya? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I believe they will/should be null if the transaction fails. We still want to record our attempt at a transaction even if the API call fails. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @whit537 I believe that it should be null if it fails in the
Going to look into this now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmmm ... we should probably differentiate "failure" state as reported by Braintree from an "error" state due to, e.g., network failure during the API call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can always do that in the note field of the exchange record. (sorry for stating the obvious) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just build a URL to the transaction? E.g., Stripe builds URLs like https://dashboard.stripe.com/payments/ch_19tIgqD5FJNWkW8j1uWePP8O with the charge ID. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After a ridiculous long time researching it...yes we can build a URL to the transaction using where |
||
|
||
if result.is_success and result.transaction.status == 'authorized': | ||
error = "" | ||
|
@@ -98,7 +100,7 @@ def create_card_hold(db, participant, amount): | |
log(msg + "succeeded.") | ||
else: | ||
log(msg + "failed: %s" % error) | ||
record_exchange(db, route, amount, fee, participant, 'failed', error) | ||
record_exchange(db, route, amount, fee, participant, 'failed', ref, error) | ||
|
||
return hold, error | ||
|
||
|
@@ -118,14 +120,15 @@ def capture_card_hold(db, participant, amount, hold): | |
|
||
cents, amount_str, charge_amount, fee = _prep_hit(amount) | ||
amount = charge_amount - fee # account for possible rounding | ||
e_id = record_exchange(db, route, amount, fee, participant, 'pre') | ||
ref = hold.id | ||
e_id = record_exchange(db, route, amount, fee, participant, 'pre', ref) | ||
|
||
# TODO: Find a way to link transactions and corresponding exchanges | ||
# meta = dict(participant_id=participant.id, exchange_id=e_id) | ||
|
||
error = '' | ||
try: | ||
result = braintree.Transaction.submit_for_settlement(hold.id, str(cents/100.00)) | ||
result = braintree.Transaction.submit_for_settlement(ref, str(cents/100.00)) | ||
assert result.is_success | ||
if result.transaction.status != 'submitted_for_settlement': | ||
error = result.transaction.status | ||
|
@@ -228,7 +231,7 @@ def get_ready_payout_routes_by_network(db, network): | |
return out | ||
|
||
|
||
def record_exchange(db, route, amount, fee, participant, status, error=None): | ||
def record_exchange(db, route, amount, fee, participant, status, ref, error=None): | ||
"""Given a Bunch of Stuff, return an int (exchange_id). | ||
|
||
Records in the exchanges table have these characteristics: | ||
|
@@ -240,6 +243,8 @@ def record_exchange(db, route, amount, fee, participant, status, error=None): | |
|
||
fee The payment processor's fee. It's always positive. | ||
|
||
ref transaction id in the external system. | ||
|
||
""" | ||
|
||
assert route.participant.id == participant.id | ||
|
@@ -248,10 +253,10 @@ def record_exchange(db, route, amount, fee, participant, status, error=None): | |
|
||
exchange_id = cursor.one(""" | ||
INSERT INTO exchanges | ||
(amount, fee, participant, status, route, note) | ||
VALUES (%s, %s, %s, %s, %s, %s) | ||
(amount, fee, participant, status, route, note, ref) | ||
VALUES (%s, %s, %s, %s, %s, %s, %s) | ||
RETURNING id | ||
""", (amount, fee, participant.username, status, route.id, error)) | ||
""", (amount, fee, participant.username, status, route.id, error, ref)) | ||
|
||
if status == 'failed': | ||
propagate_exchange(cursor, participant, route, error, 0) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to set this here? For when the transaction fails and we don't have a ref? Shouldn't we just stick with null in that case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to set this (but it could be Null) because it kept giving the error that the variable was being referenced before being assigned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some consideration...yes it should be
Null
if thetry..except
fails