Forum OpenACS Q&A: Paypal IPN handling in TCl with OACS3.25

I am trying, but have yet to succeed at getting the following code to work.

Paypal IS sending the data it should, and I am getting a socket opened to it, but NOT receiving any response.  I believe I'm doing something wrong in the socket handling, but can't figure out what AND can't find docs to really describe how to handle it all.

Any help (or even sympathetic murmurs) will be greatly appreciated!  Thank You In Advance!!

Here's my code:
-----
#Start of paypal_poster.tcl
#Acknowledges and updates information from paypal IPN process
#
set vars [ ec_export_entire_form_as_url_vars_maybe ]
set formvars "cmd=_notify-validate"

if {![string match $vars ""] } {
set formvars $formvars&$vars
}
#
set server www.paypal.com
set port 443
#set port 80
set cmd POST
set path cgi-bin/webscr

ns_log Notice "sending query to paypal at $server on port $port"

  set sock [socket $server $port]
  fconfigure $sock -blocking 0

ns_log Notice "here goes the query $cmd [string length $formvars] chars on socket $sock"

  puts $sock "$cmd $path HTTP/1.0"
  puts $sock "Host: $server"
  puts $sock "Content-Type:  \ application/x-www-form-urlencoded"
  puts $sock "Content-Length: [string length $formvars]"
  puts $sock ""
  puts $sock $formvars
  puts $sock ""
  gets $sock valid
  flush $sock

ns_log Notice "query sent"

#

ns_log Notice "Valid = $valid"
----------------------END OF TEXT-----------------
In the log, Valid always equals nothing - blank

Collapse
Posted by Jonathan Ellis on
I use util_httppost:

set vars [export_ns_set_vars]
append vars "&cmd=_notify-validate"
# https is recommended but http works; I don't think httppost works with https
set confirm [util_httppost http://www.paypal.com/cgi-bin/webscr $vars]
ns_log notice "paypal confirm is $confirm"

Collapse
Posted by Bill Millikin on
Very nice and elegant solution, Jonathan!

Thank You.