Solved

How to send an HTTPS POST to an external URL and receive response using Classic ASP

Posted on 2014-04-23
14
1,273 Views
Last Modified: 2014-05-07
Many years ago, I needed a way to send POST requests via HTTPS in order to interact with APIs such as Authorize.Net and UPS Online Tools, among others, from my Classic ASP pages.

I found a Windows server component (DLL) called KHTTP that would send out the requests and put the results into an ASP variable. It worked with HTTP and HTTPS, as well as regular POST and XML data, could pull in the content from any external URL (even if not POSTing data), and I have used it with a variety of APIs (and for other purposes) over the years. The component seems to be out-of-date at this point, although I still use it for many websites since it works well at what it does:

http://webscripts.softpedia.com/script/Networking-Tools/KHTTP-Component-30862.html

However, at the moment, I am trying to do HTTPS POST requests to one of the PayPal APIs. KHTTP does not seem to be working, because (I am told by PayPal tech support) it is likely the component is making my data urlencoded.

I'm a little confused by that, because I have used it successfully with many other APIs… notable ones too, such as the aforementioned Authorize.Net and UPS Online Tools. I don't know why their requirements for a POST request would be different from PayPal's.

I even setup my own listener, received my own POST, and the data seems to come through fine.

I don't fully understand the urlencoded issue, I thought that is something related to GET requests, not POST.

Using KHTTP, I don't have any option to send the data any other way. I simply assign a bunch of name/value pairs, I have the option to POST or GET, then put in the https URL, and the component does the rest, putting the response into a variable.

So if PayPal tech support is right, then a different component/utility function would be the next thing to try.

So, I need an alternative to KHTTP that will interface correctly with PayPal's API. Does this need to be a DLL that I install on the server? Or is there some other way to do this? It has been a long while since I have researched this kind of thing. All I really need to do is:

1. Assign name/value pairs.
2. POST to PayPal's API.
3. Receive the response back.

Would really appreciate any advice.
Thank you!
0
Comment
Question by:bbdesign
  • 6
  • 5
  • 3
14 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
Comment Utility
Below is a function I have for something similar.

The top portion creates the xml soap to be posted.  If you were to response.write the strRequest, you would get the xml.  

Where you see, Dim objXMLHTTP is the start of sending the post to the url

strResult is where the results (xml) is held and below that you can see how I am parsing the xml.

function searchTransactions(SalesOrderID)

strRequest =""
strRequest =strRequest&"<?xml version=""1.0"" encoding=""UTF-8""?>"
strRequest =strRequest&"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" "
strRequest =strRequest&"xmlns:ns1=""urn:usaepay"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" "
strRequest =strRequest&"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" "
strRequest =strRequest&"xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/"" "
strRequest =strRequest&"SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">"
strRequest =strRequest&"<SOAP-ENV:Body>"

strRequest =strRequest&"<ns1:searchTransactions> "

strRequest =strRequest&"<Token xsi:type=""ns1:ueSecurityToken"">"
strRequest =strRequest&"<ClientIP xsi:type=""xsd:string"">"& clientip &"</ClientIP>"
strRequest =strRequest&"<PinHash xsi:type=""ns1:ueHash"">"
strRequest =strRequest&"<HashValue xsi:type=""xsd:string"">"& hash &"</HashValue>"
strRequest =strRequest&"<Seed xsi:type=""xsd:string"">"& seed &"</Seed>"
strRequest =strRequest&"<Type xsi:type=""xsd:string"">"& hashtype &"</Type>"
strRequest =strRequest&"</PinHash>"
strRequest =strRequest&"<SourceKey xsi:type=""xsd:string"">"& sourcekey &"</SourceKey>"
strRequest =strRequest&"</Token>"

