lib/active_merchant/billing/gateways/cashnet.rb in activemerchant-1.49.0 vs lib/active_merchant/billing/gateways/cashnet.rb in activemerchant-1.50.0

- old
+ new

@@ -52,20 +52,23 @@ private def commit(action, money, fields) fields[:amount] = amount(money) - url = live_url + @options[:merchant_gateway_name] - response = parse(ssl_post(url, post_data(action, fields))) + url = live_url + CGI.escape(@options[:merchant_gateway_name]) + raw_response = ssl_post(url, post_data(action, fields)) + parsed_response = parse(raw_response) - success = (response[:result] == '0') + return unparsable_response(raw_response) unless parsed_response + + success = (parsed_response[:result] == '0') Response.new( success, - CASHNET_CODES[response[:result]], - response, + CASHNET_CODES[parsed_response[:result]], + parsed_response, test: test?, - authorization: (success ? response[:tx] : '') + authorization: (success ? parsed_response[:tx] : '') ) end def post_data(action, parameters = {}) post = {} @@ -111,20 +114,28 @@ "#{month}#{year}" end def parse(body) - response_data = body.match(/<cngateway>(.*)<\/cngateway>/)[1] - Hash[CGI::parse(response_data).map{|k,v| [k.to_sym,v.first]}] + match = body.match(/<cngateway>(.*)<\/cngateway>/) + return nil unless match + + Hash[CGI::parse(match[1]).map{|k,v| [k.to_sym,v.first]}] end def handle_response(response) if (200...300).include?(response.code.to_i) return response.body elsif 302 == response.code.to_i return ssl_get(URI.parse(response['location'])) end raise ResponseError.new(response) + end + + def unparsable_response(raw_response) + message = "Unparsable response received from Cashnet. Please contact Cashnet if you continue to receive this message." + message += " (The raw response returned by the API was #{raw_response.inspect})" + return Response.new(false, message) end CASHNET_CODES = { '0' => 'Success', '1' => 'Invalid customer code, no customer code specified',