From de86dc69ef67c32a6fead56304455016d008dc8e Mon Sep 17 00:00:00 2001 From: Joshua Krall Date: Sun, 12 Feb 2012 16:38:39 -0600 Subject: [PATCH] Add content-type header to requests, Changed Connection post/put body to be raw xml so that it isn't sent as url-encoded params Added spec to test for Ronin::ResourceNotFound failure --- lib/ronin/connection.rb | 10 +++++----- lib/ronin/gateway.rb | 2 +- lib/ronin/models/payment_method.rb | 4 ++-- lib/ronin/models/processor.rb | 2 +- lib/ronin/models/transaction.rb | 2 +- spec/integration/processor_spec.rb | 6 ++++++ 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/ronin/connection.rb b/lib/ronin/connection.rb index fe0d5d8..11bde3c 100644 --- a/lib/ronin/connection.rb +++ b/lib/ronin/connection.rb @@ -2,17 +2,17 @@ module Ronin::Connection private def get(uri, id) - request = HTTParty::Request.new(Net::HTTP::Get, "#{self.gateway.site}#{uri}/#{id}.xml", :format => :xml, :basic_auth => self.gateway.merchant_auth) + request = HTTParty::Request.new(Net::HTTP::Get, "#{self.gateway.site}#{uri}/#{id}.xml", :headers => {'content-type' => 'text/xml'}, :format => :xml, :basic_auth => self.gateway.merchant_auth) request.perform end - def post(uri, params) - request = HTTParty::Request.new(Net::HTTP::Post, "#{self.gateway.site}#{uri}.xml", :body => params, :format => :xml, :basic_auth => self.gateway.merchant_auth) + def post(uri, xml) + request = HTTParty::Request.new(Net::HTTP::Post, "#{self.gateway.site}#{uri}.xml", :headers => {'content-type' => 'text/xml'}, :body => xml, :format => :xml, :basic_auth => self.gateway.merchant_auth) request.perform end - def put(uri, id, params) - request = HTTParty::Request.new(Net::HTTP::Put, "#{self.gateway.site}#{uri}/#{id}.xml", :body => params, :format => :xml, :basic_auth => self.gateway.merchant_auth) + def put(uri, id, xml) + request = HTTParty::Request.new(Net::HTTP::Put, "#{self.gateway.site}#{uri}/#{id}.xml", :headers => {'content-type' => 'text/xml'}, :body => xml, :format => :xml, :basic_auth => self.gateway.merchant_auth) request.perform end diff --git a/lib/ronin/gateway.rb b/lib/ronin/gateway.rb index 033f2d0..c144b98 100644 --- a/lib/ronin/gateway.rb +++ b/lib/ronin/gateway.rb @@ -14,7 +14,7 @@ def initialize(options={}) end def create_payment_method(params={}) - response = post('payment_methods', :payment_method => params) + response = post('payment_methods', params.to_xml(:root=>'payment_method')) process_response(Ronin::PaymentMethod, 'payment_method', response) end diff --git a/lib/ronin/models/payment_method.rb b/lib/ronin/models/payment_method.rb index e1a894b..b20ba37 100644 --- a/lib/ronin/models/payment_method.rb +++ b/lib/ronin/models/payment_method.rb @@ -11,14 +11,14 @@ def token end def update(params={}) - response = put('payment_methods', self.token, :payment_method => params) + response = put('payment_methods', self.token, params.to_xml(:root=>'payment_method')) process_response(Ronin::PaymentMethod, 'payment_method', response) end private def payment_method(method) - response = post("payment_methods/#{token}/#{method}", {}) + response = post("payment_methods/#{token}/#{method}", '') process_response(Ronin::PaymentMethod, 'payment_method', response) end end diff --git a/lib/ronin/models/processor.rb b/lib/ronin/models/processor.rb index 1323094..0e4a7bd 100644 --- a/lib/ronin/models/processor.rb +++ b/lib/ronin/models/processor.rb @@ -26,7 +26,7 @@ def purchase(payment_method_token, amount, params={}) def create_transaction(payment_method_token, amount, method, params={}) transaction_params = params.merge(:payment_method_token => payment_method_token, :amount => amount) - response = post("processors/#{token}/#{method}", :transaction => transaction_params) + response = post("processors/#{token}/#{method}", transaction_params.to_xml(:root=>'transaction')) process_response(Ronin::Transaction, 'transaction', response) end end diff --git a/lib/ronin/models/transaction.rb b/lib/ronin/models/transaction.rb index 454728f..3736f9b 100644 --- a/lib/ronin/models/transaction.rb +++ b/lib/ronin/models/transaction.rb @@ -38,7 +38,7 @@ def reverse(amount = self.amount) def transaction_method(method, amount) transaction_params = {:amount => amount} - response = post("transactions/#{self.token}/#{method}", :transaction => transaction_params) + response = post("transactions/#{self.token}/#{method}", transaction_params.to_xml(:root=>'transaction')) process_response(Ronin::Transaction, 'transaction', response) end end diff --git a/spec/integration/processor_spec.rb b/spec/integration/processor_spec.rb index 6b029c7..6a6363c 100644 --- a/spec/integration/processor_spec.rb +++ b/spec/integration/processor_spec.rb @@ -51,6 +51,12 @@ purchase.success.should be_false purchase.errors['input.amount'].should == [ 'The transaction amount was invalid.' ] end + + it 'should raise Ronin::ResourceNotFound on invalid token' do + lambda { + @processor.purchase('bad_token', 1.10, :billing_reference=>rand(1000)) + }.should raise_error(Ronin::ResourceNotFound, "Couldn't find PaymentMethod with token = bad_token") + end end describe 'cvv responses' do