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
85 changes: 84 additions & 1 deletion commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,67 @@
)
raise err

def create_charge_payment_transaction(
self,
payment_id: str,
payment_version: int,
charge_id: str,
amount_in_cents: int,
currency_code: str,
charge_created: datetime.datetime,
) -> Payment:
"""
Add a Charge transaction to an existing CT Payment, mirroring
create_return_payment_transaction but for TransactionType.CHARGE.

Args:
payment_id: CT Payment ID (UUID)
payment_version: Current version of the CT payment
charge_id: Stripe Charge ID (used as interaction_id for idempotency)
amount_in_cents: Charge amount in minor currency units
currency_code: ISO 4217 currency code (e.g. 'USD')
charge_created: Timestamp of the Stripe charge

Returns:
Updated Payment object with the new Charge transaction
"""
try:
logger.info(
f"[CommercetoolsAPIClient] - Creating charge transaction for "
f"payment {payment_id} with charge {charge_id}"
)

amount_as_money = Money(
cent_amount=amount_in_cents,
currency_code=currency_code.upper(),
)

transaction_draft = TransactionDraft(
type=TransactionType.CHARGE,
amount=amount_as_money,
timestamp=charge_created,
state=TransactionState.SUCCESS,
interaction_id=charge_id,
)

add_transaction_action = PaymentAddTransactionAction(
transaction=transaction_draft
)

return self.base_client.payments.update_by_id(
id=payment_id,
version=payment_version,
actions=[add_transaction_action],
)
except CommercetoolsError as err:
handle_commercetools_error(
"[CommercetoolsAPIClient.create_charge_payment_transaction]",
err,
f"Unable to create charge transaction for payment {payment_id}, "
f"charge {charge_id}",
)
raise err

Check failure on line 1019 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 1012-1019

def update_line_item_on_fulfillment(
self,
entitlement_uuid: str,
Expand Down Expand Up @@ -1340,8 +1401,30 @@
err,
f"Failed to update customer: {customer.id}",
)
raise err

Check failure on line 1404 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 line 1404

@conditional_retry
def get_cart_by_id(self, cart_id: str) -> Cart:
"""
Fetch a cart by its ID.

Args:
cart_id (str): Cart ID (UUID)

Returns:
Cart object
"""
try:
logger.info(f"[CommercetoolsAPIClient] - Attempting to find cart with ID {cart_id}")
return self.base_client.carts.get_by_id(cart_id)
except CommercetoolsError as err:
handle_commercetools_error(
"[CommercetoolsAPIClient.get_cart_by_id]",
err,
f"Failed to find cart with ID {cart_id}",
)
raise err

Check failure on line 1426 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 1417-1426

@conditional_retry
def get_customer_cart(self, customer_id: str) -> Optional[Cart]:
"""
Expand Down Expand Up @@ -1800,7 +1883,7 @@
)

if not response or not response.results:
raise Exception(f"No order found for payment ID {payment_id}")
raise ValueError(f"No order found for payment ID {payment_id}")

return response.results[0]

Expand Down
14 changes: 14 additions & 0 deletions commerce_coordinator/apps/commercetools/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from commerce_coordinator.apps.commercetools.catalog_info.constants import TwoUKeys
from commerce_coordinator.apps.commercetools.tasks import (
finalize_commercetools_stripe_payment_task,
fulfillment_completed_update_ct_line_item_task,
refund_from_mobile_task,
refund_from_paypal_task,
Expand Down Expand Up @@ -94,6 +95,19 @@
return async_result.id


@log_receiver(logger)
def finalize_commercetools_stripe_payment(**kwargs):
"""
Receive the payment_succeeded_commercetools_signal and dispatch
the shared finalize task for a CT Stripe PaymentIntent.
"""
async_result = finalize_commercetools_stripe_payment_task.delay(
payment_intent_id=kwargs["payment_intent_id"],
source="webhook",
)
return async_result.id

Check failure on line 108 in commerce_coordinator/apps/commercetools/signals.py

View workflow job for this annotation

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

Missing coverage

Missing coverage on lines 104-108


@log_receiver(logger)
def revoke_line_mobile_order(**kwargs):
"""
Expand Down
Loading
Loading