Skip to content

Commit

Permalink
Merge pull request #4147 from open-formulieren/fix/4145-payment-statu…
Browse files Browse the repository at this point in the history
…s-stuf-ZDS

[#4145] Fix payment status for StUF ZDS backend
  • Loading branch information
sergei-maertens authored Apr 10, 2024
2 parents f96c091 + 61cb926 commit f77c242
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 10 deletions.
22 changes: 18 additions & 4 deletions src/openforms/registrations/contrib/stuf_zds/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
)
from openforms.submissions.models import Submission, SubmissionReport
from openforms.utils.mixins import JsonSchemaSerializerMixin
from stuf.stuf_zds.client import NoServiceConfigured, ZaakOptions, get_client
from stuf.stuf_zds.client import (
NoServiceConfigured,
PaymentStatus,
ZaakOptions,
get_client,
)
from stuf.stuf_zds.constants import VertrouwelijkheidsAanduidingen
from stuf.stuf_zds.models import StufZDSConfig

Expand Down Expand Up @@ -266,10 +271,19 @@ def items(self):
yield ("language_code", submission.language_code)
yield from extra_data.items()

payment_status = (
PaymentStatus.NVT
if not submission.payment_required
else (
PaymentStatus.FULL
if submission.payment_user_has_paid
else PaymentStatus.NOT_YET
)
)
zaak_data.update({"betalings_indicatie": payment_status})

execute_unless_result_exists(
lambda: client.create_zaak(
zaak_id, zaak_data, LangInjection(), submission.payment_required
),
lambda: client.create_zaak(zaak_id, zaak_data, LangInjection()),
submission,
"intermediate.zaak_created",
default=False,
Expand Down
198 changes: 197 additions & 1 deletion src/openforms/registrations/contrib/stuf_zds/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@
from decimal import Decimal
from unittest.mock import patch

from django.test import override_settings, tag

import requests_mock
from freezegun import freeze_time
from privates.test import temp_private_root
from requests import ConnectTimeout

from openforms.authentication.tests.factories import RegistratorInfoFactory
from openforms.config.models import GlobalConfiguration
from openforms.forms.tests.factories import FormVariableFactory
from openforms.logging.models import TimelineLogProxy
from openforms.payments.constants import PaymentStatus
from openforms.submissions.constants import PostSubmissionEvents
from openforms.submissions.tasks import pre_registration
from openforms.submissions.tasks import on_post_submission_event, pre_registration
from openforms.submissions.tests.factories import (
SubmissionFactory,
SubmissionFileAttachmentFactory,
SubmissionValueVariableFactory,
)
from openforms.variables.constants import FormVariableDataTypes, FormVariableSources
from stuf.stuf_zds.client import PaymentStatus as StuFPaymentStatus
from stuf.stuf_zds.models import StufZDSConfig
from stuf.stuf_zds.tests import StUFZDSTestBase
from stuf.stuf_zds.tests.utils import load_mock, match_text, xml_from_request_history
Expand Down Expand Up @@ -2851,3 +2856,194 @@ def test_plugin_with_extra_unmapped_number_data(self, m, mock_task):
"//stuf:extraElementen/stuf:extraElement[@naam='extra_number']",
"2023",
)


@freeze_time("2020-12-22")
@temp_private_root()
@requests_mock.Mocker()
@override_settings(CELERY_TASK_ALWAYS_EAGER=True)
class StufZDSPluginPaymentTests(StUFZDSTestBase):
def setUp(self):
super().setUp()

self.service = StufServiceFactory.create()
config = StufZDSConfig.get_solo()
config.service = self.service
config.save()

@tag("gh-4145")
def test_payment_status_is_correct_on_payment_update(self, m):
"""
#4145:
Testing that if 'wait_for_payment_to_register' is True, when the payment is completed and the submission is
registered, the right payment status is sent to the StUF-ZDS backend.
"""
submission = SubmissionFactory.from_components(
components_list=[
{
"key": "email",
"type": "email",
"label": "Email",
},
],
submitted_data={"email": "[email protected]"},
with_public_registration_reference=True,
confirmation_email_sent=True,
form__registration_backend="stuf-zds-create-zaak",
form__registration_backend_options={
"zds_zaaktype_code": "zt-code",
"zds_documenttype_omschrijving_inzending": "aaabbc",
},
form__name="Pretty Form",
with_completed_payment=True,
registration_result={"intermediate": {"zaaknummer": "ZAAK-TRALALA"}},
)

m.post(
self.service.soap_service.url,
content=load_mock("creeerZaak.xml"),
additional_matcher=match_text("zakLk01"),
)
m.post(
self.service.soap_service.url,
content=load_mock(
"genereerDocumentIdentificatie.xml",
{"document_identificatie": "bar-document"},
),
additional_matcher=match_text("genereerDocumentIdentificatie_Di02"),
)
m.post(
self.service.soap_service.url,
content=load_mock("voegZaakdocumentToe.xml"),
additional_matcher=match_text("edcLk01"),
)

