44from bitpay .exceptions .bitpay_exception import BitPayException
55from bitpay .exceptions .payout_cancellation_exception import PayoutCancellationException
66from bitpay .exceptions .payout_creation_exception import PayoutCreationException
7+ from bitpay .exceptions .payout_exception import PayoutException
78from bitpay .exceptions .payout_notification_exception import PayoutNotificationException
89from bitpay .exceptions .payout_query_exception import PayoutQueryException
910from bitpay .models .facade import Facade
1011from bitpay .models .payout .payout import Payout
12+ from bitpay .models .payout .payout_group import PayoutGroup
13+ from bitpay .models .payout .payout_group_failed import PayoutGroupFailed
1114from bitpay .utils .token_container import TokenContainer
1215
1316
@@ -34,7 +37,7 @@ def submit(self, payout: Payout) -> Payout:
3437 response_json = self .__bitpay_client .post ("payouts" , payout .to_json (), True )
3538 except BitPayException as exe :
3639 raise PayoutCreationException (
37- "failed to serialize Payout object : %s" % str (exe ), exe .get_api_code ()
40+ "failed to serialize Payout object : %s" % str (exe ), api_code = exe .get_api_code ()
3841 )
3942
4043 try :
@@ -62,7 +65,7 @@ def get(self, payout_id: str) -> Payout:
6265 response_json = self .__bitpay_client .get ("payouts/%s" % payout_id , params )
6366 except BitPayException as exe :
6467 raise PayoutQueryException (
65- "failed to serialize Payout object : %s" % str (exe ), exe .get_api_code ()
68+ "failed to serialize Payout object : %s" % str (exe ), api_code = exe .get_api_code ()
6669 )
6770
6871 try :
@@ -115,7 +118,7 @@ def get_payouts(
115118 response_json = self .__bitpay_client .get ("payouts" , params )
116119 except BitPayException as exe :
117120 raise PayoutQueryException (
118- "failed to serialize Payout object : %s" % str (exe ), exe .get_api_code ()
121+ "failed to serialize Payout object : %s" % str (exe ), api_code = exe .get_api_code ()
119122 )
120123
121124 try :
@@ -146,7 +149,8 @@ def cancel(self, payout_id: str) -> bool:
146149 )
147150 except BitPayException as exe :
148151 raise PayoutCancellationException (
149- "failed to serialize Payout object : %s" % str (exe ), exe .get_api_code ()
152+ "failed to serialize Payout object : %s" % str (exe ),
153+ api_code = exe .get_api_code (),
150154 )
151155
152156 try :
@@ -174,7 +178,8 @@ def request_notification(self, payout_id: str) -> bool:
174178 )
175179 except BitPayException as exe :
176180 raise PayoutNotificationException (
177- "failed to serialize Payout object : %s" % str (exe ), exe .get_api_code ()
181+ "failed to serialize Payout object : %s" % str (exe ),
182+ api_code = exe .get_api_code (),
178183 )
179184
180185 try :
@@ -184,3 +189,49 @@ def request_notification(self, payout_id: str) -> bool:
184189 "failed to deserialize BitPay server response "
185190 " (Payout) : %s" % str (exe )
186191 )
192+
193+ @staticmethod
194+ def get_payout_group_response (
195+ response_json : dict , response_type : str
196+ ) -> PayoutGroup :
197+ try :
198+ payouts = []
199+ for payout in response_json [response_type ]:
200+ payouts .append (Payout (** payout ))
201+
202+ failed = []
203+ for fail in response_json ["failed" ]:
204+ failed .append (PayoutGroupFailed (** fail ))
205+
206+ return PayoutGroup (payouts , failed )
207+ except :
208+ raise PayoutException ("Unable to parse payouts" )
209+
210+ def create_group (self , payouts : List [Payout ]) -> PayoutGroup :
211+ params = {"token" : self .__token_container .get_access_token (Facade .PAYOUT )}
212+ instructions = []
213+
214+ try :
215+ for payout in payouts :
216+ instructions .append (payout .to_json ())
217+
218+ params ["instructions" ] = instructions
219+
220+ response_json = self .__bitpay_client .post ("payouts/group" , params )
221+ return self .get_payout_group_response (response_json , "completed" )
222+ except BitPayException as exe :
223+ raise PayoutCreationException (
224+ "failed to serialize Payout object : %s" % str (exe ),
225+ api_code = exe .get_api_code (),
226+ )
227+
228+ def cancel_group (self , group_id : str ) -> PayoutGroup :
229+ params = {"token" : self .__token_container .get_access_token (Facade .PAYOUT )}
230+ try :
231+ response_json = self .__bitpay_client .delete ("payouts/group/" + group_id , params )
232+ return self .get_payout_group_response (response_json , "cancelled" )
233+ except BitPayException as exe :
234+ raise PayoutCancellationException (
235+ "failed to serialize Payout object : %s" % str (exe ),
236+ api_code = exe .get_api_code (),
237+ )
0 commit comments