Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ChargeWebhook for charge.dispute events #591

Merged
merged 1 commit into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pinax/stripe/tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
AccountExternalAccountCreatedWebhook,
AccountUpdatedWebhook,
ChargeCapturedWebhook,
ChargeDisputeFundsWithdrawnWebhook,
CustomerDeletedWebhook,
CustomerSourceCreatedWebhook,
CustomerSourceDeletedWebhook,
Expand Down Expand Up @@ -241,6 +242,29 @@ def test_process_webhook_connect(self, SyncMock, RetrieveMock):
self.assertEquals(kwargs["expand"], ["balance_transaction"])
self.assertEquals(kwargs["stripe_account"], "acc_A")

@patch("stripe.Charge.retrieve")
@patch("pinax.stripe.actions.charges.sync_charge_from_stripe_data")
def test_process_webhook_dispute(self, SyncMock, RetrieveMock):
account = Account.objects.create(stripe_id="acc_A")
event = Event.objects.create(
kind=ChargeDisputeFundsWithdrawnWebhook.name,
webhook_message={},
valid=True,
processed=False,
stripe_account=account
)
event.validated_message = dict(data=dict(object=dict(
id=1,
object="dispute",
charge="ch_XXX",
)))
ChargeDisputeFundsWithdrawnWebhook(event).process_webhook()
self.assertTrue(SyncMock.called)
args, kwargs = RetrieveMock.call_args
self.assertEquals(args, ("ch_XXX",))
self.assertEquals(kwargs["expand"], ["balance_transaction"])
self.assertEquals(kwargs["stripe_account"], "acc_A")


class CustomerDeletedWebhookTest(TestCase):

Expand Down
9 changes: 7 additions & 2 deletions pinax/stripe/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,15 @@ class BitcoinReceiverTransactionCreatedWebhook(Webhook):


class ChargeWebhook(Webhook):

def process_webhook(self):
message = self.event.message
if message["data"]["object"].get("object", "charge") == "charge":
stripe_id = message["data"]["object"]["id"]
else:
stripe_id = message["data"]["object"]["charge"]

charges.sync_charge(
self.event.message["data"]["object"]["id"],
stripe_id,
stripe_account=self.event.stripe_account_stripe_id,
)

Expand Down