Skip to content

Commit

Permalink
Connect: fix webhook processing for connected accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Oct 13, 2017
1 parent 723f53f commit c7ed716
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/reference/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Args:
- api_version: the version of the Stripe API used.
- request_id: the id of the request that initiated the webhook.
- pending_webhooks: the number of pending webhooks. Defaults to `0`.
- stripe_account: the stripe_id of a Connect account

#### pinax.stripe.actions.events.dupe_event_exists

Expand Down
4 changes: 3 additions & 1 deletion docs/user-guide/connect.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Using Stripe Connect

[Stripe Connect](https://stripe.com/connect) allows you to perform charges on behalf of your
users and then payout to thier bank accounts.
users and then payout to their bank accounts.

There are several ways to integrate Connect and these result in the creation of different
[account types](https://stripe.com/connect/account-types). Before you begin your Connect
Expand All @@ -17,6 +17,8 @@ This project allows use of any account type.
immediately know when an Account has changed. It may also be necessary for Standard and
Express integrations in order to detect when an Account has been created.

The minimum required Stripe API version for using the Connect integration with
this project is [version 2017-05-26](https://stripe.com/docs/upgrades#2017-05-25).

## Standard and Express Accounts

Expand Down
7 changes: 5 additions & 2 deletions pinax/stripe/actions/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from ..webhooks import registry


def add_event(stripe_id, kind, livemode, message, api_version="", request_id="", pending_webhooks=0):
def add_event(stripe_id, kind, livemode, message, api_version="",
request_id="", pending_webhooks=0, stripe_account=None):
"""
Adds and processes an event from a received webhook
Expand All @@ -14,8 +15,10 @@ def add_event(stripe_id, kind, livemode, message, api_version="", request_id="",
api_version: the version of the Stripe API used
request_id: the id of the request that initiated the webhook
pending_webhooks: the number of pending webhooks
stripe_account: the stripe_id of a Connect account
"""
event = models.Event.objects.create(
stripe_account=stripe_account,
stripe_id=stripe_id,
kind=kind,
livemode=livemode,
Expand All @@ -30,7 +33,7 @@ def add_event(stripe_id, kind, livemode, message, api_version="", request_id="",
webhook.process()


def dupe_event_exists(stripe_id):
def dupe_event_exists(stripe_id, stripe_account=None):
"""
Checks if a duplicate event exists
Expand Down
7 changes: 7 additions & 0 deletions pinax/stripe/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ def test_add_event(self, ProcessMock):
self.assertEquals(event.kind, "account.updated")
self.assertTrue(ProcessMock.called)

@patch("pinax.stripe.webhooks.AccountUpdatedWebhook.process")
def test_add_event_connect(self, ProcessMock):
events.add_event(stripe_id="evt_001", kind="account.updated", livemode=True, message={}, stripe_account="acct_001")
event = Event.objects.get(stripe_id="evt_001", stripe_account="acct_001")
self.assertEquals(event.kind, "account.updated")
self.assertTrue(ProcessMock.called)

def test_add_event_new_webhook_kind(self):
events.add_event(stripe_id="evt_002", kind="patrick.got.coffee", livemode=True, message={})
event = Event.objects.get(stripe_id="evt_002")
Expand Down
2 changes: 1 addition & 1 deletion pinax/stripe/tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_webhook_associated_with_stripe_account(self, TransferMock, StripeEventM
connect_event_data = self.event_data.copy()
stripe_account = "acct_123123123"
# only difference is that we'll have a user_id value
connect_event_data["user_id"] = stripe_account
connect_event_data["account"] = stripe_account
StripeEventMock.return_value.to_dict.return_value = connect_event_data
TransferMock.return_value = connect_event_data["data"]["object"]
msg = json.dumps(connect_event_data)
Expand Down
8 changes: 4 additions & 4 deletions pinax/stripe/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def validate(self):
"""
Validate incoming events.
We fetch the event data to ensure it's legit. For Connect
accounts we must fetch the event using the `stripe_account`
parameter, else we won't find it.
We fetch the event data to ensure it is legit.
For Connect accounts we must fetch the event using the `stripe_account`
parameter.
"""
self.stripe_account = self.event.webhook_message.get("user_id")
self.stripe_account = self.event.webhook_message.get("account")
self.event.stripe_account = self.stripe_account
evt = stripe.Event.retrieve(
self.event.stripe_id,
Expand Down

0 comments on commit c7ed716

Please sign in to comment.