Forum: Ruby on Rails Server-to-Server SSL Connection

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
33eb32997ccb3a5497cb1df8b075c2b2?d=identicon&s=25 ChrisN (Guest)
on 2007-06-26 22:22
Hi everyone,

I need to make a server-to-server connection in my rails app to confirm
online credit card payments on the fly.  When the confirm payment action
is run it calls a method in the 'Order' model object, the model attempts
to make an SSL connection to the payment gateway to confirm the payment
using the following code:


# url: payment gateway domain name
# path: relative path to confirmation script
# params: string of HTTP params
http = Net::HTTP.new(url, 443)
http.use_ssl = true
resp, data = http.post(path, params)


The http.post() call returns the following error:


No such file or directory

/usr/lib/ruby/1.8/net/http.rb:586:in `connect'
/usr/lib/ruby/1.8/net/http.rb:586:in `connect'
/usr/lib/ruby/1.8/net/http.rb:553:in `do_start'
/usr/lib/ruby/1.8/net/http.rb:542:in `start'
/usr/lib/ruby/1.8/net/http.rb:1032:in `request'
/usr/lib/ruby/1.8/net/http.rb:842:in `post'
#{RAILS_ROOT}/app/models/order.rb:59:in `confirm_payment'
#{RAILS_ROOT}/app/controllers/store_controller.rb:221:in
`payment_approved'


Now, the code snippet above works 100% everywhere except when I try to
use it in a Rails app.  The only way I can make this work in Rails is to
use nonsecure https rather than SSL, however the payment gateway
requires an SSL POST request to do the confirmation.  The error is
obviously SSL specific, I have traced back through the Ruby code as far
as I can, any further entails tracing back through openssl.so.  Has
anyone been ably to make this work?

Thanks,
Chris
33eb32997ccb3a5497cb1df8b075c2b2?d=identicon&s=25 ChrisN (Guest)
on 2007-07-03 22:17
Nobody has any insight on this one?  Has no one needed to make a secure
server-to-server connection in a Rails app before?
F2d6c742a86c801856006fae167ba1f2?d=identicon&s=25 George Bailey (Guest)
on 2007-07-03 22:57
(Received via mailing list)
On Jul 3, 2007, at 2:17 PM, ChrisN wrote:

> Nobody has any insight on this one?  Has no one needed to make a
> secure
> server-to-server connection in a Rails app before?


This works for me, but I don't need to do a post.


require 'socket'
require 'openssl'

socket = TCPSocket.new("www.url.net")
ssl_context = OpenSSL::SSL::SSLContext.new()
unless ssl_context.verify_mode
   warn "warning: peer certificate won't be verified this session."
   ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
sslsocket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
sslsocket.sync_close = true
sslsocket.connect
sslsocket.puts("some text")

while line = sslsocket.gets
p line
end
This topic is locked and can not be replied to.