strRequest =strRequest&"<Search SOAP-ENC:arrayType=""ns1:SearchParam[1]"" xsi:type=""ns1:SearchParamArray""> "
strRequest =strRequest&"<item xsi:type=""ns1:SearchParam""> "
strRequest =strRequest&"<Field xsi:type=""xsd:string"">Invoice</Field> "
strRequest =strRequest&"<Type xsi:type=""xsd:string"">eq</Type> "
strRequest =strRequest&"<Value xsi:type=""xsd:string"">"&SalesOrderID&"</Value> "
strRequest =strRequest&"</item> "
strRequest =strRequest&"</Search> "
strRequest =strRequest&"<MatchAll xsi:type=""xsd:boolean"">true</MatchAll> "
strRequest =strRequest&"<Start xsi:type=""xsd:integer"">0</Start> "
strRequest =strRequest&"<Limit xsi:type=""xsd:integer"">10</Limit>" 
strRequest =strRequest&"<Sort xsi:type=""xsd:string"">created</Sort> "

strRequest =strRequest&"</ns1:searchTransactions> "

strRequest =strRequest&"</SOAP-ENV:Body> "
strRequest =strRequest&"</SOAP-ENV:Envelope>"



Dim objXMLHTTP
set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")

strURL = PostToUrl
objXMLHTTP.setTimeouts 10000, 60000 , 60000, 360000 
   objXMLHTTP.open "POST", "" & strURL & "" , False
   objXMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
   objXMLHTTP.setRequestHeader "Content-Length", Len(strRequest)
   objXMLHTTP.send(strRequest)

   strResult = objXMLHTTP.responseText

   
dim getCustPayMethText
   getCustPayMethText=strResult

 
    Set objReturn = CreateObject("MSXML2.DOMDocument.3.0")
    objReturn.loadXML objXMLHTTP.responseText

' ********** READ XML OUTPUT *********'


if not objReturn.getElementsByTagName("TransactionsMatched").Item(0) is Nothing  then
	TransactionsMatched=			objReturn.getElementsByTagName("TransactionsMatched").Item(0).text
	else
	TransactionsMatched="0"
end if
if cdbl(TransactionsMatched)>0 then
for x = 0 to cint(TransactionsMatched)-1

if not objReturn.getElementsByTagName("RefNum").Item(x) is Nothing  then
	RefNum=			objReturn.getElementsByTagName("RefNum").Item(x).text
	else
	RefNum="N/A"
end if

if not objReturn.getElementsByTagName("Result").Item(x) is Nothing  then
	Result=			objReturn.getElementsByTagName("Result").Item(x).text
	else
	Result="N/A"
end if

if not objReturn.getElementsByTagName("ResultCode").Item(x) is Nothing  then
	ResultCode=			objReturn.getElementsByTagName("ResultCode").Item(x).text
	else
	ResultCode="N/A"
end if

if not objReturn.getElementsByTagName("CardNumber").Item(x) is Nothing  then
	CardNumber=			objReturn.getElementsByTagName("CardNumber").Item(x).text
	else
	CardNumber="NA/A"
end if

if not objReturn.getElementsByTagName("TransactionType").Item(x) is Nothing  then
	TransactionType=			objReturn.getElementsByTagName("TransactionType").Item(x).text
	else
	TransactionType="0"
end if

if ResultCode = "A" then
	searchTransactions="Approved"
	else
	searchTransactions="Not Approved"
end if

end Function

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
Looking at paypal you can see their soap header https://developer.paypal.com/docs/classic/api/PayPalSOAPAPIArchitecture/

<SOAP-ENV:Header>
<RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType">
<Credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType">
<Username>api_username</Username>
<Password>api_password</Password>
<Signature>api_signature</Signature>
<Subject>authorizing_account_emailaddress</Subject>
</Credentials>
</RequesterCredentials>
</SOAP-ENV:Header>

Open in new window


Where you see items like
<Username>api_username</Username>
that could be
<%

theXML="<Username>hard_coded_username</Username>"
' or
my_user_name="abc123"
theXML="<Username>"&my_user_name&"</Username>"

