Class: MonopayRuby::Invoices::SimpleInvoice

Inherits:
Base
  • Object
show all
Defined in:
lib/monopay-ruby/invoices/simple_invoice.rb

Constant Summary collapse

API_CREATE_INVOICE_URL =
"#{API_URL}/merchant/invoice/create".freeze
DEFAULT_CURRENCY =
"UAH".freeze
PAGE_URL_KEY =
"pageUrl".freeze
INVOICE_ID_KEY =
"invoiceId".freeze

Constants inherited from Base

Base::API_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redirect_url: nil, webhook_url: nil) ⇒ SimpleInvoice

Initialize SimpleInvoice

Parameters:

  • redirect_url (String) (defaults to: nil)
    • url where user will be redirected after payment

  • webhook_url (String) (defaults to: nil)
    • url where Monobank will send webhook after payment



20
21
22
23
24
25
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 20

def initialize(redirect_url: nil, webhook_url: nil)
  @redirect_url = redirect_url
  @webhook_url = webhook_url

  @error_messages = []
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



7
8
9
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 7

def amount
  @amount
end

#destinationObject (readonly)

Returns the value of attribute destination.



7
8
9
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 7

def destination
  @destination
end

#error_messagesObject (readonly)

Returns the value of attribute error_messages.



7
8
9
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 7

def error_messages
  @error_messages
end

#invoice_idObject (readonly)

Returns the value of attribute invoice_id.



7
8
9
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 7

def invoice_id
  @invoice_id
end

#page_urlObject (readonly)

Returns the value of attribute page_url.



7
8
9
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 7

def page_url
  @page_url
end

#redirect_urlObject (readonly)

Returns the value of attribute redirect_url.



7
8
9
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 7

def redirect_url
  @redirect_url
end

#referenceObject (readonly)

Returns the value of attribute reference.



7
8
9
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 7

def reference
  @reference
end

#webhook_urlObject (readonly)

Returns the value of attribute webhook_url.



7
8
9
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 7

def webhook_url
  @webhook_url
end

Instance Method Details

#create(amount, destination: nil, reference: nil) ⇒ Boolean

Create invoice

Parameters:

  • amount (BigDecimal, Integer)

    in UAH (cents) to request payment

  • destination (String) (defaults to: nil)
    • additional info about payment

  • reference (String) (defaults to: nil)
    • bill number or other reference

Returns:

  • (Boolean)

    true if invoice was created successfully, false otherwise



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/monopay-ruby/invoices/simple_invoice.rb', line 33

def create(amount, destination: nil, reference: nil)
  begin
    @amount = convert_to_cents(amount)
    @destination = destination
    @reference = reference

    response = RestClient.post(API_CREATE_INVOICE_URL, request_body, headers)
    response_body = JSON.parse(response.body)

    @page_url = JSON.parse(response.body)[PAGE_URL_KEY]
    @invoice_id = JSON.parse(response.body)[INVOICE_ID_KEY]

    true
  rescue Exception => e
    response_body = JSON.parse(e.response.body)
    @error_messages << [e.message, response_body].join(", ")
    # TODO: use errors and full_messages like rails
    # TODO: use logger to log errors or successful invoice creation

    false
  end
end