Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ pkg

# Bundler
Gemfile.lock

.ruby-version
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in new_gem.gemspec
gemspec
gemspec
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ A payment slip can have the following attributes:
* instruction_line_1, instruction_line_2, instruction_line_3: lines of instruction (up to 63 characters each), added to the payment slip.
* logo_url: an URL pointing to an image which will be added to the body of the payment slip.

### Consultation

``` ruby
# Create a consultation request with the moip transaction token
request = MyMoip::ConsultationRequest.new('U260F1E2P0G4N0Z2T1S0M4T3C5E4J5M8L5U0G0I0U0M0H0Y0E3X7S9X6I783')
request.api_call
# After api_call you can access the xml string response and parse it
Hash.from_xml(request.xml_str)
```

### Logger

The methods that make api calls to Moip, log request and response informations. The default logger is `Logger.new(STDOUT)`, but you can set the logger you want as `api_call` option. For instance:
Expand Down
1 change: 1 addition & 0 deletions lib/mymoip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def ensure_key_and_token_set!
$LOAD_PATH << "./lib/mymoip"

require File.dirname(__FILE__) + "/mymoip/validators.rb"
require File.dirname(__FILE__) + "/mymoip/params_mapper.rb"

files = Dir[File.dirname(__FILE__) + "/mymoip/*.rb"]
files.each { |f| require f }
Expand Down
28 changes: 28 additions & 0 deletions lib/mymoip/js_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module MyMoip
class JsResponse
PARAMS_MAPPER = {
'Codigo' => 'code',
'CodigoMoIP' => 'moip_code',
'Mensagem' => 'message',
'StatusPagamento' => 'payment_status',
'Status' => 'status',
'TaxaMoIP' => 'moip_tax_value',
'TotalPago' => 'total_paid_value',
'CodigoRetorno' => 'return_code',
'url' => 'url',
'Classificacao' => {
'classification' => { 'Codigo' => 'code', 'Descricao' => 'description' }
}
}

include MyMoip::ParamsMapper

def success?
payment_status.eql?('Sucesso')
end

def failed?
payment_status.eql?('Falha')
end
end
end
22 changes: 22 additions & 0 deletions lib/mymoip/nasp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module MyMoip
class Nasp
PARAMS_MAPPER = {
'id_transacao' => 'transaction_id',
'valor' => 'paid_value',
'status_pagamento' => 'status',
'cod_moip' => 'moip_code',
'forma_pagamento' => 'payment_method_code', # 1
'tipo_pagamento' => 'payment_method', # BoletoBancario
'parcelas' => 'installments',
'email_consumidor' => 'payer_mail',
'recebedor_login' => 'seller_mail',
'cartao_bin' => 'card_first_numbers',
'cartao_final' => 'card_last_numbers',
'cartao_bandeira' => 'credit_card_logo',
'cofre' => 'moip_lock_number',
'classificacao' => 'classification'
}

include MyMoip::ParamsMapper
end
end
26 changes: 26 additions & 0 deletions lib/mymoip/params_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module MyMoip
module ParamsMapper
def initialize(params)
self.class::PARAMS_MAPPER.values.each_with_index do |attribute, index|
value = params[self.class::PARAMS_MAPPER.key(attribute)]

if attribute.is_a?(Hash)
define_multiple_attributes(attribute, value); next
end

define_singleton_method attribute do value end
end
end

def define_multiple_attributes(hash, value)
method_name = hash.keys.first
mapped_methods = hash[method_name]

if value
value = OpenStruct.new(Hash[ value.map { |k, v| [mapped_methods[k], v] } ])
end

define_singleton_method method_name do value end
end
end
end
12 changes: 4 additions & 8 deletions lib/mymoip/payment_slip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,10 @@ def to_xml(root = nil)
root.DataVencimento(expiration_date.strftime('%Y-%m-%dT%H:%M:%S.%L%:z')) unless expiration_date.blank?

