payrex-python 0.1.0__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.
- payrex/__init__.py +32 -0
- payrex/api_resource.py +3 -0
- payrex/client.py +12 -0
- payrex/config.py +6 -0
- payrex/error.py +5 -0
- payrex/http_client.py +64 -0
- payrex_python-0.1.0.dist-info/LICENSE +21 -0
- payrex_python-0.1.0.dist-info/METADATA +114 -0
- payrex_python-0.1.0.dist-info/RECORD +11 -0
- payrex_python-0.1.0.dist-info/WHEEL +5 -0
- payrex_python-0.1.0.dist-info/top_level.txt +1 -0
payrex/__init__.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from payrex.api_resource import ApiResource
|
|
2
|
+
from payrex.error import Error
|
|
3
|
+
|
|
4
|
+
from payrex.exceptions.base_exception import BaseException
|
|
5
|
+
from payrex.exceptions.authentication_invalid_exception import AuthenticationInvalidException
|
|
6
|
+
from payrex.exceptions.request_invalid_exception import RequestInvalidException
|
|
7
|
+
from payrex.exceptions.resource_not_found_exception import ResourceNotFoundException
|
|
8
|
+
from payrex.exceptions.signature_invalid_exception import SignatureInvalidException
|
|
9
|
+
from payrex.exceptions.value_unexpected_exception import ValueUnexpectedException
|
|
10
|
+
|
|
11
|
+
from payrex.http_client import HttpClient
|
|
12
|
+
|
|
13
|
+
from payrex.entities.checkout_session_entity import CheckoutSessionEntity
|
|
14
|
+
from payrex.entities.event_entity import EventEntity
|
|
15
|
+
from payrex.entities.listing_entity import ListingEntity
|
|
16
|
+
from payrex.entities.merchant_entity import MerchantEntity
|
|
17
|
+
from payrex.entities.payment_intent_entity import PaymentIntentEntity
|
|
18
|
+
from payrex.entities.payment_method_entity import PaymentMethodEntity
|
|
19
|
+
from payrex.entities.refund_entity import RefundEntity
|
|
20
|
+
from payrex.entities.webhook_entity import WebhookEntity
|
|
21
|
+
|
|
22
|
+
from payrex.services.base_service import BaseService
|
|
23
|
+
from payrex.services.checkout_session_service import CheckoutSessionService
|
|
24
|
+
from payrex.services.merchant_service import MerchantService
|
|
25
|
+
from payrex.services.payment_intent_service import PaymentIntentService
|
|
26
|
+
from payrex.services.payment_method_service import PaymentMethodService
|
|
27
|
+
from payrex.services.refund_service import RefundService
|
|
28
|
+
from payrex.services.webhook_service import WebhookService
|
|
29
|
+
from payrex.services.service_factory import ServiceFactory
|
|
30
|
+
|
|
31
|
+
from payrex.config import Config
|
|
32
|
+
from payrex.client import Client
|
payrex/api_resource.py
ADDED
payrex/client.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from payrex import Config
|
|
2
|
+
from payrex import ServiceFactory
|
|
3
|
+
|
|
4
|
+
class Client:
|
|
5
|
+
def __init__(self, api_key):
|
|
6
|
+
self.config = Config(api_key)
|
|
7
|
+
self._initialize_services()
|
|
8
|
+
|
|
9
|
+
def _initialize_services(self):
|
|
10
|
+
for name in ServiceFactory.names():
|
|
11
|
+
service = ServiceFactory.get(name)
|
|
12
|
+
setattr(self, f'{name}s', service(self))
|
payrex/config.py
ADDED
payrex/error.py
ADDED
payrex/http_client.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import requests
|
|
3
|
+
|
|
4
|
+
from urllib.parse import urlencode
|
|
5
|
+
|
|
6
|
+
from payrex import ApiResource
|
|
7
|
+
from payrex import BaseException
|
|
8
|
+
from payrex import RequestInvalidException
|
|
9
|
+
from payrex import AuthenticationInvalidException
|
|
10
|
+
from payrex import ResourceNotFoundException
|
|
11
|
+
|
|
12
|
+
class HttpClient:
|
|
13
|
+
def __init__(self, api_key, base_url):
|
|
14
|
+
self.api_key = api_key
|
|
15
|
+
self.base_url = base_url
|
|
16
|
+
|
|
17
|
+
def request(self, method, params=None, path=None):
|
|
18
|
+
url = f'{self.base_url}/{path}'
|
|
19
|
+
|
|
20
|
+
auth = requests.auth.HTTPBasicAuth(self.api_key, '')
|
|
21
|
+
|
|
22
|
+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
|
23
|
+
|
|
24
|
+
if method.lower() in ['post', 'put']:
|
|
25
|
+
data = self._encode_params(params)
|
|
26
|
+
else:
|
|
27
|
+
data = None
|
|
28
|
+
|
|
29
|
+
response = requests.request(method, url, auth=auth, headers=headers, data=data)
|
|
30
|
+
|
|
31
|
+
if response.status_code != 200:
|
|
32
|
+
self._handle_error(response)
|
|
33
|
+
|
|
34
|
+
return ApiResource(response.json())
|
|
35
|
+
|
|
36
|
+
def _handle_error(self, response):
|
|
37
|
+
json_response_body = response.json()
|
|
38
|
+
|
|
39
|
+
if response.status_code == 400:
|
|
40
|
+
raise RequestInvalidException(json_response_body)
|
|
41
|
+
elif response.status_code == 401:
|
|
42
|
+
raise AuthenticationInvalidException(json_response_body)
|
|
43
|
+
elif response.status_code == 404:
|
|
44
|
+
raise ResourceNotFoundException(json_response_body)
|
|
45
|
+
else:
|
|
46
|
+
raise BaseException(json_response_body)
|
|
47
|
+
|
|
48
|
+
def _encode_params(self, params):
|
|
49
|
+
encoded_params = {}
|
|
50
|
+
for key, value in params.items():
|
|
51
|
+
if isinstance(value, list):
|
|
52
|
+
for item in value:
|
|
53
|
+
encoded_params.setdefault(f'{key}[]', []).append(item)
|
|
54
|
+
elif isinstance(value, dict):
|
|
55
|
+
for k, v in value.items():
|
|
56
|
+
if isinstance(v, dict):
|
|
57
|
+
for nk, nv in v.items():
|
|
58
|
+
encoded_params.setdefault(f'{key}[{k}][{nk}]', []).append(nv)
|
|
59
|
+
else:
|
|
60
|
+
encoded_params.setdefault(f'{key}[{k}]', []).append(v)
|
|
61
|
+
else:
|
|
62
|
+
encoded_params[key] = value
|
|
63
|
+
|
|
64
|
+
return urlencode(encoded_params, doseq=True)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Evolut10n Labs, Inc. (https://payrexhq.com)
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: payrex-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: PayRex Python Library
|
|
5
|
+
Home-page: https://github.com/payrexhq/payrex-python
|
|
6
|
+
Author: PayRex
|
|
7
|
+
Author-email: support@payrexhq.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Keywords: payrex
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: requests
|
|
19
|
+
|
|
20
|
+
# PayRex Python
|
|
21
|
+
|
|
22
|
+
PayRex Python library provides Python applications an easy access to the PayRex API. Explore various Python classes that represents PayRex API resources on object instantiation.
|
|
23
|
+
|
|
24
|
+
## Requirements
|
|
25
|
+
|
|
26
|
+
Python 3.9.+
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
If you want to use the package, run the following command:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
pip install payrex-python
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
If you want to build the library from source:
|
|
37
|
+
|
|
38
|
+
Create a virtual environment
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
python -m venv venv
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Activate the virtual environment
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
source venv/bin/activate
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Install the package to the virtual environment
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install -e /Your/Local/Path/payrex-python
|
|
54
|
+
|
|
55
|
+
python
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Getting Started
|
|
59
|
+
|
|
60
|
+
Simple usage looks like:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from payrex import Client as PayrexClient
|
|
64
|
+
|
|
65
|
+
payrex_client = PayrexClient('sk_test_...')
|
|
66
|
+
payment_intent = payrex_client.payment_intents.retrieve('pi_...')
|
|
67
|
+
|
|
68
|
+
payment_intent = payrex_client.payment_intents.create(
|
|
69
|
+
{
|
|
70
|
+
'amount': 10000,
|
|
71
|
+
'currency': 'PHP',
|
|
72
|
+
'description': 'Dino Treat',
|
|
73
|
+
'payment_methods': ['gcash']
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Handle errors
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
try:
|
|
82
|
+
payrex_client = PayrexClient('sk_test_...')
|
|
83
|
+
|
|
84
|
+
payment_intent = payrex_client.payment_intents.create(
|
|
85
|
+
{
|
|
86
|
+
'amount': 10000,
|
|
87
|
+
'description': 'Dino Treat',
|
|
88
|
+
'payment_methods': ['gcash']
|
|
89
|
+
}
|
|
90
|
+
)
|
|
91
|
+
except BaseException as e:
|
|
92
|
+
# Handle error
|
|
93
|
+
print(type(e))
|
|
94
|
+
print(e.errors[0].code)
|
|
95
|
+
print(e.errors[0].detail)
|
|
96
|
+
print(e.errors[0].parameter)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Verify webhook signature
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
try:
|
|
103
|
+
payload = '{"id":"evt_...","resource":"event","type":"payment_intent.succeeded","data":{...'
|
|
104
|
+
signature_header = 't=1715236958,te=,li=...'
|
|
105
|
+
webhook_secret_key = 'whsk_...'
|
|
106
|
+
|
|
107
|
+
payrex_client.webhooks.parse_event(
|
|
108
|
+
payload,
|
|
109
|
+
signature_header,
|
|
110
|
+
webhook_secret_key
|
|
111
|
+
)
|
|
112
|
+
except SignatureVerificationException as e:
|
|
113
|
+
# Handle invalid signature
|
|
114
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
payrex/__init__.py,sha256=sFTKHmjoYjI2OrkX-s_-ch01t5Madgb5tg3AzA4AIts,1676
|
|
2
|
+
payrex/api_resource.py,sha256=XcOQdF74HyFICdVfYerOA3JUZR-0w29GBYjolW6TpnU,82
|
|
3
|
+
payrex/client.py,sha256=ze_DuOug3JVlFOPAPoAQQDPapCybcQNZNjdymRli7w4,363
|
|
4
|
+
payrex/config.py,sha256=6O8XR88BsX_kg3yWyRlndc304Z1JgeXhKJxjZYcxf10,171
|
|
5
|
+
payrex/error.py,sha256=NFLwYqwxR1Bstdxl9Re5_0bTOq8qaDI2Y2KmLOFcH58,172
|
|
6
|
+
payrex/http_client.py,sha256=h0yn2Maq9gsc_gE2sYoGovR-IOKgiC6Aey7QQSEyTy4,2191
|
|
7
|
+
payrex_python-0.1.0.dist-info/LICENSE,sha256=D2u8aAyW7ANg0dWTyPnBcJX1_3zAlnskYOyONZ3Jeww,1099
|
|
8
|
+
payrex_python-0.1.0.dist-info/METADATA,sha256=cYdWqY-jwgoj8rAUJVxS11rnbkLMkJey1-xAYm7_knk,2487
|
|
9
|
+
payrex_python-0.1.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
10
|
+
payrex_python-0.1.0.dist-info/top_level.txt,sha256=dd4RklAV7AIV9-puyriCF1CW2j9ndidUw1u_HdRkJvw,7
|
|
11
|
+
payrex_python-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
payrex
|