zaypay 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/zaypay/error.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Zaypay
2
+ # Errors that can be raised by the Zaypay gem
2
3
  class Error < StandardError
3
- def initialize(type = nil, message = "default message")
4
+ def initialize(type = nil, message = "Error thrown by the Zaypay gem.")
4
5
  @type = type
5
6
  super(message)
6
7
  end
@@ -1,40 +1,102 @@
1
1
  module Zaypay
2
2
  require 'yaml'
3
+
4
+ # PriceSetting instances allows you to communicate to the Zaypay platform.
5
+ #
6
+ # 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.
3
7
  class PriceSetting
4
8
  include HTTParty
5
- attr_reader :price_setting_id, :key
9
+ attr_reader :price_setting_id, :api_key
6
10
  attr_accessor :locale, :payment_method_id
7
11
 
8
12
  base_uri 'https://secure.zaypay.com'
9
13
  headers :Accept => 'application/xml'
10
14
 
11
- def initialize(price_setting_id=nil, key=nil)
12
- @price_setting_id, @key = price_setting_id, key
15
+ # Creates instances of Zaypay::PriceSetting.
16
+ #
17
+ # To instantiate, one must provide a PriceSetting-id and its API-Key.
18
+ # You can obtain these information once you have created a PriceSetting on the Zaypay platform (http://www.zaypay.com).
19
+ #
20
+ # You can also call the "one-arg" or the "no-args" version of the initializer,
21
+ # but to do that, you must first create config/zaypay.yml in your Rails app, see the {file:/README.rdoc README} file.
22
+ #
23
+ # @param [Integer] price_setting_id your PriceSetting's id
24
+ # @param [String] api_key your PriceSetting's api-key
25
+ def initialize(price_setting_id=nil, api_key=nil)
26
+ @price_setting_id, @api_key = price_setting_id, api_key
13
27
  select_settings
14
28
  end
15
29
 
16
- def locale_for_ip(ip)
17
- get "/#{ip}/pay/#{price_setting_id}/locale_for_ip" do |data|
18
- parts = data[:locale].split('-')
19
- {:country => parts[1], :language => parts[0]}
30
+ def locale=(arg)
31
+ case arg
32
+ when Hash
33
+ if arg.has_key?(:language) && arg.has_key?(:country)
34
+ @locale = Zaypay::Util.stringify_locale_hash(arg)
35
+ else
36
+ raise Zaypay::Error.new(:locale_not_set, "The hash you provided was invalid. Please make sure it contains the keys :language and :country")
37
+ end
38
+ when String
39
+ @locale = arg
20
40
  end
21
41
  end
22
-
42
+
43
+ # Returns the default locale as a string for a given ip_address, with the first part representing the language, the second part the country
44
+ #
45
+ # This method comes in handy when you want to preselect the language and country when your customer creates a payment on your website.
46
+ #
47
+ # = Example:
48
+ # # We take an ip-address from Great Britain for example:
49
+ # ip = "212.58.226.75"
50
+ # @price_setting.locale_string_for_ip(ip)
51
+ # => 'en-GB'
52
+ #
53
+ # Also see {#locale_for_ip}
54
+ # @param [String] ip an ip-address (e.g. from your site's visitors)
55
+ # @return [String] a string that represents the default locale for the given IP, in a language-country format.
23
56
  def locale_string_for_ip(ip)
24
57
  get "/#{ip}/pay/#{price_setting_id}/locale_for_ip" do |data|
25
58
  parts = data[:locale].split('-')
26
59
  Zaypay::Util.stringify_locale_hash({:country => parts[1], :language => parts[0]})
27
60
  end
28
61
  end
62
+
63
+ # Returns the default locale as a hash for a given ip_address.
64
+ #
65
+ # It is similar to {#locale_string_for_ip}, except that this method returns the locale as a hash
66
+ # This method comes in handy when you want to preselect only the langauge or the country for your customer
67
+ #
68
+ # = Example:
69
+ # # We take an ip-address from Great Britain for example:
70
+ # ip = "212.58.226.75"
71
+ # @price_setting.locale_string_for_ip(ip)
72
+ # => { :country => 'GB', :language => 'en' }
73
+ #
74
+ # @param [String] ip an ip-address (e.g. from your site's visitors)
75
+ # @return [Hash] a hash with :country and :language as keys
76
+ def locale_for_ip(ip)
77
+ get "/#{ip}/pay/#{price_setting_id}/locale_for_ip" do |data|
78
+ parts = data[:locale].split('-')
79
+ {:country => parts[1], :language => parts[0]}
80
+ end
81
+ end
29
82
 
