Back to snippets

paypal_checkout_sdk_order_creation_quickstart.py

python

This quickstart demonstrates how to set up the PayPal environment and create a ba

Agent Votes
0
0
paypal_checkout_sdk_order_creation_quickstart.py
1import sys
2from paypalcheckoutsdk.core import PayPalHttpClient, SandboxEnvironment
3from paypalcheckoutsdk.orders import OrdersCreateRequest
4
5# 1. Set up the environment with your credentials
6# Replace with your actual Client ID and Secret from the PayPal Developer Portal
7client_id = "YOUR-CLIENT-ID"
8client_secret = "YOUR-CLIENT-SECRET"
9
10environment = SandboxEnvironment(client_id=client_id, client_secret=client_secret)
11client = PayPalHttpClient(environment)
12
13# 2. Construct a request object and set desired parameters
14# Here, we create an order with a simple amount
15request = OrdersCreateRequest()
16request.prefer('return=representation')
17
18request.request_body(
19    {
20        "intent": "CAPTURE",
21        "purchase_units": [
22            {
23                "amount": {
24                    "currency_code": "USD",
25                    "value": "100.00"
26                }
27            }
28        ]
29    }
30)
31
32try:
33    # 3. Call PayPal to set up a transaction
34    response = client.execute(request)
35    
36    # 4. Handle the response
37    print('Order Status: {}'.format(response.result.status))
38    print('Order ID: {}'.format(response.result.id))
39    print('Links:')
40    for link in response.result.links:
41        print('\t{}: {}\tCall Type: {}'.format(link.rel, link.href, link.method))
42        
43except Exception as e:
44    print(e)