Skip to content

Commit 1850e30

Browse files
Beta Release 0.5.1 (#3)
* Automated commit message * Automated commit message * Automated commit message * Automated commit message --------- Co-authored-by: PayPalServerSDKs <[email protected]>
1 parent 4368e04 commit 1850e30

File tree

548 files changed

+40830
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

548 files changed

+40830
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

LICENSE

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
The PayPal Server SDK is released under the following license:
2+
3+
Copyright (c) 2024 PAYPAL, INC.
4+
5+
SDK LICENSE
6+
7+
NOTICE TO USER: PayPal, Inc. is providing the Software and Documentation for use under the terms of this Agreement. Any use, reproduction, modification or distribution of the Software or Documentation, or any derivatives or portions hereof, constitutes your acceptance of this Agreement.
8+
As used in this Agreement, "PayPal" means PayPal, Inc. "Software" means the software code accompanying this agreement. "Documentation" means the documents, specifications and all other items accompanying this Agreement other than the Software.
9+
10+
1. LICENSE GRANT
11+
Subject to the terms of this Agreement, PayPal hereby grants you a non-exclusive, worldwide, royalty free license to use, reproduce, prepare derivative works from, publicly display, publicly perform, distribute and sublicense the Software for any purpose, other than perpetration of any fraud or violation of any law, statute, ordinance, or regulation (for example, those governing financial services, consumer protections, unfair competition, anti-discrimination or false advertising), provided the copyright notice below appears in a conspicuous location within the source code of the distributed Software and this license is distributed in the supporting documentation of the Software you distribute. Furthermore, you must comply with all third party licenses in order to use the third party software contained in the Software.
12+
Subject to the terms of this Agreement, PayPal hereby grants you a non-exclusive, worldwide, royalty free license to use, reproduce, publicly display, publicly perform, distribute and sublicense the Documentation for any purpose, other than perpetration of any fraud or violation of any law, statute, ordinance, or regulation (for example, those governing financial services, consumer protections, unfair competition, anti-discrimination or false advertising). You may not modify the Documentation.
13+
No title to the intellectual property in the Software or Documentation is transferred to you under the terms of this Agreement. You do not acquire any rights to the Software or the Documentation except as expressly set forth in this Agreement.
14+
Nothing in this Agreement provides you with any right, title, or license to use PayPal’s or any third party’s copyright, patent, trademark, trade secret, or other intellectual property rights, or rights of publicity or privacy, beyond the rights specifically granted herein. You may not infringe PayPal’s or any third party’s copyright, patent, trademark, trade secret, or other intellectual property rights, or rights of publicity or privacy.
15+
If you choose to distribute the Software in a commercial product, you do so with the understanding that you agree to defend, indemnify and hold harmless PayPal and its suppliers against any losses, damages and costs arising from the claims, lawsuits or other legal actions arising out of such distribution. You may distribute the Software in object code form under your own license, provided that your license agreement:
16+
(a) complies with the terms and conditions of this license agreement;
17+
(b) effectively disclaims all warranties and conditions, express or implied, on behalf of PayPal;
18+
(c) effectively excludes all liability for damages on behalf of PayPal;
19+
(d) states that any provisions that differ from this Agreement are offered by you alone and not PayPal; and
20+
(e) states that the Software is available from you or PayPal and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange.
21+
22+
2. DISCLAIMER OF WARRANTY
23+
PAYPAL LICENSES THE SOFTWARE AND DOCUMENTATION TO YOU ONLY ON AN "AS IS" BASIS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. PAYPAL MAKES NO WARRANTY THAT THE SOFTWARE OR DOCUMENTATION WILL BE ERROR-FREE. Each user of the Software or Documentation is solely responsible for determining the appropriateness of using and distributing the Software and Documentation and assumes all risks associated with its exercise of rights under this Agreement, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs, or equipment, and unavailability or interruption of operations. Use of the Software and Documentation is made with the understanding that PayPal will not provide you with any technical or customer support or maintenance. Some states or jurisdictions do not allow the exclusion of implied warranties or limitations on how long an implied warranty may last, so the above limitations may not apply to you. To the extent permissible, any implied warranties are limited to ninety (90) days.
24+
25+
3. LIMITATION OF LIABILITY
26+
PAYPAL AND ITS SUPPLIERS SHALL NOT BE LIABLE FOR LOSS OR DAMAGE ARISING OUT OF THIS AGREEMENT OR FROM THE USE OF THE SOFTWARE OR DOCUMENTATION. IN NO EVENT WILL PAYPAL OR ITS SUPPLIERS BE LIABLE TO YOU OR ANY THIRD PARTY FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, INCIDENTAL, OR SPECIAL DAMAGES INCLUDING LOST PROFITS, LOST SAVINGS, COSTS, FEES, OR EXPENSES OF ANY KIND ARISING OUT OF ANY PROVISION OF THIS AGREEMENT OR THE USE OR THE INABILITY TO USE THE SOFTWARE OR DOCUMENTATION, HOWEVER CAUSED AND UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT INCLUDING NEGLIGENCE (OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. PAYPAL'S AGGREGATE LIABILITY AND THAT OF ITS SUPPLIERS UNDER OR IN CONNECTION WITH THIS AGREEMENT SHALL BE LIMITED TO THE AMOUNT PAID BY YOU FOR THE SOFTWARE AND DOCUMENTATION.
27+
28+
4. TRADEMARK USAGE
29+
PayPal is a trademark PayPal, Inc. in the United States and other countries. Such trademarks may not be used to endorse or promote any product unless expressly permitted under separate agreement with PayPal.
30+
31+
5. TERM
32+
Your rights under this Agreement shall terminate if you fail to comply with any of the material terms or conditions of this Agreement and do not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all your rights under this Agreement terminate, you agree to cease use and distribution of the Software and Documentation as soon as reasonably practicable.
33+
34+
6. GOVERNING LAW AND JURISDICTION.
35+
This Agreement is governed by the statutes and laws of the State of California, without regard to the conflicts of law principles thereof. If any part of this Agreement is found void and unenforceable, it will not affect the validity of the balance of the Agreement, which shall remain valid and enforceable according to its terms. Any dispute arising out of or related to this Agreement shall be brought in the courts of Santa Clara County, California, USA.

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include LICENSE
2+
include README.md

README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,107 @@
1-
# PayPal-Python-Server-SDK
1+
2+
# Getting Started with Paypal Server SDK
3+
4+
## Introduction
5+
6+
An order represents a payment between two or more parties. Use the Orders API to create, update, retrieve, authorize, and capture orders., Call the Payments API to authorize payments, capture authorized payments, refund payments that have already been captured, and show payment information. Use the Payments API in conjunction with the <a href="/docs/api/orders/v2/">Orders API</a>. For more information, see the <a href="/docs/checkout/">PayPal Checkout Overview</a>., The Payment Method Tokens API saves payment methods so payers don't have to enter details for future transactions. Payers can check out faster or pay without being present after they agree to save a payment method.<br><br>The API associates a payment method with a temporary setup token. Pass the setup token to the API to exchange the setup token for a permanent token.<br><br>The permanent token represents a payment method that's saved to the vault. This token can be used repeatedly for checkout or recurring transactions such as subscriptions.<br><br>The Payment Method Tokens API is available in the US only.
7+
8+
Find out more here: [https://developer.paypal.com/docs/api/orders/v2/](https://developer.paypal.com/docs/api/orders/v2/)
9+
10+
## Install the Package
11+
12+
The package is compatible with Python versions `3 >=3.7, <= 3.11`.
13+
Install the package from PyPi using the following pip command:
14+
15+
```python
16+
pip install paypal-server-sdk==0.5.1
17+
```
18+
19+
You can also view the package at:
20+
https://pypi.python.org/pypi/paypal-server-sdk/0.5.1
21+
22+
## Initialize the API Client
23+
24+
**_Note:_** Documentation for the client can be found [here.](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/client.md)
25+
26+
The following parameters are configurable for the API Client:
27+
28+
| Parameter | Type | Description |
29+
| --- | --- | --- |
30+
| `environment` | `Environment` | The API environment. <br> **Default: `Environment.SANDBOX`** |
31+
| `http_client_instance` | `HttpClient` | The Http Client passed from the sdk user for making requests |
32+
| `override_http_client_configuration` | `bool` | The value which determines to override properties of the passed Http Client from the sdk user |
33+
| `http_call_back` | `HttpCallBack` | The callback value that is invoked before and after an HTTP call is made to an endpoint |
34+
| `timeout` | `float` | The value to use for connection timeout. <br> **Default: 60** |
35+
| `max_retries` | `int` | The number of times to retry an endpoint call if it fails. <br> **Default: 0** |
36+
| `backoff_factor` | `float` | A backoff factor to apply between attempts after the second try. <br> **Default: 2** |
37+
| `retry_statuses` | `Array of int` | The http statuses on which retry is to be done. <br> **Default: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524]** |
38+
| `retry_methods` | `Array of string` | The http methods on which retry is to be done. <br> **Default: ['GET', 'PUT']** |
39+
| `logging_configuration` | [`LoggingConfiguration`](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/logging-configuration.md) | The SDK logging configuration for API calls |
40+
| `client_credentials_auth_credentials` | [`ClientCredentialsAuthCredentials`](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/auth/oauth-2-client-credentials-grant.md) | The credential object for OAuth 2 Client Credentials Grant |
41+
42+
The API client can be initialized as follows:
43+
44+
```python
45+
client = PaypalserversdkClient(
46+
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
47+
o_auth_client_id='OAuthClientId',
48+
o_auth_client_secret='OAuthClientSecret'
49+
),
50+
environment=Environment.SANDBOX,
51+
logging_configuration=LoggingConfiguration(
52+
log_level=logging.INFO,
53+
request_logging_config=RequestLoggingConfiguration(
54+
log_body=True
55+
),
56+
response_logging_config=ResponseLoggingConfiguration(
57+
log_headers=True
58+
)
59+
)
60+
)
61+
```
62+
63+
API calls return an `ApiResponse` object that includes the following fields:
64+
65+
| Field | Description |
66+
| --- | --- |
67+
| `status_code` | Status code of the HTTP response |
68+
| `reason_phrase` | Reason phrase of the HTTP response |
69+
| `headers` | Headers of the HTTP response as a dictionary |
70+
| `text` | The body of the HTTP response as a string |
71+
| `request` | HTTP request info |
72+
| `errors` | Errors, if they exist |
73+
| `body` | The deserialized body of the HTTP response |
74+
75+
## Environments
76+
77+
The SDK can be configured to use a different environment for making API calls. Available environments are:
78+
79+
### Fields
80+
81+
| Name | Description |
82+
| --- | --- |
83+
| Production | PayPal Live Environment |
84+
| Sandbox | **Default** PayPal Sandbox Environment |
85+
86+
## Authorization
87+
88+
This API uses the following authentication schemes.
89+
90+
* [`Oauth2 (OAuth 2 Client Credentials Grant)`](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/auth/oauth-2-client-credentials-grant.md)
91+
92+
## List of APIs
93+
94+
* [Orders](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/controllers/orders.md)
95+
* [Payments](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/controllers/payments.md)
96+
* [Vault](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/controllers/vault.md)
97+
98+
## Classes Documentation
99+
100+
* [Utility Classes](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/utility-classes.md)
101+
* [HttpResponse](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/http-response.md)
102+
* [HttpRequest](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/http-request.md)
103+
* [LoggingConfiguration](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/logging-configuration.md)
104+
* [RequestLoggingConfiguration](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/request-logging-configuration.md)
105+
* [ResponseLoggingConfiguration](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/response-logging-configuration.md)
106+
* [AbstractLogger](https://www.github.com/paypal/PayPal-Python-Server-SDK/tree/0.5.1/doc/abstract-logger.md)
107+

doc/abstract-logger.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
# AbstractLogger Class
3+
4+
An abstract class for custom logger implementation.
5+
6+
## Abstract Methods
7+
8+
| Name | Description | Return Type | Parameters | Method Signature |
9+
| --- | --- | --- | --- | --- |
10+
| log | Logs a message with a specified log level and additional parameters. | None | 1. level (int): The log level of the message.<br>2. message (str): The message template to log.<br>3. params: (Dict[str, Any]): The parameters to include in the log message. | log(self, level, message, params) |
11+
12+
## Usage Example
13+
14+
### Client Initialization with Custom Logger
15+
16+
In order to provide custom logger implementation, the `AbstractLogger` class must be inherited so that you can override the `log` behavior.
17+
18+
```python
19+
import structlog
20+
from paypalserversdk.logging.sdk_logger import AbstractLogger
21+
22+
23+
class CustomLogger(AbstractLogger):
24+
25+
def __init__(self):
26+
structlog.configure(
27+
processors=[
28+
structlog.processors.TimeStamper(fmt="iso"),
29+
structlog.processors.JSONRenderer(),
30+
]
31+
)
32+
self._logger = structlog.get_logger()
33+
34+
def log(self, level, message, params):
35+
self._logger.log(level, message, *params.values(), **params)
36+
```
37+
38+
Following is how the custom logger implementation can be injected in the SDK client.
39+
40+
```python
41+
client = PaypalserversdkClient(
42+
logging_configuration=LoggingConfiguration(
43+
logger=CustomLogger()
44+
)
45+
)
46+
```
47+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
# OAuth 2 Client Credentials Grant
3+
4+
5+
6+
Documentation for accessing and setting credentials for Oauth2.
7+
8+
## Auth Credentials
9+
10+
| Name | Type | Description | Getter |
11+
| --- | --- | --- | --- |
12+
| OAuthClientId | `str` | OAuth 2 Client ID | `o_auth_client_id` |
13+
| OAuthClientSecret | `str` | OAuth 2 Client Secret | `o_auth_client_secret` |
14+
| OAuthToken | `OAuthToken` | Object for storing information about the OAuth token | `o_auth_token` |
15+
| OAuthTokenProvider | `Callable[[OAuthToken, OAuth2], OAuthToken]` | Registers a callback for oAuth Token Provider used for automatic token fetching/refreshing. | `o_auth_token_provider` |
16+
| OAuthOnTokenUpdate | `Callable[[OAuthToken], None]` | Registers a callback for token update event. | `o_auth_on_token_update` |
17+
| OAuthClockSkew | `int` | Clock skew time in seconds applied while checking the OAuth Token expiry. | `o_auth_clock_skew` |
18+
19+
20+
21+
**Note:** Auth credentials can be set using `ClientCredentialsAuthCredentials` object, passed in as named parameter `client_credentials_auth_credentials` in the client initialization.
22+
23+
## Usage Example
24+
25+
### Client Initialization
26+
27+
You must initialize the client with *OAuth 2.0 Client Credentials Grant* credentials as shown in the following code snippet. This will fetch the OAuth token automatically when any of the endpoints, requiring *OAuth 2.0 Client Credentials Grant* autentication, are called.
28+
29+
```python
30+
client = PaypalserversdkClient(
31+
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
32+
o_auth_client_id='OAuthClientId',
33+
o_auth_client_secret='OAuthClientSecret'
34+
)
35+
)
36+
```
37+
38+
39+
40+
Your application can also manually provide an OAuthToken using the setter `o_auth_token` in `ClientCredentialsAuthCredentials` object. This function takes in an instance of OAuthToken containing information for authorizing client requests and refreshing the token itself.
41+
42+
### Adding OAuth Token Update Callback
43+
44+
Whenever the OAuth Token gets updated, the provided callback implementation will be executed. For instance, you may use it to store your access token whenever it gets updated.
45+
46+
```python
47+
client = PaypalserversdkClient(
48+
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
49+
o_auth_client_id='OAuthClientId',
50+
o_auth_client_secret='OAuthClientSecret',
51+
o_auth_on_token_update=(lambda o_auth_token:
52+
# Add the callback handler to perform operations like save to DB or file etc.
53+
# It will be triggered whenever the token gets updated
54+
save_token_to_database(o_auth_token))
55+
)
56+
)
57+
```
58+
59+
### Adding Custom OAuth Token Provider
60+
61+
To authorize a client using a stored access token, set up the `o_auth_token_provider` in `ClientCredentialsAuthCredentials` along with the other auth parameters before creating the client:
62+
63+
```python
64+
def _o_auth_token_provider(last_oauth_token, auth_manager):
65+
# Add the callback handler to provide a new OAuth token
66+
# It will be triggered whenever the last provided o_auth_token is null or expired
67+
o_auth_token = load_token_from_database()
68+
if o_auth_token is None:
69+
o_auth_token = auth_manager.fetch_token()
70+
return o_auth_token
71+
72+
73+
client = PaypalserversdkClient(
74+
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
75+
o_auth_client_id='OAuthClientId',
76+
o_auth_client_secret='OAuthClientSecret',
77+
o_auth_token_provider=_o_auth_token_provider
78+
)
79+
)
80+
```
81+
82+

0 commit comments

Comments
 (0)