Skip to content

Commit

Permalink
Fix ChargeWebhook for charge.dispute events
Browse files Browse the repository at this point in the history
Fixes #590
blueyed committed Sep 13, 2018
1 parent 06c2ec8 commit fda2dcf
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
@@ -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):

9 changes: 7 additions & 2 deletions pinax/stripe/webhooks.py
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit fda2dcf

Please sign in to comment.