Wininet vs. curl

Started by stevengraff, July 23, 2014, 02:39:13 PM

Previous topic - Next topic

stevengraff

Does the wininet extender pretty much do everything I'd ever consider doing with curl?

Deana

Basically yes. The WinInet extender contains a slew of functions which allow communication between host computers, Internet Service Providers and supported networks for Windows operating systems.
Deana F.
Technical Support
Wilson WindowWare Inc.

stevengraff

I'm trying to convert a payment system's curl code to winbatch. I tested their example, and it returns an html message showing "approval:"

curl -k -i -X POST "https://secure.bluepay.com/interfaces/bp10emu" -d MERCHANT=100144703153 -d TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9 -d TRANSACTION_TYPE=SALE -d AMOUNT=3.00 -d NAME1="BluePay Customer" -d CC_NUM=4111111111111111 -d CC_EXPIRES=1215


My attempt to mimic this, which only returns a logon form, is:

Code (winbatch) Select

host = "secure.bluepay.com"
mainUrl= "/interfaces/bp10emu"

formdata = ""
formdata = iContenturl(formdata,"Merchant","100144703153")
formdata = iContenturl(formdata,"TAMPER_PROOF_SEAL","74acff4399d6d72dbcab8dc130038ef9")
formdata = iContenturl(formdata,"TRANSACTION_TYPE","SALE")
formdata = iContenturl(formdata,"AMOUNT","3.00")
formdata = iContenturl(formdata,"NAME1","BluePay Customer")
formdata = iContenturl(formdata,"CC_NUM","4111111111111111")
formdata = iContenturl(formdata,"CC_EXPIRES","1215")

tophandle=iBegin(0,"","")
connecthandle=iHostConnect(tophandle, host, @HTTPs, "", "")
datahandle=iHttpInit(connecthandle,"POST","/","", 2)
if datahandle==0
   err=iGetLastError()
   Message("Problem with connection; please try again","Error code: %err%")
   iClose(tophandle)
   exit
endif
debug(z)
rsltCode = iHttpOpen(datahandle, 0, formdata, -1)
if rsltCode=="ERROR" || rsltCode!=200
   if rsltCode == "ERROR"
       errstr = "Connection Error"
       rsltCode = iGetLastError()
   else
       errstr = "HTTP Error"
   endif
   Message(errstr,"Error code: %rsltCode%")
   iClose(tophandle)
   exit
endif

curDir = dirScript()
ticks = GetTickCount( )
fileDelete( "*_result.tmp" )
tmpResFile = strCat(curDir,"%ticks%_result.tmp")
; errorMode(@off) ; commented out by sg 4/5/14
canWrite = FilePut( tmpResFile, "xyzzy" )
if canWrite <> 5
message("Problem writing %tmpResFile%.", "Unable to write result file to disk. Please check permissions on %curDir%. Please contact support.")
Exit
Endif
errorMode(@cancel)
log = iReadData(datahandle, tmpResFile)
log = fileGet(tmpResFile)
iclose(datahandle)
iclose(connecthandle)
iclose(tophandle)



I seem to be missing the mark. Is it something simple?

Thanks,
SG

Deana

You need to include the mainURL variable in the iHttpInit.

Change:
Code (winbatch) Select

datahandle=iHttpInit(connecthandle,"POST","/","", 2)


To:
Code (winbatch) Select

datahandle=iHttpInit(connecthandle,"POST",mainUrl,"", 2)
Deana F.
Technical Support
Wilson WindowWare Inc.

stevengraff

Right! I tried that, too, but then I get a 404 from the open command.

Deana

Check the URL. Maybe try removing the leading backslash?

I recommend using DebugTrace. Simply add DebugTrace(@on,"trace.txt") to the beginning of the script and inside any UDF, run it until the error or completion, then inspect the resulting trace file for clues as to the problem. Feel free to post the trace file here ( removing any private info) if you need further assistance.
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

SIDE NOTE: I see the curl command line is using the -X parameter that specifies to use the default proxy. You might want to change your iBegin statement to option 1 - Proxy server connect:

