mymoip 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 401ae2832ae5de6b706bf0581872a9554bd06f7e
4
- data.tar.gz: d8ec3a961608600adae70bd42211e07ea9aae631
3
+ metadata.gz: 53ae6a3acf07d96de7fdeb44ec9f36da4b4d8302
4
+ data.tar.gz: 9a35890d9732b300341b60e8184d690b0cf6ca4f
5
5
  SHA512:
6
- metadata.gz: e5b9b24159c3122829f406ee1e09e19db8f087f3d4e63474461b57cab065a6095aec3d7f984b6b7f2516108f94bf1e21a55800d30c7467747c7f6aa320b6e80a
7
- data.tar.gz: f3bfdf0541cab984fbf4f535fe01725a83670e4429120407d6d535b9d04ef7e7e9689ca39cebbaff0320106998898c7bfc394657f04b9a3ee20022847d7d2c46
6
+ metadata.gz: 4d2460af5d3c904b87d6ebe4f3deec91964e00a68331b191265abdedd9be87ca5fcd2e1df50c021bf83e684c541fea1fceed6e29970fadbb258b4b3c083129ef
7
+ data.tar.gz: 39a6d852ca28a90ab3dbfeee666f912c7eb61dd86ea2fd94f869d7b54968bf29dc1907bef22715f1b462e0458011c5a8410ff0dc9586134526bb5fc07dafddde
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.7.0
4
+
5
+ * Allow payments using redirection to Moip's domain. (by @oznek)
6
+ * Include PaymentSlip configuration class. (by @oznek)
7
+ * Remove backward compatibility with CreditCardPayment w/o options hash.
8
+
3
9
  ## 0.6.2
4
10
 
5
11
  * Removed development dependency of jeweler. Gems are now managed
data/README.md CHANGED
@@ -24,11 +24,13 @@ Any patch are welcome, even removing extra white spaces.
24
24
  ## First of all
25
25
 
26
26
  **Bundler - Gemfile**
27
+
27
28
  ```ruby
28
29
  gem 'mymoip'
29
30
  ```
30
31
 
31
32
  **Configuration**
33
+
32
34
  ```ruby
33
35
  MyMoip.environment = 'production' # 'sandbox' by default
34
36
 
@@ -89,6 +91,7 @@ purchase.code # Moip code or nil, depending of the checkout's return
89
91
  ## The hard way
90
92
 
91
93
  **First request: what and from who**
94
+
92
95
  ```ruby