%>

Open in new window

0
 

Author Comment

by:bbdesign
Comment Utility
You appear to be using XML in all of these cases. Isn't that different than regular POST "name value pairs"? In fact, the DLL I was using (KHTTP) had a switch that I had to use, to tell it I was posting XML data rather than "regular" data (which I assume is the same as what an HTML form would send, as opposed to XML)?

I'm not sure how to format non-XML data, if that is in fact what PayPal wants. That other answer you pasted above looks kind of promising too, so I will check that out.

Or maybe I am overcomplicating this, and its basically the same thing?

Thanks!
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
The api from paypal I  looked up requires SOAP.  

For a direct post  http://support.microsoft.com/kb/290591
<%
	DataToSend = "id=1"
	dim xmlhttp 
	set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
	xmlhttp.Open "POST","http://somesite.com/Receiver.asp",false
	xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
	xmlhttp.send DataToSend
	Response.ContentType = "text/xml"
	Response.Write xmlhttp.responsexml.xml
        Set xmlhttp = nothing
%>

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
That's an interesting comment from the Paypal tech.  I'm using the Express Checkout API and everything that is posted is (and must be) urlencoded.  That is standard practice throughout the web.  Some simple values without any punctuation or special characters can be sent without urlencoding but everything else needs it.  I'm sure that if you looked at the data that Scott's code is sending you would see that it is definitely urlencoded.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Your link for KHTTP is no longer valid, it redirects to some other web site.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
ChillKat has this but you don't need it.  http://www.chilkatsoft.com/HttpActiveX.asp
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
But I can't check KHTTP for this question if I can't download it.
0
 

Author Comment

by:bbdesign
Comment Utility
I am using this for Payflow Gateway. Does that tell you anything, regarding SOAP and so forth?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
Since the question here was about how to do an httppost, I think we have shown that. To prevent this question from getting off tangent, see if what we have shown makes sense.

As for integrating with paypal, I would suggest reading up and giving it a try.  We will need more details on your next question such as which exact api are you using, what code have you tried, what error are you getting.

There is payflow link https://www.paypal.com/webapps/mpp/referral/paypal-payflow-link and payflow pro https://www.paypal.com/webapps/mpp/referral/paypal-payflow-pro which is going to be more like what you used on authorize.net.  

Paypal pro has developer info https://developer.paypal.com/docs/classic/products/payflow-pro/  and you will see a list of pdf's.  One shows you can use either xml or name value pairs https://www.paypalobjects.com/webstatic/en_US/developer/docs/pdf/pp_payflowpro_guide.pdf  
TRXTYPE=S&TENDER=C&USER=SuperMerchant&PWD=SuperUserPassword&PARTNER=PayPal&
ACCT=5105105105105100&EXPDATE=1209&AMT=99.06&COMMENT1=Reservation&FIRSTNAME
=John&LASTNAME=Jones&STREET=123 Main St.&CITY=San 
Jose&STATE=CA&ZIP=123451234&BILLTOCOUNTRY=US&CVV2=123&CUSTIP=0.0.0.0 

Open in new window

Using this example http:#a40018806 you might be able to add that information in the "datatosend" variable.
0
 

Author Comment

by:bbdesign
Comment Utility
Thank you so much, I will be checking into all of this later today.
0
 

Author Comment

by:bbdesign
Comment Utility
In order to verify that I am sending the data correctly, I am trying to setup a listener to bounce back what it is receiving. Before I started messing around with XML, I was using regular FORM-style post data, and I wrote this simple ASP script:

for each item in request.form
            response.write(item & "=" & request.form(item) & chr(13) & chr(10))
next

How can I do something similar for my XML data so I can know for sure what I am sending?
0
 

Author Comment

by:bbdesign
Comment Utility
Sorry for the long delay, I was able to get this to work using the sample code provided above. Thank you!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now