with patch(
"openforms.registrations.tasks.GlobalConfiguration.get_solo",
return_value=GlobalConfiguration(wait_for_payment_to_register=True),
):
on_post_submission_event(
submission.id, PostSubmissionEvents.on_payment_complete
)

submission.refresh_from_db()

self.assertTrue(
submission.payments.filter(status=PaymentStatus.registered).exists()
)

xml_doc = xml_from_request_history(m, 0)

self.assertXPathEquals(
xml_doc,
"//zkn:betalingsIndicatie",
StuFPaymentStatus.FULL,
)

def test_payment_status_is_correct_if_not_waiting_for_payment(self, m):
submission = SubmissionFactory.from_components(
components_list=[
{
"key": "email",
"type": "email",
"label": "Email",
},
],
submitted_data={"email": "[email protected]"},
with_public_registration_reference=True,
form__registration_backend="stuf-zds-create-zaak",
form__registration_backend_options={
"zds_zaaktype_code": "zt-code",
"zds_documenttype_omschrijving_inzending": "aaabbc",
},
form__name="Pretty Form",
form__product__price=Decimal("10.00"),
form__payment_backend="demo",
registration_result={},
)

m.post(
self.service.soap_service.url,
content=load_mock("creeerZaak.xml"),
additional_matcher=match_text("zakLk01"),
)
m.post(
self.service.soap_service.url,
content=load_mock(
"genereerDocumentIdentificatie.xml",
{"document_identificatie": "bar-document"},
),
additional_matcher=match_text("genereerDocumentIdentificatie_Di02"),
)
m.post(
self.service.soap_service.url,
content=load_mock("voegZaakdocumentToe.xml"),
additional_matcher=match_text("edcLk01"),
)

with patch(
"openforms.registrations.tasks.GlobalConfiguration.get_solo",
return_value=GlobalConfiguration(wait_for_payment_to_register=False),
):
on_post_submission_event(submission.id, PostSubmissionEvents.on_completion)

submission.refresh_from_db()

xml_doc = xml_from_request_history(m, 0)

self.assertXPathEquals(
xml_doc,
"//zkn:betalingsIndicatie",
StuFPaymentStatus.NOT_YET,
)

def test_payment_status_is_correct_when_no_payment_required(self, m):
submission = SubmissionFactory.from_components(
components_list=[
{
"key": "email",
"type": "email",
"label": "Email",
},
],
submitted_data={"email": "[email protected]"},
with_public_registration_reference=True,
form__registration_backend="stuf-zds-create-zaak",
form__registration_backend_options={
"zds_zaaktype_code": "zt-code",
"zds_documenttype_omschrijving_inzending": "aaabbc",
},
form__name="Pretty Form",
registration_result={},
)

m.post(
self.service.soap_service.url,
content=load_mock("creeerZaak.xml"),
additional_matcher=match_text("zakLk01"),
)
m.post(
self.service.soap_service.url,
content=load_mock(
"genereerDocumentIdentificatie.xml",
{"document_identificatie": "bar-document"},
),
additional_matcher=match_text("genereerDocumentIdentificatie_Di02"),
)
m.post(
self.service.soap_service.url,
content=load_mock("voegZaakdocumentToe.xml"),
additional_matcher=match_text("edcLk01"),
)

on_post_submission_event(submission.id, PostSubmissionEvents.on_completion)

submission.refresh_from_db()

xml_doc = xml_from_request_history(m, 0)

self.assertXPathEquals(
xml_doc,
"//zkn:betalingsIndicatie",
StuFPaymentStatus.NVT,
)
4 changes: 0 additions & 4 deletions src/stuf/stuf_zds/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ def create_zaak(
zaak_identificatie: str,
zaak_data: dict,
extra_data,
payment_required=False,
) -> None:
now = timezone.now()
context = {
Expand All @@ -240,9 +239,6 @@ def create_zaak(
"zaak_omschrijving": self.zds_options["omschrijving"],
"zaak_identificatie": zaak_identificatie,
"extra": extra_data,
"betalings_indicatie": (
PaymentStatus.NOT_YET if payment_required else PaymentStatus.NVT
),
"global_config": GlobalConfiguration.get_solo(),
**zaak_data,
}
Expand Down
6 changes: 5 additions & 1 deletion src/stuf/stuf_zds/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ def test_create_zaak(self, m):
additional_matcher=match_text("zakLk01"),
)

client.create_zaak("foo", {"bsn": "111222333"}, {}, payment_required=True)
client.create_zaak(
"foo",
{"bsn": "111222333", "betalings_indicatie": PaymentStatus.NOT_YET},
{},
)

request = m.request_history[0]
self.assertEqual(
Expand Down

0 comments on commit f77c242

Please sign in to comment.