30
- def ip_country_is_configured?(ip, options={})
83
+ # Returns a country as a Hash, if the country of the given IP has been configured for your Price Setting.
84
+ #
85
+ # 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*.
86
+ #
87
+ # @param [String] ip an ip-address (e.g. from your site's visitors)
88
+ # @return [Hash] a hash containing *:country* and *:locale* subhashes
89
+ def country_has_been_configured_for_ip(ip, options={})
31
90
  # options can take a :amount key
32
91
  locale = locale_for_ip(ip)
33
92
  country = list_countries(options).select{ |c| c.has_value? locale[:country] }.first
34
93
  {:country => country, :locale => locale} if country
35
94
  end
36
95
 
37
- # returns a hash containing keys countries and languages
96
+ # Returns a hash containing the countries and languages that are available to your Price Setting.
97
+ #
98
+ # @param [Hash] options an options-hash that can take an *:amount* option, in case you want to use dynamic amounts
99
+ # @return [Hash] a hash containing subhashes of countries and languages
38
100
  def list_locales(options={})
39
101
  get "/#{options[:amount]}/pay/#{price_setting_id}/list_locales" do |data|
40
102
  {:countries => Zaypay::Util.arrayify_if_not_an_array(data[:countries][:country]),
@@ -42,20 +104,31 @@ module Zaypay
42
104
  end
43
105
  end
44
106
 
45
- # returns an array with countries
107
+ # Returns an array of countries that are available to your Price Setting.
108
+ #
109
+ # @param [Hash] options an options-hash that can take an *:amount* option, in case you want to use dynamic pricing
110
+ # @return [Array] an array of countries, each represented by a hash with *:code* and *:name*
46
111
  def list_countries(options={})
47
112
  get "/#{options[:amount]}/pay/#{price_setting_id}/list_locales" do |data|
48
113
  Zaypay::Util.arrayify_if_not_an_array(data[:countries][:country])
49
114
  end
50
115
  end
51
116
 
52
- # returns an array with languages
117
+ # Returns an array of languages that are available to your Price Setting.
118
+ #
119
+ # @param [Hash] options an options-hash that can take an *:amount* option, in case you want to use dynamic pricing
120
+ # @return [Array] an array of languages, each represented by a hash with *:code*, *:english_name*, *:native_name*
53
121
  def list_languages(options={})
54
122
  get "/#{options[:amount]}/pay/#{price_setting_id}/list_locales" do |data|
55
123
  data[:languages][:language]
56
124
  end
57
125
  end
58
126
 
127
+ # Returns an array of payment methods that are available to your Price Setting with a given locale
128
+ #
129
+ # @param [Hash] options an options-hash that can take an *:amount* option, in case you want to use dynamic amounts
130
+ # @return [Array] an array of payment methods, each represented by a hash.
131
+ # @raise [Zaypay::Error] in case you call this method before setting a locale
59
132
  def list_payment_methods(options={})
60
133
  raise Zaypay::Error.new(:locale_not_set, "locale was not set for your price setting") if @locale.nil?
61
134
  get "/#{options[:amount]}/#{@locale}/pay/#{price_setting_id}/payments/new" do |data|
@@ -63,7 +136,21 @@ module Zaypay
63
136
  end
64
137
  end
65
138
 
66
- # Example: @price_setting.create_payment(:custom_variable => "my value", :payalogue_id => payalogue_id, :amount => optional_amount )
139
+ # Creates a payment on the Zaypay platform.
140
+ #
141
+ # You can provide an options-hash, which will add additional data to your payment. The following keys have special functionalities:
142
+ #
143
+ # :amount # Enables dynamic pricing. It must be an integer representing the price in cents.
144
+ # :payalogue_id # Adds the URL of the payalogue specified to your payment as :payalogue_url.
145
+ #
146
+ # Any other keys will be added to a key named :your_variables, which can be used for your future reference. Please check the {file:/README.rdoc README} for the structure of the payment returned.
147
+ #
148
+ # = Example:
149
+ # @price_setting.create_payment(:payalogue_id => payalogue_id, :amount => optional_amount, :my_variable_1 => "value_1", :my_variable_2 => "value_2")
150
+ #
151
+ # @param [Hash] options an options-hash that can take an *:amount*, *:payalogue_id* as options, and any other keys can be used as your custom variables for your own reference
152
+ # @return [Hash] a hash containing data of the payment you just created
153
+ # @raise [Zaypay::Error] in case you call this method before setting a *locale* or a *payment_method_id*
67
154
  def create_payment(options={})
68
155
  raise Zaypay::Error.new(:locale_not_set, "locale was not set for your price setting") if @locale.nil?
69
156
  raise Zaypay::Error.new(:payment_method_id_not_set, "payment_method_id was not set for your price setting") if @payment_method_id.nil?
@@ -75,18 +162,35 @@ module Zaypay
75
162
  end
76
163
  end
77
164
 
165
+ # Returns the specified payment as a hash.
166
+ #
167
+ # @param [Integer] payment_id your payment's id
168
+ # @return [Hash] a hash containing data of the specified payment
78
169
  def show_payment(payment_id)
79
170
  get "///pay/#{price_setting_id}/payments/#{payment_id}" do |data|
80
171
  payment_hash data
81
172
  end
82
173
  end
83
174
 
175
+ # Submits a verification code to the Zaypay platform.
176
+ #
177
+ # In some countries, the end-user must submit a verification code in order to complete a payment. Please refer to the {file:/README.rdoc README} for more details
178
+ #
179
+ # @param [Integer] payment_id the id of the payment that needs to be finalized
180
+ # @param [Integer] verification_code a code that the end-user receives through an sms and that he must submit to complete this payment
181
+ # @return [Hash] a hash containing data of the specified payment
84
182
  def verification_code(payment_id, verification_code)
85
183
  post "///pay/#{price_setting_id}/payments/#{payment_id}/verification_code", :verification_code => verification_code do |data|
86
184
  payment_hash data
87
185
  end
88
186
  end
89
187
 
188
+ # Posts a request to the Zaypay platform to mark that you have delivered the 'goodies' to your customer.
189
+ #
190
+ # Please refer to {file:/README.rdoc README} for more details.
191
+ #
192
+ # @param [Integer] payment_id the payment's id
193
+ # @return [Hash] a hash containing data of the specified payment
90
194
  def mark_payload_provided(payment_id)
91
195
  post "///pay/#{price_setting_id}/payments/#{payment_id}/mark_payload_provided" do |data|
92
196
  payment_hash data
@@ -95,7 +199,7 @@ module Zaypay
95
199
 
96
200
  protected
97
201
  def select_settings
98
- unless @price_setting_id and @key
202
+ unless @price_setting_id and @api_key
99
203
  begin
100
204
  config = YAML.load_file("#{Rails.root}/config/zaypay.yml")
101
205
  rescue => e
@@ -107,8 +211,8 @@ module Zaypay
107
211
  raise config_error unless config
108
212
 
109
213
  @price_setting_id = config['default'] unless @price_setting_id
110
- @key = config[@price_setting_id]
111
- if @key.nil? || @price_setting_id.nil?
214
+ @api_key = config[@price_setting_id]
215
+ if @api_key.nil? || @price_setting_id.nil?
112
216
  raise config_error
113
217
  end
114
218
  end
@@ -130,7 +234,7 @@ module Zaypay
130
234
  end
131
235
 
132
236
  def default_query
133
- {:key => key}
237
+ {:key => api_key}
134
238
  end
135
239
 
136
240
  def payment_hash(data)
data/lib/zaypay/util.rb CHANGED
@@ -1,6 +1,19 @@
1
1
  module Zaypay
2
+
3
+ # A class containing some utility methods.
2
4
  class Util
3
5
 
6
+ # Symbolize keys recursively - Not just for hashes within a Hash, but also for hashes within an Array.
7
+ #
8
+ # = Example:
9
+ #
10
+ # hashie = { 'a' => 'A', 'b' => ['B'], 'c' => [ {'cc' => 'CC'} ], 'd' => { 'e' => 'E', :f => { :ff => 'FF' } } }
11
+ # Zaypay::Util.uber_symbolize(hashie)
12
+ #
13
+ # => { :a => "A", :b => ["B"], :c => [ {:cc=>"CC"} ], :d => { :e => "E", :f => { :ff => "FF" } } }
14
+ #
15
+ # @param data A hash or an array that you want to symbolize recursively
16
+ # @return A hash or an array that has been symbolized recursively for you
4
17
  def self.uber_symbolize(data)
5
18
  if data.is_a?(Hash)
6
19
  data.keys.each do |key|
@@ -16,13 +29,22 @@ module Zaypay
16
29
  data
17
30
  end
18
31
 
32
+ # Turns a hash with *:language* and *:country* keys to a string that represents the locale
33
+ #
34
+ # = Example:
35
+ #
36
+ # Zaypay::Util.stringify_locale_hash( { :country=>"NL", :language=>"nl" } )
37
+ # => 'nl-NL'
38
+ #
39
+ # @param [Hash] locale_hash a hash that represents the locale
40
+ # @return [String] a string representing the locale in the format "language-country"
19
41
  def self.stringify_locale_hash(locale_hash)
20
42
  locale_hash[:language] << '-' << locale_hash[:country]
21
43
  end
22
44
 
45
+ # Wraps something in an array if it is not an array
23
46
  def self.arrayify_if_not_an_array(obj)
24
47
  obj.is_a?(Array) ? obj : [obj]
25
48
  end
26
-
27
49
  end
28
50
  end
@@ -1,3 +1,3 @@
1
1
  module Zaypay
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -24,7 +24,7 @@ class PriceSettingTest < Test::Unit::TestCase
24
24
  YAML.expects(:load_file).with('anywhere/config/zaypay.yml').returns({ @price_setting_id => @api_key, "default"=>@price_setting_id})
25
25
  ps = Zaypay::PriceSetting.new
26
26
  assert_equal @price_setting_id, ps.price_setting_id
27
- assert_equal @api_key, ps.key
27
+ assert_equal @api_key, ps.api_key
28
28
  end
29
29
  should "raise Error if yml is blank" do
30
30
  error = assert_raise Zaypay::Error do
@@ -56,7 +56,7 @@ class PriceSettingTest < Test::Unit::TestCase
56
56
  YAML.expects(:load_file).never
57
57
  ps = Zaypay::PriceSetting.new(@price_setting_id, @api_key)
58
58
  assert_equal @price_setting_id, ps.price_setting_id
59
- assert_equal @api_key, ps.key
59
+ assert_equal @api_key, ps.api_key
60
60
  end
61
61
  end
62
62
 
@@ -66,7 +66,7 @@ class PriceSettingTest < Test::Unit::TestCase
66
66
  YAML.expects(:load_file).with('anywhere/config/zaypay.yml').returns({@price_setting_id => @api_key})
67
67
  ps = Zaypay::PriceSetting.new(@price_setting_id)
68
68
  assert_equal @price_setting_id, ps.price_setting_id
69
- assert_equal @api_key, ps.key
69
+ assert_equal @api_key, ps.api_key
70
70
  end
71
71
  end
72
72
  context "without a valid yml file" do
@@ -82,6 +82,22 @@ class PriceSettingTest < Test::Unit::TestCase
82
82
  end
83
83
  end
84
84
 
85
+ context "#locale=()" do
86
+ context "string as arg" do
87
+ should 'return set locale as a string' do
88
+ @ps.locale = 'nl-NL'
89
+ assert_equal 'nl-NL', @ps.locale
90
+ end
91
+ end
92
+
93
+ context "hash as arg" do
94
+ should 'return set locale as a string' do
95
+ @ps.locale = {:country=>"NL", :language=>"nl"}
96
+ assert_equal 'nl-NL', @ps.locale
97
+ end
98
+ end
99
+ end
100
+
85
101
  context "#locale_for_ip" do
86
102
  setup do
87
103
  FakeWeb.register_uri(:get,"#{@base_uri}/#{@ip}/pay/#{@ps.price_setting_id}/locale_for_ip?key=#{@api_key}", :body => 'test/locale_for_ip.xml', :content_type => "text/xml")
@@ -449,7 +465,7 @@ class PriceSettingTest < Test::Unit::TestCase
449
465
  {:query => {:key => @api_key}, :headers => @headers }).returns @ip_response
450
466
  Zaypay::PriceSetting.expects(:get).with("#{@base_uri}//pay/#{@ps.price_setting_id}/list_locales",
451
467
  {:query => {:key => @api_key}, :headers => @headers }).returns @single_country_response
452
- assert_nil @ps.ip_country_is_configured?(@ip)
468
+ assert_nil @ps.country_has_been_configured_for_ip(@ip)
453
469
  end
454
470
  end
455
471
 
@@ -464,7 +480,7 @@ class PriceSettingTest < Test::Unit::TestCase
464
480
  {:query => {:key => @api_key}, :headers => @headers }).returns @ip_response
465
481
  Zaypay::PriceSetting.expects(:get).with("#{@base_uri}//pay/#{@ps.price_setting_id}/list_locales",
466
482
  {:query => {:key => @api_key}, :headers => @headers }).returns @multi_country_response
467
- assert_equal({:country => {:name => 'Netherlands', :code => 'NL'}, :locale => {:country => 'NL', :language => 'nl'}} , @ps.ip_country_is_configured?(@ip))
483
+ assert_equal({:country => {:name => 'Netherlands', :code => 'NL'}, :locale => {:country => 'NL', :language => 'nl'}} , @ps.country_has_been_configured_for_ip(@ip))
468
484
  end
469
485
  end
470
486
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaypay
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-24 00:00:00.000000000 Z
12
+ date: 2012-10-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -155,6 +155,22 @@ dependencies:
155
155
  - - ~>
156
156
  - !ruby/object:Gem::Version
157
157
  version: 2.0.0
158
+ - !ruby/object:Gem::Dependency
159
+ name: yard
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - '='
164
+ - !ruby/object:Gem::Version
165
+ version: 0.8.2.1
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - '='
172
+ - !ruby/object:Gem::Version
173
+ version: 0.8.2.1
158
174
  description: This gem provides you a PriceSetting class which allows you to send request
159
175
  to the Zaypay API in order to create payments, display payment details and contains
160
176
  other useful methods. Pleases refer to http://zaypay.com/developers for more information