Class: Zaypay::PriceSetting
- Inherits:
-
Object
- Object
- Zaypay::PriceSetting
- Includes:
- HTTParty
- Defined in:
- lib/zaypay/price_setting.rb
Overview
PriceSetting instances allows you to communicate to the Zaypay platform.
It is basically a Ruby wrapper for the Zaypay-API, which provides you a bunch of payment-related methods, as well as some utility methods.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#payment_method_id ⇒ Object
Returns the value of attribute payment_method_id.
-
#price_setting_id ⇒ Object
readonly
Returns the value of attribute price_setting_id.
Instance Method Summary collapse
-
#country_has_been_configured_for_ip(ip, options = {}) ⇒ Hash
Returns a country as a Hash, if the country of the given IP has been configured for your Price Setting.
-
#create_payment(options = {}) ⇒ Hash
Creates a payment on the Zaypay platform.
-
#initialize(price_setting_id = nil, api_key = nil) ⇒ PriceSetting
constructor
Creates instances of Zaypay::PriceSetting.
-
#list_countries(options = {}) ⇒ Array
Returns an array of countries that are available to your Price Setting.
-
#list_languages(options = {}) ⇒ Array
Returns an array of languages that are available to your Price Setting.
-
#list_locales(options = {}) ⇒ Hash
Returns a hash containing the countries and languages that are available to your Price Setting.
-
#list_payment_methods(options = {}) ⇒ Array
Returns an array of payment methods that are available to your Price Setting with a given locale.
-
#locale_for_ip(ip) ⇒ Hash
Returns the default locale as a hash for a given ip_address.
-
#locale_string_for_ip(ip) ⇒ String
Returns the default locale as a string for a given ip_address, with the first part representing the language, the second part the country.
-
#mark_payload_provided(payment_id) ⇒ Hash
Posts a request to the Zaypay platform to mark that you have delivered the ‘goodies’ to your customer.
-
#show_payment(payment_id) ⇒ Hash
Returns the specified payment as a hash.
-
#verification_code(payment_id, verification_code) ⇒ Hash
Submits a verification code to the Zaypay platform.
Constructor Details
#initialize(price_setting_id = nil, api_key = nil) ⇒ PriceSetting
Creates instances of Zaypay::PriceSetting.
To instantiate, one must provide a PriceSetting-id and its API-Key. You can obtain these information once you have created a PriceSetting on the Zaypay platform (www.zaypay.com).
You can also call the “one-arg” or the “no-args” version of the initializer, but to do that, you must first create config/zaypay.yml in your Rails app, see the README file.
25 26 27 28 |
# File 'lib/zaypay/price_setting.rb', line 25 def initialize(price_setting_id=nil, api_key=nil) @price_setting_id, @api_key = price_setting_id, api_key select_settings end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, url, extra_query_string = {}) ⇒ Object (protected)
221 222 223 224 225 226 227 228 229 |
# File 'lib/zaypay/price_setting.rb', line 221 def method_missing(method, url, extra_query_string={}) super unless [:get, :post, :put, :delete].include?(method) response = self.class.send(method, ('https://secure.zaypay.com' + url), {:query => default_query.merge!(extra_query_string), :headers => {'Accept' => 'application/xml' } }) Zaypay::Util.uber_symbolize(response) check response block_given? ? yield(response[:response]) : response[:response] end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/zaypay/price_setting.rb', line 9 def api_key @api_key end |
#locale ⇒ Object
Returns the value of attribute locale.
10 11 12 |
# File 'lib/zaypay/price_setting.rb', line 10 def locale @locale end |
#payment_method_id ⇒ Object
Returns the value of attribute payment_method_id.
10 11 12 |
# File 'lib/zaypay/price_setting.rb', line 10 def payment_method_id @payment_method_id end |
#price_setting_id ⇒ Object (readonly)
Returns the value of attribute price_setting_id.
9 10 11 |
# File 'lib/zaypay/price_setting.rb', line 9 def price_setting_id @price_setting_id end |
Instance Method Details
#country_has_been_configured_for_ip(ip, options = {}) ⇒ Hash
Returns a country as a Hash, if the country of the given IP has been configured for your Price Setting.
If the country of the given IP has been configured for this Price Setting, it returns a hash with :country and :locale subhashes, else it returns nil.
89 90 91 92 93 94 |
# File 'lib/zaypay/price_setting.rb', line 89 def country_has_been_configured_for_ip(ip, ={}) # options can take a :amount key locale = locale_for_ip(ip) country = list_countries().select{ |c| c.has_value? locale[:country] }.first {:country => country, :locale => locale} if country end |
#create_payment(options = {}) ⇒ Hash
Creates a payment on the Zaypay platform.
You can provide an options-hash, which will add additional data to your payment. The following keys have special functionalities:
:amount # Enables dynamic pricing. It must be an integer representing the price in cents.
:payalogue_id # Adds the URL of the payalogue specified to your payment as :payalogue_url.
Any other keys will be added to a key named :your_variables, which can be used for your future reference. Please check the README for the structure of the payment returned.
Example:
@price_setting.create_payment(:payalogue_id => payalogue_id, :amount => optional_amount, :my_variable_1 => "value_1", :my_variable_2 => "value_2")
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/zaypay/price_setting.rb', line 154 def create_payment(={}) raise Zaypay::Error.new(:locale_not_set, "locale was not set for your price setting") if @locale.nil? raise Zaypay::Error.new(:payment_method_id_not_set, "payment_method_id was not set for your price setting") if @payment_method_id.nil? query = {:payment_method_id => payment_method_id} query.merge!() amount = query.delete(:amount) post "/#{amount}/#{@locale}/pay/#{price_setting_id}/payments", query do |data| payment_hash data end end |
#list_countries(options = {}) ⇒ Array
Returns an array of countries that are available to your Price Setting.
111 112 113 114 115 |
# File 'lib/zaypay/price_setting.rb', line 111 def list_countries(={}) get "/#{[:amount]}/pay/#{price_setting_id}/list_locales" do |data| Zaypay::Util.arrayify_if_not_an_array(data[:countries][:country]) end end |
#list_languages(options = {}) ⇒ Array
Returns an array of languages that are available to your Price Setting.
121 122 123 124 125 |
# File 'lib/zaypay/price_setting.rb', line 121 def list_languages(={}) get "/#{[:amount]}/pay/#{price_setting_id}/list_locales" do |data| data[:languages][:language] end end |
#list_locales(options = {}) ⇒ Hash
Returns a hash containing the countries and languages that are available to your Price Setting.
100 101 102 103 104 105 |
# File 'lib/zaypay/price_setting.rb', line 100 def list_locales(={}) get "/#{[:amount]}/pay/#{price_setting_id}/list_locales" do |data| {:countries => Zaypay::Util.arrayify_if_not_an_array(data[:countries][:country]), :languages => data[:languages][:language]} end end |
#list_payment_methods(options = {}) ⇒ Array
Returns an array of payment methods that are available to your Price Setting with a given locale
132 133 134 135 136 137 |
# File 'lib/zaypay/price_setting.rb', line 132 def list_payment_methods(={}) raise Zaypay::Error.new(:locale_not_set, "locale was not set for your price setting") if @locale.nil? get "/#{[:amount]}/#{@locale}/pay/#{price_setting_id}/payments/new" do |data| Zaypay::Util.arrayify_if_not_an_array(data[:payment_methods][:payment_method]) end end |
#locale_for_ip(ip) ⇒ Hash
Returns the default locale as a hash for a given ip_address.
It is similar to #locale_string_for_ip, except that this method returns the locale as a hash This method comes in handy when you want to preselect only the langauge or the country for your customer
Example:
# We take an ip-address from Great Britain for example:
ip = "212.58.226.75"
@price_setting.locale_string_for_ip(ip)
=> { :country => 'GB', :language => 'en' }
76 77 78 79 80 81 |
# File 'lib/zaypay/price_setting.rb', line 76 def locale_for_ip(ip) get "/#{ip}/pay/#{price_setting_id}/locale_for_ip" do |data| parts = data[:locale].split('-') {:country => parts[1], :language => parts[0]} end end |
#locale_string_for_ip(ip) ⇒ String
Returns the default locale as a string for a given ip_address, with the first part representing the language, the second part the country
This method comes in handy when you want to preselect the language and country when your customer creates a payment on your website.
Example:
# We take an ip-address from Great Britain for example:
ip = "212.58.226.75"
@price_setting.locale_string_for_ip(ip)
=> 'en-GB'
Also see #locale_for_ip
56 57 58 59 60 61 |
# File 'lib/zaypay/price_setting.rb', line 56 def locale_string_for_ip(ip) get "/#{ip}/pay/#{price_setting_id}/locale_for_ip" do |data| parts = data[:locale].split('-') Zaypay::Util.stringify_locale_hash({:country => parts[1], :language => parts[0]}) end end |
#mark_payload_provided(payment_id) ⇒ Hash
Posts a request to the Zaypay platform to mark that you have delivered the ‘goodies’ to your customer.
Please refer to README for more details.
194 195 196 197 198 |
# File 'lib/zaypay/price_setting.rb', line 194 def mark_payload_provided(payment_id) post "///pay/#{price_setting_id}/payments/#{payment_id}/mark_payload_provided" do |data| payment_hash data end end |
#show_payment(payment_id) ⇒ Hash
Returns the specified payment as a hash.
169 170 171 172 173 |
# File 'lib/zaypay/price_setting.rb', line 169 def show_payment(payment_id) get "///pay/#{price_setting_id}/payments/#{payment_id}" do |data| payment_hash data end end |
#verification_code(payment_id, verification_code) ⇒ Hash
Submits a verification code to the Zaypay platform.
In some countries, the end-user must submit a verification code in order to complete a payment. Please refer to the README for more details
182 183 184 185 186 |
# File 'lib/zaypay/price_setting.rb', line 182 def verification_code(payment_id, verification_code) post "///pay/#{price_setting_id}/payments/#{payment_id}/verification_code", :verification_code => verification_code do |data| payment_hash data end end |