93
96
  payer = MyMoip::Payer.new(
94
97
  id: 'payer_id_defined_by_you',
@@ -138,6 +141,7 @@ payment_request.api_call(credit_card_payment,
138
141
  ```
139
142
 
140
143
  **Success?**
144
+
141
145
  ```ruby
142
146
  payment_request.success?
143
147
  ```
@@ -195,7 +199,7 @@ installments = [
195
199
  { min: 2, max: 12, forward_taxes: true, fee: 1.99 } # 1.99 fee = 1.99% per month
196
200
  ]
197
201
 
198
- MyMoip::Instruction.new(
202
+ instruction = MyMoip::Instruction.new(
199
203
  id: 'instruction_id_defined_by_you',
200
204
  payment_reason: 'Order in Buy Everything Store',
201
205
  values: [100.0],
@@ -206,6 +210,79 @@ MyMoip::Instruction.new(
206
210
 
207
211
  [More](https://github.com/Irio/mymoip/wiki/Installments-use).
208
212
 
213
+ ### Notification and return URLs
214
+
215
+ URLs configured at MoIP account can be overrided by passing new URLs values to the instruction object.
216
+ A notification URL is used for MoIP NASP notification system, responsible for transaction changes signals,
217
+ and a return URL is used to return to your website when a payment is using external websites.
218
+
219
+ ```ruby
220
+ instruction = MyMoip::Instruction.new(
221
+ id: 'instruction_id_defined_by_you',
222
+ payment_reason: 'Order in Buy Everything Store',
223
+ values: [100.0],
224
+ payer: payer,
225
+ notification_url: 'https://example.com/payments/notification',
226
+ return_url: 'https://example.com/payments/return'
227
+ )
228
+ ```
229
+
230
+ ### Payment methods configuration
231
+
232
+ If you don't need all the payment methods available, you can choose some by configuring
233
+ PaymentMethods and adding it to the instruction:
234
+
235
+ ```ruby
236
+ payment_methods = MyMoip::PaymentMethods.new(
237
+ payment_slip: false,
238
+ credit_card: true,
239
+ debit: true,
240
+ debit_card: true,
241
+ financing: true,
242
+ moip_wallet: true
243
+ )
244
+
245
+ instruction = MyMoip::Instruction.new(
246
+ id: 'instruction_id_defined_by_you',
247
+ payment_reason: 'Order in Buy Everything Store',
248
+ values: [100.0],
249
+ payer: payer,
250
+ payment_methods: payment_methods
251
+ )
252
+ ```
253
+
254
+ ### Payment slip (aka boleto) configuration
255
+
256
+ You can optionally configure your payment slip creating a PaymentSlip and adding to the instruction:
257
+
258
+ ```ruby
259
+ payment_slip = MyMoip::PaymentSlip.new(
260
+ expiration_date: Date.today.next_day.to_datetime,
261
+ expiration_days: 5,
262
+ expiration_days_type: :business_day,
263
+ instruction_line_1: 'This is the first instruction line.',
264
+ instruction_line_2: 'Please do not pay this slip.',
265
+ instruction_line_3: 'This is a test! :)',
266
+ logo_url: 'https://example.com/logo.png'
267
+ )
268
+
269
+ instruction = MyMoip::Instruction.new(
270
+ id: 'instruction_id_defined_by_you',
271
+ payment_reason: 'Order in Buy Everything Store',
272
+ values: [100.0],
273
+ payer: payer,
274
+ payment_slip: payment_slip
275
+ )
276
+ ```
277
+
278
+ A payment slip can have the following attributes:
279
+
280
+ * expiration_date: a DateTime indicating the last payment date to this slip.
281
+ * expiration_days: expiration days after which the printed payment slip will be considered expired.
282
+ * expiration_days_type: type of expiration day, which can be :business_day or :calendar_day.
283
+ * instruction_line_1, instruction_line_2, instruction_line_3: lines of instruction (up to 63 characters each), added to the payment slip.
284
+ * logo_url: an URL pointing to an image which will be added to the body of the payment slip.
285
+
209
286
  ## Going alive!
210
287
 
211
288
  If you are ready to get your application using MyMoip approved by MoIP or already have valid production keys, you can read a specific [documentation](https://github.com/Irio/mymoip/wiki/Going-alive).
@@ -5,7 +5,7 @@ module MyMoip
5
5
  attr_accessor :reason, :receiver_login, :fixed_value, :percentage_value
6
6
 
7
7
  validates_presence_of :reason, :receiver_login
8
- validates_presence_of :fixed_value, if: -> { percentage_value.nil? }
8
+ validates_presence_of :fixed_value, if: -> { percentage_value.nil? }
9
9
  validates_presence_of :percentage_value, if: -> { fixed_value.nil? }
10
10
  validates_numericality_of :fixed_value, greater_than_or_equal_to: 0,
11
11
  allow_nil: true
@@ -14,10 +14,9 @@ module MyMoip
14
14
  allow_nil: true
15
15
 
16
16
  def initialize(attrs)
17
- self.reason = attrs[:reason]
18
- self.receiver_login = attrs[:receiver_login]
19
- self.fixed_value = attrs[:fixed_value]
20
- self.percentage_value = attrs[:percentage_value]
17
+ attrs.each do |attr, value|
18
+ public_send(:"#{attr}=", value)
19
+ end
21
20
  end
22
21
 
23
22
  def gross_amount(instruction)
@@ -3,18 +3,13 @@ module MyMoip
3
3
  attr_accessor :credit_card, :installments
4
4
 
5
5
  def initialize(credit_card, opts = {})
6
- self.credit_card = credit_card
7
- # Backward compatibility. See 0.2.3 CHANGELOG
8
- self.installments = if opts.kind_of?(Integer)
9
- opts
10
- else
11
- opts[:installments] || 1
12
- end
6
+ self.credit_card = credit_card
7
+ self.installments = opts[:installments] || 1
13
8
  end
14
9
 
15
10
  def to_json(formatter = MyMoip::Formatter)
16
11
  raise InvalidCreditCard, 'No credit card provided.' if credit_card.nil?
17
- raise InvalidCreditCard if credit_card.invalid?
12
+ raise InvalidCreditCard if credit_card.invalid?
18
13
 
19
14
  json = {
20
15
  Forma: "CartaoCredito",
@@ -1,11 +1,13 @@
1
1
  module MyMoip
2
- class Error < StandardError; end
2
+ class Error < StandardError; end
3
3
 
4
- class InvalidComission < Error; end
4
+ class InvalidComission < Error; end
5
5
 
6
- class InvalidCreditCard < Error; end
6
+ class InvalidCreditCard < Error; end
7
7
 
8
8
  class InvalidInstruction < Error; end
9
9
 
10
- class InvalidPayer < Error; end
10
+ class InvalidPayer < Error; end
11
+
12
+ class InvalidPaymentSlip < Error; end
11
13
  end
@@ -1,7 +1,7 @@
1
1
  module MyMoip
2
2
  class Formatter
3
3
  def self.cep(plain_cep)
4
- raise ArgumentError, 'Cannot format CEP nil' if plain_cep.nil?
4
+ raise ArgumentError, 'Cannot format CEP nil' if plain_cep.nil?
5
5
  plain_cep.gsub(/(\d{5})/, '\1-')
6
6
  end
7
7
 
@@ -11,12 +11,12 @@ module MyMoip
11
11
  end
12
12
 
13
13
  def self.date(plain_date)
14
- raise ArgumentError, 'Cannot format date nil' if plain_date.nil?
14
+ raise ArgumentError, 'Cannot format date nil' if plain_date.nil?
15
15
  plain_date.strftime("%d/%m/%Y")
16
16
  end
17
17
 
18
18
  def self.cpf(plain_cpf)
19
- raise ArgumentError, 'Cannot format cpf nil' if plain_cpf.nil?
19
+ raise ArgumentError, 'Cannot format CPF nil' if plain_cpf.nil?
20
20
  plain_cpf.gsub(/(\d{3})(\d{3})(\d{3})(\d{2})/, '\1.\2.\3-\4')
21
21
  end
22
22
  end
@@ -1,35 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/validators.rb')
1
2
  module MyMoip
2
3
  class Instruction
4
+ include Validators
3
5
  include ActiveModel::Validations
4
6
 
5
7
  attr_accessor :id, :payment_reason, :values, :payer,
6
8
  :commissions, :fee_payer_login, :payment_receiver_login,
7
- :payment_receiver_name,:installments
9
+ :payment_receiver_name, :installments,
10
+ :notification_url, :return_url,
11
+ :payment_slip, :payment_methods
8
12
 
9
13
  validates_presence_of :id, :payment_reason, :values, :payer
10
14
  validate :commissions_value_must_be_lesser_than_values
11
15
  validate :payment_receiver_presence_in_commissions
16
+ validate :url_format_validation
12
17
 
13
18
  def initialize(attrs)
14
- self.id = attrs[:id]
15
- self.payment_reason = attrs[:payment_reason]
16
- self.values = attrs[:values]
17
- self.payer = attrs[:payer]
18
- self.commissions = attrs[:commissions] || []
19
- self.fee_payer_login = attrs[:fee_payer_login]
20
- self.payment_receiver_login = attrs[:payment_receiver_login]
21
- self.payment_receiver_name = attrs[:payment_receiver_name]
22
- self.installments = attrs[:installments]
19
+ attrs.each do |attr, value|
20
+ public_send(:"#{attr}=", value)
21
+ end
22
+
23
+ self.commissions ||= []
23
24
  end
24
25
 
25
26
  def to_xml(root = nil)
26
- raise InvalidPayer if payer.invalid?
27
+ raise InvalidPayer if payer.invalid?
28
+ raise InvalidPaymentSlip if payment_slip and payment_slip.invalid?
27
29
  raise InvalidInstruction if self.invalid?
28
30
  if invalid_commission = commissions.detect { |c| c.invalid? }
29
31
  raise InvalidComission, invalid_commission
30
32
  end
31
33
 
32
- xml = ""
34
+ xml = ""
33
35
  root = Builder::XmlMarkup.new(target: xml)
34
36
 
35
37
  root.EnviarInstrucao do |n1|
@@ -39,24 +41,39 @@ module MyMoip
39
41
  @values.each { |v| n3.Valor("%.2f" % v, moeda: "BRL") }
40
42
  end
41
43
  n2.IdProprio(@id)
42
-
44
+
43
45
  if @installments
44
46
  n2.Parcelamentos do |n4|
45
- @installments.each do |i|
47
+ @installments.each do |installments|
46
48
  n4.Parcelamento do |n5|
47
- n5.MinimoParcelas i[:min]
48
- n5.MaximoParcelas i[:max]
49
- n5.Repassar i[:forward_taxes] if i[:forward_taxes]
50
- n5.Juros i[:fee]
49
+ n5.MinimoParcelas(installments[:min])
50
+ n5.MaximoParcelas(installments[:max])
51
+ if installments[:forward_taxes]
52
+ n5.Repassar(installments[:forward_taxes])
53
+ end
54
+ n5.Juros(installments[:fee])
51
55
  end
52
56
  end
53
57
  end
54
58
  end
55
59
 
56
- commissions_to_xml n2 if !commissions.empty?
57
- payment_receiver_to_xml n2 if payment_receiver_login
60
+ commissions_to_xml(n2) if commissions.any?
61
+ payment_receiver_to_xml(n2) if payment_receiver_login
58
62
 
59
63
  n2.Pagador { |n3| @payer.to_xml(n3) }
64
+
65
+ unless @payment_methods.blank? or @payment_methods.using_all?
66
+ n2.FormasPagamento { |n3| @payment_methods.to_xml(n3) }
67
+ end
68
+ unless @payment_slip.blank?
69
+ n2.Boleto { |n3| @payment_slip.to_xml(n3) }
70
+ end
71
+ unless @notification_url.blank?
72
+ n2.URLNotificacao(@notification_url)
73
+ end
74
+ unless @return_url.blank?
75
+ n2.URLRetorno(@return_url)
76
+ end
60
77
  end
61
78
  end
62
79
 
@@ -100,5 +117,15 @@ module MyMoip
100
117
  n.Apelido(payment_receiver_name)
101
118
  end
102
119
  end
120
+
121
+ def url_format_validation
122
+ if not notification_url.blank? and not valid_url?(notification_url)
123
+ errors.add(:notification_url, 'Invalid URL format')
124
+ end
125
+
126
+ if not return_url.blank? and not valid_url?(return_url)
127
+ errors.add(:return_url, 'Invalid URL format.')
128
+ end
129
+ end
103
130
  end
104
131
  end
data/lib/mymoip/payer.rb CHANGED
@@ -3,17 +3,18 @@ module MyMoip
3
3
  include ActiveModel::Validations
4
4
 
5
5
  attr_accessor :id, :name, :email,
6
- :address_street, :address_street_number, :address_street_extra,
7
- :address_neighbourhood, :address_city, :address_state,
8
- :address_country, :address_cep, :address_phone
6
+ :address_street, :address_street_number,
7
+ :address_street_extra, :address_neighbourhood,
8
+ :address_city, :address_state, :address_country,
9
+ :address_cep, :address_phone
9
10
 
10
11
  validates_presence_of :id, :name, :email, :address_street,
11
12
  :address_street_number, :address_neighbourhood,
12
13
  :address_city, :address_state, :address_country,
13
14
  :address_cep, :address_phone
14
- validates_length_of :address_state, is: 2
15
+ validates_length_of :address_state, is: 2
15
16
  validates_length_of :address_country, is: 3
16
- validates_length_of :address_cep, is: 8
17
+ validates_length_of :address_cep, is: 8
17
18
  validates_length_of :address_phone, within: 10..11
18
19
 
19
20
  def initialize(attrs)
@@ -0,0 +1,46 @@
1
+ module MyMoip
2
+ class PaymentMethods
3
+ include ActiveModel::Validations
4
+
5
+ attr_accessor :payment_slip, :credit_card, :debit, :debit_card, :financing,
6
+ :moip_wallet
7
+
8
+ validates_inclusion_of :payment_slip, :credit_card, :debit, :debit_card,
9
+ :financing, :moip_wallet, in: [true, false]
10
+
11
+ def initialize(attrs = {})
12
+ self.payment_slip = true
13
+ self.credit_card = true
14
+ self.debit = true
15
+ self.debit_card = true
16
+ self.financing = true
17
+ self.moip_wallet = true
18
+
19
+ attrs.each do |attr, value|
20
+ public_send(:"#{attr}=", value)
21
+ end
22
+ end
23
+
24
+ def to_xml(root = nil)
25
+ if root.nil?
26
+ xml = ""
27
+ root ||= Builder::XmlMarkup.new(target: xml)
28
+ end
29
+
30
+ if not using_all?
31
+ root.FormaPagamento('BoletoBancario') if payment_slip
32
+ root.FormaPagamento('CartaoDeCredito') if credit_card
33
+ root.FormaPagamento('DebitoBancario') if debit
34
+ root.FormaPagamento('CartaoDeDebito') if debit_card
35
+ root.FormaPagamento('FinanciamentoBancario') if financing
36
+ root.FormaPagamento('CarteiraMoIP') if moip_wallet
37
+ end
38
+
39
+ xml
40
+ end
41
+
42
+ def using_all?
43
+ payment_slip and credit_card and debit and debit_card and financing and moip_wallet
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,74 @@
1
+ module MyMoip
2
+ class PaymentSlip
3
+ include Validators
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :expiration_date, :expiration_days, :expiration_days_type,
7
+ :instruction_line_1, :instruction_line_2,
8
+ :instruction_line_3, :logo_url
9
+
10
+ validates_length_of :instruction_line_1, maximum: 63, allow_nil: true
11
+ validates_length_of :instruction_line_2, maximum: 63, allow_nil: true
12
+ validates_length_of :instruction_line_3, maximum: 63, allow_nil: true
13
+
14
+ validates_numericality_of :expiration_days, less_than_or_equal_to: 99,
15
+ allow_nil: true
16
+ validates_inclusion_of :expiration_days_type,
17
+ in: [:business_day, :calendar_day], allow_nil: true
18
+
19
+ validate :logo_url_format
20
+ validate :expiration_date_format
21
+
22
+ def initialize(attrs)
23
+ attrs.each do |attr, value|
24
+ public_send(:"#{attr}=", value)
25
+ end
26
+ end
27
+
28
+ def to_xml(root = nil)
29
+ raise InvalidPaymentSlip if invalid?
30
+
31
+ if root.nil?
32
+ xml = ""
33
+ root ||= Builder::XmlMarkup.new(target: xml)
34
+ end
35
+
36
+ root.DataVencimento(expiration_date.strftime('%Y-%m-%dT%H:%M:%S.%L%:z')) unless expiration_date.blank?
37
+
38
+ if expiration_days
39
+ type = nil
40
+ if expiration_days_type
41
+ if expiration_days_type == :business_day
42
+ type = {'Tipo' => 'Uteis'}
43
+ elsif expiration_days_type == :calendar_day
44
+ type = {'Tipo' => 'Corridos'}
45
+ end
46
+ end
47
+
48
+ root.DiasExpiracao(expiration_days, type)
49
+ end
50
+
51
+ root.Instrucao1(instruction_line_1) unless instruction_line_1.blank?
52
+ root.Instrucao2(instruction_line_2) unless instruction_line_2.blank?
53
+ root.Instrucao3(instruction_line_3) unless instruction_line_3.blank?
54
+
55
+ root.URLLogo(logo_url) unless logo_url.blank?
56
+
57
+ xml
58
+ end
59
+
60
+ private
61
+
62
+ def logo_url_format
63
+ if not logo_url.blank? and not valid_url?(logo_url)
64
+ errors.add(:logo_url, 'Invalid URL format.')
65
+ end
66
+ end
67
+
68
+ def expiration_date_format
69
+ if not expiration_date.blank? and not expiration_date.instance_of?(DateTime)
70
+ errors.add(:expiration_date, 'Expiration date must be a DateTime object.')
71
+ end
72
+ end
73
+ end
74
+ end
@@ -17,7 +17,7 @@ module MyMoip
17
17
  MyMoip.ensure_key_and_token_set!
18
18
  end
19
19
 
20
- opts[:logger].info "New #{self.class} being sent to MoIP."
20
+ opts[:logger].info "New #{self.class} being sent to MoIP."
21
21
  opts[:logger].debug "#{self.class} of ##{@id} with #{params.inspect}"
22
22
 
23
23
  url = MyMoip.api_url + params.delete(:path)
@@ -17,21 +17,19 @@ module MyMoip
17
17
  end
18
18
 
19
19
  def success?
20
- @response && @response["EnviarInstrucaoUnicaResponse"]["Resposta"]["Status"] == "Sucesso"
21
- rescue NoMethodError => e
20
+ @response["EnviarInstrucaoUnicaResponse"]["Resposta"]["Status"] == "Sucesso"
21
+ rescue NoMethodError
22
22
  false
23
23
  end
24
24
 
25
25
  def token
26
- @response["EnviarInstrucaoUnicaResponse"]["Resposta"]["Token"] || nil
27
- rescue NoMethodError => e
28
- nil
26
+ @response["EnviarInstrucaoUnicaResponse"]["Resposta"]["Token"]
27
+ rescue NoMethodError
29
28
  end
30
29
 
31
30
  def id
32
31
  @response["EnviarInstrucaoUnicaResponse"]["Resposta"]["ID"]
33
- rescue NoMethodError => e
34
- nil
32
+ rescue NoMethodError
35
33
  end
36
34
 
37
35
  end
@@ -0,0 +1,12 @@
1
+ module MyMoip
2
+ module Validators
3
+ private
4
+
5
+ def valid_url?(url)
6
+ uri = URI.parse(url)
7
+ uri.kind_of?(URI::HTTP) or uri.kind_of?(URI::HTTPS)
8
+ rescue URI::InvalidURIError
9
+ false
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module MyMoip
2
- VERSION = '0.6.2'
2
+ VERSION = '0.7.0'
3
3
  end
data/lib/mymoip.rb CHANGED
@@ -48,8 +48,10 @@ end
48
48
 
49
49
  $LOAD_PATH << "./lib/mymoip"
50
50
 
51
+ require File.dirname(__FILE__) + "/mymoip/validators.rb"
52
+
51
53
  files = Dir[File.dirname(__FILE__) + "/mymoip/*.rb"]
52
54
  files.each { |f| require f }
53
55
 
54
56
  MyMoip.environment = "sandbox"
55
- MyMoip.logger = Logger.new(STDOUT)
57
+ MyMoip.logger = Logger.new(STDOUT)
@@ -53,4 +53,17 @@ class Fixture
53
53
  MyMoip::Commission.new(params)
54
54
  end
55
55
 
56
+ def self.payment_slip(params = {})
57
+ params = {
58
+ expiration_date: DateTime.new(2020, 1, 1),
59
+ expiration_days: 7,
60
+ expiration_days_type: :business_day,
61
+ instruction_line_1: 'Line 1',
62
+ instruction_line_2: 'Line 2',
63
+ instruction_line_3: 'Line 3',
64
+ logo_url: 'http://www.myurl.com/logo.png'
65
+ }.merge(params)
66
+
67
+ MyMoip::PaymentSlip.new(params)
68
+ end
56
69
  end
@@ -1,13 +1,6 @@
1
1
  require_relative '../test_helper'
2
2
 
3
3
  class TestCreditCardPayment < Test::Unit::TestCase
4
- def test_initialization_and_getters
5
- credit_card = Fixture.credit_card
6
- subject = MyMoip::CreditCardPayment.new(credit_card, 1)
7
- assert_equal credit_card, subject.credit_card
8
- assert_equal 1, subject.installments
9
- end
10
-
11
4
  def test_allow_initialization_with_a_hash_of_options
12
5
  credit_card = Fixture.credit_card
13
6
  subject = MyMoip::CreditCardPayment.new(credit_card, installments: 2)
@@ -17,13 +10,13 @@ class TestCreditCardPayment < Test::Unit::TestCase
17
10
 
18
11
  def test_cash_method_with_one_tranch
19
12
  credit_card = Fixture.credit_card
20
- subject = MyMoip::CreditCardPayment.new(credit_card, 1)
13
+ subject = MyMoip::CreditCardPayment.new(credit_card, installments: 1)
21
14
  assert_equal true, subject.cash?
22
15
  end
23
16
 
24
17
  def test_cash_method_with_more_than_one_installments
25
18
  credit_card = Fixture.credit_card
26
- subject = MyMoip::CreditCardPayment.new(credit_card, 3)
19
+ subject = MyMoip::CreditCardPayment.new(credit_card, installments: 3)
27
20
  assert_equal false, subject.cash?
28
21
  end
29
22
 
@@ -14,7 +14,9 @@ class TestInstruction < Test::Unit::TestCase
14
14
  fee_payer_login: "fee_payer_login",
15
15
  payment_receiver_login: "payment_receiver_login",
16
16
  payment_receiver_name: "nick_fury",
17
- installments: installments
17
+ installments: installments,
18
+ notification_url: 'http://please.notify.me',
19
+ return_url: 'http://return.to.my.address.com'
18
20
  )
19
21
 
20
22
  assert_equal "some id", instruction.id
@@ -26,6 +28,8 @@ class TestInstruction < Test::Unit::TestCase
26
28
  assert_equal "fee_payer_login", instruction.fee_payer_login
27
29
  assert_equal "payment_receiver_login", instruction.payment_receiver_login
28
30
  assert_equal "nick_fury", instruction.payment_receiver_name
31
+ assert_equal 'http://please.notify.me', instruction.notification_url
32
+ assert_equal 'http://return.to.my.address.com', instruction.return_url
29
33
  end
30
34
 
31
35
  def test_should_generate_a_string_when_converting_to_xml
@@ -45,6 +49,18 @@ XML
45
49
  assert_equal expected_format.rstrip, instruction.to_xml
46
50
  end
47
51
 
52
+ def test_xml_format_with_urls
53
+ payer = Fixture.payer
54
+ instruction = Fixture.instruction(payer: payer)
55
+ instruction.notification_url = 'http://qwerty.me/nasp'
56
+ instruction.return_url = 'http://return.to.me'
57
+
58
+ expected_format = <<XML
59
+ <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Parcelamentos><Parcelamento><MinimoParcelas>2</MinimoParcelas><MaximoParcelas>12</MaximoParcelas><Repassar>true</Repassar><Juros>1.99</Juros></Parcelamento></Parcelamentos><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador><URLNotificacao>http://qwerty.me/nasp</URLNotificacao><URLRetorno>http://return.to.me</URLRetorno></InstrucaoUnica></EnviarInstrucao>
60
+ XML
61
+ assert_equal expected_format.rstrip, instruction.to_xml
62
+ end
63
+
48
64
  def test_xml_format_with_installments
49
65
  payer = Fixture.payer
50
66
  installments = [
@@ -214,4 +230,14 @@ XML
214
230
  assert subject.invalid? && subject.errors[:payer].present?,
215
231
  'should be invalid without a payer'
216
232
  end
233
+
234
+ def test_validate_url
235
+ subject = Fixture.instruction
236
+ assert subject.valid?
237
+
238
+ subject.notification_url = 'ftp://sdfsdfs.com'
239
+ subject.return_url = 'xxftpsdfsdfs.com'
240
+
241
+ assert subject.invalid? && subject.errors[:notification_url].present? && subject.errors[:return_url].present?
242
+ end
217
243
  end
@@ -0,0 +1,54 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestPaymentMethods < Test::Unit::TestCase
4
+ def test_initialization_and_setters
5
+ params = {
6
+ payment_slip: true,
7
+ credit_card: false,
8
+ debit: true,
9
+ debit_card: false,
10
+ financing: true,
11
+ moip_wallet: false
12
+ }
13
+
14
+ subject = MyMoip::PaymentMethods.new(params)
15
+ assert_equal params[:payment_slip], subject.payment_slip
16
+ assert_equal params[:credit_card], subject.credit_card
17
+ assert_equal params[:debit], subject.debit
18
+ assert_equal params[:debit_card], subject.debit_card
19
+ assert_equal params[:financing], subject.financing
20
+ assert_equal params[:moip_wallet], subject.moip_wallet
21
+ end
22
+
23
+ def test_validates_boolean
24
+ subject = MyMoip::PaymentMethods.new
25
+ assert subject.valid?
26
+
27
+ subject.payment_slip = 1
28
+ subject.credit_card = 1
29
+ subject.debit = 1
30
+ subject.debit_card = 1
31
+ subject.financing = 1
32
+ subject.moip_wallet = 1
33
+
34
+ assert subject.invalid? &&
35
+ subject.errors[:payment_slip].present? &&
36
+ subject.errors[:credit_card].present? &&
37
+ subject.errors[:debit].present? &&
38
+ subject.errors[:debit_card].present? &&
39
+ subject.errors[:financing].present? &&
40
+ subject.errors[:moip_wallet].present?
41
+ end
42
+
43
+ def test_xml_format
44
+ subject = MyMoip::PaymentMethods.new
45
+ assert_equal '', subject.to_xml
46
+
47
+ subject.payment_slip = false
48
+ expected_format = <<XML
49
+ <FormaPagamento>CartaoDeCredito</FormaPagamento><FormaPagamento>DebitoBancario</FormaPagamento><FormaPagamento>CartaoDeDebito</FormaPagamento><FormaPagamento>FinanciamentoBancario</FormaPagamento><FormaPagamento>CarteiraMoIP</FormaPagamento>
50
+ XML
51
+ assert_equal expected_format.strip, subject.to_xml
52
+ end
53
+
54
+ end
@@ -75,7 +75,7 @@ class TestPaymentRequest < Test::Unit::TestCase
75
75
  VCR.use_cassette('transparent_request') do
76
76
  transparent_request.api_call(instruction)
77
77
  end
78
- credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, 1)
78
+ credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, installments: 1)
79
79
  payment_request = MyMoip::PaymentRequest.new("your_own_id")
80
80
  VCR.use_cassette('payment_request') do
81
81
  payment_request.api_call(credit_card_payment, token: transparent_request.token)
@@ -89,7 +89,7 @@ class TestPaymentRequest < Test::Unit::TestCase
89
89
  VCR.use_cassette('transparent_request') do
90
90
  transparent_request.api_call(instruction)
91
91
  end
92
- credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, 1)
92
+ credit_card_payment = MyMoip::CreditCardPayment.new(Fixture.credit_card, installments: 1)
93
93
  payment_request = MyMoip::PaymentRequest.new("your_own_id")
94
94
  assert_nil payment_request.code
95
95
  end
@@ -0,0 +1,78 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestPaymentSlip < Test::Unit::TestCase
4
+ def test_validate_length_of_instruction_lines
5
+ subject = Fixture.payment_slip()
6
+ assert subject.valid?
7
+
8
+ subject.instruction_line_1 = ('*' * 63)
9
+ subject.instruction_line_2 = ('*' * 63)
10
+ subject.instruction_line_3 = ('*' * 63)
11
+
12
+ assert subject.valid?
13
+
14
+ subject.instruction_line_1 = ('*' * 64)
15
+ subject.instruction_line_2 = ('*' * 64)
16
+ subject.instruction_line_3 = ('*' * 64)
17
+
18
+ assert subject.invalid?
19
+ assert subject.errors[:instruction_line_1].present? &&
20
+ subject.errors[:instruction_line_2].present? &&
21
+ subject.errors[:instruction_line_3].present?
22
+ end
23
+
24
+ def test_validate_expiration_days
25
+ subject = Fixture.payment_slip(expiration_days: 99)
26
+ assert subject.valid?
27
+
28
+ subject.expiration_days = 100
29
+ assert subject.invalid? && subject.errors[:expiration_days].present?
30
+ end
31
+
32
+ def test_validate_expiration_type
33
+ subject = Fixture.payment_slip(expiration_days_type: :calendar_day)
34
+ assert subject.valid?
35
+
36
+ subject.expiration_days_type = :business_day
37
+ assert subject.valid?
38
+
39
+ subject.expiration_days_type = :another_type
40
+ assert subject.invalid? && subject.errors[:expiration_days_type].present?
41
+ end
42
+
43
+ def test_validate_logo_url
44
+ subject = Fixture.payment_slip(logo_url: 'http://www.uol.com.br')
45
+ assert subject.valid?
46
+
47
+ subject.logo_url = 'https://www.google.com/sdfsdf.png'
48
+ assert subject.valid?
49
+
50
+ subject.logo_url = 'file://www.google.com/sdfsdf.png'
51
+ assert subject.invalid? && subject.errors[:logo_url].present?
52
+ end
53
+
54
+ def test_validate_expiration_date_format
55
+ subject = Fixture.payment_slip(expiration_date: DateTime.new)
56
+ assert subject.valid?
57
+
58
+ subject.expiration_date = Date.new
59
+ assert subject.invalid? && subject.errors[:expiration_date].present?
60
+ end
61
+
62
+ def test_xml_format
63
+ subject = Fixture.payment_slip()
64
+ expected_format = <<XML
65
+ <DataVencimento>2020-01-01T00:00:00.000+00:00</DataVencimento><DiasExpiracao Tipo="Uteis">7</DiasExpiracao><Instrucao1>Line 1</Instrucao1><Instrucao2>Line 2</Instrucao2><Instrucao3>Line 3</Instrucao3><URLLogo>http://www.myurl.com/logo.png</URLLogo>
66
+ XML
67
+ assert_equal expected_format.rstrip, subject.to_xml
68
+ end
69
+
70
+ def test_xml_method_raises_exception_when_called_with_invalid_params
71
+ subject = Fixture.payment_slip
72
+ subject.stubs(:invalid?).returns(true)
73
+ assert_raise MyMoip::InvalidPaymentSlip do
74
+ subject.to_xml
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TestValidators < Test::Unit::TestCase
4
+ include MyMoip::Validators
5
+
6
+ def test_valid_url_validator
7
+ assert_equal valid_url?('http://valid.url.me'), true
8
+ assert_equal valid_url?('https://valid.url.me'), true
9
+ assert_equal valid_url?('ftp://valid.url.me'), false
10
+ assert_equal valid_url?('valid.url.me'), false
11
+ assert_equal valid_url?('me'), false
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mymoip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Irio Irineu Musskopf Junior
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-14 00:00:00.000000000 Z
11
+ date: 2013-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -160,10 +160,13 @@ files:
160
160
  - lib/mymoip/instruction.rb
161
161
  - lib/mymoip/json_parser.rb
162
162
  - lib/mymoip/payer.rb
163
+ - lib/mymoip/payment_methods.rb
164
+ - lib/mymoip/payment_slip.rb
163
165
  - lib/mymoip/purchase.rb
164
166
  - lib/mymoip/request.rb
165
167
  - lib/mymoip/requests/payment_request.rb
166
168
  - lib/mymoip/requests/transparent_request.rb
169
+ - lib/mymoip/validators.rb
167
170
  - lib/mymoip/version.rb
168
171
  - mymoip.gemspec
169
172
  - test/fixtures/fixture.rb
@@ -177,10 +180,13 @@ files:
177
180
  - test/lib/test_instruction.rb
178
181
  - test/lib/test_mymoip.rb
179
182
  - test/lib/test_payer.rb
183
+ - test/lib/test_payment_methods.rb
180
184
  - test/lib/test_payment_request.rb
185
+ - test/lib/test_payment_slip.rb
181
186
  - test/lib/test_purchase.rb
182
187
  - test/lib/test_request.rb
183
188
  - test/lib/test_transparent_request.rb
189
+ - test/lib/test_validators.rb
184
190
  - test/live_test.rb
185
191
  - test/test_helper.rb
186
192
  homepage: https://github.com/Irio/mymoip
@@ -203,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
209
  version: '0'
204
210
  requirements: []
205
211
  rubyforge_project:
206
- rubygems_version: 2.0.0
212
+ rubygems_version: 2.0.3
207
213
  signing_key:
208
214
  specification_version: 4
209
215
  summary: MoIP transactions in a gem to call your own.
@@ -219,9 +225,13 @@ test_files:
219
225
  - test/lib/test_instruction.rb
220
226
  - test/lib/test_mymoip.rb
221
227
  - test/lib/test_payer.rb
228
+ - test/lib/test_payment_methods.rb
222
229
  - test/lib/test_payment_request.rb
230
+ - test/lib/test_payment_slip.rb
223
231
  - test/lib/test_purchase.rb
224
232
  - test/lib/test_request.rb
225
233
  - test/lib/test_transparent_request.rb
234
+ - test/lib/test_validators.rb
226
235
  - test/live_test.rb
227
236
  - test/test_helper.rb
237
+ has_rdoc: