Skip to content

Commit

Permalink
fix: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
mubbsharanwar committed Dec 17, 2024
1 parent 86b61a5 commit 68da628
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 47 deletions.
15 changes: 0 additions & 15 deletions commerce_coordinator/apps/commercetools/catalog_info/edx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from commercetools.platform.models import TransactionType

from commerce_coordinator.apps.commercetools.catalog_info.constants import (
EDX_STRIPE_PAYMENT_INTERFACE_NAME,
EDX_PAYPAL_PAYMENT_INTERFACE_NAME,
PAYMENT_STATUS_INTERFACE_CODE_SUCCEEDED,
EdXFieldNames,
TwoUKeys
Expand Down Expand Up @@ -49,17 +47,6 @@ def get_edx_lms_user_name(customer: CTCustomer):
return customer.custom.fields[EdXFieldNames.LMS_USER_NAME]


def get_edx_successful_stripe_payment(order: CTOrder) -> Union[CTPayment, None]:
for pr in order.payment_info.payments:
pmt = pr.obj
if pmt.payment_status.interface_code == PAYMENT_STATUS_INTERFACE_CODE_SUCCEEDED \
and pmt.payment_method_info.payment_interface == EDX_STRIPE_PAYMENT_INTERFACE_NAME and \
pmt.interface_id:
return pmt
return None


# TODO remove get_edx_successful_stripe_payment if there is no more use.
def get_edx_successful_payment_info(order: CTOrder):
for pr in order.payment_info.payments:
pmt = pr.obj
Expand Down Expand Up @@ -90,9 +77,7 @@ def get_edx_refund_info(payment: CTPayment) -> decimal:
refund_amount = decimal.Decimal(0.00)
interaction_id = None
for transaction in payment.transactions:
print('\n\n\n\n\n\nget_edx_refund_amount transaction.type = ', transaction.type)
if transaction.type == TransactionType.CHARGE: # pragma no cover
print('\n\n\n\n\n\nget_edx_refund_amount transaction.amount = ', transaction.amount)
refund_amount += decimal.Decimal(typed_money_to_string(transaction.amount, money_as_decimal_string=True))
interaction_id = transaction.interaction_id
return refund_amount, interaction_id
Expand Down
5 changes: 2 additions & 3 deletions commerce_coordinator/apps/commercetools/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from requests import HTTPError

from commerce_coordinator.apps.commercetools.catalog_info.constants import (
EDX_STRIPE_PAYMENT_INTERFACE_NAME,
EDX_PAYPAL_PAYMENT_INTERFACE_NAME
EDX_PAYPAL_PAYMENT_INTERFACE_NAME,
EDX_STRIPE_PAYMENT_INTERFACE_NAME
)
from commerce_coordinator.apps.commercetools.catalog_info.edx_utils import (
get_edx_refund_info,
Expand Down Expand Up @@ -337,7 +337,6 @@ def run_filter(
payment_on_order = ct_api_client.get_payment_by_key(payment_key)
elif psp == EDX_PAYPAL_PAYMENT_INTERFACE_NAME:
payment_on_order = ct_api_client.get_payment_by_key(payment_intent_id)
print('\n\n\n paypal payment_on_order = ', payment_on_order)

updated_payment = ct_api_client.create_return_payment_transaction(
payment_id=payment_on_order.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from unittest import TestCase
from unittest.mock import patch

from commerce_coordinator.apps.commercetools.catalog_info.constants import EDX_STRIPE_PAYMENT_INTERFACE_NAME
from commercetools.platform.models import ReturnInfo, ReturnPaymentState, ReturnShipmentState, TransactionType
from django.contrib.auth import get_user_model
from django.test import RequestFactory
from rest_framework.test import APITestCase

from commerce_coordinator.apps.commercetools.catalog_info.constants import EDX_STRIPE_PAYMENT_INTERFACE_NAME
from commerce_coordinator.apps.commercetools.constants import COMMERCETOOLS_ORDER_MANAGEMENT_SYSTEM
from commerce_coordinator.apps.commercetools.pipeline import (
AnonymizeRetiredUser,
Expand Down Expand Up @@ -148,6 +148,7 @@ def test_commercetools_transaction_create(self, mock_returned_payment, mock_paym
refund_response={"payment_intent": "mock_payment_intent"},
active_order_management_system=COMMERCETOOLS_ORDER_MANAGEMENT_SYSTEM,
has_been_refunded=False,
payment_intent_id="pi_4MtwBwLkdIwGlenn28a3tqPa",
psp=EDX_STRIPE_PAYMENT_INTERFACE_NAME
)
mock_payment_result = ret['returned_payment']
Expand All @@ -170,6 +171,7 @@ def test_commercetools_transaction_create_no_payment_data(self, mock_returned_pa
refund_response={"payment_intent": "mock_payment_intent"},
active_order_management_system=COMMERCETOOLS_ORDER_MANAGEMENT_SYSTEM,
has_been_refunded=False,
payment_intent_id="pi_4MtwBwLkdIwGlenn28a3tqPa",
psp=EDX_STRIPE_PAYMENT_INTERFACE_NAME
)
mock_payment_result = ret['returned_payment']
Expand All @@ -188,6 +190,7 @@ def test_commercetools_transaction_create_has_refund(self, mock_logger, mock_has
refund_response="charge_already_refunded",
active_order_management_system=COMMERCETOOLS_ORDER_MANAGEMENT_SYSTEM,
has_been_refunded=True,
payment_intent_id="pi_4MtwBwLkdIwGlenn28a3tqPa",
psp=EDX_STRIPE_PAYMENT_INTERFACE_NAME
)
mock_logger.assert_called_once_with('[CreateReturnPaymentTransaction] refund has already been processed, '
Expand Down
2 changes: 0 additions & 2 deletions commerce_coordinator/apps/commercetools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ def post(self, request):
**request.data
}

print('\n\n\n\n\n\nrequest.data', request.data)

logger.debug(f'[CT-{tag}] Message received from commercetools with details: {input_data}')

message_details = OrderReturnedViewMessageInputSerializer(data=input_data)
Expand Down
23 changes: 18 additions & 5 deletions commerce_coordinator/apps/paypal/clients.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
"""PayPal client"""
import json

from django.conf import settings
from paypalserversdk.api_helper import ApiHelper
from paypalserversdk.controllers.payments_controller import PaymentsController
from paypalserversdk.http.auth.o_auth_2 import ClientCredentialsAuthCredentials
from paypalserversdk.paypalserversdk_client import PaypalserversdkClient
from paypalserversdk.controllers.payments_controller import PaymentsController
from paypalserversdk.api_helper import ApiHelper


class PayPalClient:
"""
PayPal SDK client to call PayPal APIs.
"""
def __init__(self):
self.paypal_client: PaypalserversdkClient = PaypalserversdkClient(
client_credentials_auth_credentials=ClientCredentialsAuthCredentials(
Expand All @@ -17,16 +22,24 @@ def __init__(self):


def refund_order(self, capture_id):
"""
Capture PayPal refund.
Args:
capture_id (str): The identifier of the PayPal order to cpture refund.
Returns:
The response from PayPal.
"""

paypal_client = self.paypal_client
payments_controller: PaymentsController = paypal_client.payments

collect = {"capture_id": capture_id, "prefer": "return=representation"}
refund_response = payments_controller.captures_refund(collect)
print('\n\n\n\n\n refund_response.body = ', refund_response.body)

if refund_response.body:
response = json.loads(ApiHelper.json_serialize(refund_response.body))
print('\n\n\n\n\n refund_order response serialized = ', response)

return {
"id": response.get("id"),
"created": response.get("create_time"),
Expand Down
10 changes: 4 additions & 6 deletions commerce_coordinator/apps/paypal/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from urllib.parse import urlencode

from django.conf import settings
from commerce_coordinator.apps.paypal.clients import PayPalClient
from commerce_coordinator.apps.core.constants import PipelineCommand
from openedx_filters import PipelineStep
from requests import RequestException

from commerce_coordinator.apps.commercetools.catalog_info.constants import EDX_PAYPAL_PAYMENT_INTERFACE_NAME
from commerce_coordinator.apps.core.constants import PipelineCommand
from commerce_coordinator.apps.paypal.clients import PayPalClient

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -71,7 +71,7 @@ def run_filter(
try:
paypal_client = PayPalClient()
paypal_refund_response = paypal_client.refund_order(capture_id=ct_transaction_interaction_id)
print('\n\n\n\n\n paypal_refund_response = ', paypal_refund_response)

return {
'refund_response': paypal_refund_response,
}
Expand All @@ -81,6 +81,4 @@ def run_filter(
f'message_id: {kwargs["message_id"]} '
f'exception: {ex}')

raise RequestException(str(ex))


raise RequestException(str(ex)) from ex
1 change: 0 additions & 1 deletion commerce_coordinator/apps/paypal/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
"""Constants for PayPal app tests."""
3 changes: 0 additions & 3 deletions commerce_coordinator/apps/stripe/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ def run_filter(
"""

tag = type(self).__name__
print('\n\n\n\n\n\nRefundPaymentIntent psp = ', psp)
print('\n\n\n\n\n\nRefundPaymentIntent payment_intent_id = ', payment_intent_id)
print('\n\n\n\n\n\nRefundPaymentIntent amount_in_cents = ', amount_in_cents)

if psp != EDX_STRIPE_PAYMENT_INTERFACE_NAME or not payment_intent_id or not amount_in_cents: # pragma: no cover
logger.info(f'[{tag}] payment_intent_id or amount_in_cents not set, skipping.')
Expand Down
4 changes: 2 additions & 2 deletions commerce_coordinator/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ def root(*path_fragments):
'paypal': {
'user_activity_page_url': '',
'paypal_webhook_id': PAYPAL_WEBHOOK_ID,
'client_id': 'ASoOt8z1BmLEzJGLV-N_gWP083ghlpWaj9eOj4BxQ9k8rQ-jDoSO5e_5-gRR3uzwp-hOt_YmfzKsnrFV',
'client_secret': 'EL_StzjNHS0lUbtVLPOUxC-fa27E4x12WIAN4XRHjYNNYM3kkUNxXoaFd_XextoDC1h3TvF9vuC74J1Z',
'client_id': '',
'client_secret': '',
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions commerce_coordinator/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@
'paypal': {
'user_activity_page_url': 'https://sandbox.paypal.com/myaccount/activities/',
'paypal_webhook_id': 'SET-ME-PLEASE',
'client_id': 'ASoOt8z1BmLEzJGLV-N_gWP083ghlpWaj9eOj4BxQ9k8rQ-jDoSO5e_5-gRR3uzwp-hOt_YmfzKsnrFV',
'client_secret': 'EL_StzjNHS0lUbtVLPOUxC-fa27E4x12WIAN4XRHjYNNYM3kkUNxXoaFd_XextoDC1h3TvF9vuC74J1Z',
'client_id': '',
'client_secret': '',
},
},
}
Expand Down
2 changes: 2 additions & 0 deletions commerce_coordinator/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
'paypal': {
'user_activity_page_url': 'https://test.paypal.com/myaccount/activities/',
'paypal_webhook_id': PAYPAL_WEBHOOK_ID,
'client_id': '',
'client_secret': '',
},
},
}
Expand Down
5 changes: 0 additions & 5 deletions commerce_coordinator/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
from commerce_coordinator.apps.titan import urls as titan_urls
from commerce_coordinator.settings.base import FAVICON_URL

from commerce_coordinator.apps.paypal.clients import PayPalClient

paypal_client = PayPalClient()

admin.autodiscover()

Expand Down Expand Up @@ -74,7 +71,6 @@
re_path(r'^$', lambda r: JsonResponse(data=[
"Welcome to Commerce Coordinator",
"This is an API app that provides a backend for Commerce.",
str(paypal_client),
], status=status.HTTP_200_OK, safe=False), name='root'),

path('favicon.ico', RedirectView.as_view(url=FAVICON_URL), name='favicon'),
Expand All @@ -89,4 +85,3 @@
import debug_toolbar

urlpatterns.append(re_path(r'^__debug__/', include(debug_toolbar.urls)))

1 change: 1 addition & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ edx-rest-api-client
mysqlclient
openedx-filters
Pillow
paypal-server-sdk==0.5.1
pytz
segment-analytics-python
stripe
2 changes: 0 additions & 2 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ openedx-filters==1.11.0
# via -r requirements/base.in
packaging==24.1
# via marshmallow
paypal-server-sdk==0.5.1
# via -r requirements/base.in
pbr==6.1.0
# via stevedore
pillow==11.0.0
Expand Down
4 changes: 4 additions & 0 deletions requirements/common_constraints.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@





# A central location for most common version constraints
# (across edx repos) for pip-installation.
#
Expand Down

0 comments on commit 68da628

Please sign in to comment.