-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unfulfillment logic consolidation
Encapsulates logic around canceling platform fulfillment/enrollment, external fulfillment (e.g. GEAG), and transaction reversal into a new transaction.api module. Then refactors and/or modifies three main integration points to make use of this new module as follows: 1. The reversal-writing management command now calls the common business logic functions to cancel external fulfillments before reversing a transaction. This is just a refactor, no new behavior here. 2. Modified the listen_for_transaction_reversal signal handler to cancel both external (GEAG) and platform fulfillments. New behavior here is to cancel the external fulfillment. For background, this signal handler is what enables the "Unenroll & Refund" Transaction Django Admin action to perform the "unenroll" portion. 3. Modified the "Unenroll only" Transaction Django Admin view to cancel both external and platform fulfillments. Prior to this change, only the platform fulfillment was canceled. ENT-8633
- Loading branch information
1 parent
66fd2d4
commit 7210ba0
Showing
11 changed files
with
241 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Core business logic around transactions. | ||
""" | ||
import logging | ||
|
||
import requests | ||
from django.utils import timezone | ||
from openedx_ledger.api import reverse_full_transaction | ||
from openedx_ledger.models import TransactionStateChoices | ||
|
||
from enterprise_subsidy.apps.api_client.enterprise import EnterpriseApiClient | ||
from enterprise_subsidy.apps.fulfillment.api import GEAGFulfillmentHandler | ||
from enterprise_subsidy.apps.transaction.utils import generate_transaction_reversal_idempotency_key | ||
|
||
from .exceptions import TransactionFulfillmentCancelationException | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def cancel_transaction_fulfillment(transaction): | ||
""" | ||
Cancels the edx-platform fulfillment (typically the verified enrollment gets moved | ||
to the ``audit`` mode). | ||
""" | ||
if transaction.state != TransactionStateChoices.COMMITTED: | ||
logger.info( | ||
"[fulfillment cancelation] %s is not committed, will not cancel fulfillment", | ||
transaction.uuid, | ||
) | ||
raise TransactionFulfillmentCancelationException( | ||
"Transaction is not committed" | ||
) | ||
if not transaction.fulfillment_identifier: | ||
logger.info( | ||
"[fulfillment cancelation] %s has no fulfillment uuid, will not cancel fulfillment", | ||
transaction.uuid, | ||
) | ||
raise TransactionFulfillmentCancelationException( | ||
"Transaction has no associated platform fulfillment identifier" | ||
) | ||
|
||
try: | ||
EnterpriseApiClient().cancel_fulfillment(transaction.fulfillment_identifier) | ||
except requests.exceptions.HTTPError as exc: | ||
error_msg = ( | ||
"Error canceling platform fulfillment " | ||
f"{transaction.fulfillment_identifier}: {exc}" | ||
) | ||
logger.exception("[fulfillment cancelation] %s", error_msg) | ||
raise TransactionFulfillmentCancelationException(error_msg) from exc | ||
|
||
|
||
def cancel_transaction_external_fulfillment(transaction): | ||
""" | ||
Cancels all related external GEAG allocations for the given transaction. | ||
raises: | ||
FulfillmentException if the related external references for the transaction | ||
are not for a GEAG fulfillment provider. | ||
""" | ||
if transaction.state != TransactionStateChoices.COMMITTED: | ||
logger.info( | ||
"[fulfillment cancelation] %s is not committed, will not cancel fulfillment", | ||
transaction.uuid, | ||
) | ||
raise TransactionFulfillmentCancelationException( | ||
"Transaction is not committed" | ||
) | ||
|
||
for external_reference in transaction.external_reference.all(): | ||
provider_slug = external_reference.external_fulfillment_provider.slug | ||
geag_handler = GEAGFulfillmentHandler() | ||
if provider_slug == geag_handler.EXTERNAL_FULFILLMENT_PROVIDER_SLUG: | ||
geag_handler.cancel_fulfillment(external_reference) | ||
else: | ||
logger.warning( | ||
'[fulfillment cancelation] dont know how to cancel transaction %s with provider %s', | ||
transaction.uuid, | ||
provider_slug, | ||
) | ||
|
||
|
||
def reverse_transaction(transaction, unenroll_time=None): | ||
""" | ||
Creates a reversal for the provided transaction. | ||
""" | ||
idempotency_key = generate_transaction_reversal_idempotency_key( | ||
transaction.fulfillment_identifier, | ||
unenroll_time or timezone.now(), | ||
) | ||
return reverse_full_transaction( | ||
transaction=transaction, | ||
idempotency_key=idempotency_key, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
Common exceptions related to Transactions. | ||
""" | ||
|
||
|
||
class TransactionException(Exception): | ||
""" | ||
Base exception class around transactions. | ||
""" | ||
|
||
|
||
class TransactionFulfillmentCancelationException(TransactionException): | ||
""" | ||
Raised when a Transaction cannot be unfulfilled (un-enrolled). | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.