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, #status, #token

Instance Method Summary collapse

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_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
87
88
89
90
91
92
93
# File 'lib/mpower/checkout/redirect_invoice.rb', line 63

def confirm(token)
  result = http_get_request("#{MPower::Setup.checkout_confirm_base_url}#{token}")
  if result.size > 0
    case result["status"]
    when "completed"
      @status = result["status"]
      @customer = result["customer"]
      @items = result["invoice"]["items"]
      @taxes = result["invoice"]["taxes"]
      @description = result["invoice"]["description"]
      @custom_data = result["custom_data"]
      @total_amount = result["invoice"]["total_amount"]
      @receipt_url = result["receipt_url"]
      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
  else
    @response_text = "Invoice Not Found"
    @response_code = 1002
    @status = MPower::FAIL
    false
  end
end

#createObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mpower/checkout/redirect_invoice.rb', line 95

def create
  checkout_payload = {
    :invoice => {
      :items => @items,
      :taxes => @taxes,
      :total_amount => @total_amount,
      :description => description
    },
    :store => {
      :name => @store.name,
      :tagline => @store.tagline,
      :postal_address => @store.postal_address,
      :phone => @store.phone_number,
      :logo_url => @store.logo_url,
      :website_url => @store.website_url
    },
    :custom_data => @custom_data,
    :actions => {
      :cancel_url => @cancel_url,
      :return_url => @return_url
    }
  }

  result = http_json_request(MPower::Setup.checkout_base_url,checkout_payload)
  case result["response_code"]
  when "00"
    @token = result["token"]
    @response_text = result["response_description"]
    @response_code = result["response_code"]
    @invoice_url = result["response_text"]
    @status = MPower::SUCCESS
    true
  else
    @response_text = result["response_text"]
    @response_code = result["response_code"]
    @invoice_url = nil
    @status = MPower::FAIL
    false
  end
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
  hash_to_json @items
end

#get_taxesObject



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

def get_taxes
  hash_to_json @taxes
end