diff --git a/src/hope_payment_gateway/apps/fsp/western_union/handlers.py b/src/hope_payment_gateway/apps/fsp/western_union/handlers.py index 52b2277..d26f227 100644 --- a/src/hope_payment_gateway/apps/fsp/western_union/handlers.py +++ b/src/hope_payment_gateway/apps/fsp/western_union/handlers.py @@ -1,18 +1,8 @@ -from hope_payment_gateway.apps.fsp.western_union.endpoints.send_money import send_money -from hope_payment_gateway.apps.gateway.models import ( - FinancialServiceProvider, - FinancialServiceProviderConfig, - PaymentRecord, -) +from hope_payment_gateway.apps.gateway.models import FinancialServiceProvider, FinancialServiceProviderConfig from hope_payment_gateway.apps.gateway.registry import FSPProcessor class WesternUnionHandler(FSPProcessor): - def notify(self, records): - to_process = list(PaymentRecord.objects.filter(id__in=records).values_list("id", flat=True)) - records.update(marked_for_payment=True) - for record in PaymentRecord.objects.filter(id__in=to_process): - send_money(record.get_payload()) def get_configuration(self, config_key, delivery_mechanism): wu = FinancialServiceProvider.objects.get(vision_vendor_number="1900723202") diff --git a/src/hope_payment_gateway/apps/fsp/western_union/tasks.py b/src/hope_payment_gateway/apps/fsp/western_union/tasks.py index a9cd7d7..962c9b6 100644 --- a/src/hope_payment_gateway/apps/fsp/western_union/tasks.py +++ b/src/hope_payment_gateway/apps/fsp/western_union/tasks.py @@ -1,4 +1,5 @@ import logging +from typing import List from constance import config @@ -6,6 +7,7 @@ das_countries_currencies, das_delivery_option_template, ) +from hope_payment_gateway.apps.fsp.western_union.endpoints.send_money import send_money from hope_payment_gateway.apps.fsp.western_union.models import Corridor from hope_payment_gateway.apps.gateway.models import ( FinancialServiceProvider, @@ -19,26 +21,33 @@ @app.task def western_union_send_task(vision_vendor_number="1900723202", tag=None, threshold=10000): - fsps = FinancialServiceProvider.objects.filter(vision_vendor_number=vision_vendor_number) - threshold = threshold or config.DEFAULT_THREASHOLD + fsp = FinancialServiceProvider.objects.get(vision_vendor_number=vision_vendor_number) + threshold = threshold or config.WESTERN_UNION_THREASHOLD records_count = 0 - for fsp in fsps: - qs = PaymentInstruction.objects.filter(status=PaymentInstructionState.READY, fsp=fsp) - if tag: - qs = qs.filter(tag=tag) - - for pi in qs: - records = pi.paymentrecord_set.filter(status=PaymentRecordState.PENDING, marked_for_payment=False) - records_count += records.count() - if records_count > threshold: - break - - logging.info(f"Sending {records_count} records {pi} to Western Union") - fsp.strategy.notify(records) - pi.status = PaymentInstructionState.PROCESSED - pi.save() + qs = PaymentInstruction.objects.filter(status=PaymentInstructionState.READY, fsp=fsp) + if tag: + qs = qs.filter(tag=tag) + + for pi in qs: + records = pi.paymentrecord_set.filter(status=PaymentRecordState.PENDING, marked_for_payment=False) + records_count += records.count() + if records_count > threshold: + break + + logging.info(f"Sending {records_count} records {pi} to Western Union") + records_ids = list(records.values_list("id", flat=True)) + western_union_notify.delay(records_ids) + pi.status = PaymentInstructionState.PROCESSED + pi.save() + + +@app.task +def western_union_notify(to_process_ids: List[PaymentRecord]) -> None: + PaymentRecord.objects.filter(id__in=to_process_ids).update(marked_for_payment=True) + for record in PaymentRecord.objects.filter(id__in=to_process_ids): + send_money(record.get_payload()) @app.task diff --git a/src/hope_payment_gateway/config/fragments/constance.py b/src/hope_payment_gateway/config/fragments/constance.py index 3597bf0..3b13fab 100644 --- a/src/hope_payment_gateway/config/fragments/constance.py +++ b/src/hope_payment_gateway/config/fragments/constance.py @@ -1,7 +1,7 @@ from hope_payment_gateway.config.settings import CACHE_URL CONSTANCE_CONFIG = { - "DEFAULT_THREASHOLD": ( + "WESTERN_UNION_THREASHOLD": ( 10000, "Hourly threshold of calls to be made to Western Union API", int, diff --git a/tests/western_union/test_tasks.py b/tests/western_union/test_tasks.py index 4365024..d06d1d8 100644 --- a/tests/western_union/test_tasks.py +++ b/tests/western_union/test_tasks.py @@ -24,7 +24,8 @@ ], ) @pytest.mark.django_db -@patch("hope_payment_gateway.apps.fsp.western_union.handlers.send_money") +# @patch("hope_payment_gateway.apps.fsp.western_union.handlers.send_money") +@patch("hope_payment_gateway.apps.fsp.western_union.tasks.send_money") def test_send_money_task(mock_class, wu, rec_a, rec_b, total): instr_a = PaymentInstructionFactory(status=PaymentInstructionState.READY, fsp=wu, tag="tag") instr_b = PaymentInstructionFactory(status=PaymentInstructionState.READY, fsp=wu, tag="tag") @@ -41,5 +42,5 @@ def test_send_money_task(mock_class, wu, rec_a, rec_b, total): 5, parent__status=PaymentRecordState.PENDING, status=PaymentRecordState.PENDING, marked_for_payment=True ) - western_union_send_task(vision_vendor_number="1900723202", tag="tag", threshold=10) + western_union_send_task(tag="tag", threshold=10) assert len(mock_class.mock_calls) == total