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

link_customer: create missing customers #108

Merged
merged 1 commit into from
Dec 5, 2017
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
18 changes: 12 additions & 6 deletions pinax/stripe/actions/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,22 @@ def link_customer(event):
"customer.updated",
"customer.deleted"
]
event_data_object = event.message["data"]["object"]
if event.kind in customer_crud_events:
cus_id = event.message["data"]["object"]["id"]
cus_id = event_data_object["id"]
else:
cus_id = event.message["data"]["object"].get("customer", None)
cus_id = event_data_object.get("customer", None)

if cus_id is not None:
customer = models.Customer.objects.filter(stripe_id=cus_id).first()
if customer is not None:
event.customer = customer
event.save()
customer, created = models.Customer.objects.get_or_create(
stripe_id=cus_id,
stripe_account=event.stripe_account,
)
if event.kind in customer_crud_events:
sync_customer(customer, event_data_object)

event.customer = customer
event.save()


def set_default_source(customer, source):
Expand Down
23 changes: 18 additions & 5 deletions pinax/stripe/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,14 @@ def test_can_charge_false_no_default_source(self):
customer = Customer()
self.assertFalse(customers.can_charge(customer))

def test_link_customer(self):
@patch("pinax.stripe.actions.customers.sync_customer")
def test_link_customer(self, SyncMock):
Customer.objects.create(stripe_id="cu_123")
message = dict(data=dict(object=dict(id="cu_123")))
event = Event.objects.create(validated_message=message, kind="customer.created")
customers.link_customer(event)
self.assertEquals(event.customer.stripe_id, "cu_123")
self.assertTrue(SyncMock.called)

def test_link_customer_non_customer_event(self):
Customer.objects.create(stripe_id="cu_123")
Expand All @@ -544,18 +546,29 @@ def test_link_customer_non_customer_event(self):
customers.link_customer(event)
self.assertEquals(event.customer.stripe_id, "cu_123")

def test_link_customer_no_customer(self):
def test_link_customer_non_customer_event_no_customer(self):
Customer.objects.create(stripe_id="cu_123")
message = dict(data=dict(object=dict()))
event = Event.objects.create(validated_message=message, kind="transfer.created")
customers.link_customer(event)
self.assertIsNone(event.customer, "cu_123")

def test_link_customer_does_not_exist(self):
@patch("pinax.stripe.actions.customers.sync_customer")
def test_link_customer_does_not_exist(self, SyncMock):
message = dict(data=dict(object=dict(id="cu_123")))
event = Event.objects.create(validated_message=message, kind="customer.created")
event = Event.objects.create(stripe_id="evt_1", validated_message=message, kind="customer.created")
customers.link_customer(event)
self.assertIsNone(event.customer)
Customer.objects.get(stripe_id="cu_123")
self.assertTrue(SyncMock.called)

@patch("pinax.stripe.actions.customers.sync_customer")
def test_link_customer_does_not_exist_connected(self, SyncMock):
message = dict(data=dict(object=dict(id="cu_123")))
account = Account.objects.create(stripe_id="acc_XXX")
event = Event.objects.create(stripe_id="evt_1", validated_message=message, kind="customer.created", stripe_account=account)
customers.link_customer(event)
Customer.objects.get(stripe_id="cu_123", stripe_account=account)
self.assertTrue(SyncMock.called)


class CustomersWithConnectTests(TestCase):
Expand Down
39 changes: 35 additions & 4 deletions pinax/stripe/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ def test_link_customer_customer_created(self):
"account_balance": 0,
"active_card": None,
"created": 1363911708,
"currency": None,
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": "[email protected]",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
"sources": {
"data": [],
},
"subscriptions": {
"data": [],
},
}
},
"id": "evt_xxxxxxxxxxxxx",
Expand All @@ -59,8 +66,11 @@ def test_link_customer_customer_created(self):
webhook_message=msg,
validated_message=msg
)
self.assertIsNone(self.customer.account_balance)
customers.link_customer(event)
self.assertEquals(event.customer, self.customer)
self.customer.refresh_from_db()
self.assertEquals(self.customer.account_balance, 0)

def test_link_customer_customer_updated(self):
msg = {
Expand Down Expand Up @@ -88,14 +98,21 @@ def test_link_customer_customer_updated(self):
"type": "MasterCard"
},
"created": 1346855596,
"currency": None,
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": "[email protected]",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
"sources": {
"data": [],
},
"subscriptions": {
"data": [],
},
},
"previous_attributes": {
"active_card": None
Expand Down Expand Up @@ -125,14 +142,21 @@ def test_link_customer_customer_deleted(self):
"account_balance": 0,
"active_card": None,
"created": 1348286302,
"currency": None,
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": "[email protected]",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
"sources": {
"data": [],
},
"subscriptions": {
"data": [],
},
}
},
"id": "evt_xxxxxxxxxxxxx",
Expand Down Expand Up @@ -162,14 +186,21 @@ def test_process_customer_deleted(self, CustomerMock, EventMock):
"account_balance": 0,
"active_card": None,
"created": 1348286302,
"currency": None,
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": "[email protected]",
"id": "cus_xxxxxxxxxxxxxxx",
"livemode": True,
"object": "customer",
"subscription": None
"sources": {
"data": [],
},
"subscriptions": {
"data": [],
}
}
},
"id": "evt_xxxxxxxxxxxxx",
Expand Down