python-payway 0.0.10__tar.gz → 0.0.11__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. {python_payway-0.0.10/python_payway.egg-info → python_payway-0.0.11}/PKG-INFO +22 -1
  2. {python_payway-0.0.10 → python_payway-0.0.11}/README.md +21 -0
  3. {python_payway-0.0.10 → python_payway-0.0.11}/payway/client.py +17 -0
  4. {python_payway-0.0.10 → python_payway-0.0.11}/payway/constants.py +1 -0
  5. {python_payway-0.0.10 → python_payway-0.0.11}/payway/model.py +11 -0
  6. {python_payway-0.0.10 → python_payway-0.0.11}/pyproject.toml +1 -1
  7. {python_payway-0.0.10 → python_payway-0.0.11/python_payway.egg-info}/PKG-INFO +22 -1
  8. {python_payway-0.0.10 → python_payway-0.0.11}/tests/test_client.py +62 -0
  9. {python_payway-0.0.10 → python_payway-0.0.11}/LICENSE +0 -0
  10. {python_payway-0.0.10 → python_payway-0.0.11}/payway/__init__.py +0 -0
  11. {python_payway-0.0.10 → python_payway-0.0.11}/payway/customers.py +0 -0
  12. {python_payway-0.0.10 → python_payway-0.0.11}/payway/exceptions.py +0 -0
  13. {python_payway-0.0.10 → python_payway-0.0.11}/payway/test_utils.py +0 -0
  14. {python_payway-0.0.10 → python_payway-0.0.11}/payway/transactions.py +0 -0
  15. {python_payway-0.0.10 → python_payway-0.0.11}/payway/utils.py +0 -0
  16. {python_payway-0.0.10 → python_payway-0.0.11}/python_payway.egg-info/SOURCES.txt +0 -0
  17. {python_payway-0.0.10 → python_payway-0.0.11}/python_payway.egg-info/dependency_links.txt +0 -0
  18. {python_payway-0.0.10 → python_payway-0.0.11}/python_payway.egg-info/requires.txt +0 -0
  19. {python_payway-0.0.10 → python_payway-0.0.11}/python_payway.egg-info/top_level.txt +0 -0
  20. {python_payway-0.0.10 → python_payway-0.0.11}/setup.cfg +0 -0
  21. {python_payway-0.0.10 → python_payway-0.0.11}/tests/test_customers.py +0 -0
  22. {python_payway-0.0.10 → python_payway-0.0.11}/tests/test_transactions.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-payway
3
- Version: 0.0.10
3
+ Version: 0.0.11
4
4
  Summary: Python client for working with Westpac's PayWay REST API
5
5
  Author-email: Ben Napper <reppan197@gmail.com>
6
6
  License: MIT
@@ -274,6 +274,27 @@ Update a customer's payment setup with a new credit card or bank account in PayW
274
274
  payment_setup, errors = client.update_payment_setup(new_token, payway_customer.customer_number)
275
275
  ```
276
276
 
277
+ ## Renewing the secret API key
278
+
279
+ Secret API keys expire one year after they are created. PayWay generates the replacement
280
+ 40 days before that, so an application that asks for the latest key once a day and stores
281
+ what it gets back rolls onto the new key without an administrator creating one in the
282
+ PayWay website.
283
+
284
+ ```python
285
+ api_key, errors = client.get_latest_api_key()
286
+ if api_key and api_key.key != stored_secret_api_key:
287
+ # Persist api_key.key. Log api_key.key_name - it is masked; the key itself is a password.
288
+ save_secret_api_key(api_key.key)
289
+ ```
290
+
291
+ Usually the key returned is the one that authenticated the call. Renewal chains off the
292
+ live key, so if the stored key is left to expire the call fails with a `PaywayError` and
293
+ recovery means minting a key by hand — poll daily and alert on repeated failures.
294
+
295
+ To test your renewal code, create two secret API keys in the PayWay website, configure the
296
+ first, and confirm the application switches to the second on its own.
297
+
277
298
  ## Additional notes
278
299
 
279
300
  PayWay API documentation <https://www.payway.com.au/docs/rest.html>
@@ -251,6 +251,27 @@ Update a customer's payment setup with a new credit card or bank account in PayW
251
251
  payment_setup, errors = client.update_payment_setup(new_token, payway_customer.customer_number)
252
252
  ```
253
253
 
