The response body on the call back is usually:
{
"Body":
{
"stkCallback":
{
"MerchantRequestID": "21605-295434-4",
"CheckoutRequestID": "ws_CO_04112017184930742",
"ResultCode": 0,
"ResultDesc": "The service request is processed successfully.",
"CallbackMetadata":
{
"Item":
[
{
"Name": "Amount",
"Value": 500
},
{
"Name": "MpesaReceiptNumber",
"Value": "WHAkLK451H35OP"
},
{
"Name": "Balance"
},
{
"Name": "TransactionDate",
"Value": 20171104184944
},
{
"Name": "PhoneNumber",
"Value": 254710549039
}
]
}
}
}
}
This is my callback URL, which works fine and saves the data in a model. How can you associate a user with a payment once the response is received and saved in a general model not associated with any user?
@csrf_exempt
def callbackurl(request):
if request.method == 'POST':
m_body =request.body.decode('utf-8')
mpesa_payment = json.loads(m_body)
payment = StatusPayment(
phoneno=mpesa_payment['Body']['stkCallback']['CallbackMetadata']['Item'][4]['Value'],
transcode=mpesa_payment['Body']['stkCallback']['CallbackMetadata']['Item'][1]['Value'],
)
payment.save()
context = {
"ResultCode": 0,
"ResultDesc": "Accepted"
}
return JsonResponse(dict(context))`