-
Notifications
You must be signed in to change notification settings - Fork 47
Description
Describe the bug
Recently one of the PRs changed the behaviour for obtaining PSP reference first from the body and then looking at the header:
More specifically the following code [here] was added:
@staticmethod
def _get_psp(response, header):
psp_ref = response.get('pspReference')
if psp_ref == "":
return header.get('pspReference')
else:
return psp_ref
The problem here is that if pspReference
is not present in the response, the variable psp_ref
is set to None. The comparison will fail and in the end the psp_ref
is returned which is None.
Proof
response_1 = {'test': 1, 'pspReference': 'ABCDE'}
response_2 = {'test': 1}
headers = {'pspReference': 'ABCDE'}
# Code taken from repository
def _get_psp(response, header):
psp_ref = response.get('pspReference')
if psp_ref == "":
return header.get('pspReference')
else:
return psp_ref
print('Response 1 - PSP Reference: {}'.format(_get_psp(response_1, headers)))
print('Response 2 - PSP Reference: {}'.format(_get_psp(response_2, headers)))
when run will display the following:
Response 1 - PSP Reference: ABCDE
Response 2 - PSP Reference: None
** Process exited - Return Code: 0 **
Expected behavior
I expect the def _get_psp(response, header)
method to following the documentation:
Solution
I suggest something along the lines of:
def _get_psp(response, header):
psp_ref = response.get('pspReference', header.get('pspReference'))
if psp_ref is None: # If `pspReference` is not present in response or header, raise some exception or error
raise something
if psp_ref == "": # If `pspReference` is an empty string, raise some exception or error
raise something
return psp_ref
It will first attempt to get the pspReference
from the response, and if not found, it will get it from the header. You should probably still validate if psp_ref
is set to None or is an empty string.
Additional context
I discovered this bug while using the Sessions endpoint within the Checkout API [docs], as the response does not contain a pspReference
. As a result we do not have the original PSP reference and this makes any attempts at cancel, refund or reversals not possible (as the original PSP is required and not the PSP reference of any of the webhooks which are received later).