diff --git a/CHANGELOG.txt b/CHANGELOG.txt index dc98156..dfede8e 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,5 +1,9 @@ # Change Log All notable changes to this project will be documented in this file. +## [0.6.6] +### Fixed +- refresh_customers command + ## [0.6.5] ### Fixed - setting StripeCustomer.default_source when creating a new customer from API diff --git a/aa_stripe/__init__.py b/aa_stripe/__init__.py index 817dbf9..ce68afc 100644 --- a/aa_stripe/__init__.py +++ b/aa_stripe/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- __title__ = "Arabella Stripe" -__version__ = "0.6.5" +__version__ = "0.6.6" __author__ = "Jacek Ostanski" __license__ = "MIT" __copyright__ = "Copyright 2017 Arabella" diff --git a/aa_stripe/management/commands/refresh_customers.py b/aa_stripe/management/commands/refresh_customers.py index e43440e..5de3cf7 100644 --- a/aa_stripe/management/commands/refresh_customers.py +++ b/aa_stripe/management/commands/refresh_customers.py @@ -32,7 +32,7 @@ def handle(self, *args, **options): for stripe_customer in response["data"]: updated_count += StripeCustomer.objects.filter(stripe_customer_id=stripe_customer["id"]).update( - sources=stripe_customer["sources"], default_source=stripe_customer["default_source"] + sources=stripe_customer["sources"]["data"], default_source=stripe_customer["default_source"] ) if not response["has_more"]: diff --git a/tests/test_customers.py b/tests/test_customers.py index 7b849e2..3fab1e2 100644 --- a/tests/test_customers.py +++ b/tests/test_customers.py @@ -147,37 +147,36 @@ def setUp(self): @requests_mock.Mocker() def test_command(self, m): - customer1_data = { - "id": "cus_xyz", - "object": "customer", - "sources": [ - {"id": "card_1"} - ], - "default_source": "card_1" - } - customer2_data = customer1_data.copy() - customer2_data["id"] = "cus_2" - customer2_data["sources"] = [{"id": "card_2"}] - customer2_data["default_source"] = "card_2" - customer2_data = customer1_data.copy() - customer2_data["id"] = "cus_2" - customer2_data["sources"] = [] - customer2_data["default_source"] = None + def get_customer_data(customer_id, sources, default_source=None): + return { + "id": customer_id, + "object": "customer", + "sources": { + "object": "list", + "data": sources, + "has_more": False, + "total_count": 1, + "url": f"/v1/customers/{customer_id}/sources" + }, + "default_source": default_source + } + stripe_response_part1 = { + "object": "list", + "url": "/v1/customers", + "has_more": True, + "data": [get_customer_data("cus_xyz", [{"id": "card_1"}], default_source="card_1")] + } + stripe_response_part2 = { "object": "list", "url": "/v1/customers", "has_more": False, - "data": [ - customer1_data - ] + "data": [get_customer_data("cus_b", [{"id": "card_2"}])] } - stripe_response_part2 = stripe_response_part1.copy() - stripe_response_part2["has_more"] = False - stripe_response_part2["data"] = [customer2_data] m.register_uri("GET", "https://api.stripe.com/v1/customers", text=json.dumps(stripe_response_part1)) m.register_uri("GET", "https://api.stripe.com/v1/customers?starting_after=cus_xyz", [ {"text": "", "status_code": 500}, # make sure the command will try again - {"text": json.dumps(stripe_response_part2), "status_code": "200"} + {"text": json.dumps(stripe_response_part2), "status_code": 200} ]) call_command("refresh_customers") self.active_customer.refresh_from_db()