diff --git a/localstripe/resources.py b/localstripe/resources.py index 41743c5e..0d45b838 100644 --- a/localstripe/resources.py +++ b/localstripe/resources.py @@ -318,6 +318,22 @@ def _attaching_is_declined(self): def _charging_is_declined(self): return PaymentMethod._charging_is_declined(self) + @classmethod + def _api_list_all(cls, url, customer=None, limit=10): + try: + if customer is not None: + assert type(customer) is str and customer.startswith('cus_') + except AssertionError: + raise UserError(400, 'Bad request') + + if customer: + Customer._api_retrieve(customer) # to return 404 if not existant + + li = super(Card, cls)._api_list_all(url, limit=limit) + if customer: + li._list = [c for c in li._list if c.customer == customer] + return li + class Charge(StripeObject): object = 'charge' @@ -678,6 +694,28 @@ def _api_delete(cls, id): schedule_webhook(Event('customer.deleted', obj)) return super()._api_delete(id) + @classmethod + def _api_list_sources(cls, id, object=None, limit=10): + print(object) + try: + if object is not None: + assert object in ('card', 'bank_account') + except AssertionError: + raise UserError(400, 'Bad request') + + obj = cls._api_retrieve(id) # return 404 if does not exist + + url = '/v1/customers/' + obj.id + '/sources' + + if object is 'card': + return Card._api_list_all(url, customer=obj.id) + + #TODO: implement bank accounts + if object is 'bank_account': + return List(url, limit=limit) + + return obj.sources + @classmethod def _api_retrieve_source(cls, id, source_id, **kwargs): if kwargs: @@ -818,6 +856,9 @@ def _api_update_subscription(cls, id, subscription_id, **data): extra_apis.extend(( + # Retrieve list of sources: + ('GET', '/v1/customers/{id}/sources', Customer._api_list_sources), + # Add a source ('POST', '/v1/customers/{id}/sources', Customer._api_add_source), # Retrieve single source by id: ('GET', '/v1/customers/{id}/sources/{source_id}', diff --git a/test.sh b/test.sh index ac5d2813..8d3923e5 100755 --- a/test.sh +++ b/test.sh @@ -197,6 +197,20 @@ res=$( | grep -oE $card) [ -n "$res" ] +# observe new card in customer sources response +res=$( + curl -sSf -u $SK: $HOST/v1/customers/$cus/sources \ + | grep -oE $card) +[ -n "$res" ] + + +# observe new card in customer sources response when requesting object=card +res=$( + curl -sSfG -u $SK: $HOST/v1/customers/$cus/sources \ + -d object=card \ + | grep -oE $card) +[ -n "$res" ] + # delete the card curl -sSf -u $SK: $HOST/v1/customers/$cus/sources/$card \ -X DELETE