Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from ferblape/master
Browse files Browse the repository at this point in the history
Mejora en el manejo de reportes
  • Loading branch information
Ernesto Tagwerker committed Jan 15, 2012
2 parents da7c411 + 2bed573 commit 384f605
Show file tree
Hide file tree
Showing 13 changed files with 315 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .rvmrc
@@ -1 +1 @@
rvm use 1.9.2-p180@dm
rvm use 1.9.2-p180@dm --create
14 changes: 8 additions & 6 deletions Gemfile.lock
Expand Up @@ -2,17 +2,19 @@ PATH
remote: .
specs:
dinero_mail_ipn (0.1.0)
httparty (~> 0.7.6)
nokogiri (~> 1.4.4)
httparty (~> 0.8.1)
nokogiri (~> 1.5.0)

GEM
remote: http://rubygems.org/
specs:
crack (0.1.8)
fakeweb (1.3.0)
httparty (0.7.8)
crack (= 0.1.8)
nokogiri (1.4.7)
httparty (0.8.1)
multi_json
multi_xml
multi_json (1.0.4)
multi_xml (0.4.1)
nokogiri (1.5.0)
shoulda (2.11.3)

PLATFORMS
Expand Down
10 changes: 7 additions & 3 deletions README.markdown
Expand Up @@ -28,9 +28,13 @@ Ruby gem para consumir los metodos de IPN 1 y IPN 2.

### IPN v2
c = DineroMailIpn::Client.new(:account => '09813581', :password => 'mipassword)
c.consulta_transacciones("31548", "XA5547")
# Response
{"REPORTE"=>{"ESTADOREPORTE"=>"8", "DETALLE"=>{"OPERACIONES"=>nil}}}
report = c.consulta_transaccion("31548")
report.id
# 1
report.amount
# 2999
report.valid?
# true

## Roadmap

Expand Down
6 changes: 3 additions & 3 deletions dinero_mail_ipn.gemspec
Expand Up @@ -14,8 +14,8 @@ Gem::Specification.new do |s|

s.rubyforge_project = "dinero_mail_ipn"

s.add_dependency 'httparty', '~> 0.7.6'
s.add_dependency 'nokogiri', '~> 1.4.4'
s.add_dependency 'httparty', '~> 0.8.1'
s.add_dependency 'nokogiri', '~> 1.5.0'


s.files = `git ls-files`.split("\n")
Expand All @@ -25,7 +25,7 @@ Gem::Specification.new do |s|
]
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_development_dependency 'shoulda', '~> 2.11.3'
s.add_development_dependency 'fakeweb', '~> 1.3.0'
end
13 changes: 7 additions & 6 deletions lib/dinero_mail_ipn.rb
Expand Up @@ -11,7 +11,7 @@ class Client
attr_reader :email, :account, :pin, :pais, :password

DEFAULT_COUNTRY = 'argentina'

def initialize(opts)
@email = opts[:email]
@account = opts[:account]
Expand All @@ -29,7 +29,7 @@ def consulta_pago(start_date, end_date)
def default_params
{:XML => 1, :Acount => @account, :Pin => @pin, :Email => @email}
end

# formatea una date a 20110201
def format_date(a_date)
a_date.strftime("%Y%m%d")
Expand All @@ -44,8 +44,8 @@ def consulta_transacciones(*transacciones)
xml.CLAVE @password
xml.TIPO 1
xml.OPERACIONES {
transacciones.each do |transaccion|
xml.ID transaccion
transacciones.each do |transaction_id|
xml.ID transaction_id
end
}
}
Expand All @@ -56,8 +56,9 @@ def consulta_transacciones(*transacciones)
body.sub!("<?xml version=\"1.0\"?>", "")
body.gsub!(/\s/, '')

self.class.post("http://#{@pais}.dineromail.com/Vender/Consulta_IPN.asp", :body => "DATA=#{body}",
:headers => {"Content-type" => "application/x-www-form-urlencoded", "Content-length" => "#{body.length}" }).parsed_response
response = self.class.post("http://#{@pais}.dineromail.com/Vender/Consulta_IPN.asp", :body => "DATA=#{body}",
:headers => {"Content-type" => "application/x-www-form-urlencoded", "Content-length" => "#{body.length}" }).response.body
DineroMailIpn::Reporter.new(response)
end
end

Expand Down
32 changes: 32 additions & 0 deletions lib/dinero_mail_ipn/report.rb
@@ -0,0 +1,32 @@
# coding: UTF-8

module DineroMailIpn
class Report

# Transaction status list
PENDING_STATUS = 1
COMPLETED_STATUS = 2
CANCELLED_STATUS = 3

