Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

YC-1188 - Update unpaid PF transactions status in cron task, refactor c… #1018

Closed
wants to merge 1 commit into from

Conversation

danickfort
Copy link
Collaborator

…onfirm transaction code

Copy link
Collaborator Author

@danickfort danickfort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monodo @rbovard Dispo pour review! J'ai mis quelques commentaires pour expliquer certains choix que j'ai fait pour cette feature. A noter que j'ai déplacé quelques bouts de codes dans les modèles pour rendre le code plus propre et moins couplé aux vues, donc il y a moins de changement qu'il n'y parait.

@@ -1308,6 +1308,39 @@ def get_last_prolongation_transaction(self):
.first()
)

def set_prolongation_requested_and_notify(self, prolongation_date):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai profité de l'occasion pour déplacer dans le modèle cette fonction qui sert à setter la date de prolongation . Aussi, j'ai enlevé l'argument request, qui était utilisé auparavant pour construire l'URL absolue de la soumission, parce qu'il est possible qu'une prolongation soit trigger depuis le cronjob, où on ne peut pas avoir le contexte d'une requête HTTP.

@@ -74,6 +74,8 @@ class Transaction(models.Model):
default=TYPE_SUBMISSION,
)

extra_data = models.JSONField(_("Données supplémentaires"), default=dict)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilisé pour stocker la date de prolongation, quand il y a un paiement requis. Elle était avant renvoyée par paramètre dans l'URL au retour de PostFinance, mais il est nécéssaire de la stocker avant de déclencher le paiement, parce qu'il est possible que le paiement soit validé par le cronjob (donc pas de retour depuis PF)

@@ -2639,22 +2605,36 @@ def get(self, request, *args, **kwargs):
return redirect(reverse_lazy("submissions:submissions_list"))


@method_decorator(login_required, name="dispatch")
class ConfirmTransactionCallback(View):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai enlevé la nécéssité d'être authentifié pour accéder aux views de confirmation de paiement. Cela permet de valider le paiement sur un autre device (p. ex sur mobile) que celui qui a été utilisé pour remplir la demande. Vu que l'on vérifie que le paiement a été effectué sur PF avant de le valider (ligne 2636), cela ne pose pas de problème de sécurité

or not transaction.status == transaction.STATUS_UNPAID
):
raise PermissionDenied
if transaction.has_been_confirmed:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Petites améliorations dans le cas où la transaction est déjà confirmée, ou déjà échouée.

Copy link
Collaborator

@rbovard rbovard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the work @danickfort

Is it possible to document somewhere how to setup this cron job?

I also noticed that only the transaction is updated but not the related submission (it should be not be anymore in STATUS_DRAFT). And the confirmation and notification emails are not sent. I think you "forgot" to continue the original workflow after the transaction is confirmed.

I've also made some remarks directly in the code.

Don't hesitate to contact me to see it in live.


class Command(BaseCommand):
help = gettext(
"Update the status of transactions that are pending and not older than %s hours."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Update the status of transactions that are pending and not older than %s hours."
"Update the status of transactions that are pending and not older than %s minutes."

@@ -771,6 +771,9 @@ def show_toolbar(request):
os.getenv("PAYMENT_PROCESSING_TEST_ENVIRONMENT", "true").lower() == "true" or DEBUG
)
PAYMENT_CURRENCY = os.getenv("PAYMENT_CURRENCY")
PAYMENT_PENDING_TRANSACTION_MAX_AGE_MINS = int(
os.getenv("PAYMENT_PENDING_TRANSACTION_MAX_AGE_MINS", 30)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the same value as in env.example?

# Get all unpaid transactions that are not older than the specified time
transactions_to_update = PostFinanceTransaction.objects.filter(
status=Transaction.STATUS_UNPAID,
authorization_timeout_on__gte=timezone.now()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an older issue here, for a reason I don't get, the value of this attribute in DB is truncated to the day, the time is not stored (2024-06-12 --> 2024-06-12 00:00:00.000 +0200). So with the default value of 30 minutes, it will never work.

If we use the value as provided in env.example, it works because it's 24 hours and not 30 minutes.

But it's not clean, is there a way to store the timeout value with the hours?

@OdyX
Copy link
Collaborator

OdyX commented Jun 24, 2024

@OdyX OdyX closed this Jun 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants