pretix-eupago 2.0.2__tar.gz → 2.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. {pretix_eupago-2.0.2/pretix_eupago.egg-info → pretix_eupago-2.0.3}/PKG-INFO +1 -1
  2. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/__init__.py +1 -1
  3. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/payment.py +37 -14
  4. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3/pretix_eupago.egg-info}/PKG-INFO +1 -1
  5. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pyproject.toml +1 -1
  6. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/tests/test_payment_multibanco.py +14 -1
  7. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/LICENSE +0 -0
  8. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/MANIFEST.in +0 -0
  9. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/README.rst +0 -0
  10. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/apps.py +0 -0
  11. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/locale/pt_PT/LC_MESSAGES/django.mo +0 -0
  12. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/locale/pt_PT/LC_MESSAGES/django.po +0 -0
  13. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/signals.py +0 -0
  14. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/checkout_mbway.html +0 -0
  15. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/checkout_multibanco.html +0 -0
  16. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/control_mbway.html +0 -0
  17. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/control_multibanco.html +0 -0
  18. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/mail_multibanco.txt +0 -0
  19. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/orders.html +0 -0
  20. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/pending_mbway.html +0 -0
  21. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/pending_multibanco.html +0 -0
  22. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/templates/pretix_eupago/settings.html +0 -0
  23. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/urls.py +0 -0
  24. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago/views.py +0 -0
  25. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago.egg-info/SOURCES.txt +0 -0
  26. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago.egg-info/dependency_links.txt +0 -0
  27. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago.egg-info/entry_points.txt +0 -0
  28. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago.egg-info/requires.txt +0 -0
  29. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/pretix_eupago.egg-info/top_level.txt +0 -0
  30. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/setup.cfg +0 -0
  31. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/tests/test_orders_view.py +0 -0
  32. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/tests/test_payment_mbway.py +0 -0
  33. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/tests/test_settings.py +0 -0
  34. {pretix_eupago-2.0.2 → pretix_eupago-2.0.3}/tests/test_webhook.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pretix-eupago
3
- Version: 2.0.2
3
+ Version: 2.0.3
4
4
  Summary: Accept Multibanco and MB WAY payments via euPago in pretix
5
5
  Author: Afonso Santos
6
6
  License: Apache Software License
@@ -3,6 +3,6 @@ try:
3
3
  except ImportError:
4
4
  raise RuntimeError("Please, don't run pip install directly. Use pip install -e .")
5
5
 
6
- __version__ = "2.0.2"
6
+ __version__ = "2.0.3"
7
7
 
8
8
  from .apps import PluginApp # NOQA isort:skip
@@ -7,6 +7,7 @@ from datetime import date, timedelta
7
7
  import requests
8
8
  from django import forms
9
9
  from django.core.exceptions import ValidationError
10
+ from django.db import transaction as db_transaction
10
11
  from django.http import HttpRequest
11
12
  from django.template.loader import get_template
12
13
  from django.urls import reverse
@@ -14,12 +15,18 @@ from django.utils.html import conditional_escape, format_html
14
15
  from django.utils.safestring import mark_safe
15
16
  from django.utils.timezone import now
16
17
  from django.utils.translation import gettext_lazy as _
18
+ from django_scopes import scopes_disabled
17
19
  from pretix.base.models import OrderPayment
18
20
  from pretix.base.payment import BasePaymentProvider, PaymentException
19
21
  from pretix.multidomain.urlreverse import eventreverse_absolute
20
22
 
21
23
  logger = logging.getLogger(__name__)
22
24
 
25
+ # (connect, read) seconds. euPago's API occasionally takes ~15s to answer; the read timeout
26
+ # stays well under the web worker's own timeout (pretix's gunicorn, 30s by default — raise
27
+ # it to at least 90s with GUNICORN_CMD_ARGS="--timeout 90" when using this plugin).
28
+ API_TIMEOUT = (10, 45)
29
+
23
30
  MULTIBANCO_URL = {
24
31
  "production": "https://clientes.eupago.pt/clientes/rest_api/multibanco/create",
25
32
  "sandbox": "https://sandbox.eupago.pt/clientes/rest_api/multibanco/create",
@@ -188,19 +195,34 @@ class EupagoMultibanco(EupagoSettingsMixin, BasePaymentProvider):
188
195
  # request already created a reference at euPago. Creating another one would leave
189
196
  # the customer with two payable references (and two emails) while pretix only
190
197
  # knows the last, so an existing, still-valid reference is reused as is.
191
- existing = payment.info_data or {}
192
- if (
193
- payment.state == OrderPayment.PAYMENT_STATE_PENDING
194
- and existing.get("identifier") == identifier
195
- and existing.get("referencia")
196
- and existing.get("entidade")
197
- and existing.get("expiry", "") >= today.isoformat()
198
- ):
199
- logger.info(
200
- "euPago Multibanco: reusing reference for payment %s", payment.pk
201
- )
202
- return
198
+ #
199
+ # euPago's create call can take ~15s, long enough for the customer's browser to
200
+ # give up and the confirm step to be submitted again while the first request is
201
+ # still waiting for euPago. The row lock makes that second request wait for the
202
+ # first one, so it finds the finished reference instead of creating another.
203
+ with db_transaction.atomic():
204
+ with scopes_disabled():
205
+ OrderPayment.objects.select_for_update().filter(pk=payment.pk).first()
206
+ payment.refresh_from_db(fields=["info", "state"])
207
+ existing = payment.info_data or {}
208
+ if (
209
+ payment.state == OrderPayment.PAYMENT_STATE_PENDING
210
+ and existing.get("identifier") == identifier
211
+ and existing.get("referencia")
212
+ and existing.get("entidade")
213
+ and existing.get("expiry", "") >= today.isoformat()
214
+ ):
215
+ logger.info(
216
+ "euPago Multibanco: reusing reference for payment %s", payment.pk
217
+ )
218
+ return
219
+ self._create_reference(payment, identifier, expiry)
220
+
221
+ # Outside the transaction, so the mail task never runs before the reference is
222
+ # committed.
223
+ self._send_reference_email(payment)
203
224
 
225
+ def _create_reference(self, payment, identifier, expiry):
204
226
  payload = {
205
227
  "chave": self._api_key,
206
228
  "valor": float(payment.amount),
@@ -213,7 +235,7 @@ class EupagoMultibanco(EupagoSettingsMixin, BasePaymentProvider):
213
235
  resp = requests.post(
214
236
  MULTIBANCO_URL[self._env()],
215
237
  json=payload,
216
- timeout=30,
238
+ timeout=API_TIMEOUT,
217
239
  headers={"ApiKey": self._api_key},
218
240
  )
219
241
  resp.raise_for_status()
@@ -253,6 +275,7 @@ class EupagoMultibanco(EupagoSettingsMixin, BasePaymentProvider):
253
275
  payment.save(update_fields=["info", "state"])
254
276
  # Payment is pending — confirmed via webhook when customer pays.
255
277
 
278
+ def _send_reference_email(self, payment):
256
279
  # The "order placed" email is sent by pretix core before execute_payment runs
257
280
  # (it can't know the reference yet), so {payment_info} is empty there. Send the
258
281
  # reference separately once we actually have it.
@@ -406,7 +429,7 @@ class EupagoMBWAY(EupagoSettingsMixin, BasePaymentProvider):
406
429
  resp = requests.post(
407
430
  MBWAY_URL[self._env()],
408
431
  json=payload,
409
- timeout=30,
432
+ timeout=API_TIMEOUT,
410
433
  headers={"Authorization": f"ApiKey {self._api_key}"},
411
434
  )
412
435
  data = resp.json()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pretix-eupago
3
- Version: 2.0.2
3
+ Version: 2.0.3
4
4
  Summary: Accept Multibanco and MB WAY payments via euPago in pretix
5
5
  Author: Afonso Santos
6
6
  License: Apache Software License
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pretix-eupago"
7
- version = "2.0.2"
7
+ version = "2.0.3"
8
8
  description = "Accept Multibanco and MB WAY payments via euPago in pretix"
9
9
  readme = "README.rst"
10
10
  requires-python = ">=3.11"
@@ -10,7 +10,7 @@ from django_scopes import scopes_disabled
10
10
  from pretix.base.models import OrderPayment
11
11
  from pretix.base.payment import PaymentException
12
12
 
13
- from pretix_eupago.payment import MULTIBANCO_URL, EupagoMultibanco
13
+ from pretix_eupago.payment import API_TIMEOUT, MULTIBANCO_URL, EupagoMultibanco
14
14
 
15
15
  from .conftest import make_payment
16
16
 
@@ -263,6 +263,19 @@ def test_execute_payment_twice_reuses_reference(provider, order):
263
263
  assert len(mail.outbox) == 1
264
264
 
265
265
 
266
+ @pytest.mark.django_db
267
+ @responses.activate
268
+ def test_execute_payment_uses_generous_api_timeout(provider, order):
269
+ # euPago's create call sometimes takes ~15s; 30s total used to be too tight a margin.
270
+ payment = make_payment(order, provider.identifier)
271
+ responses.add(responses.POST, MULTIBANCO_URL["sandbox"], json=SUCCESS, status=200)
272
+
273
+ provider.execute_payment(RequestFactory().post("/"), payment)
274
+
275
+ assert responses.calls[0].request.req_kwargs["timeout"] == API_TIMEOUT
276
+ assert API_TIMEOUT[1] > 30
277
+
278
+
266
279
  @pytest.mark.django_db
267
280
  @responses.activate
268
281
  def test_execute_payment_creates_new_reference_when_previous_expired(provider, order):
File without changes
File without changes
File without changes
File without changes