You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@receiver(pre_save, sender=Transaction)defupdate_date_paid(sender, instance, **kwargs):
""" Signal handler to update transaction status and handle related logic. This function ensures that a transaction is not marked as both paid and cancelled, and updates the date_paid field based on the transaction status. """# Ensure the transaction is not both paid and cancelledifinstance.is_paidandinstance.is_cancelled:
raiseValidationError(_("A transaction cannot be both paid and cancelled."))
# Update date_paid based on the payment statusifinstance.status==PaymentStatus.CONFIRMEDandnotinstance.date_paid:
# Mark transaction as paid by setting the date_paidinstance.is_paid=Trueinstance.date_paid=timezone.now()
instance.is_cancelled=Falseelifinstance.status!=PaymentStatus.CONFIRMEDandinstance.date_paid:
instance.is_paid=Falseinstance.date_paid=None# Handle cancellation logicifinstance.is_cancelled:
instance.is_paid=Falseinstance.date_paid=None@receiver(post_save, sender=Transaction)defchange_user_plan_on_transaction_paid(sender, instance=None, created=False, **kwargs):
# A lot of others stuffs making changes in another models, not important now
The signals are working correctly:
When I update a field in my Transaction (payment) model, both signals are triggered.
When processing a payment via Stripe, the signals execute as expected.
However, when processing a payment via PayPal, the PayPal response is received, and both signals are triggered (I can confirm the changes to instance.is_paid, instance.date_paid, and instance.is_cancelled in debug mode). Yet, when checking the database after, the field values are incorrect:
I'm quite certain I'm missing something on my end because otherwise, someone else would have reported this issue already.
Nevertheless, after reviewing the PayPal process_data I found that adding a save() call at the end resolves the issue and ensures the signals behave as expected:
And if you notice on the other providers, it's customary to save the payment object before sending the client (as is the person who clicks the pay button) outside the django application.
Hello,
Today, I'm troubleshooting an issue with the PayPal variant. My setup:
myapp/models.py
myapp/signals.py
The signals are working correctly:
Transaction
(payment) model, both signals are triggered.However, when processing a payment via PayPal, the PayPal response is received, and both signals are triggered (I can confirm the changes to instance.is_paid, instance.date_paid, and instance.is_cancelled in debug mode). Yet, when checking the database after, the field values are incorrect:
Worklow:
process_data
in django-paymentsprocess_data
updates the status to CONFIRMED (payment.change_status(PaymentStatus.CONFIRMED)
)Transaction
model receives thepre_save
signalpre_save
updates the fields: is_paid=True, date_paid=Now(), is_cancelled=Falseprocess_data
redirects to success_urlI'm quite certain I'm missing something on my end because otherwise, someone else would have reported this issue already.
Nevertheless, after reviewing the PayPal
process_data
I found that adding a save() call at the end resolves the issue and ensures the signals behave as expected:It is a bug ?? If not, what I'm doing wrong ?
The text was updated successfully, but these errors were encountered: