Class: Moiper::Payment

Inherits:
Object
  • Object
show all
Defined in:
lib/moiper/payment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Payment

Create a new Moip Payment request

Parameters:

  • options (Hash) (defaults to: {})

    the options to create the payment with

Options Hash (options):

Raises:

  • (ArgumentError)

    if description or price options are blank



40
41
42
43
44
45
46
47
# File 'lib/moiper/payment.rb', line 40

def initialize(options = {})
  raise ArgumentError, "You must inform a description" if options[:description].nil? || options[:description].empty?
  raise ArgumentError, "You must inform a price" if options[:price].nil? || options[:price].to_f <= 0

  options.each do |attribute, value|
    send "#{attribute}=", value
  end
end

Instance Attribute Details

#accretionFloat

Returns the amount to be increased from the final price.

Returns:

  • (Float)

    the amount to be increased from the final price



16
17
18
# File 'lib/moiper/payment.rb', line 16

def accretion
  @accretion
end

#deductionFloat

Returns the amount to be deducted from the final price.

Returns:

  • (Float)

    the amount to be deducted from the final price



19
20
21
# File 'lib/moiper/payment.rb', line 19

def deduction
  @deduction
end

#descriptionString

Returns the description of the purchase.

Returns:

  • (String)

    the description of the purchase



10
11
12
# File 'lib/moiper/payment.rb', line 10

def description
  @description
end

#idString

Returns the unique ID you can set for this payment. This is usefull to keep track of which payment we will receive on the Moip’s notification process.

Returns:

  • (String)

    the unique ID you can set for this payment. This is usefull to keep track of which payment we will receive on the Moip’s notification process



7
8
9
# File 'lib/moiper/payment.rb', line 7

def id
  @id
end

#notification_urlString

Returns the URL where Moip will send notifications about your purchase updates.

Returns:

  • (String)

    the URL where Moip will send notifications about your purchase updates



26
27
28
# File 'lib/moiper/payment.rb', line 26

def notification_url
  @notification_url
end

#priceFloat

Returns the amount to be billed from the user.

Returns:

  • (Float)

    the amount to be billed from the user



13
14
15
# File 'lib/moiper/payment.rb', line 13

def price
  @price
end

#return_urlString

Returns the URL where the user will be redirected after he finishes the payment process.

Returns:

  • (String)

    the URL where the user will be redirected after he finishes the payment process.



23
24
25
# File 'lib/moiper/payment.rb', line 23

def return_url
  @return_url
end

Instance Method Details

#checkoutResponse

Send a new payment request to Moip

Returns:



75
76
77
78
# File 'lib/moiper/payment.rb', line 75

def checkout
  request = Request.new
  request.process(to_xml)
end

#to_xmlString

Create the payment XML representation

Returns:

  • (String)

    Moip’s formatted XML



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/moiper/payment.rb', line 51

def to_xml
  builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
    xml.EnviarInstrucao {
      xml.InstrucaoUnica {
        xml.Razao description
        xml.IdProprio id if id

        xml.Valores {
          xml.Valor price, :moeda => "BRL"
          xml.Acrescimo accretion, :moeda => "BRL" if accretion
          xml.Deducao deduction, :moeda => "BRL" if deduction
        }

        xml.URLNotificacao notification_url if notification_url
        xml.URLRetorno return_url if return_url
      }
    }
  end

  builder.to_xml
end