attr_reader :id, :amount

def initialize(attributes)
@id = attributes[:id]
@amount = attributes[:amount]
@state = attributes[:state]
end

def transaction_completed?
@state == COMPLETED_STATUS
end

def transaction_pending?
@state == PENDING_STATUS
end

def transaction_cancelled?
@state == CANCELLED_STATUS
end

end
end
39 changes: 39 additions & 0 deletions lib/dinero_mail_ipn/reporter.rb
@@ -0,0 +1,39 @@
# coding: UTF-8

module DineroMailIpn
class Reporter

# Report status list
VALID_REPORT_STATUS = 1
MALFORMED_REPORT_STATUS = 2
INVALID_ACCOUNT_NUMBER_REPORT_STATUS = 3
INVALID_PASSWORD_REPORT_STATUS = 4
INVALID_REQUEST_TYPE_STATUS = 5
INVALID_TRANSACTION_ID_REQUEST_STATUS = 6
INVALID_PASSWORD_OR_ACCOUNT_NUMBER_REQUEST_STATUS = 7
TRANSACTION_NOT_FOUND_REQUEST_STATUS = 8

attr_reader :reports

def initialize(xml_response)
@doc = Nokogiri::XML(xml_response.downcase)
@reports = build_reports
end

def valid?
@doc.xpath("//estadoreporte").first.content.to_i == VALID_REPORT_STATUS
end

private

def build_reports
return [] if @doc.xpath("//operacion").empty?
@doc.xpath("//operacion").map do |report_xml|
DineroMailIpn::Report.new :id => report_xml.xpath("//id").first.content.to_i,
:amount => report_xml.xpath("//monto").first.content.to_i,
:state => report_xml.xpath("//estado").first.content.to_i
end
end

end
end
50 changes: 50 additions & 0 deletions test/fixtures/ConsultaTransacciones1-2Success.xml
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<REPORTE>
<ESTADOREPORTE>1</ESTADOREPORTE>
<DETALLE>
<OPERACIONES>
<OPERACION>
<ID>1</ID>
<FECHA>12/29/2011 7:38:00 PM</FECHA>
<ESTADO>2</ESTADO>
<COMPRADOR>
<EMAIL>email@customer.com</EMAIL>
<DIRECCION/>
<COMENTARIO/>
</COMPRADOR>
<MONTO>1010</MONTO>
<MONTONETO>510</MONTONETO>
<METODOPAGO>3</METODOPAGO>
<ITEMS>
<ITEM>
<DESCRIPCION>Camiseta amarilla</DESCRIPCION>
<MONEDA>1</MONEDA>
<PRECIOUNITARIO>1010</PRECIOUNITARIO>
<CANTIDAD>1</CANTIDAD>
</ITEM>
</ITEMS>
</OPERACION>
<OPERACION>
<ID>2</ID>
<FECHA>12/30/2011 17:38:00 PM</FECHA>
<ESTADO>2</ESTADO>
<COMPRADOR>
<EMAIL>other-email@other-customer.com</EMAIL>
<DIRECCION/>
<COMENTARIO/>
</COMPRADOR>
<MONTO>2000</MONTO>
<MONTONETO>710</MONTONETO>
<METODOPAGO>3</METODOPAGO>
<ITEMS>
<ITEM>
<DESCRIPCION>Sombrero rojo</DESCRIPCION>
<MONEDA>1</MONEDA>
<PRECIOUNITARIO>1000</PRECIOUNITARIO>
<CANTIDAD>2</CANTIDAD>
</ITEM>
</ITEMS>
</OPERACION>
</OPERACIONES>
</DETALLE>
</REPORTE>
29 changes: 29 additions & 0 deletions test/fixtures/ConsultaTransacciones1Success.xml
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<REPORTE>
<ESTADOREPORTE>1</ESTADOREPORTE>
<DETALLE>
<OPERACIONES>
<OPERACION>
<ID>1</ID>
<FECHA>12/29/2011 7:38:00 PM</FECHA>
<ESTADO>2</ESTADO>
<COMPRADOR>
<EMAIL>email@customer.com</EMAIL>
<DIRECCION/>
<COMENTARIO/>
</COMPRADOR>
<MONTO>1010</MONTO>
<MONTONETO>510</MONTONETO>
<METODOPAGO>3</METODOPAGO>
<ITEMS>
<ITEM>
<DESCRIPCION>Camiseta amarilla</DESCRIPCION>
<MONEDA>1</MONEDA>
<PRECIOUNITARIO>1010</PRECIOUNITARIO>
<CANTIDAD>1</CANTIDAD>
</ITEM>
</ITEMS>
</OPERACION>
</OPERACIONES>
</DETALLE>
</REPORTE>
23 changes: 15 additions & 8 deletions test/helper.rb
@@ -1,6 +1,7 @@
require 'test/unit'
require 'shoulda'
require 'fakeweb'
require File.expand_path('../../lib/dinero_mail_ipn', __FILE__)

FakeWeb.allow_net_connect = false

Expand All @@ -10,15 +11,21 @@ def fixture_file(filename)
File.read(file_path)
end

def dinero_mail_url(url)
url =~ /^https/ ? url : "https://argentina.dineromail.com#{url}"
def dinero_mail_url(path, options = {})
options[:https] = true if options[:https].nil?
options[:country] ||= 'argentina'
protocol = (options[:https] == true) ? 'https' : 'http'
"#{protocol}://#{options[:country]}.dineromail.com#{path}"
end

def stub_get(url, filename, options={})
def stub_get(path, filename, options={})
opts = {:body => fixture_file(filename)}.merge(options)

headers = {
}

FakeWeb.register_uri(:get, dinero_mail_url(url), headers.merge(opts))

FakeWeb.register_uri(:get, dinero_mail_url(path), opts)
end

def stub_post(path, filename, options={})
opts = {:body => fixture_file(filename)}.merge(options)

FakeWeb.register_uri(:post, dinero_mail_url(path, options[:url]), opts)
end
45 changes: 36 additions & 9 deletions test/test_dinero_mail_ipn.rb
@@ -1,29 +1,56 @@
# encoding: utf-8

require 'helper'
require 'dinero_mail_ipn'
require File.expand_path('../helper', __FILE__)

class TestDineroMailIpn < Test::Unit::TestCase

context "when I query the Dinero Mail API via GET" do

should "return OK for the query with valid Pin and Account parameters" do
stub_get("/vender/ConsultaPago.asp?XML=1&Acount=17128254&Pin=AYCN7IXDTM&Email=ernesto%40ombushop.com&StartDate=20110702&EndDate=20110703",
stub_get("/vender/ConsultaPago.asp?XML=1&Acount=17128254&Pin=AYCN7IXDTM&Email=ernesto%40ombushop.com&StartDate=20110702&EndDate=20110703",
"ConsultaPagoOkWithoutCollections.xml")
client = DineroMailIpn::Client.new(:email => 'ernesto@ombushop.com', :account => '17128254', :pin => 'AYCN7IXDTM')
response = client.consulta_pago(Date.new(2011,7,2), Date.new(2011,7,3))
assert_equal response.state, "1"
assert_equal response.state_description, "La consulta se realizó correctamente"
end
should "return Error for the query" do
stub_get("/vender/ConsultaPago.asp?XML=1&Acount=123&Pin=UasdM&Email=ernesto%40ombushop.com&StartDate=20110702&EndDate=20110703",
"ConsultaPagoErrorBadCredentials.xml")

should "return Error for the query" do
stub_get("/vender/ConsultaPago.asp?XML=1&Acount=123&Pin=UasdM&Email=ernesto%40ombushop.com&StartDate=20110702&EndDate=20110703",
"ConsultaPagoErrorBadCredentials.xml")
client = DineroMailIpn::Client.new(:email => 'ernesto@ombushop.com', :account => '123', :pin => 'UasdM')
response = client.consulta_pago(Date.new(2011,7,2), Date.new(2011,7,3))
assert_equal response.state, "2"
assert_equal response.state_description, "Los valores son incorrectos para realizar la consulta"
end

end

context "when I query for some transactions" do

should "return a new report object" do
stub_post("/Vender/Consulta_IPN.asp", "ConsultaTransacciones1-2Success.xml",
{:url => {:https => false}})

client = DineroMailIpn::Client.new(:password => 'fuckingipnpasswordinplaintext', :account => '17128254')
reporter = client.consulta_transacciones(1,2)
assert reporter.kind_of?(DineroMailIpn::Reporter)
assert_equal 2, reporter.reports.size
end

end

context "when I query for a transactions" do

should "return a new report object" do
stub_post("/Vender/Consulta_IPN.asp", "ConsultaTransacciones1Success.xml",
{:url => {:https => false}})

client = DineroMailIpn::Client.new(:password => 'fuckingipnpasswordinplaintext', :account => '17128254')
reporter = client.consulta_transacciones(1)
assert reporter.kind_of?(DineroMailIpn::Reporter)
assert_equal 1, reporter.reports.size
end

end
end

0 comments on commit 384f605

Please sign in to comment.