getswish 0.2.4__tar.gz → 0.4.0__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.
- getswish-0.4.0/CHANGELOG.md +154 -0
- getswish-0.4.0/PKG-INFO +335 -0
- getswish-0.4.0/README.md +311 -0
- getswish-0.4.0/pyproject.toml +77 -0
- getswish-0.4.0/pyproject.toml.orig +60 -0
- getswish-0.4.0/src/getswish/__init__.py +27 -0
- getswish-0.4.0/src/getswish/certificates/mss_test_3.0/Swish_Merchant_TestCertificate_1234679304.key +52 -0
- {getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates → getswish-0.4.0/src/getswish/certificates/mss_test_3.0}/Swish_Merchant_TestCertificate_1234679304.pem +27 -27
- getswish-0.4.0/src/getswish/certificates/mss_test_3.0/Swish_Merchant_TestSigningCertificate_1234679304.key +51 -0
- {getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates → getswish-0.4.0/src/getswish/certificates/mss_test_3.0}/Swish_Merchant_TestSigningCertificate_1234679304.pem +27 -27
- getswish-0.4.0/src/getswish/client.py +451 -0
- {getswish-0.2.4 → getswish-0.4.0}/src/getswish/environments.py +20 -10
- getswish-0.4.0/src/getswish/exceptions.py +21 -0
- getswish-0.4.0/src/getswish/models.py +261 -0
- {getswish-0.2.4 → getswish-0.4.0}/src/getswish/utils.py +2 -1
- getswish-0.4.0/tests/__init__.py +0 -0
- {getswish-0.2.4 → getswish-0.4.0}/tests/fixtures.py +62 -14
- getswish-0.4.0/tests/test_client.py +605 -0
- getswish-0.4.0/tests/test_environments.py +64 -0
- getswish-0.4.0/tests/test_exceptions.py +75 -0
- getswish-0.4.0/tests/test_exports.py +19 -0
- getswish-0.4.0/tests/test_models.py +120 -0
- getswish-0.2.4/.github/workflows/pytest.yml +0 -23
- getswish-0.2.4/.gitignore +0 -11
- getswish-0.2.4/CHANGELOG.md +0 -59
- getswish-0.2.4/PKG-INFO +0 -189
- getswish-0.2.4/README.md +0 -164
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_Merchant_TestCertificate_1234679304.csr +0 -28
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_Merchant_TestCertificate_1234679304.key +0 -51
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_Merchant_TestCertificate_1234679304.p12 +0 -0
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_Merchant_TestSigningCertificate_1234679304.csr +0 -28
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_Merchant_TestSigningCertificate_1234679304.key +0 -51
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_Merchant_TestSigningCertificate_1234679304.p12 +0 -0
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_TechnicalSupplier_TestCertificate_9870474641.csr +0 -28
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_TechnicalSupplier_TestCertificate_9870474641.key +0 -51
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_TechnicalSupplier_TestCertificate_9870474641.p12 +0 -0
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_TechnicalSupplier_TestCertificate_9870474641.pem +0 -98
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_TechnicalSupplier_TestCertificate_9871065216.csr +0 -28
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_TechnicalSupplier_TestCertificate_9871065216.key +0 -51
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_TechnicalSupplier_TestCertificate_9871065216.p12 +0 -0
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/Swish_TechnicalSupplier_TestCertificate_9871065216.pem +0 -97
- getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates/mss.p12 +0 -0
- getswish-0.2.4/mss_test_1.9/MSS_UserGuide_v1.9.pdf +0 -0
- getswish-0.2.4/mss_test_1.9/readme.md +0 -66
- getswish-0.2.4/noxfile.py +0 -13
- getswish-0.2.4/pyproject.toml +0 -68
- getswish-0.2.4/src/getswish/__init__.py +0 -7
- getswish-0.2.4/src/getswish/client.py +0 -181
- getswish-0.2.4/src/getswish/exceptions.py +0 -6
- getswish-0.2.4/src/getswish/models.py +0 -95
- getswish-0.2.4/tests/test_client.py +0 -115
- getswish-0.2.4/tests/test_exceptions.py +0 -29
- getswish-0.2.4/tests/test_models.py +0 -40
- {getswish-0.2.4 → getswish-0.4.0}/LICENSE +0 -0
- {getswish-0.2.4/mss_test_1.9/Getswish_Test_Certificates → getswish-0.4.0/src/getswish/certificates/mss_test_3.0}/Swish_TLS_RootCA.pem +0 -0
- /getswish-0.2.4/tests/__init__.py → /getswish-0.4.0/src/getswish/py.typed +0 -0
- {getswish-0.2.4 → getswish-0.4.0}/tests/test_utils.py +0 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.4.0] - 2026-10-20
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Added Python 3.14 and stable Python 3.15 to the required CI test matrix and package classifiers,
|
|
15
|
+
retaining Python 3.10 support.
|
|
16
|
+
- Added a development lockfile and source-only ty checking.
|
|
17
|
+
- Included a `py.typed` marker for downstream type checking.
|
|
18
|
+
- Declared public package exports with `__all__` and made the version metadata helper private.
|
|
19
|
+
- Added configurable client HTTP timeouts, defaulting to 10 seconds to connect and 30 seconds to read.
|
|
20
|
+
Set `timeout=None` to restore unlimited waiting; timeout exceptions retain their Requests types.
|
|
21
|
+
- Added experimental Recurring Payments API: `create_recurring_ecommerce_consent`, `create_recurring_mcommerce_consent`,
|
|
22
|
+
`update_recurring_consent_callback_url`, `retrieve_recurring_consent`, `delete_recurring_consent`,
|
|
23
|
+
`create_recurring_payment`, `retrieve_recurring_payment`, `check_recurring_tin_availability`,
|
|
24
|
+
`check_recurring_alias_availability`.
|
|
25
|
+
- Added experimental QR Code API: `create_prefilled_qr_code`, `create_commerce_qr_code`.
|
|
26
|
+
- Added `RecurringConsent`, `RecurringPayment`, `RecurringAvailability` model classes.
|
|
27
|
+
- Added `SwishError.raw_errors` and `SwishError.trace_id` attributes; `SwishError` now accepts
|
|
28
|
+
`list[dict] | dict | str` inputs.
|
|
29
|
+
- Added `recurring_base` and `qr_base` fields to `Environment`.
|
|
30
|
+
- Added `_recurring_url` and `_qr_url` URL helpers to `SwishClient`.
|
|
31
|
+
- Added `on_behalf_of` (partner header) support to recurring methods.
|
|
32
|
+
- Added CI workflow gates: Ruff lint, ty type checking, package build, Twine validation, and isolated
|
|
33
|
+
installed-certificate checks for wheel and source distributions.
|
|
34
|
+
- Added Python 3.10-3.13 classifiers to `pyproject.toml`.
|
|
35
|
+
|
|
36
|
+
### Changed
|
|
37
|
+
|
|
38
|
+
- Migrated maintainer setup, checks, builds, version bumps, and publishing to native uv commands and the
|
|
39
|
+
`uv_build` backend, replacing Make, Nox, Flit, and the custom version-bumping script.
|
|
40
|
+
- Moved the package version to `pyproject.toml`; `getswish.__version__` reads installed package metadata.
|
|
41
|
+
Release bumps update project metadata and lockfile without automatically committing or tagging.
|
|
42
|
+
- Incomplete environment/certificates configuration and missing required request or payout certificates now raise
|
|
43
|
+
`ValueError` instead of incidental `AttributeError` or `AssertionError`.
|
|
44
|
+
- All HTTP 4xx/5xx responses now raise `SwishError`, including empty bodies and statuses previously raised as
|
|
45
|
+
`requests.HTTPError`. JSON and text error details are preserved; empty errors fall back to the HTTP status.
|
|
46
|
+
- `SwishError` now inherits from `requests.HTTPError` and exposes the original `response` and its `request`.
|
|
47
|
+
Existing `HTTPError` and `RequestException` handlers now also catch `SwishError`; put specific `SwishError`
|
|
48
|
+
handlers first. Network, timeout, and TLS exceptions remain unchanged.
|
|
49
|
+
- Existing `SwishError` handlers now also receive authentication, rate-limit, and server errors, not just
|
|
50
|
+
payment rejections. Inspect `response.status_code` to distinguish them.
|
|
51
|
+
- Moved CI coverage, lint, and package checks to Python 3.15 and aligned Ruff's target with the Python 3.10 minimum.
|
|
52
|
+
- Marked Recurring and QR APIs experimental in 0.4.0; contract validation and stabilization are deferred to 0.5.0.
|
|
53
|
+
- Recurring endpoints corrected to match Swish documentation: id-bearing PUT/POST paths,
|
|
54
|
+
`PUT .../callback` for callback updates, `POST` with request body for availability checks,
|
|
55
|
+
`/payments/single/{paymentId}/status` for payment retrieval.
|
|
56
|
+
- QR prefilled payload now uses nested `{"value": ..., "editable": bool}` objects for
|
|
57
|
+
`payee`, `amount`, and `message` fields per Swish QR API spec.
|
|
58
|
+
- `RecurringConsent` model updated: `consent_id` replaces `id`; `sub_status`, `activated_at`,
|
|
59
|
+
`payer_tin` fields added; nested payer/payee objects handled in `to_service`/`from_service`.
|
|
60
|
+
- `RecurringPayment` model updated: `payment_id` replaces `id`; `sub_status`, `status_at` fields
|
|
61
|
+
added; nested payer/payee and amount objects handled in `to_service`/`from_service`.
|
|
62
|
+
- `RecurringAvailability` simplified to `recurring_is_offered` field matching service response.
|
|
63
|
+
- Updated type hints to use `str | None` union syntax throughout.
|
|
64
|
+
- Updated README development and release instructions to use uv and documented all commands.
|
|
65
|
+
|
|
66
|
+
Error-handling example (using an existing client and callback URL):
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import requests
|
|
70
|
+
import getswish
|
|
71
|
+
|
|
72
|
+
try:
|
|
73
|
+
payment = swish_client.create_payment(100.00, callback_url)
|
|
74
|
+
except getswish.SwishError as error:
|
|
75
|
+
print(error.response.status_code, error.errors, error.trace_id)
|
|
76
|
+
except requests.RequestException as error:
|
|
77
|
+
print("Request failed:", error)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Fixed
|
|
81
|
+
|
|
82
|
+
- Fixed custom payment payee aliases, UTC payout instruction timestamps, and shared mutable default client configuration.
|
|
83
|
+
- Preserved empty JSON/text values in `SwishError.raw_errors` and safely normalized unexpected error fields to strings.
|
|
84
|
+
- Replaced expired MSS 2.0 test credentials with MSS 3.0 credentials and included them in installed packages.
|
|
85
|
+
- Corrected the README Commerce QR example to use a fresh pending M-commerce payment token and repaired its code blocks.
|
|
86
|
+
- Corrected the README refund example to use the completed payment's reference and payer alias.
|
|
87
|
+
- Clarified that the custom certificate configuration examples use test credentials, not production credentials.
|
|
88
|
+
|
|
89
|
+
## [0.3.0] - 2025-03-10
|
|
90
|
+
|
|
91
|
+
### Added
|
|
92
|
+
|
|
93
|
+
- Added the now documented field callbackIdentifier to Payment and Refund objects.
|
|
94
|
+
- Added python 3.13 to the nox file and GitHub actions.
|
|
95
|
+
|
|
96
|
+
### Changed
|
|
97
|
+
|
|
98
|
+
- Updated all certificate references from `mss_test_1.9` to `mss_test_2.0` in `README.md` and `TestEnvironment`.
|
|
99
|
+
- Changed `utcnow` to `now` with timezone information in the `create_payout` method.
|
|
100
|
+
- Updated ruff settings in `pyproject.toml`.
|
|
101
|
+
- Updated documentation and added example.
|
|
102
|
+
|
|
103
|
+
## [0.2.4] - 2023-06-09
|
|
104
|
+
|
|
105
|
+
### Added
|
|
106
|
+
|
|
107
|
+
- Added the undocumented Payout field callbackIdentifier that is
|
|
108
|
+
included when retrieving payout updates and only in production environment.
|
|
109
|
+
|
|
110
|
+
## [0.2.3] - 2023-06-02
|
|
111
|
+
|
|
112
|
+
### Added
|
|
113
|
+
|
|
114
|
+
- Added logging.
|
|
115
|
+
- Added nox as optional dependency.
|
|
116
|
+
|
|
117
|
+
### Changed
|
|
118
|
+
|
|
119
|
+
- Updated documentation.
|
|
120
|
+
- Updated tests for coverage.
|
|
121
|
+
|
|
122
|
+
## [0.2.2] - 2023-04-13
|
|
123
|
+
|
|
124
|
+
### Changed
|
|
125
|
+
|
|
126
|
+
- Updated production environment default url.
|
|
127
|
+
- Updated default test environment parameters.
|
|
128
|
+
- Corrected project source url in pyproject.toml.
|
|
129
|
+
|
|
130
|
+
## [0.2.0] - 2023-04-12
|
|
131
|
+
|
|
132
|
+
### Added
|
|
133
|
+
|
|
134
|
+
- Added nox.
|
|
135
|
+
|
|
136
|
+
### Changed
|
|
137
|
+
|
|
138
|
+
- Updated documentation, build and testing.
|
|
139
|
+
|
|
140
|
+
## [0.1.3] - 2023-02-14
|
|
141
|
+
|
|
142
|
+
### Changed
|
|
143
|
+
|
|
144
|
+
- Updated build, CHANGELOG.md and README.md.
|
|
145
|
+
|
|
146
|
+
## [0.1.2] - 2022-10-17
|
|
147
|
+
|
|
148
|
+
- Added test cases.
|
|
149
|
+
|
|
150
|
+
## [0.1.1] - 2022-09-14
|
|
151
|
+
|
|
152
|
+
### Added
|
|
153
|
+
|
|
154
|
+
- Initial version
|
getswish-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: getswish
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Getswish python client library
|
|
5
|
+
Keywords: swish,getswish,payment,payout
|
|
6
|
+
Author: Daniel Nibon
|
|
7
|
+
Author-email: Daniel Nibon <daniel@nibon.se>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Requires-Dist: requests>=2.27
|
|
20
|
+
Requires-Dist: rsa>=4.7.2
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Project-URL: Source, https://github.com/nibon/getswish-python
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# Swish - Python client
|
|
26
|
+
|
|
27
|
+
Client library to integrate with the swish commerce and payout api.
|
|
28
|
+
|
|
29
|
+
## Supported APIs
|
|
30
|
+
|
|
31
|
+
* https://developer.swish.nu/documentation/getting-started/swish-commerce-api
|
|
32
|
+
* https://developer.swish.nu/documentation/getting-started/swish-payout-api
|
|
33
|
+
* https://developer.swish.nu/documentation/getting-started/swish-recurring-payments-api (experimental)
|
|
34
|
+
* https://developer.swish.nu/documentation/getting-started/swish-qr-code (experimental)
|
|
35
|
+
|
|
36
|
+
Recurring and QR support is experimental in 0.4.0. Contract validation and stabilization are deferred to 0.5.0;
|
|
37
|
+
these interfaces may change. Commerce and Payout are not experimental.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
pip install getswish
|
|
42
|
+
|
|
43
|
+
## Prerequisites
|
|
44
|
+
|
|
45
|
+
Swish api integration require communication certificates and keys to integrate with the commerce api and additional
|
|
46
|
+
signing certificates and keys for signing payout in the payout api. The client setup require that all the certificates
|
|
47
|
+
are available through file paths which are given on client setup. The communication certificates are create through your
|
|
48
|
+
bank portal after the appropriate agreements are signed and signing certificates are set up after additional payout
|
|
49
|
+
agreements are signed, trough https://portal.swish.nu/.
|
|
50
|
+
|
|
51
|
+
The signing certificate, private key and serial is only required when using the payout API.
|
|
52
|
+
|
|
53
|
+
## Example - Test Client
|
|
54
|
+
|
|
55
|
+
The example uses the public MSS 3.0 test credentials bundled with the installed library, valid until September 11, 2027.
|
|
56
|
+
Do not use these credentials in production. Source: the [MSS 3.0 bundle](https://assets.ctfassets.net/4dca8u8ebqnn/4CgQJUfc6ncjKHooAkqvDx/d27012ce097a5b0f6b151a8ccdfe285d/MSS_test_3.0.zip)
|
|
57
|
+
from [Swish test environments](https://developer.swish.nu/documentation/environments).
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import getswish
|
|
61
|
+
|
|
62
|
+
swish_client = getswish.SwishClient()
|
|
63
|
+
|
|
64
|
+
callback_url = "https://example.com/callback/"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
All requests default to a 10-second connect timeout and a 30-second read timeout.
|
|
68
|
+
Configure these with `getswish.SwishClient(timeout=(5.0, 60.0))`, use a single number for both,
|
|
69
|
+
or set `timeout=None` to disable timeouts. The read timeout limits waiting between received data,
|
|
70
|
+
not the total request duration. Timeouts raise Requests exceptions, not `SwishError`.
|
|
71
|
+
After a payment or payout timeout, check its status before retrying; the server may have accepted it.
|
|
72
|
+
|
|
73
|
+
### Example - Commerce API
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
# Perform a payment request - M-commerce.
|
|
77
|
+
payment_m = swish_client.create_payment(
|
|
78
|
+
100.00, callback_url, message="Product name."
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
# Perform a payment request - E-commerce.
|
|
82
|
+
payment_e = swish_client.create_payment(
|
|
83
|
+
100.00, callback_url, "46701234567", message="Product name."
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
# Retrieve info about the payment
|
|
87
|
+
payment_retrieved = swish_client.retrieve_payment(payment_e.id)
|
|
88
|
+
|
|
89
|
+
# Cancel the payment
|
|
90
|
+
payment_cancelled = swish_client.cancel_payment(payment_e.id)
|
|
91
|
+
|
|
92
|
+
# After the callback confirms payment_m is PAID, retrieve its payment reference and payer.
|
|
93
|
+
paid_payment = swish_client.retrieve_payment(payment_m.id)
|
|
94
|
+
|
|
95
|
+
# Refund the whole amount to the previous payer, who is now the refund payee.
|
|
96
|
+
payment_refund = swish_client.create_refund(
|
|
97
|
+
paid_payment.payment_reference, callback_url, paid_payment.payer_alias, paid_payment.amount
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
# Retrieve info about the refund
|
|
101
|
+
payment_refund_retrieved = swish_client.retrieve_refund(payment_refund.id)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Example - Recurring Payments API (experimental)
|
|
105
|
+
|
|
106
|
+
Recurring payments allow a merchant to charge a customer on a consent basis. The customer signs
|
|
107
|
+
a consent once (via e-commerce or m-commerce flow), and the merchant can then initiate payments
|
|
108
|
+
against that consent without further customer interaction.
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
# Check if the payer's bank supports recurring payments for their alias.
|
|
112
|
+
availability = swish_client.check_recurring_alias_availability("46701234567")
|
|
113
|
+
if not availability.recurring_is_offered:
|
|
114
|
+
raise Exception("Recurring payments not available for this payer.")
|
|
115
|
+
|
|
116
|
+
# Create a recurring consent - E-commerce flow (payer alias known).
|
|
117
|
+
consent = swish_client.create_recurring_ecommerce_consent(
|
|
118
|
+
callback_url, "46701234567", payee_payment_reference="subscription-001"
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
# Create a recurring consent - M-commerce flow (payer alias unknown; token shown to user).
|
|
122
|
+
consent_m = swish_client.create_recurring_mcommerce_consent(
|
|
123
|
+
callback_url, payee_payment_reference="subscription-001"
|
|
124
|
+
)
|
|
125
|
+
# consent_m.payment_request_token -- show this to the payer in the Swish app.
|
|
126
|
+
|
|
127
|
+
# Retrieve consent info (e.g. after callback confirms ACTIVE status).
|
|
128
|
+
consent_active = swish_client.retrieve_recurring_consent(consent.consent_id)
|
|
129
|
+
|
|
130
|
+
# Charge the customer against the active consent.
|
|
131
|
+
payment = swish_client.create_recurring_payment(
|
|
132
|
+
consent.consent_id, 100.00, callback_url, message="Monthly subscription."
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
# Check payment status.
|
|
136
|
+
payment_status = swish_client.retrieve_recurring_payment(payment.payment_id)
|
|
137
|
+
|
|
138
|
+
# Update the callback URL on an existing consent.
|
|
139
|
+
swish_client.update_recurring_consent_callback_url(consent.consent_id, "https://example.com/new-callback/")
|
|
140
|
+
|
|
141
|
+
# Delete (cancel) the consent.
|
|
142
|
+
swish_client.delete_recurring_consent(consent.consent_id)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Example - QR Code API (experimental)
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
# Generate a prefilled QR code (payee + amount pre-set, editable by payer).
|
|
149
|
+
qr_png = swish_client.create_prefilled_qr_code(
|
|
150
|
+
"png",
|
|
151
|
+
payee="1234679304",
|
|
152
|
+
amount=100.00,
|
|
153
|
+
amount_editable=True,
|
|
154
|
+
message="Order #1234",
|
|
155
|
+
size=300,
|
|
156
|
+
)
|
|
157
|
+
# qr_png is bytes -- write to a file or serve as an image response.
|
|
158
|
+
|
|
159
|
+
# Generate an SVG QR code.
|
|
160
|
+
qr_svg = swish_client.create_prefilled_qr_code("svg", payee="1234679304")
|
|
161
|
+
# qr_svg is a str containing the SVG markup.
|
|
162
|
+
|
|
163
|
+
# Create a fresh M-commerce request without a payer alias; use its token while pending.
|
|
164
|
+
qr_payment = swish_client.create_payment(100.00, callback_url, message="QR purchase.")
|
|
165
|
+
|
|
166
|
+
# Generate a QR code from this Commerce payment token, not a recurring consent token.
|
|
167
|
+
qr_commerce = swish_client.create_commerce_qr_code(
|
|
168
|
+
qr_payment.payment_request_token, "png", size=300
|
|
169
|
+
)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Example - Payout API
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
# Generate a merchant specific reference.
|
|
176
|
+
# This reference could be order id or similar.
|
|
177
|
+
# Use generate_transaction_id for convenience.
|
|
178
|
+
|
|
179
|
+
from getswish.utils import generate_transaction_id
|
|
180
|
+
|
|
181
|
+
reference_id = generate_transaction_id()
|
|
182
|
+
|
|
183
|
+
# Perform a payment request
|
|
184
|
+
payout = swish_client.create_payout(
|
|
185
|
+
reference_id, "46701234567", "197001019876", 10.00, callback_url, message="Test payout message."
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
# Retrieve info about the payout
|
|
189
|
+
payout_retrieved = swish_client.retrieve_payout(payout.payout_instruction_uuid)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Example - Custom Test Client
|
|
193
|
+
|
|
194
|
+
This example explicitly uses `getswish.TestEnvironment` and the bundled test certificates. For production, use
|
|
195
|
+
`getswish.ProductionEnvironment`, your issued communication certificate and key, the appropriate TLS trust certificate,
|
|
196
|
+
and your merchant Swish number. Do not use the bundled test credentials in production.
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
from pathlib import Path
|
|
200
|
+
import getswish
|
|
201
|
+
|
|
202
|
+
cert_base = Path(getswish.__file__).resolve().parent / "certificates" / "mss_test_3.0"
|
|
203
|
+
|
|
204
|
+
swish_client = getswish.SwishClient(
|
|
205
|
+
environment=getswish.TestEnvironment,
|
|
206
|
+
certificates=getswish.Certificates(
|
|
207
|
+
communication=getswish.Certificate(
|
|
208
|
+
public=f"{cert_base}/Swish_Merchant_TestCertificate_1234679304.pem",
|
|
209
|
+
private_key=f"{cert_base}/Swish_Merchant_TestCertificate_1234679304.key",
|
|
210
|
+
),
|
|
211
|
+
verify=getswish.Certificate(public=f"{cert_base}/Swish_TLS_RootCA.pem"),
|
|
212
|
+
),
|
|
213
|
+
merchant_swish_number="1234679304",
|
|
214
|
+
)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Example - Custom Test Client with Payout
|
|
218
|
+
|
|
219
|
+
This is also a test configuration. Payout additionally requires a signing certificate, private key, and serial number.
|
|
220
|
+
For production, use `getswish.ProductionEnvironment` and your issued communication and signing credentials instead of
|
|
221
|
+
these bundled test files.
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
from pathlib import Path
|
|
225
|
+
import getswish
|
|
226
|
+
|
|
227
|
+
cert_base = Path(getswish.__file__).resolve().parent / "certificates" / "mss_test_3.0"
|
|
228
|
+
|
|
229
|
+
swish_client = getswish.SwishClient(
|
|
230
|
+
environment=getswish.TestEnvironment,
|
|
231
|
+
certificates=getswish.Certificates(
|
|
232
|
+
communication=getswish.Certificate(
|
|
233
|
+
public=f"{cert_base}/Swish_Merchant_TestCertificate_1234679304.pem",
|
|
234
|
+
private_key=f"{cert_base}/Swish_Merchant_TestCertificate_1234679304.key",
|
|
235
|
+
),
|
|
236
|
+
verify=getswish.Certificate(public=f"{cert_base}/Swish_TLS_RootCA.pem"),
|
|
237
|
+
signing=getswish.Certificate(
|
|
238
|
+
public=f"{cert_base}/Swish_Merchant_TestSigningCertificate_1234679304.pem",
|
|
239
|
+
private_key=f"{cert_base}/Swish_Merchant_TestSigningCertificate_1234679304.key",
|
|
240
|
+
public_serial="4F24C03A0295A0B53596240EA8C0F430",
|
|
241
|
+
),
|
|
242
|
+
),
|
|
243
|
+
merchant_swish_number="1234679304",
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
callback_url = "https://example.com/callback/"
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Generating public_serial for signing certificate
|
|
250
|
+
|
|
251
|
+
The signing certificate `public_serial` is extracted from the certificate using this command on linux.
|
|
252
|
+
|
|
253
|
+
openssl x509 -in Swish_Merchant_TestSigningCertificate_1234679304.pem -serial -noout
|
|
254
|
+
|
|
255
|
+
## Error handling
|
|
256
|
+
|
|
257
|
+
All HTTP 4xx/5xx responses raise `getswish.SwishError`, including responses with empty bodies.
|
|
258
|
+
It inherits from `requests.HTTPError` (and `requests.RequestException`). Inspect `errors` for normalized error
|
|
259
|
+
messages, `raw_errors` for the unchanged parsed JSON or response text, `trace_id` when supplied, and
|
|
260
|
+
`response.status_code` to distinguish rejections, authentication failures, rate limits, and server errors.
|
|
261
|
+
Unexpected error fields are converted to strings; empty errors use an HTTP status message without changing `raw_errors`.
|
|
262
|
+
Connection failures, timeouts, and TLS errors retain their Requests exception types.
|
|
263
|
+
|
|
264
|
+
```python
|
|
265
|
+
import requests
|
|
266
|
+
import getswish
|
|
267
|
+
|
|
268
|
+
try:
|
|
269
|
+
payment = swish_client.create_payment(100.00, callback_url)
|
|
270
|
+
except getswish.SwishError as error:
|
|
271
|
+
print(error.response.status_code, error.errors, error.trace_id)
|
|
272
|
+
except requests.RequestException as error:
|
|
273
|
+
print("Request failed:", error)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Development setup
|
|
277
|
+
|
|
278
|
+
Install [uv](https://docs.astral.sh/uv/getting-started/installation/) (CI uses uv 0.13.1), clone the repository,
|
|
279
|
+
and sync the locked development environment. uv installs the required Python if necessary.
|
|
280
|
+
|
|
281
|
+
git clone https://github.com/nibon/getswish-python.git && cd getswish-python
|
|
282
|
+
|
|
283
|
+
uv sync --locked
|
|
284
|
+
|
|
285
|
+
### Testing
|
|
286
|
+
|
|
287
|
+
Run all tests.
|
|
288
|
+
|
|
289
|
+
uv run --locked pytest
|
|
290
|
+
|
|
291
|
+
Run one focused test.
|
|
292
|
+
|
|
293
|
+
uv run --locked pytest tests/test_client.py::test_requests_create_payment
|
|
294
|
+
|
|
295
|
+
Run coverage the same way as CI.
|
|
296
|
+
|
|
297
|
+
uv run --locked pytest --cov=src
|
|
298
|
+
|
|
299
|
+
Run the source lint check.
|
|
300
|
+
|
|
301
|
+
uv run --locked ruff check src
|
|
302
|
+
|
|
303
|
+
Run the source-only type check (Python 3.10 is the minimum supported version).
|
|
304
|
+
|
|
305
|
+
uv run --locked ty check src
|
|
306
|
+
|
|
307
|
+
Build and check the package.
|
|
308
|
+
|
|
309
|
+
uv build
|
|
310
|
+
uv run --locked twine check dist/getswish-*.whl dist/getswish-*.tar.gz
|
|
311
|
+
|
|
312
|
+
To test another interpreter locally, run `uv run --locked --python 3.10 pytest` (or 3.11-3.15).
|
|
313
|
+
CI runs the full six-version matrix; uv does not provide a single-command matrix runner.
|
|
314
|
+
|
|
315
|
+
The lockfile is for development; library consumers use the dependency ranges in `pyproject.toml`.
|
|
316
|
+
|
|
317
|
+
### Release workflow
|
|
318
|
+
|
|
319
|
+
Update the changelog, then bump the version with `uv version --bump patch` (or `minor` or `major`).
|
|
320
|
+
uv normally updates `pyproject.toml` and `uv.lock` and syncs the environment; it does not commit or tag.
|
|
321
|
+
Review the diff and commit the version, lockfile, and changelog with ordinary Git commands. To keep unrelated
|
|
322
|
+
staged changes out of the release commit, specify paths with `git commit --only -- pyproject.toml uv.lock CHANGELOG.md`.
|
|
323
|
+
CI runs the full matrix, coverage, lint, types, build, distribution validation, and installed-resource checks.
|
|
324
|
+
|
|
325
|
+
uv build
|
|
326
|
+
uv run --locked twine check dist/getswish-*.whl dist/getswish-*.tar.gz
|
|
327
|
+
|
|
328
|
+
Review the changes and wait for CI. `uv publish` uploads existing artifacts; it does not build them.
|
|
329
|
+
Select only the artifacts for the current version, not all files under `dist/`. For example, for version 0.4.0:
|
|
330
|
+
|
|
331
|
+
uv publish --index testpypi dist/getswish-0.4.0-py3-none-any.whl dist/getswish-0.4.0.tar.gz
|
|
332
|
+
uv publish dist/getswish-0.4.0-py3-none-any.whl dist/getswish-0.4.0.tar.gz
|
|
333
|
+
|
|
334
|
+
Set `UV_PUBLISH_TOKEN` to the appropriate API token for each destination (TestPyPI and PyPI use different tokens).
|
|
335
|
+
The `testpypi` publish index is configured in `pyproject.toml`. Never reuse a TestPyPI token on PyPI.
|