Skip to content

Commit b9f568d

Browse files
Tim MartinakUmar Nafeez Abdul Phatip
authored andcommitted
Add python flask based advanced-integration
1 parent f919846 commit b9f568d

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Create an application to obtain credentials at
2+
# https://developer.paypal.com/dashboard/applications/sandbox
3+
4+
PAYPAL_CLIENT_ID=YOUR_CLIENT_ID_GOES_HERE
5+
PAYPAL_CLIENT_SECRET=YOUR_SECRET_GOES_HERE

advanced-integration/v2/server/python/README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
# Standard Integration Python Flask Sample
1+
# Standard Integration Ruby Sinatra Sample
22

3-
PayPal Standard Integration sample in Python using Flask
3+
PayPal Standard Integration sample in Ruby using Sinatra
44

55
## Running the sample
66

7-
1. **Setup a virtual environment**
8-
9-
```sh
10-
python3 -m venv .venv
11-
```
7+
1. **Ensure you have a supported Ruby version installed**: [Ruby Maintenance Branches](https://www.ruby-lang.org/en/downloads/branches/)
128

139
1. **Install the dependencies**
1410

15-
```sh
16-
pip install -r requirements.txt
11+
```bash
12+
bundle install
1713
```
1814

1915
1. **Add your API credentials to the environment:**
@@ -34,8 +30,8 @@ PayPal Standard Integration sample in Python using Flask
3430
3531
1. **Run the server**
3632
37-
```sh
38-
flask --app server run
33+
```bash
34+
bundle exec ruby server.rb
3935
```
4036

41-
1. Go to [http://localhost:8080/](http://localhost:8080/)
37+
1. Go to [http://localhost:8080/](http://localhost:8080/)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import logging
2+
import os
3+
4+
from dotenv import load_dotenv
5+
from flask import Flask, request
6+
from paypalserversdk.http.auth.o_auth_2 import ClientCredentialsAuthCredentials
7+
from paypalserversdk.logging.configuration.api_logging_configuration import LoggingConfiguration, \
8+
RequestLoggingConfiguration, ResponseLoggingConfiguration
9+
from paypalserversdk.paypalserversdk_client import PaypalserversdkClient
10+
from paypalserversdk.controllers.orders_controller import OrdersController
11+
from paypalserversdk.models.amount_with_breakdown import AmountWithBreakdown
12+
from paypalserversdk.models.checkout_payment_intent import CheckoutPaymentIntent
13+
from paypalserversdk.models.order_request import OrderRequest
14+
from paypalserversdk.models.purchase_unit_request import PurchaseUnitRequest
15+
from paypalserversdk.api_helper import ApiHelper
16+
17+
app = Flask(__name__)
18+
19+
load_dotenv()
20+
21+
paypal_client: PaypalserversdkClient = PaypalserversdkClient(
22+
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
23+
o_auth_client_id=os.getenv('PAYPAL_CLIENT_ID'),
24+
o_auth_client_secret=os.getenv('PAYPAL_CLIENT_SECRET')
25+
),
26+
logging_configuration=LoggingConfiguration(
27+
log_level=logging.INFO,
28+
# Disable masking of sensitive headers for Sandbox testing.
29+
# This should be set to True (the default if unset)in production.
30+
mask_sensitive_headers=False,
31+
request_logging_config=RequestLoggingConfiguration(
32+
log_headers=True,
33+
log_body=True
34+
),
35+
response_logging_config=ResponseLoggingConfiguration(
36+
log_headers=True,
37+
log_body=True
38+
)
39+
)
40+
)
41+
42+
'''
43+
Health check
44+
'''
45+
@app.route('/', methods=['GET'])
46+
def index():
47+
return {"message": "Server is running"}
48+
49+
orders_controller: OrdersController = paypal_client.orders
50+
51+
'''
52+
Create an order to start the transaction.
53+
54+
@see https://developer.paypal.com/docs/api/orders/v2/#orders_create
55+
'''
56+
@app.route('/api/orders', methods=['POST'])
57+
def create_order():
58+
request_body = request.get_json()
59+
# use the cart information passed from the front-end to calculate the order amount detals
60+
cart = request_body['cart']
61+
order = orders_controller.orders_create({
62+
"body": OrderRequest(
63+
intent=CheckoutPaymentIntent.CAPTURE,
64+
purchase_units=[
65+
PurchaseUnitRequest(
66+
AmountWithBreakdown(
67+
currency_code='USD',
68+
value='100.00'
69+
)
70+
)
71+
]
72+
),
73+
"prefer": 'return=representation'
74+
}
75+
)
76+
return ApiHelper.json_serialize(order.body)
77+
78+
'''
79+
Capture payment for the created order to complete the transaction.
80+
81+
@see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
82+
'''
83+
@app.route('/api/orders/<order_id>/capture', methods=['POST'])
84+
def capture_order(order_id):
85+
order = orders_controller.orders_capture({
86+
'id': order_id,
87+
'prefer': 'return=representation'
88+
})
89+
return ApiHelper.json_serialize(order.body)

0 commit comments

Comments
 (0)