Am having issue sending xml request in pthon because i don't know how to render python code in xml. I have no idea how to render or navigate xml text like in example below or how to render python code in xml text before sending the python request.
def start_xml_payment(request):
if request.method == 'POST':
form = FormPaymentForm(request.POST)
if form.is_valid():
payment = form.save()
first_name = request.user.first_name
last_name = request.user.last_name
email = request.user.email
serdate = datetime.datetime.now()
ref = payment.ref
amount = payment.amount
fees = payment.fees
datejson_str = json.dumps({'created_at': serdate}, default=str)
url = 'https://secure.powerpay.com/API/v6/'
xml = ''' <?xml version="1.0" encoding="utf-8"?>
<API3G>
<code>4332-9D7F-4E09-96D4-3D44E7A83EA3</Code>
<Transaction>
<PaymentAmount>{amount}</PaymentAmount >
<customerEmail>{email}</customerEmail>
</Transaction>
<Services>
<Service>
<ServiceDate>{datejson_str}</ServiceDate>
</Service>
</Services>
</API3G>
'''
headers = {'Content-Type': 'application/xml'}
response = requests.post(url, data=xml, headers=headers)
response_data = response.json()
TransToken = response_data["TransToken"]
cashier_response = 'https://secure.powerpay.com/p.php?ID={TransToken}'
return HttpResponseRedirect (cashier_response)
else:
form = FormPaymentForm(request.POST)
return render(request, 'dpo/start_dpo_payment.html', {'form': form})
Response
<?xml version="1.0" encoding="utf-8"?><API3G><Result>000</Result><ResultExplanation>Transaction created</ResultExplanation><TransToken>803E06BC-CB80-4671-9F92-90326411ACFC</TransToken><TransRef>R39855046</TransRef></API3G>
I want to navigate through this xml response and fetch TransToken
TransToken = response_data["TransToken"]
i want something like this
cashier_response = 'https://secure.powerpay.com/p.php?ID={TransToken}'
Issue 1 i have issue fetching the Transtoken because am unable to navigate through the xml response to get the transtoken and merge the transtoken to my url.
I need help in navigating xml text like example above
Issue 2 How do I render data from the form into the xml text before sending the post request. since xml is always commeneted with """ xml code """ in python file the normal python code will not always work when i try to render my python code in xml . i tried using the normal python {} like {amount} in rendering the amount or {ref} in rendering ref but it didnt work.
I need help to fix this issue
See below
output