Django: how to save data to database in django when a condition is met?

i am writing a logic to allow user pay for items using thier debit card, the logic is working fine. Now i want to save some data to the database when the status=successful.

When a user purchase a course i want to add the course and the user who purchased the course to a model that i have created called UserCourse.

I have tried adding slug to the process_payment view that i created but it seems not to be working. I also have another view called payment_response that checks if a payment was success or not.

How do i add the purchased course and the user that purchase the course in the db?

@require_http_methods(['GET', 'POST'])
def payment_response(request):
    status=request.GET.get('status', None)
    tx_ref=request.GET.get('tx_ref', None)
    if status == "successful":
        # return HttpResponse('Payment Successful')
        return render(request, "payment/payment-success.html")
    if status == "cancelled":
        return render(request, "payment/payment-failed.html")
    	# return HttpResponse('Payment Cancelled')

When payment_response is called, what information do you have available to you to associate that request with the request initiated in process_payment?

In the general case, you want to save the data being used to submit the payment in process_payment (aside from the credit card data you’re not allowed to retain ) in a table with some key that will be returned to you in payment_response. You’ll then use that key to retrieve the data saved in process_payment to create whatever objects you want to create.

More frequently, what I’ve seen is that the data is all saved in the database when the request is submitted, with some kind of status indicating that the payment is pending. Then, when the response is received, the status is updated to show that the payment was successful.
This makes it easier to track cases where you never receive acknowledgement from the payment processor.

1 Like

I thought about doing this, but I don’t have an idea on how to go about writing the logic

The only information that connects the process_payment and payment_response is the the car back url "redirect_url":"http://localhost:8000/callback that’s in the process_payment

i have just implemented this now, thanks for the idea

So, if you’ve got two people who are each submitting payments at about the same time, how do you tell which response you’re processing?

1 Like

:thinking: that’s true, there is no way for me to tell which response I’m processing. I thought of maybe using payment id to distinguish the payment process, but I don’t know how to go about that.?

Based on what you’re showing here, along with the idea of saving the data to the database before issuing the request, it appears to me that you could pass some kind of “payment id” as a url parameter to be supplied in the response.

For example, in your process_payment view where the payment is being prepared:

payment = Payment(...)
# now contains the primary key for this new entry in the Payment model
redirect_url = "http://localhost:8000/callback/%s/" %

You can then use this when building the data dict:

"redirect_url": redirect_url,

Your url for the callback would then be:
path('callback/<int:payment_id>/', payment_response, name='payment_response')

And the definition for payment_response becomes:
def payment_response(request, payment_id):
allowing you to reference the payment data related to this response

1 Like

That was it, it;s Solved. Thanks a ton for your time and patience.