Class: MPower::Checkout::Invoice
- Defined in:
- lib/mpower/checkout/redirect_invoice.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cancel_url ⇒ Object
Returns the value of attribute cancel_url.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#custom_data ⇒ Object
Returns the value of attribute custom_data.
-
#customer ⇒ Object
Returns the value of attribute customer.
-
#description ⇒ Object
Returns the value of attribute description.
-
#invoice_url ⇒ Object
Returns the value of attribute invoice_url.
-
#items ⇒ Object
Returns the value of attribute items.
-
#receipt_url ⇒ Object
Returns the value of attribute receipt_url.
-
#return_url ⇒ Object
Returns the value of attribute return_url.
-
#store ⇒ Object
Returns the value of attribute store.
-
#taxes ⇒ Object
Returns the value of attribute taxes.
-
#total_amount ⇒ Object
Returns the value of attribute total_amount.
Attributes inherited from Core
#response_code, #response_text, #status, #token
Instance Method Summary collapse
- #add_custom_data(key, value) ⇒ Object
-
#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.
-
#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.
- #confirm(token) ⇒ Object
- #create ⇒ Object
- #get_custom_data(key) ⇒ Object
- #get_customer_info(key) ⇒ Object
- #get_items ⇒ Object
- #get_taxes ⇒ Object
-
#initialize ⇒ Invoice
constructor
A new instance of Invoice.
Methods included from Utilities
#hash_to_json, #http_get_request, #http_json_request, #json_to_hash
Constructor Details
#initialize ⇒ Invoice
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_url ⇒ Object
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 |
#currency ⇒ Object
Returns the value of attribute currency.
5 6 7 |
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5 def currency @currency end |
#custom_data ⇒ Object
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 |
#customer ⇒ Object
Returns the value of attribute customer.
6 7 8 |
# File 'lib/mpower/checkout/redirect_invoice.rb', line 6 def customer @customer end |
#description ⇒ Object
Returns the value of attribute description.
5 6 7 |
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5 def description @description end |
#invoice_url ⇒ Object
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 |
#items ⇒ Object
Returns the value of attribute items.
5 6 7 |
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5 def items @items end |
#receipt_url ⇒ Object
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_url ⇒ Object
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 |
#store ⇒ Object
Returns the value of attribute store.
5 6 7 |
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5 def store @store end |
#taxes ⇒ Object
Returns the value of attribute taxes.
5 6 7 |
# File 'lib/mpower/checkout/redirect_invoice.rb', line 5 def taxes @taxes end |
#total_amount ⇒ Object
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 |
#create ⇒ Object
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_items ⇒ Object
47 48 49 |
# File 'lib/mpower/checkout/redirect_invoice.rb', line 47 def get_items hash_to_json @items end |
#get_taxes ⇒ Object
51 52 53 |
# File 'lib/mpower/checkout/redirect_invoice.rb', line 51 def get_taxes hash_to_json @taxes end |