Skip to content

Commit

Permalink
TaxID: Expand API to allow retrieving or deleting a single TaxID
Browse files Browse the repository at this point in the history
Creating and listing TaxID have been supported for a long time, but retrieving
or deleting a single TaxID wasn't implemented, so let's change that.

Stripe doc:
- https://stripe.com/docs/api/customer_tax_ids/retrieve
- https://stripe.com/docs/api/customer_tax_ids/delete
  • Loading branch information
feliixx committed Jul 6, 2023
1 parent cb071d2 commit 0c6df55
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
29 changes: 28 additions & 1 deletion localstripe/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,29 @@ def _api_list_tax_ids(cls, id, **kwargs):
obj = cls._api_retrieve(id)
return obj.tax_ids

@classmethod
def _api_retrieve_tax_id(cls, id, tax_id, **kwargs):
if kwargs:
raise UserError(400, 'Unexpected ' + ', '.join(kwargs.keys()))

obj = cls._api_retrieve(id)
for txi in obj.tax_ids._list:
if txi.id == tax_id:
return txi
raise UserError(404, 'Customer ' + id + ' does not have a tax ID with '
'ID ' + tax_id)

@classmethod
def _api_delete_tax_id(cls, id, tax_id, **kwargs):
if kwargs:
raise UserError(400, 'Unexpected ' + ', '.join(kwargs.keys()))

obj = cls._api_retrieve(id)
tax_id_obj = cls._api_retrieve_tax_id(id, tax_id)
obj.tax_ids._list.remove(tax_id_obj)

return {'id': tax_id_obj.id, 'object': 'tax_id', 'deleted': True}

@classmethod
def _api_list_subscriptions(cls, id, **kwargs):
if kwargs:
Expand Down Expand Up @@ -1034,8 +1057,12 @@ def _api_update_subscription(cls, id, subscription_id, **data):
Customer._api_update_subscription),
# This is the old API route:
('POST', '/v1/customers/{id}/cards', Customer._api_add_source),
('GET', '/v1/customers/{id}/tax_ids', Customer._api_list_tax_ids),
('GET', '/v1/customers/{id}/tax_ids/{tax_id}',
Customer._api_retrieve_tax_id),
('POST', '/v1/customers/{id}/tax_ids', Customer._api_add_tax_id),
('GET', '/v1/customers/{id}/tax_ids', Customer._api_list_tax_ids)))
('DELETE', '/v1/customers/{id}/tax_ids/{tax_id}',
Customer._api_delete_tax_id)))


class Event(StripeObject):
Expand Down
6 changes: 5 additions & 1 deletion localstripe/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,12 @@ async def f(request):
data['source_id'] = request.match_info['source_id']
if 'subscription_id' in request.match_info:
data['subscription_id'] = request.match_info['subscription_id']
if 'tax_id' in request.match_info:
data['tax_id'] = request.match_info['tax_id']
expand = data.pop('expand', None)
return json_response(func(**data)._export(expand=expand))
ret = func(**data)
return json_response(ret if not hasattr(ret, '_export') else
ret._export(expand=expand))
return f


Expand Down
16 changes: 15 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ cus=$(curl -sSfg -u $SK: $HOST/v1/customers \
| grep -oE 'cus_\w+' | head -n 1)

curl -sSfg -u $SK: $HOST/v1/customers/$cus/tax_ids \
-d type=eu_vat -d value=DE123456789 \
-d type=eu_vat \
-d value=DE123456789 \
-d expand[]=customer

tax_id=$(curl -sSfg -u $SK: $HOST/v1/customers/$cus/tax_ids \
-d type=eu_vat \
-d value=BE00111222333 \
| grep -oE 'txi_\w+' | head -n 1)

curl -sSfg -u $SK: $HOST/v1/customers/$cus?expand[]=tax_ids.data.customer

curl -sSfg -u $SK: $HOST/v1/customers/$cus?expand[]=subscriptions.data.items.data
Expand All @@ -48,6 +54,14 @@ code=$(curl -sg -o /dev/null -w '%{http_code}' -u $SK: \
$HOST/v1/customers/$cus?expand[]=subscriptions.data.items.data.tax_ids)
[ "$code" -eq 400 ]

curl -sSfg -u $SK: $HOST/v1/customers/$cus/tax_ids/$tax_id

curl -sSfg -u $SK: -X DELETE $HOST/v1/customers/$cus/tax_ids/$tax_id

code=$(curl -sg -o /dev/null -w '%{http_code}' -u $SK: \
$HOST/v1/customers/$cus/tax_ids/$tax_id)
[ "$code" -eq 404 ]

txr1=$(curl -sSfg -u $SK: $HOST/v1/tax_rates \
-d display_name=VAT \
-d description='TVA France taux normal' \
Expand Down

0 comments on commit 0c6df55

Please sign in to comment.