Class: Hps::HpsService

Inherits:
Object
  • Object
show all
Defined in:
lib/hps/services/hps_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HpsService

Returns a new instance of HpsService.



14
15
16
17
18
19
20
21
22
23
# File 'lib/hps/services/hps_service.rb', line 14

def initialize(options={})

    merged_options = Hps.options.merge(options)

    Configuration::VALID_CONFIG_KEYS.each do |key|
      send("#{key}=", merged_options[key])
    end

  @exception_mapper = Hps::ExceptionMapper.new
end

Instance Attribute Details

#exception_mapperObject

Returns the value of attribute exception_mapper.



10
11
12
# File 'lib/hps/services/hps_service.rb', line 10

def exception_mapper
  @exception_mapper
end

Instance Method Details

#doTransaction(transaction, client_txn_id = nil) ⇒ Object

protected



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hps/services/hps_service.rb', line 27

def doTransaction(transaction, client_txn_id = nil)

  if configuration_invalid
      raise @exception_mapper.map_sdk_exception(SdkCodes.invalid_transaction_id)
  end

    xml = Builder::XmlMarkup.new
    xml.instruct!(:xml, :encoding => "UTF-8")
    xml.SOAP :Envelope, {
      'xmlns:SOAP' => 'http://schemas.xmlsoap.org/soap/envelope/',
      'xmlns:hps' => 'http://Hps.Exchange.PosGateway' } do
      xml.SOAP :Body do
        xml.hps :PosRequest do
          xml.hps 'Ver1.0'.to_sym do
            xml.hps :Header do
              if self.secret_api_key
                self.service_uri = gateway_url_for_key self.secret_api_key
                xml.hps :SecretAPIKey, self.secret_api_key.strip
              else
                xml.hps :UserName, self.user_name
                xml.hps :Password, self.password
                xml.hps :DeviceId, self.device_id
                xml.hps :LicenseId, self.license_id
                xml.hps :SiteId, self.site_id
              end
              xml.hps :DeveloperID, self.developer_id if self.developer_id
              xml.hps :VersionNbr, self.version_number if self.version_number
              xml.hps :SiteTrace, self.site_trace if self.site_trace
              xml.hps :ClientTxnId, client_txn_id if client_txn_id
              xml.hps :SDKNameVersion, 'ruby;version=2.3.3'
            end

            xml << transaction

          end
        end
      end
    end

  begin

      uri = URI.parse(self.service_uri)
      http = Net::HTTP.new uri.host, uri.port
      http.use_ssl = true

      # allow SSL verification as opt-in

      if self.http_options && self.http_options.verify_mode
        http.verify_mode = self.http_options.verify_mode
        http.ca_file = self.http_options.ca_file if self.http_options.ca_file
      else
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end

      data = xml.target!

      response = http.post(uri.path, data, 'Content-type' => 'text/xml')
      # NOTE: If the HTTP request was successful

      if response.is_a? Net::HTTPOK

        # NOTE: Convert XML to a Hash

        soap_hash = Hash.from_xml(response.body)
        # NOTE: Peel away the layers and return only the PosRespose

        soap_hash["Envelope"]["Body"]["PosResponse"]["Ver1.0"]

      else
        raise @exception_mapper.map_sdk_exception(SdkCodes.unable_to_process_transaction)
      end

  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
     Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    raise @exception_mapper.map_sdk_exception(SdkCodes.unable_to_process_transaction, e)
  end

end

#gateway_url_for_key(api_key) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hps/services/hps_service.rb', line 102

def gateway_url_for_key(api_key)

  gateway_url = "https://api2.heartlandportico.com/Hps.Exchange.PosGateway/PosGatewayService.asmx?wsdl"

  if api_key.include? "_uat_"

    gateway_url = "https://posgateway.uat.secureexchange.net/Hps.Exchange.PosGateway/PosGatewayService.asmx?wsdl"

  elsif api_key.include? "_cert_"

    gateway_url = "https://cert.api2.heartlandportico.com/Hps.Exchange.PosGateway/PosGatewayService.asmx?wsdl"
  end

  gateway_url
end

#hydrate_transaction_header(header) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/hps/services/hps_service.rb', line 118

def hydrate_transaction_header(header)
  result = HpsTransactionHeader.new
  result.gateway_response_code = header["GatewayRspCode"]
  result.gateway_response_message = header["GatewayRspMsg"]
  result.response_dt = header["RspDT"]
  result.client_txn_id = header["GatewayTxnId"]
  result
end