Skip to content

Commit

Permalink
Merge pull request #4026 from open-formulieren/feature/3688-payment-u…
Browse files Browse the repository at this point in the history
…pdate

Handle payment status updates
  • Loading branch information
sergei-maertens authored Mar 20, 2024
2 parents e297079 + d8d81b1 commit f889bba
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/openforms/registrations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class BasePlugin(ABC, AbstractBasePlugin):
def register_submission(self, submission: Submission, options: dict) -> dict | None:
raise NotImplementedError()

def update_payment_status(self, submission: Submission, options: dict):
def update_payment_status(
self, submission: Submission, options: dict
) -> dict | None:
raise NotImplementedError()

def pre_register_submission(
Expand Down
17 changes: 7 additions & 10 deletions src/openforms/registrations/contrib/objects_api/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from typing_extensions import override

from openforms.registrations.utils import execute_unless_result_exists
from openforms.utils.date import get_today
from openforms.variables.service import get_static_variables

from ...base import BasePlugin
Expand Down Expand Up @@ -108,7 +107,7 @@ def get_custom_templatetags_libraries(self) -> list[str]:
@override
def update_payment_status(
self, submission: Submission, options: RegistrationOptions
) -> None:
) -> dict[str, Any] | None:
config = ObjectsAPIConfig.get_solo()
assert isinstance(config, ObjectsAPIConfig)
config.apply_defaults_to(options)
Expand All @@ -121,21 +120,19 @@ def update_payment_status(
if updated_object_data is None:
return

updated_object_data = {
"record": {
"data": updated_object_data,
"startAt": get_today(),
},
}

object_url = submission.registration_result["url"]
with get_objects_client() as objects_client:
response = objects_client.patch(
operation = (
objects_client.patch if options["version"] == 1 else objects_client.put
)

response = operation(
url=object_url,
json=updated_object_data,
headers={"Content-Crs": "EPSG:4326"},
)
response.raise_for_status()
return response.json()

@override
def get_variables(self) -> list[FormVariable]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def get_object_data(
def get_update_payment_status_data(
self, submission: Submission, options: OptionsT
) -> dict[str, Any] | None:
"""Get the object data payload to be sent (either as a PATCH or PUT request) to the Objects API."""
pass


Expand Down Expand Up @@ -320,7 +321,16 @@ def get_update_payment_status_data(
"payment": self.get_payment_context_data(submission),
}

return render_to_json(options["payment_status_update_json"], context)
record_data = cast(
dict[str, Any],
render_to_json(options["payment_status_update_json"], context),
)

return prepare_data_for_registration(
record_data=record_data,
objecttype=options["objecttype"],
objecttype_version=options["objecttype_version"],
)


class ObjectsAPIV2Handler(ObjectsAPIRegistrationHandler[RegistrationOptionsV2]):
Expand Down Expand Up @@ -381,9 +391,11 @@ def get_object_data(
@override
def get_update_payment_status_data(
self, submission: Submission, options: RegistrationOptionsV2
) -> None:
# TODO
return None
) -> dict[str, Any]:
# In V2, a PUT request is made, so we essentially return the same
# payload from the initial registration. Payment related variables
# will have their value updated.
return self.get_object_data(submission, options)


HANDLER_MAPPING: dict[ConfigVersion, ObjectsAPIRegistrationHandler[Any]] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
interactions:
- request:
body: '{"type": "http://objecttypes-web:8000/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48",
"record": {"typeVersion": 3, "data": {"age": 20, "name": {"last.name": "My last
name"}, "submission_pdf_url": "http://example.com", "submission_csv_url": "http://example.com",
"submission_payment_completed": false, "submission_payment_amount": "0", "submission_payment_public_ids":
[], "submission_date": "2020-02-02T00:00:00+00:00"}, "startAt": "2020-02-02"}}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Token 7657474c3d75f56ae0abd0d1bf7994b09964dca9
Connection:
- keep-alive
Content-Crs:
- EPSG:4326
Content-Length:
- '456'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: POST
uri: http://localhost:8002/api/v2/objects
response:
body:
string: '{"url":"http://localhost:8002/api/v2/objects/2c287cd4-7737-473d-84de-8b0a018a46bb","uuid":"2c287cd4-7737-473d-84de-8b0a018a46bb","type":"http://objecttypes-web:8000/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48","record":{"index":1,"typeVersion":3,"data":{"age":20,"name":{"last.name":"My
last name"},"submission_pdf_url":"http://example.com","submission_csv_url":"http://example.com","submission_payment_completed":false,"submission_payment_amount":"0","submission_payment_public_ids":[],"submission_date":"2020-02-02T00:00:00+00:00"},"geometry":null,"startAt":"2020-02-02","endAt":null,"registrationAt":"2024-03-19","correctionFor":null,"correctedBy":null}}'
headers:
Allow:
- GET, POST, HEAD, OPTIONS
Content-Crs:
- EPSG:4326
Content-Length:
- '669'
Content-Type:
- application/json
Cross-Origin-Opener-Policy:
- same-origin
Location:
- http://localhost:8002/api/v2/objects/2c287cd4-7737-473d-84de-8b0a018a46bb
Referrer-Policy:
- same-origin
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
status:
code: 201
message: Created
- request:
body: '{"type": "http://objecttypes-web:8000/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48",
"record": {"typeVersion": 3, "data": {"age": 20, "name": {"last.name": "My last
name"}, "submission_date": "2020-02-02T00:00:00+00:00", "submission_pdf_url":
"", "submission_csv_url": "", "submission_payment_completed": true, "submission_payment_amount":
"10.01", "submission_payment_public_ids": ["TEST-123"]}, "startAt": "2020-02-02",
"geometry": {"type": "Point", "coordinates": [52.36673378967122, 4.893164274470299]}}}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Token 7657474c3d75f56ae0abd0d1bf7994b09964dca9
Connection:
- keep-alive
Content-Crs:
- EPSG:4326
Content-Length:
- '519'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: PUT
uri: http://localhost:8002/api/v2/objects/2c287cd4-7737-473d-84de-8b0a018a46bb
response:
body:
string: '{"url":"http://localhost:8002/api/v2/objects/2c287cd4-7737-473d-84de-8b0a018a46bb","uuid":"2c287cd4-7737-473d-84de-8b0a018a46bb","type":"http://objecttypes-web:8000/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48","record":{"index":2,"typeVersion":3,"data":{"age":20,"name":{"last.name":"My
last name"},"submission_date":"2020-02-02T00:00:00+00:00","submission_pdf_url":"","submission_csv_url":"","submission_payment_completed":true,"submission_payment_amount":"10.01","submission_payment_public_ids":["TEST-123"]},"geometry":{"type":"Point","coordinates":[52.36673378967122,4.893164274470299]},"startAt":"2020-02-02","endAt":null,"registrationAt":"2024-03-19","correctionFor":null,"correctedBy":null}}'
headers:
Allow:
- GET, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Crs:
- EPSG:4326
Content-Length:
- '710'
Content-Type:
- application/json
Cross-Origin-Opener-Policy:
- same-origin
Referrer-Policy:
- same-origin
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


@requests_mock.Mocker()
class ObjectsAPIPaymentStatusUpdateTests(TestCase):
class ObjectsAPIPaymentStatusUpdateV1Tests(TestCase):
def test_update_payment_status(self, m):
submission = SubmissionFactory.from_components(
[
Expand Down Expand Up @@ -59,6 +59,7 @@ def test_update_payment_status(self, m):

m.patch(
"https://objecten.nl/api/v1/objects/111-222-333",
json={}, # Unused in our case, but required as .json() is called on the response
status_code=200,
)

Expand Down Expand Up @@ -117,11 +118,11 @@ def test_template_overwritten_through_options(self, m):

m.patch(
"https://objecten.nl/api/v1/objects/111-222-333",
json={}, # Unused in our case, but required as .json() is called on the response
status_code=200,
)

plugin = ObjectsAPIRegistration(PLUGIN_IDENTIFIER)

with freeze_time("2020-02-02"):
with patch(
"openforms.registrations.contrib.objects_api.models.ObjectsAPIConfig.get_solo",
Expand Down Expand Up @@ -190,6 +191,7 @@ def test_no_template_specified(self, m):

m.patch(
"https://objecten.nl/api/v1/objects/111-222-333",
json={}, # Unused in our case, but required as .json() is called on the response
status_code=200,
)

Expand Down
Loading

0 comments on commit f889bba

Please sign in to comment.