if expiration_days
type = nil
if expiration_days_type
if expiration_days_type == :business_day
type = {'Tipo' => 'Uteis'}
elsif expiration_days_type == :calendar_day
type = {'Tipo' => 'Corridos'}
end
end
type = {
business_day: {'Tipo' => 'Uteis'},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside { missing.
Space inside } missing.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

calendar_day: {'Tipo' => 'Corridos'}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside { missing.
Space inside } missing.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

}[expiration_days_type]

root.DiasExpiracao(expiration_days, type)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/mymoip/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def api_call(params, opts = {})
opts[:logger].debug "#{self.class} of ##{@id} with #{params.inspect}"

url = MyMoip.api_url + params.delete(:path)

params[:timeout] = opts[:timeout]
params[:basic_auth] = { username: opts[:username], password: opts[:password] }

@response = HTTParty.send params.delete(:http_method), url, params
Expand Down
52 changes: 52 additions & 0 deletions lib/mymoip/requests/consultation_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'active_support/core_ext/hash'

module MyMoip
class ConsultationRequest < Request

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body beginning.

HTTP_METHOD = :get
PATH = '/ws/alpha/ConsultarInstrucao'
REQUIRES_AUTH = true

def api_call(opts = {})
params = {
http_method: HTTP_METHOD,
requires_auth: REQUIRES_AUTH,
path: [PATH, id].join('/')
}

super(params, opts)
end

def response_hash
Hash.from_xml(@response.body)
end

def transactions
@transactions ||= get_transaction_params.map do |transaction_params|
MyMoip::Transaction.new(transaction_params)
end
end

def transaction(moip_code)
transaction_params = get_transaction_params.select {
|i| i.key(format_moip_code(moip_code))
}.first
MyMoip::Transaction.new(transaction_params)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body end.

private

def format_moip_code(moip_code)
moip_code.to_s.rjust(12, '0').scan(/\d{4}/).join('.')
end

def get_transaction_params
payments = response_hash['ConsultarTokenResponse']\
['RespostaConsultar']\
['Autorizacao']\
['Pagamento']

payments.is_a?(Hash) ? [payments] : payments
end
end
end
24 changes: 23 additions & 1 deletion lib/mymoip/requests/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ class PaymentRequest < Request
REQUIRES_AUTH = false
FORMAT = :json
PAYMENT_SLIP_PATH = "/Instrucao.do?token="
STATUSES = {
"Autorizado" => 1,
"Iniciado" => 2,
"BoletoImpresso" => 3,
"Concluido" => 4,
"Cancelado" => 5,
"EmAnalise" => 6,
"Estornado" => 7,
"Reembolsado" => 9
}

attr_reader :token

Expand Down Expand Up @@ -45,7 +55,19 @@ def url

def code
@response["CodigoMoIP"]
rescue NoMethodError => e
rescue NoMethodError
nil
end

def status
STATUSES[@response["Status"]]
rescue NoMethodError
nil
end

def total_payed
@response["TotalPago"]
rescue NoMethodError
nil
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body end.

Expand Down
21 changes: 21 additions & 0 deletions lib/mymoip/transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module MyMoip
class Transaction
PARAMS_MAPPER = {
'Data' => 'date', # 2012-04-02T10:45:44.000-03:00
'DataCredito' => 'credit_date', # 2012-04-16T00:00:00.000-03:00
'TotalPago' => 'total_paid_value', # 1.00
'TaxaParaPagador' => 'payer_tax_value', # 0.00
'TaxaMoIP' => 'moip_tax_value', # 0.46
'ValorLiquido' => 'equity_value', # 0.54
'FormaPagamento' => 'payment_method', # CartaoDeCredito
'InstituicaoPagamento' => 'payment_method_institution', # AmericanExpress
'Status' => 'status', # Autorizado
'CodigoMoIP' => 'moip_code', # 0000.2524.0547
'Parcela' => {
'installment' => { 'TotalParcelas' => 'number' }
}
}

include MyMoip::ParamsMapper
end
end
2 changes: 2 additions & 0 deletions mymoip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rdoc"
spec.add_development_dependency "vcr"
spec.add_development_dependency "webmock"
spec.add_development_dependency "minitest"
spec.add_development_dependency "pry"
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading