From fda2dcf50462ac13ea2a92358748cdaf67910187 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 13 Sep 2018 07:53:55 +0200 Subject: [PATCH] Fix ChargeWebhook for charge.dispute events Fixes https://github.com/pinax/pinax-stripe/issues/590 --- pinax/stripe/tests/test_webhooks.py | 24 ++++++++++++++++++++++++ pinax/stripe/webhooks.py | 9 +++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pinax/stripe/tests/test_webhooks.py b/pinax/stripe/tests/test_webhooks.py index d0ccdc6a9..e3109d6b4 100644 --- a/pinax/stripe/tests/test_webhooks.py +++ b/pinax/stripe/tests/test_webhooks.py @@ -28,6 +28,7 @@ AccountExternalAccountCreatedWebhook, AccountUpdatedWebhook, ChargeCapturedWebhook, + ChargeDisputeFundsWithdrawnWebhook, CustomerDeletedWebhook, CustomerSourceCreatedWebhook, CustomerSourceDeletedWebhook, @@ -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): diff --git a/pinax/stripe/webhooks.py b/pinax/stripe/webhooks.py index d40fea2d9..00b931cee 100644 --- a/pinax/stripe/webhooks.py +++ b/pinax/stripe/webhooks.py @@ -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, )