Skip to content

Commit

Permalink
Merge pull request #11 from radekholy24/error_401
Browse files Browse the repository at this point in the history
fix PayuApiError: invalid response to refund ??? of payment 193751: {'error': 'invalid_token', 'error_description': 'Access token expired'}
  • Loading branch information
PetrDlouhy authored Jun 27, 2024
2 parents e4a29f4 + db3e289 commit 64cb184
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion payments_payu/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def post_request(self, url, *args, **kwargs):
kwargs["headers"] = self.get_token_headers()
response = requests.post(url, *args, **kwargs)
response_dict = json.loads(response.text)
if (
if (response_dict.get("error") == "invalid_token") or (
"status" in response_dict
and "statusCode" in response_dict["status"]
and response_dict["status"]["statusCode"] == "UNAUTHORIZED"
Expand Down
28 changes: 28 additions & 0 deletions tests/test_payu.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,34 @@ def test_redirect_payu_unauthorized_status(self):
headers={"Content-Type": "application/x-www-form-urlencoded"},
)

def test_redirect_payu_unauthorized_error(self):
self.set_up_provider(
True, False, get_refund_description=lambda payment, amount: "test"
)

with patch("requests.post") as mocked_post:
mocked_post.return_value = MagicMock(
status_code=401,
text='{"error": "invalid_token", "error_description": "Access token expired"}',
)

with self.assertRaisesRegex(
PayuApiError,
r"^Unable to regain authorization token "
r"\{'error': 'invalid_token', 'error_description': 'Access token expired'}$",
):
self.provider.get_form(payment=self.payment)

mocked_post.assert_called_with(
"http://mock.url/pl/standard/user/oauth/authorize",
data={
"grant_type": "client_credentials",
"client_id": "123abc",
"client_secret": "123abc",
},
headers={"Content-Type": "application/x-www-form-urlencoded"},
)

def test_get_access_token_trusted_merchant(self):
self.set_up_provider(
True, False, get_refund_description=lambda payment, amount: "test"
Expand Down

0 comments on commit 64cb184

Please sign in to comment.