Code (winbatch) Select

tophandle=iBegin(1,"","") ; Use Default proxy

Deana F.
Technical Support
Wilson WindowWare Inc.

stevengraff

************************************************************

*** Debug Initialized ***

==============================
Thu 7/24/2014 11:30:53 AM
WinBatch 32 2008B
WIL DLL 6.08bhb
C:\code\GoldPay\goldpay.wil
Windows platform: NT, version: 6.1, build: 7601 (Service Pack 1)
ErrorMode: @CANCEL
==============================

----- Extender loaded: C:\Program Files (x86)\WinBatch\SYSTEM\WWINT44I.dll (file version: 44077,0,0,0)

AddExtender("WWINT44I.dll")
(78) VALUE INT => 1

host = "secure.bluepay.com"
(78) VALUE STRING => "secure.bluepay.com"

mainUrl= "/interfaces/bp10emu"
(78) VALUE STRING => "/interfaces/bp10emu"

formdata = ""
(78) VALUE STRING => ""

formdata = iContenturl(formdata,"Merchant","100144703153")
(78) VALUE STRING => "Merchant=100144703153"

formdata = iContenturl(formdata,"TAMPER_PROOF_SEAL","74acff4399d6d72dbcab8dc130038ef9")
(78) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9"

formdata = iContenturl(formdata,"TRANSACTION_TYPE","SALE")
(78) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE"

formdata = iContenturl(formdata,"AMOUNT","3.00")
(78) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE&AMOUNT=3.00"

formdata = iContenturl(formdata,"NAME1","BluePay Customer")
(78) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE&AMOUNT=3.00&NAME1=BluePay%20Customer"

formdata = iContenturl(formdata,"CC_NUM","4111111111111111")
(78) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE&AMOUNT=3.00&NAME1=BluePay%20Customer&CC_NUM=4111111111111111"

formdata = iContenturl(formdata,"CC_EXPIRES","1215")
(78) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE&AMOUNT=3.00&NAME1=BluePay%20Customer&CC_NUM=4111111111111111&CC_EXPIRES=1215"

tophandle=iBegin(0,"","")
(94) VALUE INT => 13369348

connecthandle=iHostConnect(tophandle, host, @HTTPs, "", "")
(94) VALUE STRING => "S13369352"

datahandle=iHttpInit(connecthandle,"POST","mainurl","", 2)
(110) VALUE INT => 13369356

if datahandle==0
(110) END OPERATOR

rsltCode = iHttpOpen(datahandle, "", formdata, -1)
(328) VALUE STRING => "404"

if rsltCode=="ERROR" || rsltCode!=200
(328) IF DO==>TRUE

if rsltCode == "ERROR"
(328) ELSE DO==>TRUE

errstr = "HTTP Error"
(328) VALUE STRING => "HTTP Error"

endif
(328) END OPERATOR

Message(errstr,"Error code: 404")
(2980) VALUE INT => 1

iClose(tophandle)
(2980) VALUE INT => 1

exit
(2980) VALUE INT => 0

--- Normal termination ---

;;;END OF JOB;;;

stevengraff

Quote from: Deana on July 24, 2014, 08:33:22 AM
SIDE NOTE: I see the curl command line is using the -X parameter that specifies to use the default proxy. You might want to change your iBegin statement to option 1 - Proxy server connect:

Code (winbatch) Select

tophandle=iBegin(1,"","") ; Use Default proxy



When I do that, iBegin returns 0.

Actually, it's a cap X, meaning POST vs. GET, etc. The proxy setting would be lower case x.

Deana

You placed your variable in quotes which treats it as a string! This is telling the function to locate an HTTP resource named "mainurl", which of course doesn't exist...hence the 404 error.

Change:
Code (winbatch) Select
datahandle=iHttpInit(connecthandle,"POST","mainurl","", 2)

To:
Code (winbatch) Select
datahandle=iHttpInit(connecthandle,"POST",mainurl,"", 2)
Deana F.
Technical Support
Wilson WindowWare Inc.

stevengraff

