payfast 0.1__py3-none-any.whl

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.
payfast/__init__.py ADDED
File without changes
payfast/api.py ADDED
@@ -0,0 +1,116 @@
1
+ from datetime import datetime
2
+ import hashlib
3
+ from typing import Optional
4
+ import urllib.parse
5
+ from pydantic import BaseModel, ValidationError
6
+ import pytz
7
+ import requests
8
+
9
+
10
+ class PayfastHeaders(BaseModel):
11
+ merchant_id: int
12
+ version: str
13
+ timestamp: str
14
+
15
+
16
+ class PayfastBodyTokenization(BaseModel):
17
+ amount: int
18
+ item_name: str
19
+ item_description: Optional[str]
20
+ itn: Optional[bool] = True
21
+ m_payment_id: Optional[str]
22
+ cc_cvv: Optional[int]
23
+ setup: Optional[str]
24
+
25
+
26
+ class PayfastAPI:
27
+ def __init__(
28
+ self, merchant_id: str, merchant_key: str, passphrase: str = "", sandbox=False
29
+ ):
30
+ self.merchant_id = merchant_id
31
+ self.merchant_key = merchant_key
32
+ self.passphrase = passphrase
33
+ self.test_sandbox_mode = sandbox
34
+ self.session = requests.Session()
35
+
36
+ def _generate_headers(self) -> dict:
37
+ # Generate the headers for the Payfast API
38
+ return {
39
+ "merchant-id": self.merchant_id,
40
+ "version": "v1",
41
+ "timestamp": datetime.now(pytz.timezone("Africa/Johannesburg")).strftime(
42
+ "%Y-%m-%dT%H:%M:%S"
43
+ ),
44
+ }
45
+
46
+ def _generate_signature(self, data: dict):
47
+ # Generate a signature using the merchant_key
48
+ payload = ""
49
+ if self.passphrase != "":
50
+ data["passphrase"] = self.passphrase
51
+ sortedData = sorted(data)
52
+ for key in sortedData:
53
+ # Get all the data from Payfast and prepare parameter string
54
+ payload += (
55
+ key
56
+ + "="
57
+ + urllib.parse.quote_plus(str(data[key]).replace("+", " "))
58
+ + "&"
59
+ )
60
+ # After looping through, cut the last & or append your passphrase
61
+ payload = payload[:-1]
62
+ return hashlib.md5(payload.encode()).hexdigest()
63
+
64
+ def _request_get(self, path: str, query_params: dict) -> requests.Response:
65
+ # Make a GET request to the Payfast API
66
+ headers = self._generate_headers()
67
+ # Add the signature to the headers
68
+ headers["signature"] = self._generate_signature(headers)
69
+
70
+ if self.test_sandbox_mode:
71
+ query_params["testing"] = True
72
+
73
+ return self.session.get(
74
+ f"https://api.payfast.co.za/{path}",
75
+ headers=headers,
76
+ params=query_params,
77
+ )
78
+
79
+ def _request_post(self, path: str, body_data: dict) -> requests.Response:
80
+ # Make a POST request to the Payfast API
81
+ headers = self._generate_headers()
82
+ # Add the signature to the headers
83
+ signature_payload = {**headers, **body_data}
84
+ headers["signature"] = self._generate_signature(signature_payload)
85
+
86
+ query_params = {}
87
+ query_params_encoded = ""
88
+
89
+ if self.test_sandbox_mode:
90
+ query_params["testing"] = "true"
91
+ query_params_encoded = "?" + urllib.parse.urlencode(query_params)
92
+
93
+ return self.session.post(
94
+ f"https://api.payfast.co.za/{path}{query_params_encoded}",
95
+ headers=headers,
96
+ json=body_data,
97
+ )
98
+
99
+ def charge_tokenization_payment(self, token: str, payload: dict):
100
+ # check to see if the schema is valid
101
+
102
+ try:
103
+ validated_payload = PayfastBodyTokenization(**payload)
104
+
105
+ try:
106
+ response = self._request_post(
107
+ f"subscriptions/{token}/adhoc", validated_payload.dict()
108
+ )
109
+ return response
110
+ except Exception as e:
111
+ print(f"Request error: {e}")
112
+ raise e
113
+
114
+ except ValidationError as e:
115
+ print(f"Validation error: {e}")
116
+ raise e
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Intentio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.1
2
+ Name: payfast
3
+ Version: 0.1
4
+ Summary: Python library for Payfast by network API
5
+ Home-page: https://github.com/intentio-software/payfast-python
6
+ Author: Max Dittmar
7
+ Author-email: Max Dittmar <max@intentio.co.za>
8
+ License: MIT License
9
+
10
+ Copyright (c) 2024 Intentio
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of this software and associated documentation files (the "Software"), to deal
14
+ in the Software without restriction, including without limitation the rights
15
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ copies of the Software, and to permit persons to whom the Software is
17
+ furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ SOFTWARE.
29
+
30
+ Project-URL: Homepage, https://github.com/intentio-software/payfast-python
31
+ Classifier: Programming Language :: Python :: 3
32
+ Classifier: Development Status :: 3 - Alpha
33
+ Description-Content-Type: text/markdown
34
+ License-File: LICENSE
35
+ Requires-Dist: requests
36
+ Requires-Dist: pytz
37
+ Requires-Dist: pydantic
38
+
39
+ # payfast-python
40
+ Python library for Payfast by network API
@@ -0,0 +1,7 @@
1
+ payfast/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ payfast/api.py,sha256=tguv16GBM8qChPqLmTVNYYRyZ4JgKCPnFSFCnw9ExHI,3683
3
+ payfast-0.1.dist-info/LICENSE,sha256=a98cXWQDvRFSn-_Mev2uJKsk89PRNLHfnPY-5JlW0eY,1065
4
+ payfast-0.1.dist-info/METADATA,sha256=EDLDiJppCi44uQfSGYFjbPQifSInyxgqcqO8UXE6c6Q,1830
5
+ payfast-0.1.dist-info/WHEEL,sha256=mguMlWGMX-VHnMpKOjjQidIo1ssRlCFu4a4mBpz1s2M,91
6
+ payfast-0.1.dist-info/top_level.txt,sha256=qGqyGiRcIcwqwm7UpphEoTsDQufXjfA4FbW_vgVuu9c,8
7
+ payfast-0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (70.1.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ payfast