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 )
0 commit comments