Coverage for mindsdb / integrations / handlers / paypal_handler / paypal_handler.py: 0%

50 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1import paypalrestsdk 

2 

3from mindsdb.integrations.handlers.paypal_handler.paypal_tables import ( 

4 InvoicesTable, 

5 PaymentsTable, 

6 SubscriptionsTable, 

7 OrdersTable, 

8 PayoutsTable 

9) 

10from mindsdb.integrations.libs.api_handler import APIHandler 

11from mindsdb.integrations.libs.response import ( 

12 HandlerStatusResponse as StatusResponse, 

13) 

14 

15from mindsdb.utilities import log 

16from mindsdb.integrations.libs.const import HANDLER_CONNECTION_ARG_TYPE as ARG_TYPE 

17from mindsdb_sql_parser import parse_sql 

18from collections import OrderedDict 

19 

20logger = log.getLogger(__name__) 

21 

22 

23class PayPalHandler(APIHandler): 

24 """ 

25 The PayPal handler implementation. 

26 """ 

27 

28 name = 'paypal' 

29 

30 def __init__(self, name: str, **kwargs): 

31 """ 

32 Initialize the handler. 

33 Args: 

34 name (str): name of particular handler instance 

35 **kwargs: arbitrary keyword arguments. 

36 """ 

37 super().__init__(name) 

38 

39 connection_data = kwargs.get("connection_data", {}) 

40 self.connection_data = connection_data 

41 self.kwargs = kwargs 

42 

43 self.connection = None 

44 self.is_connected = False 

45 

46 payments_data = PaymentsTable(self) 

47 self._register_table("payments", payments_data) 

48 

49 invoices_data = InvoicesTable(self) 

50 self._register_table("invoices", invoices_data) 

51 

52 subscriptions_data = SubscriptionsTable(self) 

53 self._register_table("subscriptions", subscriptions_data) 

54 

55 orders_data = OrdersTable(self) 

56 self._register_table("orders", orders_data) 

57 

58 payouts_data = PayoutsTable(self) 

59 self._register_table("payouts", payouts_data) 

60 

61 def connect(self): 

62 """ 

63 Set up the connection required by the handler. 

64 Returns 

65 ------- 

66 StatusResponse 

67 connection object 

68 """ 

69 if self.is_connected is True: 

70 return self.connection 

71 

72 self.connection = paypalrestsdk.Api( 

73 { 

74 "mode": self.connection_data['mode'], 

75 "client_id": self.connection_data['client_id'], 

76 "client_secret": self.connection_data['client_secret'], 

77 } 

78 ) 

79 

80 self.is_connected = True 

81 

82 return self.connection 

83 

84 def check_connection(self) -> StatusResponse: 

85 """ 

86 Check connection to the handler. 

87 Returns: 

88 HandlerStatusResponse 

89 """ 

90 

91 response = StatusResponse(False) 

92 

93 try: 

94 connection = self.connect() 

95 connection.get_access_token() 

96 response.success = True 

97 except Exception as e: 

98 logger.error('Error connecting to PayPal!') 

99 response.error_message = str(e) 

100 

101 self.is_connected = response.success 

102 

103 return response 

104 

105 def native_query(self, query: str) -> StatusResponse: 

106 """Receive and process a raw query. 

107 Parameters 

108 ---------- 

109 query : str 

110 query in a native format 

111 Returns 

112 ------- 

113 StatusResponse 

114 Request status 

115 """ 

116 ast = parse_sql(query) 

117 return self.query(ast) 

118 

119 

120connection_args = OrderedDict( 

121 mode={ 

122 "type": ARG_TYPE.STR, 

123 "description": "Environment mode of the app", 

124 "required": True, 

125 "label": "MODE", 

126 }, 

127 client_id={ 

128 "type": ARG_TYPE.PWD, 

129 "description": "Client id of the App", 

130 "required": True, 

131 "label": "Client ID", 

132 }, 

133 client_secret={ 

134 "type": ARG_TYPE.STR, 

135 "description": "Client secret of the App", 

136 "required": True, 

137 "label": "Client Secret", 

138 }, 

139) 

140 

141connection_args_example = OrderedDict( 

142 mode="sandbox", 

143 client_id="xxxx-xxxx-xxxx-xxxx", 

144 client_secret="<secret>", 

145)