Converting PHP Curl request of PAYGATE API to python -


i not familiar php , curl, need convert advance php curl post request python equivalent.

it's code payment gateway site called paygate, , using sample php api developer.paygate.co.za/. code tried convert python below:

<?php //the paygate payxml url define( "server_url", "https://www.paygate.co.za/payxml/process.trans" );   //construct xml document header $xmlheader = "<?xml version=\"1.0\" encoding=\"utf-8\"?><!doctype protocol system \"https://www.paygate.co.za/payxml/payxml_v4.dtd\">";  // - construct full transaction xml $xmltrans = '<protocol ver="4.0" pgid="10011013800" pwd="test"><authtx cref="abcqwerty1234" cname="patel sunny" cc="5200000000000015" exp="032022" budp="0" amt="10000" cur="zar" cvv="123" rurl="http://localhost/pg_payxml_php_final.php" nurl="http://localhost/pg_payxml_php_notify.php" /></protocol>'  // construct request xml combining xml header , transaction $request = $xmlheader.$xmltrans;   // create post data header containing transaction $header[] = "content-type: text/xml"; $header[] = "content-length: ".strlen($request)."\r\n"; $header[] = $request;   // use curl post transaction paygate // - first instantiate curl; if fails quit now. $ch = curl_init(); if (!$ch) die("error: curl initialization failed.  check curl/php configuration.");  // - set curl options; ignore ssl invalid certificates; set timeouts etc.  curl_setopt ($ch, curlopt_ssl_verifypeer, 0); curl_setopt ($ch, curlopt_ssl_verifyhost, 0); curl_setopt ($ch, curlopt_returntransfer, 1); curl_setopt ($ch, curlopt_timeout, 60); curl_setopt ($ch, curlopt_useragent, "mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)"); curl_setopt ($ch, curlopt_customrequest, "post");               // - set payxml url , transaction data curl_setopt ($ch, curlopt_url, server_url); curl_setopt ($ch, curlopt_httpheader, $header);               // connect paygate payxml , send data $response = curl_exec ($ch);  // checl connection errors , close connection. $curlerror = curl_errno($ch); curl_close($ch); 

i know basic requests in python couldn't pass attributes in request, confused passing curl data in requests.

i trying like:

import requests post_data = {'pgid':'10011013800',              'pwd':'test',              'cref': 'abcx1yty36858gh',              'cname':'patelsunny',              'cc':'5200000470000015',              'exp':'032022',              'budp':'0',              'amt':'50000',              'cur':'zar',              'cvv':'123',              'rurl':'http://localhost/pg_payxml_php_final.php',              'nurl':'http://localhost/pg_payxml_php_notify.php',              'submit':'submit'              }  r = requests.get('https://www.paygate.co.za/payxml/process.trans', params=post_data,headers=headers)  # print(r.url) print r.text  

but shows error

405 - http verb used access page not allowed.

finally solved it,

import requests import xml.etree.elementtree et   headers = {'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'accept-encoding':'gzip, deflate', 'accept-language':'en-us,en;q=0.8', 'cache-control':'max-age=0', 'connection':'keep-alive', 'content-length':'112', 'content-type':'application/x-www-form-urlencoded', 'user-agent':"mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.81 safari/537.36",}  xml = """<?xml version="1.0" encoding="utf-8"?>             <!doctype protocol system "https://www.paygate.co.za/payxml/payxml_v4.dtd">                 <protocol ver="4.0" pgid="10011013800" pwd="test">                     <authtx cref="abcx1j64564" cname="patel sunny" cc="5200000000000015" exp="032022" budp="0" amt="10000" cur="zar" cvv="123"                          rurl="http://localhost/pg_payxml_php_final.php" nurl="http://localhost/pg_payxml_php_notify.php" />                 </protocol>       """ headers = {'content-type': 'application/xml'} # set server accepts  response = requests.post('https://www.paygate.co.za/payxml/process.trans', data=xml, headers=headers).text  tree = et.fromstring(response)   node in tree.iter('authrx'):     sdesc = node.attrib.get('sdesc') # status message     tid = node.attrib.get('tid') # transaction id     cref = node.attrib.get('cref') # reference no. invoice_no or sale_order_no     auth = node.attrib.get('auth')     rdesc = node.attrib.get('rdesc') # result code description.     print sdesc, tid, cref 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -