Reputation: 143
I am experiencing an error in Django that I can't manage to solve. After receiving a post to my endpoint I am trying to compare if the result code is equal to 0 if it's equal to 0 then I redirect to the relevant view.
views.py
def home(request):
if request.method == 'POST':
messages.add_message(request, messages.SUCCESS, 'Transaction Intited Successfully. Enter PIN on your phone')
phone_no = request.POST['client_phone'][1:]
base_url = settings.BASE_URL
lipa_time = datetime.now().strftime('%Y%m%d%H%M%S')
Business_short_code = settings.BUSINESS_SHORTCODE
passkey = settings.PASSKEY
data_to_encode = Business_short_code + passkey + lipa_time
online_password = base64.b64encode(data_to_encode.encode())
decode_password = online_password.decode('utf-8')
lipa = Lipa()
access_token = lipa.get_token()
print(access_token)
api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
headers = {"Authorization": "Bearer %s" % access_token}
request = {
"BusinessShortCode": Business_short_code,
"Password": decode_password,
"Timestamp": lipa_time,
"TransactionType": "CustomerPayBillOnline",
"Amount": "1",
"PartyA": "254"+phone_no,
"PartyB": Business_short_code,
"PhoneNumber": "254"+phone_no,
"CallBackURL": base_url+"/confirmation",
"AccountReference": "Jaymoh",
"TransactionDesc": "Channel join payment"
}
response = requests.post(api_url, json=request, headers=headers)
pprint.pprint(response.json())
rendered = render_to_string('home.html', {})
response = HttpResponse(rendered)
return response
return render(request, 'home.html', {})
Post data is sent to the view below:
def confirmation(request):
print(request.body)
if request.method == 'POST':
response = json.loads(request.body)
pprint.pprint(response)
transaction_response = response['Body']['stkCallback']
save_transaction = Transaction(
MerchantRequestID = transaction_response['MerchantRequestID'],
CheckoutRequestID = transaction_response['CheckoutRequestID'],
ResultCode = transaction_response['ResultCode'],
ResultDesc = transaction_response['ResultDesc']
)
save_transaction.save()
transaction_result = transaction_response['ResultCode']
print("ResultCode = %s" % transaction_result)
print(type(transaction_result))
if transaction_result == 0:
print('Transaction successful')
return redirect("successfultransaction")
else:
print("Incomplete Transaction")
return redirect('incompletetransaction')
return render(request, 'home.html', {})
The error now occurs in this view, The if statement executes and prints but the redirects never happen.
{"Body":{"stkCallback":{"MerchantRequestID":"8995-63446-1","CheckoutRequestID":"ws_CO_131120191221087793","ResultCode":1,"ResultDesc":"The balance is insufficient for the transaction"}}}'
{'Body': {'stkCallback': {'CheckoutRequestID': 'ws_CO_131120191221087793', 'MerchantRequestID': '8995-63446-1', 'ResultCode': 1, 'ResultDesc': 'The balance is insufficient for the ' 'transaction'}}} {'CheckoutRequestID': 'ws_CO_131120191221087793', 'CustomerMessage': 'Success. Request accepted for processing', 'MerchantRequestID': '8995-63446-1', 'ResponseCode': '0', 'ResponseDescription': 'Success. Request accepted for processing'}
[13/Nov/2019 12:21:17] "POST / HTTP/1.1" 200 4081
ResultCode = 1
Incomplete Transaction
[13/Nov/2019 12:21:17] "POST /confirmation HTTP/1.1" 302 0
Before the post in the home function, I returned a HttpResponse()
. I don't think can be affecting the redirect in the confirmation callback function which receives the POST.
Upvotes: 0
Views: 350
Reputation: 183
Nope, that's happen.
Look: [13/Nov/2019 12:21:17] "POST /confirmation HTTP/1.1" 302 0
You got a response with 302 code, that's is redirect.
Upvotes: 1