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