square / square-python-sdk

Python client library for the Square API
https://developer.squareup.com
Other
101 stars 48 forks source link

square.exceptions.api_exception.APIException HTTP response not OK. for test case responses #102

Closed rechner closed 1 year ago

rechner commented 2 years ago

Describe the bug Regression in latest SDK version with payments API raises an exception for test values which indicate a payment failure.

Expected behavior Following the create payment example from the documentation should not result in an exception when payment fails, instead following the is_error() pattern and only raising an exception when there is an error in making the request.

To Reproduce Steps to reproduce the bug:

  1. Make a request using any of the error mapped source_id test values
  2. Observe raised exception square.exceptions.api_exception.APIException: HTTP response not OK.

An example payload body and code which produces this error, but works on the previous SDK version:

{'amount_money': {'amount': 1000, 'currency': 'USD'},
 'app_fee_money': {'amount': 10, 'currency': 'USD'},
 'autocomplete': True,
 'idempotency_key': '61d82da9-43e8-43cf-9977-7f7cbe36be73',
 'location_id': 'MESD3N22DWR0F',
 'note': 'Brief description',
 'reference_id': '123456',
 'source_id': 'cnon:card-nonce-rejected-cvv'}

Full traceback:

Traceback (most recent call last):
  File "test.py", line 37, in <module>
    result = payments_api.create_payment(body)
  File "/home/rechner/workspace/scratch/venv/lib/python3.8/site-packages/square/api/payments_api.py", line 155, in create_payment
    return super().new_api_call_builder.request(
  File "/home/rechner/workspace/scratch/venv/lib/python3.8/site-packages/apimatic_core/api_call.py", line 72, in execute
    return self._response_handler.endpoint_logger(self._endpoint_logger) \
  File "/home/rechner/workspace/scratch/venv/lib/python3.8/site-packages/apimatic_core/response_handler.py", line 76, in handle
    self.validate(response, global_errors)
  File "/home/rechner/workspace/scratch/venv/lib/python3.8/site-packages/apimatic_core/response_handler.py", line 103, in validate
    raise error_case.get_exception_type()(error_case.get_description(), response)
square.exceptions.api_exception.APIException: HTTP response not OK.

Screenshots

Example code ```python import os import uuid import pprint from square.client import Client client = Client( access_token=os.environ['SQUARE_ACCESS_TOKEN'], environment='sandbox' ) result = client.locations.list_locations() if result.is_success(): location_id = result.body['locations'][0]['id'] else: print(result.errors) exit() payments_api = client.payments body = {} body['source_id'] = 'cnon:card-nonce-rejected-cvv' body['idempotency_key'] = str(uuid.uuid4()) body['amount_money'] = {} body['amount_money']['amount'] = 1000 body['amount_money']['currency'] = 'USD' body['app_fee_money'] = {} body['app_fee_money']['amount'] = 10 body['app_fee_money']['currency'] = 'USD' body['autocomplete'] = True body['location_id'] = location_id body['reference_id'] = '123456' body['note'] = 'Brief description' print("Request body:") pprint.pprint(body) result = payments_api.create_payment(body) if result.is_success(): print(result.body) elif result.is_error(): print(result.errors) ```

Square SDK version 23.0.0.20221019, tested also with 24.0.0.20221116 and found to have the same issue.

Additional context I use these premade nonce values to test error conditions in my code. Tests pass on version 22 of the SDK.

icarlthiscode commented 1 year ago

@rechner, I don't know anything about the test values. But, I came across this issue because I was trying to use the is_error() pattern and getting an unhandled exception.

I can't speak for the Square devs, but after poking around the code I found that catching the APIException in an except block seems to be the new pattern for checking response status. The API errors you were expecting can be found in APIException.response.text.

try:
    result = client.locations.list_locations()
except APIException as e:
    errors = json.loads(e.response.text)['errors']

I don't know if this helps you, but it would have helped me. Hopefully the devs can comment on whether or not this is a bug or a feature. If this is intentional, the SDK docs could really use some updating.

zenmasterjobo commented 1 year ago

Hi @rechner and @icarlthiscode

@rechner thanks so much for the detailed issue description, it made getting up to speed on this a lot easier.

@icarlthiscode thanks for adding some additional thoughts and helping out!

@rechner I confirmed this issue exists in the sdk versions you mentioned about, and I have also confirmed that this has been fixed. Checking 25.2.0.20230315 and the issue has been resolved.

If you run into further issues with this, please consider posting on our Developer Forums - These forums are actively monitored by Square employees as well as has engagement from the community. You might also find your issue has been posted here before and answered!

Cheers!