I am not much familiar with php and curl, need to convert an advance PHP cURL POST request to python equivalent.
It's a code from payment gateway site called paygate, and am using their sample php API from developer.paygate.co.za/. The code that I tried to convert into python is below:
<?php
//The PayGate PayXML URL
define( "SERVER_URL", "https://www.paygate.co.za/payxml/process.trans" );
//Construct the XML document header
$XMLHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE protocol SYSTEM \"https://www.paygate.co.za/payxml/payxml_v4.dtd\">";
// - Then construct the 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 the request XML by combining the XML header and transaction
$Request = $XMLHeader.$XMLTrans;
// Create the POST data header containing the transaction
$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($Request)."
";
$header[] = $Request;
// Use cURL to post the transaction to PayGate
// - first instantiate cURL; if it fails then quit now.
$ch = curl_init();
if (!$ch) die("ERROR: cURL initialization failed. Check your cURL/PHP configuration.");
// - then set the cURL options; to 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");
// - then set the PayXML URL and the transaction data
curl_setopt ($ch, CURLOPT_URL, SERVER_URL);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
// Connect to PayGate PayXML and send data
$Response = curl_exec ($ch);
// Checl for any connection errors and then close the connection.
$curlError = curl_errno($ch);
curl_close($ch);
I know about basic requests in python but couldn't pass attributes in that request, I am also confused about passing cURL data in requests.
I am trying it 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 it shows error
405 - HTTP verb used to access this page is not allowed.