duandiaoqian5795 2015-08-03 06:09
浏览 79

将PAYGATE API的PHP Curl请求转换为python

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.

  • 写回答

1条回答 默认 最新

  • douduiwei2831 2015-08-03 11:54
    关注

    Finally solved it,

    import requests
    import xml.etree.ElementTree as 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, like 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 what your server accepts
    
    response = requests.post('https://www.paygate.co.za/payxml/process.trans', data=xml, headers=headers).text
    
    tree = ET.fromstring(response)
    
    
    for 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. like invoice_no or sale_order_no
        auth = node.attrib.get('auth')
        rdesc = node.attrib.get('rdesc') # Result Code description.
        print sdesc, tid, cref
    
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决