Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from decimal import Decimal
from functools import wraps
from types import SimpleNamespace
from typing import Generic, List, NamedTuple, Optional, Tuple, TypedDict, TypeVar, Union
from typing import Generic, List, NamedTuple, NotRequired, Optional, Tuple, TypedDict, TypeVar, Union

import requests
from commercetools import Client, CommercetoolsError
Expand Down Expand Up @@ -48,6 +48,7 @@
OrderTransitionLineItemStateAction,
Payment,
PaymentAddTransactionAction,
PaymentChangeTransactionStateAction,
PaymentDraft,
PaymentMethodInfo,
PaymentResourceIdentifier,
Expand Down Expand Up @@ -142,6 +143,7 @@
currency: str
created: Union[str, int]
status: str
payment_intent: NotRequired[str]


class ProcessedRefund(TypedDict):
Expand Down Expand Up @@ -957,6 +959,40 @@
)
raise err

def change_refund_transaction_state(
self,
payment_id: str,
payment_version: int,
transaction_id: str,
state: TransactionState,
) -> Payment:
"""Change the state of an existing CommerceTools refund transaction."""
logger.info(
"[CommercetoolsAPIClient] - Changing refund transaction %s on payment %s to %s",
transaction_id,
payment_id,
state,
)
try:
return self.base_client.payments.update_by_id(
id=payment_id,
version=payment_version,
actions=[
PaymentChangeTransactionStateAction(
transaction_id=transaction_id,
state=state,
)
],
)
except CommercetoolsError as err:
handle_commercetools_error(
"[CommercetoolsAPIClient.change_refund_transaction_state]",
err,
f"Unable to change refund transaction {transaction_id} "
f"on payment {payment_id} to {state}",
)
raise

Check failure on line 994 in commerce_coordinator/apps/commercetools/clients.py

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 3.12, django42)

Missing coverage

Missing coverage on lines 987-994

def create_charge_payment_transaction(
self,
payment_id: str,
Expand Down
17 changes: 14 additions & 3 deletions commerce_coordinator/apps/commercetools/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
is_commercetools_line_item_already_refunded
)
from commerce_coordinator.apps.rollout.waffle import is_redirect_to_commercetools_enabled_for_user
from commerce_coordinator.apps.stripe.constants import StripeRefundStatus

log = getLogger(__name__)

Expand Down Expand Up @@ -313,7 +314,15 @@ def run_filter(
refunded_line_item_refunds = kwargs['refunded_line_item_refunds']
refund_response = kwargs.get('refund_response', {})

interaction_id = refund_response.get('id') if isinstance(refund_response, dict) else None
interaction_id = refund_response.get('id') if hasattr(refund_response, 'get') else None
refund_status = refund_response.get('status') if hasattr(refund_response, 'get') else None
is_stripe_async_refund = (
kwargs.get('psp') == EDX_STRIPE_PAYMENT_INTERFACE_NAME
and refund_status in {
StripeRefundStatus.REFUND_PENDING.value,
StripeRefundStatus.REFUND_SUCCESS.value,
}
)

ct_api_client = CommercetoolsAPIClient()
updated_order = ct_api_client.update_return_payment_state_after_successful_refund(
Expand All @@ -324,11 +333,13 @@ def run_filter(
refunded_line_item_refunds=refunded_line_item_refunds,
payment_intent_id=kwargs['payment_intent_id'],
interaction_id=interaction_id,
payment_state=payment_state
payment_state=payment_state,
should_transition_state=not is_stripe_async_refund,
)

return {
"returned_order": updated_order
"returned_order": updated_order,
"refund_pending": refund_status == StripeRefundStatus.REFUND_PENDING.value,
}


Expand Down
Loading
Loading