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.
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(...)
# payment.id now contains the primary key for this new entry in the Payment model
redirect_url = "http://localhost:8000/callback/%s/" % payment.id
You can then use this when building the data dict:
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