Class: MPower::Checkout::Invoice

Inherits:
Core
  • Object
show all
Defined in:
lib/mpower/checkout/redirect_invoice.rb

Direct Known Subclasses

Onsite::Invoice

Instance Attribute Summary collapse

Attributes inherited from Core

#response_code, #response_text, #result, #status, #token, #transaction_id

Instance Method Summary collapse

Methods inherited from Core

#push_results

Methods included from Utilities

#hash_to_json, #http_get_request, #http_json_request, #json_to_hash

Constructor Details

#initializeInvoice

Returns a new instance of Invoice.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mpower/checkout/redirect_invoice.rb', line 8

def initialize
  @items = {}
  @taxes = {}
  @custom_data = {}
  @customer = {}
  @total_amount = 0.0
  @currency = "ghs"
  @store = MPower::Checkout::Store
  @return_url = @store.return_url
  @cancel_url = @store.cancel_url
end

Instance Attribute Details

#cancel_urlObject

Returns the value of attribute cancel_url.



6
7
8
# File 'lib/mpower/checkout/redirect_invoice.rb', line 6

def cancel_url
  @cancel_url
end

#currencyObject

Returns the value of attribute currency.



5
6
7
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5

def currency
  @currency
end

#custom_dataObject

Returns the value of attribute custom_data.



6
7
8
# File 'lib/mpower/checkout/redirect_invoice.rb', line 6

def custom_data
  @custom_data
end

#customerObject

Returns the value of attribute customer.



6
7
8
# File 'lib/mpower/checkout/redirect_invoice.rb', line 6

def customer
  @customer
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5

def description
  @description
end

#invoice_tokenObject

Returns the value of attribute invoice_token.



5
6
7
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5

def invoice_token
  @invoice_token
end

#invoice_urlObject

Returns the value of attribute invoice_url.



6
7
8
# File 'lib/mpower/checkout/redirect_invoice.rb', line 6

def invoice_url
  @invoice_url
end

#itemsObject

Returns the value of attribute items.



5
6
7
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5

def items
  @items
end

#receipt_urlObject

Returns the value of attribute receipt_url.



6
7
8
# File 'lib/mpower/checkout/redirect_invoice.rb', line 6

def receipt_url
  @receipt_url
end

#return_urlObject

Returns the value of attribute return_url.



6
7
8
# File 'lib/mpower/checkout/redirect_invoice.rb', line 6

def return_url
  @return_url
end

#storeObject

Returns the value of attribute store.



5
6
7
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5

def store
  @store
end

#taxesObject

Returns the value of attribute taxes.



5
6
7
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5

def taxes
  @taxes
end

#total_amountObject

Returns the value of attribute total_amount.



5
6
7
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5

def total_amount
  @total_amount
end

Instance Method Details

#add_custom_data(key, value) ⇒ Object



43
44
45
# File 'lib/mpower/checkout/redirect_invoice.rb', line 43

def add_custom_data(key,value)
  @custom_data["#{key}"] = value
end

#add_item(name, quantity, unit_price, total_price, description = "") ⇒ Object

Adds invoice items to the @items hash, the idea is to allow this function to be used in a loop



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mpower/checkout/redirect_invoice.rb', line 21

def add_item(name,quantity,unit_price,total_price,description="")
  @items.merge!({
    :"item_#{@items.size}" => {
      :name => name,
      :quantity => quantity,
      :unit_price => unit_price,
      :total_price => total_price,
      :description => description
    }
  })
end

#add_tax(name, amount) ⇒ Object

Adds invoice tax to the @taxes hash, the idea is to allow this function to be used in a loop



34
35
36
37
38
39
40
41
# File 'lib/mpower/checkout/redirect_invoice.rb', line 34

def add_tax(name,amount)
  @taxes.merge!({
    :"tax_#{@taxes.size}" => {
      :name => name,
      :amount => amount
    }
  })
end

#confirm(token) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mpower/checkout/redirect_invoice.rb', line 63

def confirm(token)
  result = http_get_request("#{MPower::Setup.checkout_confirm_base_url}#{token}")
  unless result.size > 0
    @response_text = "Invoice Not Found"
    @response_code = 1002
    @status = MPower::FAIL
    return false
  end

  if result["status"] == "completed"
    rebuild_invoice(result)
    @response_text = result["response_text"]
    true
  else
    @status = result["status"]
    @items = result["invoice"]["items"]
    @taxes = result["invoice"]["taxes"]
    @description = result["invoice"]["description"]
    @custom_data = result["custom_data"]
    @total_amount = result["invoice"]["total_amount"]
    @response_text = "Invoice status is #{result['status'].upcase}"
    false
  end
end

#createObject



88
89
90
91
# File 'lib/mpower/checkout/redirect_invoice.rb', line 88

def create
  result = http_json_request(MPower::Setup.checkout_base_url,build_invoice_payload)
  create_response(result)
end

#get_custom_data(key) ⇒ Object



59
60
61
# File 'lib/mpower/checkout/redirect_invoice.rb', line 59

def get_custom_data(key)
  @custom_data["#{key}"]
end

#get_customer_info(key) ⇒ Object



55
56
57
# File 'lib/mpower/checkout/redirect_invoice.rb', line 55

def get_customer_info(key)
  @customer["#{key}"]
end

#get_itemsObject



47
48
49
# File 'lib/mpower/checkout/redirect_invoice.rb', line 47

def get_items
  @items
end

#get_taxesObject



51
52
53
# File 'lib/mpower/checkout/redirect_invoice.rb', line 51

def get_taxes
  @taxes
end