Skip to content

Commit 843672d

Browse files
update Python Flask/Ruby Sinatra examples (#160)
1 parent 2cdd5d3 commit 843672d

File tree

16 files changed

+488
-0
lines changed

16 files changed

+488
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FLASK_RUN_PORT=8080
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Standard Integration Python Flask Sample
2+
3+
PayPal Standard Integration sample in Python using Flask
4+
5+
## Running the sample
6+
7+
1. **Setup a virtual environment**
8+
9+
```sh
10+
python3 -m venv .venv
11+
```
12+
13+
1. **Install the dependencies**
14+
15+
```sh
16+
pip install -r requirements.txt
17+
```
18+
19+
1. **Add your API credentials to the environment:**
20+
21+
- **Windows**
22+
23+
```powershell
24+
$env:PAYPAL_CLIENT_ID = "<PAYPAL_CLIENT_ID>"
25+
$env:PAYPAL_CLIENT_SECRET = "<PAYPAL_CLIENT_SECRET>"
26+
```
27+
28+
- **Unix**
29+
30+
```bash
31+
export PAYPAL_CLIENT_ID="<PAYPAL_CLIENT_ID>"
32+
export PAYPAL_CLIENT_SECRET="<PAYPAL_CLIENT_SECRET>"
33+
```
34+
35+
1. **Run the server**
36+
37+
```sh
38+
flask --app server run
39+
```
40+
41+
1. Go to [http://localhost:8080/](http://localhost:8080/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask==3.0.3
2+
paypal-server-sdk==0.5.2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import logging
2+
import os
3+
4+
from flask import Flask, request
5+
from paypalserversdk.http.auth.o_auth_2 import ClientCredentialsAuthCredentials
6+
from paypalserversdk.logging.configuration.api_logging_configuration import LoggingConfiguration, \
7+
RequestLoggingConfiguration, ResponseLoggingConfiguration
8+
from paypalserversdk.paypalserversdk_client import PaypalserversdkClient
9+
from paypalserversdk.controllers.orders_controller import OrdersController
10+
from paypalserversdk.models.amount_with_breakdown import AmountWithBreakdown
11+
from paypalserversdk.models.checkout_payment_intent import CheckoutPaymentIntent
12+
from paypalserversdk.models.order_request import OrderRequest
13+
from paypalserversdk.models.purchase_unit_request import PurchaseUnitRequest
14+
from paypalserversdk.api_helper import ApiHelper
15+
16+
app = Flask(__name__)
17+
18+
paypal_client: PaypalserversdkClient = PaypalserversdkClient(
19+
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
20+
o_auth_client_id=os.getenv('PAYPAL_CLIENT_ID'),
21+
o_auth_client_secret=os.getenv('PAYPAL_CLIENT_SECRET')
22+
),
23+
logging_configuration=LoggingConfiguration(
24+
log_level=logging.INFO,
25+
# Disable masking of sensitive headers for Sandbox testing.
26+
# This should be set to True (the default if unset)in production.
27+
mask_sensitive_headers=False,
28+
request_logging_config=RequestLoggingConfiguration(
29+
log_headers=True,
30+
log_body=True
31+
),
32+
response_logging_config=ResponseLoggingConfiguration(
33+
log_headers=True,
34+
log_body=True
35+
)
36+
)
37+
)
38+
39+
'''
40+
Health check
41+
'''
42+
@app.route('/', methods=['GET'])
43+
def index():
44+
return {"message": "Server is running"}
45+
46+
orders_controller: OrdersController = paypal_client.orders
47+
48+
'''
49+
Create an order to start the transaction.
50+
51+
@see https://developer.paypal.com/docs/api/orders/v2/#orders_create
52+
'''
53+
@app.route('/api/orders', methods=['POST'])
54+
def create_order():
55+
request_body = request.get_json()
56+
# use the cart information passed from the front-end to calculate the order amount detals
57+
cart = request_body['cart']
58+
order = orders_controller.orders_create({
59+
"body": OrderRequest(
60+
intent=CheckoutPaymentIntent.CAPTURE,
61+
purchase_units=[
62+
PurchaseUnitRequest(
63+
AmountWithBreakdown(
64+
currency_code='USD',
65+
value='100.00'
66+
)
67+
)
68+
]
69+
),
70+
"prefer": 'return=representation'
71+
}
72+
)
73+
return ApiHelper.json_serialize(order.body)
74+
75+
'''
76+
Capture payment for the created order to complete the transaction.
77+
78+
@see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
79+
'''
80+
@app.route('/api/orders/<order_id>/capture', methods=['POST'])
81+
def capture_order(order_id):
82+
order = orders_controller.orders_capture({
83+
'id': order_id,
84+
'prefer': 'return=representation'
85+
})
86+
return ApiHelper.json_serialize(order.body)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.3.5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "paypal-server-sdk", "~> 0.5.2"
6+
gem "puma", "~> 6.4"
7+
gem "rackup", "~> 2.1"
8+
gem "sinatra", "~> 4.0"
9+
gem "sinatra-contrib", "~> 4.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Standard Integration Ruby Sinatra Sample
2+
3+
PayPal Standard Integration sample in Ruby using Sinatra
4+
5+
## Running the sample
6+
7+
1. **Ensure you have a supported Ruby version installed**: [Ruby Maintenance Branches](https://www.ruby-lang.org/en/downloads/branches/)
8+
9+
1. **Install the dependencies**
10+
11+
```bash
12+
bundle install
13+
```
14+
15+
1. **Add your API credentials to the environment:**
16+
17+
- **Windows**
18+
19+
```powershell
20+
$env:PAYPAL_CLIENT_ID = "<PAYPAL_CLIENT_ID>"
21+
$env:PAYPAL_CLIENT_SECRET = "<PAYPAL_CLIENT_SECRET>"
22+
```
23+
24+
- **Unix**
25+
26+
```bash
27+
export PAYPAL_CLIENT_ID="<PAYPAL_CLIENT_ID>"
28+
export PAYPAL_CLIENT_SECRET="<PAYPAL_CLIENT_SECRET>"
29+
```
30+
31+
1. **Run the server**
32+
33+
```bash
34+
bundle exec ruby server.rb
35+
```
36+
37+
1. Go to [http://localhost:8080/](http://localhost:8080/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'paypal_server_sdk'
2+
require 'sinatra'
3+
require 'sinatra/json'
4+
5+
include PaypalServerSdk
6+
7+
set :port, 8080
8+
9+
paypal_client = PaypalServerSdk::Client.new(
10+
client_credentials_auth_credentials: ClientCredentialsAuthCredentials.new(
11+
o_auth_client_id: ENV['PAYPAL_CLIENT_ID'],
12+
o_auth_client_secret: ENV['PAYPAL_CLIENT_SECRET']
13+
),
14+
environment: Environment::SANDBOX,
15+
logging_configuration: LoggingConfiguration.new(
16+
mask_sensitive_headers: false,
17+
log_level: Logger::INFO,
18+
request_logging_config: RequestLoggingConfiguration.new(
19+
log_headers: true,
20+
log_body: true,
21+
),
22+
response_logging_config: ResponseLoggingConfiguration.new(
23+
log_headers: true,
24+
log_body: true
25+
)
26+
)
27+
)
28+
29+
# Health Check
30+
get '/' do
31+
json :message => "Server is running"
32+
end
33+
34+
# Create an order to start the transaction.
35+
#
36+
# @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
37+
post "/api/orders" do
38+
# use the cart information passed from the front-end to calculate the order amount detals
39+
cart = JSON.parse request.body.read
40+
order_response = paypal_client.orders.orders_create({
41+
'body' => OrderRequest.new(
42+
intent: CheckoutPaymentIntent::CAPTURE,
43+
purchase_units: [
44+
PurchaseUnitRequest.new(
45+
amount: AmountWithBreakdown.new(
46+
currency_code: 'USD',
47+
value: '100.00'
48+
)
49+
)
50+
]
51+
),
52+
'prefer' => 'return=representation'
53+
})
54+
json order_response.data
55+
end
56+
57+
# Capture payment for the created order to complete the transaction.
58+
#
59+
# @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
60+
post '/api/orders/:order_id/capture' do |order_id|
61+
capture_response = paypal_client.orders.orders_capture({
62+
'id' => order_id,
63+
'prefer' => 'return=representation'
64+
})
65+
json capture_response.data
66+
rescue ErrorException => e
67+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FLASK_RUN_PORT=8080
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Standard Integration Python Flask Sample
2+
3+
PayPal Standard Integration sample in Python using Flask
4+
5+
## Running the sample
6+
7+
1. **Setup a virtual environment**
8+
9+
```sh
10+
python3 -m venv .venv
11+
```
12+
13+
1. **Install the dependencies**
14+
15+
```sh
16+
pip install -r requirements.txt
17+
```
18+
19+
1. **Add your API credentials to the environment:**
20+
21+
- **Windows**
22+
23+
```powershell
24+
$env:PAYPAL_CLIENT_ID = "<PAYPAL_CLIENT_ID>"
25+
$env:PAYPAL_CLIENT_SECRET = "<PAYPAL_CLIENT_SECRET>"
26+
```
27+
28+
- **Unix**
29+
30+
```bash
31+
export PAYPAL_CLIENT_ID="<PAYPAL_CLIENT_ID>"
32+
export PAYPAL_CLIENT_SECRET="<PAYPAL_CLIENT_SECRET>"
33+
```
34+
35+
1. **Run the server**
36+
37+
```sh
38+
flask --app server run
39+
```
40+
41+
1. Go to [http://localhost:8080/](http://localhost:8080/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask==3.0.3
2+
paypal-server-sdk==0.5.2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import logging
2+
import os
3+
4+
from flask import Flask, request
5+
from paypalserversdk.http.auth.o_auth_2 import ClientCredentialsAuthCredentials
6+
from paypalserversdk.logging.configuration.api_logging_configuration import LoggingConfiguration, \
7+
RequestLoggingConfiguration, ResponseLoggingConfiguration
8+
from paypalserversdk.paypalserversdk_client import PaypalserversdkClient
9+
from paypalserversdk.controllers.orders_controller import OrdersController
10+
from paypalserversdk.models.amount_with_breakdown import AmountWithBreakdown
11+
from paypalserversdk.models.checkout_payment_intent import CheckoutPaymentIntent
12+
from paypalserversdk.models.order_request import OrderRequest
13+
from paypalserversdk.models.purchase_unit_request import PurchaseUnitRequest
14+
from paypalserversdk.api_helper import ApiHelper
15+
16+
app = Flask(__name__)
17+
18+
paypal_client: PaypalserversdkClient = PaypalserversdkClient(
19+
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
20+
o_auth_client_id=os.getenv('PAYPAL_CLIENT_ID'),
21+
o_auth_client_secret=os.getenv('PAYPAL_CLIENT_SECRET')
22+
),
23+
logging_configuration=LoggingConfiguration(
24+
log_level=logging.INFO,
25+
# Disable masking of sensitive headers for Sandbox testing.
26+
# This should be set to True (the default if unset)in production.
27+
mask_sensitive_headers=False,
28+
request_logging_config=RequestLoggingConfiguration(
29+
log_headers=True,
30+
log_body=True
31+
),
32+
response_logging_config=ResponseLoggingConfiguration(
33+
log_headers=True,
34+
log_body=True
35+
)
36+
)
37+
)
38+
39+
'''
40+
Health check
41+
'''
42+
@app.route('/', methods=['GET'])
43+
def index():
44+
return {"message": "Server is running"}
45+
46+
orders_controller: OrdersController = paypal_client.orders
47+
48+
'''
49+
Create an order to start the transaction.
50+
51+
@see https://developer.paypal.com/docs/api/orders/v2/#orders_create
52+
'''
53+
@app.route('/api/orders', methods=['POST'])
54+
def create_order():
55+
request_body = request.get_json()
56+
# use the cart information passed from the front-end to calculate the order amount detals
57+
cart = request_body['cart']
58+
order = orders_controller.orders_create({
59+
"body": OrderRequest(
60+
intent=CheckoutPaymentIntent.CAPTURE,
61+
purchase_units=[
62+
PurchaseUnitRequest(
63+
AmountWithBreakdown(
64+
currency_code='USD',
65+
value='100.00'
66+
)
67+
)
68+
]
69+
),
70+
"prefer": 'return=representation'
71+
}
72+
)
73+
return ApiHelper.json_serialize(order.body)
74+
75+
'''
76+
Capture payment for the created order to complete the transaction.
77+
78+
@see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
79+
'''
80+
@app.route('/api/orders/<order_id>/capture', methods=['POST'])
81+
def capture_order(order_id):
82+
order = orders_controller.orders_capture({
83+
'id': order_id,
84+
'prefer': 'return=representation'
85+
})
86+
return ApiHelper.json_serialize(order.body)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.3.5

0 commit comments

Comments
 (0)