Thanks for catching that. However, the outcome is the same, it still throws a 404 when trying to Open.

Deana

Quote from: stevengraff on July 24, 2014, 09:21:09 AM
Thanks for catching that. However, the outcome is the same, it still throws a 404 when trying to Open.

The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with a given server, but the server could not find what was requested.

DebugTrace output will confirm the code changes. Please post the fresh debug trace output.
Deana F.
Technical Support
Wilson WindowWare Inc.

stevengraff


************************************************************

*** Debug Initialized ***

==============================
Thu 7/24/2014 12:34:59 PM
WinBatch 32 2008B
WIL DLL 6.08bhb
C:\code\GoldPay\goldpay.wil
Windows platform: NT, version: 6.1, build: 7601 (Service Pack 1)
ErrorMode: @CANCEL
==============================

----- Extender loaded: C:\Program Files (x86)\WinBatch\SYSTEM\WWINT44I.dll (file version: 44077,0,0,0)

AddExtender("WWINT44I.dll")
(63) VALUE INT => 1

host = "secure.bluepay.com"
(63) VALUE STRING => "secure.bluepay.com"

mainUrl= "/interfaces/bp10emu"
(63) VALUE STRING => "/interfaces/bp10emu"

formdata = ""
(63) VALUE STRING => ""

formdata = iContenturl(formdata,"Merchant","100144703153")
(63) VALUE STRING => "Merchant=100144703153"

formdata = iContenturl(formdata,"TAMPER_PROOF_SEAL","74acff4399d6d72dbcab8dc130038ef9")
(63) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9"

formdata = iContenturl(formdata,"TRANSACTION_TYPE","SALE")
(63) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE"

formdata = iContenturl(formdata,"AMOUNT","3.00")
(63) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE&AMOUNT=3.00"

formdata = iContenturl(formdata,"NAME1","BluePay Customer")
(63) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE&AMOUNT=3.00&NAME1=BluePay%20Customer"

formdata = iContenturl(formdata,"CC_NUM","4111111111111111")
(63) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE&AMOUNT=3.00&NAME1=BluePay%20Customer&CC_NUM=4111111111111111"

formdata = iContenturl(formdata,"CC_EXPIRES","1215")
(63) VALUE STRING => "Merchant=100144703153&TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9&TRANSACTION_TYPE=SALE&AMOUNT=3.00&NAME1=BluePay%20Customer&CC_NUM=4111111111111111&CC_EXPIRES=1215"

debug(z)
(63) VALUE INT => 0

tophandle=iBegin(0,"","")
(110) VALUE INT => 13369348

connecthandle=iHostConnect(tophandle, host, @HTTPs, "", "")
(110) VALUE STRING => "S13369352"

datahandle=iHttpInit(connecthandle,"POST",mainurl,"", 2)
(110) VALUE INT => 13369356

if datahandle==0
(110) END OPERATOR

rsltCode = iHttpOpen(datahandle, "", formdata, -1)
(905) VALUE STRING => "404"

if rsltCode=="ERROR" || rsltCode!=200
(905) IF DO==>TRUE

if rsltCode == "ERROR"
(921) ELSE DO==>TRUE

errstr = "HTTP Error"
(921) VALUE STRING => "HTTP Error"

endif
(921) END OPERATOR

Message(errstr,"Error code: 404")
(2637) VALUE INT => 1

iClose(tophandle)
(2637) VALUE INT => 1

exit
(2637) VALUE INT => 0

--- Normal termination ---

;;;END OF JOB;;;

stevengraff

btw, you're welcome to try these scripts "live" if you'd like to see what they do. It's all test-bed access, so no harm, it's designed for testing.

Deana

Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

When I run your curl command it appears that it is REDIRECTED? Is this the expected behavior?

curl -k -i -X POST "https://secure.bluepay.com/interfaces/bp10emu" -d MERCHANT=100144703153 -d TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc130038ef9 -d TRANSACTION_TYPE=SALE -d AMOUNT=3.00 -d NAME1="BluePay Customer" -d CC_NUM=4111111111111111 -d CC_EXPIRES=1215

