From 374fc212868d0c28a6010ae7d790503c68b8fd2d Mon Sep 17 00:00:00 2001 From: Syed Sajjad Hussain Shah Date: Wed, 8 Jan 2025 15:50:55 +0500 Subject: [PATCH] fix: PayPal refund bug on prod --- commerce_coordinator/apps/commercetools/clients.py | 2 +- commerce_coordinator/apps/commercetools/views.py | 1 + commerce_coordinator/apps/paypal/clients.py | 4 ++++ commerce_coordinator/apps/paypal/pipeline.py | 2 ++ commerce_coordinator/settings/base.py | 1 + 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/commerce_coordinator/apps/commercetools/clients.py b/commerce_coordinator/apps/commercetools/clients.py index 786fc143e..99e96b5cf 100644 --- a/commerce_coordinator/apps/commercetools/clients.py +++ b/commerce_coordinator/apps/commercetools/clients.py @@ -531,7 +531,7 @@ def create_return_payment_transaction( try: logger.info( f"[CommercetoolsAPIClient] - Creating refund transaction for payment with ID {payment_id} " - f"following successful refund {refund['id']} in PSP: {psp}" + f"following successful refund {refund.get('id')} in PSP: {psp}" ) refund = self._preprocess_refund_object(refund, psp) diff --git a/commerce_coordinator/apps/commercetools/views.py b/commerce_coordinator/apps/commercetools/views.py index f159248a9..3120477f4 100644 --- a/commerce_coordinator/apps/commercetools/views.py +++ b/commerce_coordinator/apps/commercetools/views.py @@ -21,6 +21,7 @@ fulfill_order_sanctioned_message_signal ) from commerce_coordinator.apps.core.views import SingleInvocationAPIView +from commerce_coordinator.apps.paypal.clients import PayPalClient logger = logging.getLogger(__name__) diff --git a/commerce_coordinator/apps/paypal/clients.py b/commerce_coordinator/apps/paypal/clients.py index 1ef2bc99a..6db9daa58 100644 --- a/commerce_coordinator/apps/paypal/clients.py +++ b/commerce_coordinator/apps/paypal/clients.py @@ -6,6 +6,7 @@ from paypalserversdk.controllers.payments_controller import PaymentsController from paypalserversdk.http.auth.o_auth_2 import ClientCredentialsAuthCredentials from paypalserversdk.paypalserversdk_client import PaypalserversdkClient +from paypalserversdk.configuration import Environment class PayPalClient: @@ -18,6 +19,9 @@ def __init__(self): o_auth_client_id=settings.PAYMENT_PROCESSOR_CONFIG['edx']['paypal']['client_id'], o_auth_client_secret=settings.PAYMENT_PROCESSOR_CONFIG['edx']['paypal']['client_secret'], ), + environment=(Environment.SANDBOX + if settings.PAYMENT_PROCESSOR_CONFIG['edx']['paypal']['env'] == 'sandbox' + else Environment.PRODUCTION), ) def refund_order(self, capture_id): diff --git a/commerce_coordinator/apps/paypal/pipeline.py b/commerce_coordinator/apps/paypal/pipeline.py index 955bd4c60..5cb85385d 100644 --- a/commerce_coordinator/apps/paypal/pipeline.py +++ b/commerce_coordinator/apps/paypal/pipeline.py @@ -60,6 +60,8 @@ def run_filter( tag = type(self).__name__ if psp != EDX_PAYPAL_PAYMENT_INTERFACE_NAME or not amount_in_cents or not ct_transaction_interaction_id: + logger.info(f'[{tag}] capture_id or amount_in_cents not set, ' + f'skipping refund for order: {order_id} with psp: {psp}') return PipelineCommand.CONTINUE.value if has_been_refunded: diff --git a/commerce_coordinator/settings/base.py b/commerce_coordinator/settings/base.py index 3d183c000..792c17083 100644 --- a/commerce_coordinator/settings/base.py +++ b/commerce_coordinator/settings/base.py @@ -441,6 +441,7 @@ def root(*path_fragments): 'paypal_webhook_id': PAYPAL_WEBHOOK_ID, 'client_id': '', 'client_secret': '', + 'env': 'sandbox', }, }, }