From e2f12a8c71035d9ae942904528b60e2880642fcc Mon Sep 17 00:00:00 2001 From: Syed Sajjad Hussain Shah <52817156+syedsajjadkazmii@users.noreply.github.com> Date: Wed, 8 Jan 2025 17:11:14 +0500 Subject: [PATCH] fix: PayPal refund bug on prod (#317) --- commerce_coordinator/apps/commercetools/clients.py | 2 +- commerce_coordinator/apps/paypal/clients.py | 6 ++++++ commerce_coordinator/apps/paypal/pipeline.py | 2 ++ commerce_coordinator/settings/base.py | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/commerce_coordinator/apps/commercetools/clients.py b/commerce_coordinator/apps/commercetools/clients.py index 786fc143..99e96b5c 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/paypal/clients.py b/commerce_coordinator/apps/paypal/clients.py index 1ef2bc99..55bbf9cc 100644 --- a/commerce_coordinator/apps/paypal/clients.py +++ b/commerce_coordinator/apps/paypal/clients.py @@ -3,6 +3,7 @@ from django.conf import settings from paypalserversdk.api_helper import ApiHelper +from paypalserversdk.configuration import Environment from paypalserversdk.controllers.payments_controller import PaymentsController from paypalserversdk.http.auth.o_auth_2 import ClientCredentialsAuthCredentials from paypalserversdk.paypalserversdk_client import PaypalserversdkClient @@ -18,6 +19,11 @@ 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 955bd4c6..5cb85385 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 3d183c00..792c1708 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', }, }, }