Skip to content

Commit

Permalink
Synced File and balance_transactions model fields during dispute reso…
Browse files Browse the repository at this point in the history
…lution

Used Dispute._attach_objects_post_save_hook() to extract File Evidence and create corresponding File Objects as well as sync balance transaction objects.

Synced balance_transactions model field in Dispute._attach_objects_post_save hook
  • Loading branch information
arnav13081994 authored and jleclanche committed Aug 4, 2021
1 parent 861f754 commit 75d91d6
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions djstripe/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Charge(StripeModel):
related_name="charges",
help_text="The customer associated with this charge.",
)
# TODO Shouldn't this be on the Dispute model as charge field? Every dispute will have a Charge object

dispute = StripeForeignKey(
"Dispute",
on_delete=models.SET_NULL,
Expand Down Expand Up @@ -1386,6 +1386,42 @@ class Dispute(StripeModel):
def __str__(self):
return f"{self.human_readable_amount} ({enums.DisputeStatus.humanize(self.status)}) "

def _attach_objects_post_save_hook(self, cls, data, pending_relations=None):

super()._attach_objects_post_save_hook(
cls, data, pending_relations=pending_relations
)

# Retrieve and save files from the dispute.evidence object.
# todo find a better way of retrieving and syncing File Type fields from Dispute object
for field in (
"cancellation_policy",
"customer_communication",
"customer_signature",
"duplicate_charge_documentation",
"receipt",
"refund_policy",
"service_documentation",
"shipping_documentation",
"uncategorized_file",
):
file_upload_id = self.evidence.get(field, None)
if file_upload_id:
try:
File.sync_from_stripe_data(File(id=file_upload_id).api_retrieve())
except stripe.error.PermissionError:
# No permission to retrieve the data with the key
# Log a warning message
logger.warning(
"No permission to retrieve the File Evidence Object."
)
except stripe.error.InvalidRequestError:
raise

# iterate and sync every balance transaction
for stripe_balance_transaction in self.balance_transactions:
BalanceTransaction.sync_from_stripe_data(stripe_balance_transaction)


class Event(StripeModel):
"""
Expand Down Expand Up @@ -2017,7 +2053,6 @@ class Payout(StripeModel):
)
type = StripeEnumField(enum=enums.PayoutType)

# TODO Write corresponding test
def __str__(self):
return f"{self.amount} ({enums.PayoutStatus.humanize(self.status)})"

Expand Down Expand Up @@ -2282,6 +2317,7 @@ class Refund(StripeModel):
status = StripeEnumField(
blank=True, enum=enums.RefundStatus, help_text="Status of the refund."
)
# todo implement source_transfer_reversal and transfer_reversal

def get_stripe_dashboard_url(self):
return self.charge.get_stripe_dashboard_url()
Expand Down

0 comments on commit 75d91d6

Please sign in to comment.