254
+ ## Renewing the secret API key
255
+
256
+ Secret API keys expire one year after they are created. PayWay generates the replacement
257
+ 40 days before that, so an application that asks for the latest key once a day and stores
258
+ what it gets back rolls onto the new key without an administrator creating one in the
259
+ PayWay website.
260
+
261
+ ```python
262
+ api_key, errors = client.get_latest_api_key()
263
+ if api_key and api_key.key != stored_secret_api_key:
264
+ # Persist api_key.key. Log api_key.key_name - it is masked; the key itself is a password.
265
+ save_secret_api_key(api_key.key)
266
+ ```
267
+
268
+ Usually the key returned is the one that authenticated the call. Renewal chains off the
269
+ live key, so if the stored key is left to expire the call fails with a `PaywayError` and
270
+ recovery means minting a key by hand — poll daily and alert on repeated failures.
271
+
272
+ To test your renewal code, create two secret API keys in the PayWay website, configure the
273
+ first, and confirm the application switches to the second on its own.
274
+
254
275
  ## Additional notes
255
276
 
256
277
  PayWay API documentation <https://www.payway.com.au/docs/rest.html>
@@ -10,6 +10,7 @@ from typing import Any
10
10
  import requests
11
11
 
12
12
  from payway.constants import (
13
+ API_KEY_URL,
13
14
  BANK_ACCOUNT_PAYMENT_CHOICE,
14
15
  CREDIT_CARD_PAYMENT_CHOICE,
15
16
  CUSTOMER_URL,
@@ -26,6 +27,7 @@ from payway.model import (
26
27
  BankAccount,
27
28
  PaymentError,
28
29
  PaymentSetup,
30
+ PayWayApiKey,
29
31
  PayWayCard,
30
32
  PayWayCustomer,
31
33
  PayWayPayment,
@@ -361,6 +363,21 @@ class Client(CustomerRequest, TransactionRequest):
361
363
  return None, errors
362
364
  return PayWayTransaction.from_dict(response.json()), errors
363
365
 
366
+ def get_latest_api_key(self) -> tuple[PayWayApiKey | None, list[PaymentError] | None]:
367
+ """
368
+ Return the secret API key to use from now on, authenticated with the current one.
369
+ Usually this is the key already in use. PayWay generates the replacement 40 days
370
+ before the current key expires, and returns that instead once it exists, so callers
371
+ polling daily and persisting the result renew without an administrator minting a key
372
+ in the PayWay website.
373
+ https://www.payway.com.au/docs/rest.html#automate-secret-api-key-renewal
374
+ """
375
+ response = self.get_request(f"{API_KEY_URL}/latest")
376
+ errors = self._validate_response(response)
377
+ if errors:
378
+ return None, errors
379
+ return PayWayApiKey.from_dict(response.json()), errors
380
+
364
381
  def get_customer(self, customer_id: str) -> tuple[PayWayCustomer | None, list[PaymentError] | None]:
365
382
  """
366
383
  Returns a PayWay Customer's Payment Setup, [Payment] Schedule, Contact Details, Custom Fields and Notes
@@ -13,6 +13,7 @@ PAYWAY_API_URL = "https://api.payway.com.au/rest/v1"
13
13
  TOKEN_URL = PAYWAY_API_URL + "/single-use-tokens"
14
14
  TRANSACTION_URL = PAYWAY_API_URL + "/transactions"
15
15
  CUSTOMER_URL = PAYWAY_API_URL + "/customers"
16
+ API_KEY_URL = PAYWAY_API_URL + "/api-keys"
16
17
  TRANSACTION_APPROVED = "0"
17
18
 
18
19
  SUMMARY_CODES = {
@@ -295,3 +295,14 @@ class TokenResponse(PayWayModel):
295
295
  payment_method: str | None = None
296
296
  card: PayWayCard | None = field(default=None, metadata={"alias": "creditCard", "from_dict": PayWayCard.from_dict})
297
297
  bank_account: dict[str, Any] | None = None
298
+
299
+
300
+ @dataclass
301
+ class PayWayApiKey(PayWayModel):
302
+ """
303
+ key_name: str: masked form of the key, safe to log
304
+ key: str: the secret API key itself - treat it like a password
305
+ """
306
+
307
+ key_name: str | None = None
308
+ key: str | None = None
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "python-payway"
7
- version = "0.0.10"
7
+ version = "0.0.11"
8
8
  description = "Python client for working with Westpac's PayWay REST API"
9
9
  authors = [
10
10
  { name = "Ben Napper", email = "reppan197@gmail.com" }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-payway
3
- Version: 0.0.10
3
+ Version: 0.0.11
4
4
  Summary: Python client for working with Westpac's PayWay REST API
5
5
  Author-email: Ben Napper <reppan197@gmail.com>
6
6
  License: MIT
@@ -274,6 +274,27 @@ Update a customer's payment setup with a new credit card or bank account in PayW
274
274
  payment_setup, errors = client.update_payment_setup(new_token, payway_customer.customer_number)
275
275
  ```
276
276
 
277
+ ## Renewing the secret API key
278
+
279
+ Secret API keys expire one year after they are created. PayWay generates the replacement
280
+ 40 days before that, so an application that asks for the latest key once a day and stores
281
+ what it gets back rolls onto the new key without an administrator creating one in the
282
+ PayWay website.
283
+
284
+ ```python
285
+ api_key, errors = client.get_latest_api_key()
286
+ if api_key and api_key.key != stored_secret_api_key:
287
+ # Persist api_key.key. Log api_key.key_name - it is masked; the key itself is a password.
288
+ save_secret_api_key(api_key.key)
289
+ ```
290
+
291
+ Usually the key returned is the one that authenticated the call. Renewal chains off the
292
+ live key, so if the stored key is left to expire the call fails with a `PaywayError` and
293
+ recovery means minting a key by hand — poll daily and alert on repeated failures.
294
+
295
+ To test your renewal code, create two secret API keys in the PayWay website, configure the
296
+ first, and confirm the application switches to the second on its own.
297
+
277
298
  ## Additional notes
278
299
 
279
300
  PayWay API documentation <https://www.payway.com.au/docs/rest.html>
@@ -285,6 +285,68 @@ class TestClient(unittest.TestCase):
285
285
  self.assertIsNone(ps_errors)
286
286
  self.assertIsNotNone(ps)
287
287
 
288
+ @patch("requests.get")
289
+ def test_get_latest_api_key(self, mock_get) -> None:
290
+ mock_get.return_value.status_code = 200
291
+ mock_get.return_value.json.return_value = {
292
+ "keyName": "T10000_SEC...1A4",
293
+ "key": "T10000_SEC_RANDOM_RANDOM_RANDOM_1A4",
294
+ }
295
+
296
+ api_key, errors = self.client.get_latest_api_key()
297
+
298
+ self.assertIsNone(errors)
299
+ self.assertEqual(api_key.key_name, "T10000_SEC...1A4")
300
+ self.assertEqual(api_key.key, "T10000_SEC_RANDOM_RANDOM_RANDOM_1A4")
301
+ self.assertEqual(mock_get.call_args.kwargs["url"], "https://api.payway.com.au/rest/v1/api-keys/latest")
302
+ self.assertEqual(mock_get.call_args.kwargs["auth"], ("TPUBLISHABLE-SECRET", ""))
303
+
304
+ @patch("requests.get")
305
+ def test_get_latest_api_key_returns_the_replacement_key(self, mock_get) -> None:
306
+ """
307
+ PayWay generates the next secret key 40 days before the current one expires and
308
+ returns it here in place of the key that authenticated the request.
309
+ """
310
+ mock_get.return_value.status_code = 200
311
+ mock_get.return_value.json.return_value = {
312
+ "keyName": "T10000_SEC...9B2",
313
+ "key": "T10000_SEC_NEXT_NEXT_NEXT_9B2",
314
+ }
315
+
316
+ api_key, errors = self.client.get_latest_api_key()
317
+
318
+ self.assertIsNone(errors)
319
+ self.assertNotEqual(api_key.key, self.client.secret_api_key)
320
+ self.assertEqual(api_key.key, "T10000_SEC_NEXT_NEXT_NEXT_9B2")
321
+
322
+ @patch("requests.get")
323
+ def test_get_latest_api_key_returns_payway_errors(self, mock_get) -> None:
324
+ mock_get.return_value.status_code = 422
325
+ mock_get.return_value.json.return_value = {
326
+ "data": [{"fieldName": "apiKey", "message": "Invalid API key.", "fieldValue": ""}]
327
+ }
328
+
329
+ api_key, errors = self.client.get_latest_api_key()
330
+
331
+ self.assertIsNone(api_key)
332
+ self.assertEqual(len(errors), 1)
333
+ self.assertEqual(errors[0].message, "Invalid API key.")
334
+
335
+ @patch("requests.get")
336
+ def test_get_latest_api_key_raises_when_the_current_key_is_rejected(self, mock_get) -> None:
337
+ """
338
+ Renewal chains off the live key, so an expired one cannot fetch its replacement -
339
+ recovering needs an administrator to create a key in the PayWay website.
340
+ """
341
+ mock_get.return_value.status_code = 401
342
+ mock_get.return_value.reason = "Unauthorized"
343
+ mock_get.return_value.url = "https://api.payway.com.au/rest/v1/api-keys/latest"
344
+
345
+ with self.assertRaises(PaywayError) as context:
346
+ self.client.get_latest_api_key()
347
+
348
+ self.assertIn("401", str(context.exception))
349
+
288
350
 
289
351
  class TestClientRetries(unittest.TestCase):
290
352
  def setUp(self) -> None:
File without changes
File without changes