12
12
from openedx_filters .exceptions import OpenEdxFilterException
13
13
from requests import HTTPError
14
14
15
- from commerce_coordinator .apps .commercetools .catalog_info .constants import EDX_STRIPE_PAYMENT_INTERFACE_NAME
15
+ from commerce_coordinator .apps .commercetools .catalog_info .constants import (
16
+ EDX_STRIPE_PAYMENT_INTERFACE_NAME ,
17
+ EDX_PAYPAL_PAYMENT_INTERFACE_NAME
18
+ )
16
19
from commerce_coordinator .apps .commercetools .catalog_info .edx_utils import (
17
- get_edx_payment_intent_id ,
18
20
get_edx_refund_amount ,
19
21
get_edx_successful_payment_info
20
22
)
@@ -110,12 +112,12 @@ def run_filter(self, active_order_management_system, order_number, **kwargs): #
110
112
duration = (datetime .now () - start_time ).total_seconds ()
111
113
log .info (f"[Performance Check] get_order_by_number call took { duration } seconds" )
112
114
113
- intent_id , psp = get_edx_successful_payment_info (ct_order )
115
+ payment , psp = get_edx_successful_payment_info (ct_order )
114
116
115
117
ret_val = {
116
118
"order_data" : ct_order ,
117
119
"psp" : psp ,
118
- "payment_intent_id" : intent_id
120
+ "payment_intent_id" : payment . interface_id
119
121
}
120
122
121
123
return ret_val
@@ -156,21 +158,21 @@ def run_filter(self, active_order_management_system, order_id, **kwargs): # pyl
156
158
duration = (datetime .now () - start_time ).total_seconds ()
157
159
log .info (f"[Performance Check] get_order_by_id call took { duration } seconds" )
158
160
161
+ payment , psp = get_edx_successful_payment_info (ct_order )
162
+
159
163
ret_val = {
160
164
"order_data" : ct_order ,
161
- "order_id" : ct_order .id
165
+ "order_id" : ct_order .id ,
166
+ "psp" : psp ,
167
+ "payment_intent_id" : payment .interface_id
162
168
}
163
169
164
- intent_id = get_edx_payment_intent_id (ct_order )
165
-
166
- if intent_id :
167
- ct_payment = ct_api_client .get_payment_by_key (intent_id )
168
- ret_val ['payment_intent_id' ] = intent_id
170
+ if payment :
171
+ ct_payment = ct_api_client .get_payment_by_key (payment .intent_id )
169
172
ret_val ['amount_in_cents' ] = get_edx_refund_amount (ct_order )
170
173
ret_val ['has_been_refunded' ] = has_refund_transaction (ct_payment )
171
174
ret_val ['payment_data' ] = ct_payment
172
175
else :
173
- ret_val ['payment_intent_id' ] = None
174
176
ret_val ['amount_in_cents' ] = decimal .Decimal (0.00 )
175
177
ret_val ['has_been_refunded' ] = False
176
178
ret_val ['payment_data' ] = None
@@ -288,7 +290,7 @@ def run_filter(
288
290
class CreateReturnPaymentTransaction (PipelineStep ):
289
291
"""
290
292
Creates a Transaction for a return payment of a Commercetools order
291
- based on Stripes refund object on a refunded charge.
293
+ based on Stripes or PayPal refund object on a refunded charge.
292
294
"""
293
295
294
296
def run_filter (
@@ -297,12 +299,13 @@ def run_filter(
297
299
active_order_management_system ,
298
300
payment_data ,
299
301
has_been_refunded ,
302
+ psp ,
300
303
** kwargs
301
304
): # pylint: disable=arguments-differ
302
305
"""
303
306
Execute a filter with the signature specified.
304
307
Arguments:
305
- refund_response: Stripe refund object or str value "charge_already_refunded"
308
+ refund_response: PSP refund object or str value "charge_already_refunded"
306
309
active_order_management_system: The Active Order System
307
310
payment_data: CT payment object attached to the refunded order
308
311
has_been_refunded (bool): Has this payment been refunded
@@ -325,28 +328,37 @@ def run_filter(
325
328
try :
326
329
if payment_data is not None :
327
330
payment_on_order = payment_data
328
- else :
331
+ elif psp == EDX_STRIPE_PAYMENT_INTERFACE_NAME :
329
332
payment_key = refund_response ['payment_intent' ]
330
333
payment_on_order = ct_api_client .get_payment_by_key (payment_key )
334
+ elif psp == EDX_PAYPAL_PAYMENT_INTERFACE_NAME :
335
+ payment_on_order = ct_api_client .get_payment_by_transaction_interaction_id (refund_response ['paypal_capture_id' ])
331
336
332
337
updated_payment = ct_api_client .create_return_payment_transaction (
333
338
payment_id = payment_on_order .id ,
334
339
payment_version = payment_on_order .version ,
335
- refund = refund_response
340
+ refund = refund_response ,
341
+ psp = psp ,
336
342
)
337
343
338
344
return {
339
345
'returned_payment' : updated_payment
340
346
}
341
347
except CommercetoolsError as err : # pragma no cover
348
+ if psp == EDX_STRIPE_PAYMENT_INTERFACE_NAME :
349
+ error_message = f"[payment_intent_id: { refund_response ['payment_intent' ]} , "
350
+ elif psp == EDX_PAYPAL_PAYMENT_INTERFACE_NAME :
351
+ error_message = f"[paypal_capture_id: { refund_response ['paypal_capture_id' ]} , "
342
352
log .info (f"[{ tag } ] Unsuccessful attempt to create refund payment transaction with details: "
343
- f"[stripe_payment_intent_id: { refund_response ['payment_intent' ]} , "
353
+ f"psp: { psp } , "
354
+ f"{ error_message } "
344
355
f"payment_id: { payment_on_order .id } ], message_id: { kwargs ['message_id' ]} " )
345
356
log .exception (f"[{ tag } ] Commercetools Error: { err } , { err .errors } " )
346
357
return PipelineCommand .CONTINUE .value
347
358
except HTTPError as err : # pragma no cover
348
359
log .info (f"[{ tag } ] Unsuccessful attempt to create refund payment transaction with details: "
349
- f"[stripe_payment_intent_id: { refund_response ['payment_intent' ]} , "
360
+ f"psp: { psp } , "
361
+ f"{ error_message } "
350
362
f"payment_id: { payment_on_order .id } ], message_id: { kwargs ['message_id' ]} " )
351
363
log .exception (f"[{ tag } ] HTTP Error: { err } " )
352
364
return PipelineCommand .CONTINUE .value
0 commit comments