RESULT


C:\Windows\system32>curl -k -i -X POST "https://secure.bluepay.com/interfaces/bp
10emu" -d MERCHANT=100144703153 -d TAMPER_PROOF_SEAL=74acff4399d6d72dbcab8dc1300
38ef9 -d TRANSACTION_TYPE=SALE -d AMOUNT=3.00 -d NAME1="BluePay Customer" -d CC_
NUM=4111111111111111 -d CC_EXPIRES=1215
HTTP/1.1 302 Found
Date: Thu, 24 Jul 2014 17:28:55 GMT
Server: Apache
Location: /interfaces/wlcatch?INVOICE_ID=100189050919&BANK_NAME=&PAYMENT_ACCOUNT
=xxxxxxxxxxxx1111&AUTH_CODE=KMFGJ&CARD_TYPE=VISA&AMOUNT=3.00&REBID=&AVS=_&ORDER_
ID=100189050919&CARD_EXPIRE=1215&Result=APPROVED&RRNO=100189050919&CVV2=_&PAYMEN
T_TYPE=CREDIT&MESSAGE=Approved%20Sale
Vary: Accept-Encoding
Content-Length: 507
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="/interfaces/wlcatch?INVOICE_ID=100189050919&a
mp;BANK_NAME=&amp;PAYMENT_ACCOUNT=xxxxxxxxxxxx1111&amp;AUTH_CODE=KMFGJ&amp;CARD_
TYPE=VISA&amp;AMOUNT=3.00&amp;REBID=&amp;AVS=_&amp;ORDER_ID=100189050919&amp;CAR
D_EXPIRE=1215&amp;Result=APPROVED&amp;RRNO=100189050919&amp;CVV2=_&amp;PAYMENT_T
YPE=CREDIT&amp;MESSAGE=Approved%20Sale">here</a>.</p>
</body></html>

Deana F.
Technical Support
Wilson WindowWare Inc.

stevengraff

Is that bad? Can Wininet do that also?

Deana

The WinInet Extender function iHttpInit automatically redirects URLs. It appears the server is trying to redirect to the url and it returns a 404 error:
https://secure.bluepay.com/interfaces/wlcatch?INVOICE_ID=100189054283&BANK_NAME=&PAYMENT_ACCOUNT=xxxxxxxxxxxx1111&AUTH_CODE=BSHRH&CARD_TYPE=VISA&AMOUNT=3.00&REBID=&AVS=_&ORDER_ID=100189054283&CARD_EXPIRE=1215&Result=APPROVED&RRNO=100189054283&CVV2=_&PAYMENT_TYPE=CREDIT&MESSAGE=Approved%20Sale

Maybe the API requires NO redirect INTERNET_FLAG_NO_AUTO_REDIRECT. Unfortunately the iHttpInit function doesn't currently support that option. However you can use this User Defined Function en lieu of iHttpInit

Code (winbatch) Select

#DefineFunction iHttpInitNoRedir (connecthandle, verb, object, referrer, flags)
   INTERNET_FLAG_NO_AUTO_REDIRECT = 2097152
   INTERNET_FLAG_SECURE = 8388608

   flags = flags | INTERNET_FLAG_NO_AUTO_REDIRECT
   If StrSub(connecthandle, 1, 1) == "S"
      connecthandle = StrSub(connecthandle, 2, -1)
      flags = flags | INTERNET_FLAG_SECURE
   EndIf
   Return DllCall("WinInet.dll", long:"HttpOpenRequestA", long:connecthandle, lpstr:verb, lpstr:object, lpstr:"HTTP/1.1", lpstr:referrer, long:0, long:flags, long:0)
#EndFunction



Also, for debugging try adding this immediately after the iHttpOpen. This will return the response headers:
Code (winbatch) Select
if rslt!=200
      headers=iHttpHeaders(datahandle)
      AskItemList("Rslt=%rslt%",headers,@tab,@unsorted,@single)
endif
Deana F.
Technical Support
Wilson WindowWare Inc.

stevengraff

Confirmed... that works. Thanks again!