Skip to content

Commit

Permalink
Fix ChargeWebhook for charge.dispute events
Browse files Browse the repository at this point in the history
Fixes #590
  • Loading branch information
blueyed committed Sep 13, 2018
1 parent 06c2ec8 commit 27a3e31
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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

0 comments on commit 27a3e31

Please sign in to comment.