Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Example service implementation

troelskn edited this page · 1 revision

An example of a SOAP Service client class, using HTTP basic authentication:


class RecurringPaymentService < Handsoap::Service

  endpoint :uri => 'https://endpoint.uri/soap', :version => 1

  class << self 
    attr_accessor :username, :password
  end

  # Handle HTTP basic authentication
  def on_after_create_http_request(http_request)
    # beware the missing chomp, it will make you cry....
    auth = Base64.encode64("#{self.class.username}:#{self.class.password}").chomp
    http_request.set_header('authorization', "Basic #{auth}")
  end

  # Register namespaces for request
  def on_create_document(doc) 
    doc.alias 'ns1', 'http://namescape.uri/namespace'
    doc.alias 'ns2', 'http://other.namespace.uri/namesp'
  end

  # Register namespaces for response
  def on_response_document(doc) 
    doc.add_namespace 'payment', 'http://namescape.uri/namespace'
    doc.add_namespace 'common', 'http://other.namespace.uri/namespace'   
  end     

  # Invokes the submitRecurring call
  def submit!(args = {})

    # Invoke the submitRecurring function
    response = invoke('payment:submitRecurring') do |message|

      # Build the body of the SOAP envelope
      message.add('payment:recurringRequest') do |req|
        req.add('payment:amount') do |amount|
          amount.add('common:currency', args[:currency])
          amount.add('common:value', args[:value])  
        end
        req.add('payment:merchantAccount', args[:merchant_account])   
        req.add('payment:recurringReference', args[:recurring_reference])
        req.add('payment:reference', args[:reference])
        req.add('payment:shopperEmail', args[:shopper_email])
        req.add('payment:shopperReference', args[:shopper_reference])             
      end
    end

    # Ignore the response: everything is OK if the result is not a SOAP Fault.
  end
end

Calling methods on the SOAP service using this class:


RecurringPaymentService.username = 'myusername'
RecurringPaymentService.password = 'mysecretpassword'

RecurringPaymentService.submit!(:currency => 'USD', :value => '1000', 
    :merchant_account => 'my_account', ...)
Something went wrong with that request. Please try again.