pagseguro-transparente 0.0.1 → 0.0.2

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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +16 -0
  3. data/Gemfile.lock +8 -8
  4. data/README.md +6 -6
  5. data/index.html +374 -0
  6. data/lib/pagseguro.rb +11 -8
  7. data/lib/pagseguro/item.rb +5 -5
  8. data/lib/pagseguro/notification.rb +8 -74
  9. data/lib/pagseguro/payment.rb +95 -0
  10. data/lib/pagseguro/{transaction → payment}/serializer.rb +17 -17
  11. data/lib/pagseguro/request.rb +17 -4
  12. data/lib/pagseguro/sender.rb +4 -0
  13. data/lib/pagseguro/session.rb +8 -1
  14. data/lib/pagseguro/shipping.rb +5 -1
  15. data/lib/pagseguro/transaction.rb +79 -62
  16. data/lib/pagseguro/{notification → transaction}/address.rb +1 -1
  17. data/lib/pagseguro/{notification → transaction}/item.rb +1 -1
  18. data/lib/pagseguro/{notification → transaction}/items.rb +1 -1
  19. data/lib/pagseguro/{notification → transaction}/payment_method.rb +8 -1
  20. data/lib/pagseguro/{notification → transaction}/phone.rb +1 -1
  21. data/lib/pagseguro/{notification → transaction}/sender.rb +1 -1
  22. data/lib/pagseguro/{notification → transaction}/shipping.rb +1 -1
  23. data/lib/pagseguro/version.rb +1 -1
  24. data/pagseguro-transparente.gemspec +3 -3
  25. data/spec/pagseguro/item_spec.rb +1 -1
  26. data/spec/pagseguro/notification_spec.rb +37 -41
  27. data/spec/pagseguro/{transaction → payment}/serializer_spec.rb +15 -13
  28. data/spec/pagseguro/payment_spec.rb +90 -0
  29. data/spec/pagseguro/session_spec.rb +17 -4
  30. data/spec/pagseguro/{notification → transaction}/address_spec.rb +2 -2
  31. data/spec/pagseguro/{notification → transaction}/item_spec.rb +2 -2
  32. data/spec/pagseguro/{notification → transaction}/items_spec.rb +2 -2
  33. data/spec/pagseguro/{notification → transaction}/payment_method_spec.rb +3 -2
  34. data/spec/pagseguro/{notification → transaction}/phone_spec.rb +2 -2
  35. data/spec/pagseguro/{notification → transaction}/sender_spec.rb +3 -3
  36. data/spec/pagseguro/{notification → transaction}/shipping_spec.rb +3 -3
  37. data/spec/pagseguro/transaction_spec.rb +56 -77
  38. data/spec/spec_helper.rb +2 -0
  39. metadata +37 -60
  40. data/coverage/.last_run.json +0 -5
  41. data/coverage/.resultset.json +0 -1665
  42. data/coverage/assets/0.8.0/application.css +0 -799
  43. data/coverage/assets/0.8.0/application.js +0 -1559
  44. data/coverage/assets/0.8.0/colorbox/border.png +0 -0
  45. data/coverage/assets/0.8.0/colorbox/controls.png +0 -0
  46. data/coverage/assets/0.8.0/colorbox/loading.gif +0 -0
  47. data/coverage/assets/0.8.0/colorbox/loading_background.png +0 -0
  48. data/coverage/assets/0.8.0/favicon_green.png +0 -0
  49. data/coverage/assets/0.8.0/favicon_red.png +0 -0
  50. data/coverage/assets/0.8.0/favicon_yellow.png +0 -0
  51. data/coverage/assets/0.8.0/loading.gif +0 -0
  52. data/coverage/assets/0.8.0/magnify.png +0 -0
  53. data/coverage/assets/0.8.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  54. data/coverage/assets/0.8.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  55. data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  56. data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  57. data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  58. data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  59. data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  60. data/coverage/assets/0.8.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  61. data/coverage/assets/0.8.0/smoothness/images/ui-icons_222222_256x240.png +0 -0
  62. data/coverage/assets/0.8.0/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  63. data/coverage/assets/0.8.0/smoothness/images/ui-icons_454545_256x240.png +0 -0
  64. data/coverage/assets/0.8.0/smoothness/images/ui-icons_888888_256x240.png +0 -0
  65. data/coverage/assets/0.8.0/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  66. data/coverage/index.html +0 -10198
data/lib/pagseguro.rb CHANGED
@@ -5,16 +5,17 @@ require "pagseguro/session"
5
5
  require "pagseguro/session/response"
6
6
  require "pagseguro/item"
7
7
  require "pagseguro/items"
8
+ require "pagseguro/payment"
9
+ require "pagseguro/payment/serializer"
8
10
  require "pagseguro/transaction"
9
- require "pagseguro/transaction/serializer"
11
+ require "pagseguro/transaction/address"
12
+ require "pagseguro/transaction/item"
13
+ require "pagseguro/transaction/items"
14
+ require "pagseguro/transaction/payment_method"
15
+ require "pagseguro/transaction/phone"
16
+ require "pagseguro/transaction/sender"
17
+ require "pagseguro/transaction/shipping"
10
18
  require "pagseguro/notification"
11
- require "pagseguro/notification/address"
12
- require "pagseguro/notification/item"
13
- require "pagseguro/notification/items"
14
- require "pagseguro/notification/payment_method"
15
- require "pagseguro/notification/phone"
16
- require "pagseguro/notification/sender"
17
- require "pagseguro/notification/shipping"
18
19
  require "pagseguro/sender"
19
20
  require "pagseguro/phone"
20
21
  require "pagseguro/document"
@@ -26,6 +27,8 @@ require "pagseguro/shipping"
26
27
  require "pagseguro/installment"
27
28
  require "pagseguro/exceptions"
28
29
 
30
+ I18n.enforce_available_locales = false
31
+
29
32
  module PagSeguro
30
33
  class << self
31
34
  # Primary e-mail associated with this account.
@@ -19,11 +19,11 @@ module PagSeguro
19
19
  # Defaults to 1.
20
20
  attr_accessor :quantity
21
21
 
22
- def initialize(id, description, amount, quantity = 1)
23
- @id = id
24
- @description = description
25
- @amount = amount
26
- @quantity = quantity
22
+ def initialize(options = {})
23
+ @id = options[:id]
24
+ @description = options[:description]
25
+ @amount = options[:amount]
26
+ @quantity = options[:quantity] || 1
27
27
  end
28
28
  end
29
29
  end
@@ -1,82 +1,16 @@
1
1
  module PagSeguro
2
- class Notification
3
- attr_reader :response
2
+ class Notification < Request
3
+ attr_accessor :code, :type, :email, :token
4
4
 
5
- def initialize(response)
6
- @response = response
5
+ def initialize(code, type = nil, email = nil, token = nil)
6
+ @code = code
7
+ @type = type
8
+ @email = email
9
+ @token = token
7
10
  end
8
11
 
9
- def date
10
- transaction['date']
11
- end
12
-
13
- def last_event_date
14
- transaction['lastEventDate']
15
- end
16
-
17
- def code
18
- transaction['code']
19
- end
20
-
21
- def reference
22
- transaction['reference']
23
- end
24
-
25
- def type
26
- transaction['type']
27
- end
28
-
29
- def status
30
- transaction['status']
31
- end
32
-
33
- def payment_method
34
- PaymentMethod.new(transaction['paymentMethod'])
35
- end
36
-
37
- def payment_link
38
- transaction['paymentLink']
39
- end
40
-
41
- def gross_amount
42
- transaction['grossAmount']
43
- end
44
-
45
- def discount_amount
46
- transaction['discountAmount']
47
- end
48
-
49
- def fee_amount
50
- transaction['feeAmount']
51
- end
52
-
53
- def net_amount
54
- transaction['netAmount']
55
- end
56
-
57
- def extra_amount
58
- transaction['extraAmount']
59
- end
60
-
61
- def installment_count
62
- transaction['installmentCount']
63
- end
64
-
65
- def items
66
- Items.new(transaction['paymentMethod'], transaction['itemCount'])
67
- end
68
-
69
- def sender
70
- Sender.new(transaction['sender'])
71
- end
72
-
73
- def shipping
74
- Shipping.new(transaction['shipping'])
75
- end
76
-
77
- private
78
12
  def transaction
79
- response['transaction']
13
+ PagSeguro::Transaction.new get("/transactions/notifications/#{code}", email, token)
80
14
  end
81
15
  end
82
16
  end
@@ -0,0 +1,95 @@
1
+ module PagSeguro
2
+ class Payment < Request
3
+ include ActiveModel::Validations
4
+
5
+ validates_presence_of :currency, :payment_method, :items, :sender, :shipping
6
+ validates_presence_of :bank, if: :paid_with_eft?
7
+ validates_presence_of :credit_card, if: :paid_with_card?
8
+ validates_inclusion_of :payment_method, in: %w(creditCard boleto eft)
9
+
10
+ attr_accessor :email, :token
11
+
12
+ # Determines for which url PagSeguro will send the order related
13
+ # notifications codes.
14
+ # Optional. Any change happens in the transaction status, a new notification
15
+ # request will be send to this url. You can use that for update the related
16
+ # order.
17
+ attr_accessor :notification_url
18
+
19
+ # Set the payment currency.
20
+ # Defaults to BRL.
21
+ attr_accessor :currency
22
+
23
+ # Set the payment method.
24
+ # Payment method should be creditCard, boleto or eft.
25
+ attr_accessor :payment_method
26
+
27
+ # Set the payment mode
28
+ attr_accessor :payment_mode
29
+
30
+ # Set the reference code.
31
+ # Optional. You can use the reference code to store an identifier so you can
32
+ # associate the PagSeguro transaction to a transaction in your system.
33
+ # Tipically this is the order id.
34
+ attr_accessor :reference
35
+
36
+ # Get the payment sender.
37
+ attr_accessor :sender
38
+
39
+
40
+ # Get the payment sender.
41
+ attr_accessor :credit_card
42
+
43
+ attr_accessor :receiver_email
44
+
45
+ # Set the bank name.
46
+ # Optional. Bank name should be used for eft payment method
47
+ # to specify which the buyer should be redirect.
48
+ attr_accessor :bank
49
+
50
+ attr_accessor :shipping
51
+
52
+ attr_accessor :extra_amount
53
+
54
+ # Products/items in this payment request.
55
+ def items
56
+ @items ||= Items.new
57
+ end
58
+
59
+ # Normalize the items list.
60
+ def items=(_items)
61
+ _items.each {|item| items << item }
62
+ end
63
+
64
+ # Calls the PagSeguro web service and register this request for payment.
65
+ def transaction
66
+ params = Serializer.new(self).to_params
67
+ PagSeguro::Transaction.new post('/transactions', email, token, params)
68
+ end
69
+
70
+ def initialize(options = {})
71
+ @email = options[:email]
72
+ @token = options[:token]
73
+ @currency = "BRL"
74
+ @payment_mode = 'default'
75
+ @notification_url = options[:notification_url]
76
+ @payment_method = options[:payment_method]
77
+ @reference = options[:reference] if options[:reference]
78
+ @extra_amount = options[:extra_amount] if options[:extra_amount]
79
+ @receiver_email = options[:receiver_email] if options[:receiver_email]
80
+ end
81
+
82
+ private
83
+ def endpoint
84
+ PagSeguro.api_url("checkout")
85
+ end
86
+
87
+ def paid_with_card?
88
+ payment_method == "creditCard"
89
+ end
90
+
91
+ def paid_with_eft?
92
+ payment_method == "eft"
93
+ end
94
+ end
95
+ end
@@ -1,38 +1,38 @@
1
1
  module PagSeguro
2
- class Transaction
2
+ class Payment
3
3
  class Serializer
4
4
  # The payment request that will be serialized.
5
- attr_reader :transaction
5
+ attr_reader :payment
6
6
 
7
- def initialize(transaction)
8
- @transaction = transaction
7
+ def initialize(payment)
8
+ @payment = payment
9
9
  end
10
10
 
11
11
  def to_params
12
- raise InvalidTransaction, transaction.errors.messages unless transaction.valid?
12
+ raise InvalidTransaction, payment.errors.messages unless payment.valid?
13
13
 
14
14
  params[:email] = PagSeguro.email
15
15
  params[:token] = PagSeguro.token
16
16
 
17
17
 
18
- params[:notificationURL] = transaction.notification_url
19
- params[:currency] = transaction.currency
20
- params[:paymentMethod] = transaction.payment_method
21
- params[:paymentMode] = transaction.payment_mode
22
- params[:reference] = transaction.reference
23
- params[:extraAmount] = transaction.extra_amount
18
+ params[:notificationURL] = payment.notification_url
19
+ params[:currency] = payment.currency
20
+ params[:paymentMethod] = payment.payment_method
21
+ params[:paymentMode] = payment.payment_mode
22
+ params[:reference] = payment.reference
23
+ params[:extraAmount] = payment.extra_amount
24
24
 
25
- transaction.items.each.with_index(1) do |item, index|
25
+ payment.items.each.with_index(1) do |item, index|
26
26
  serialize_item(item, index)
27
27
  end
28
28
 
29
- serialize_sender(transaction.sender)
30
- serialize_bank(transaction.bank)
31
- serialize_credit_card(transaction.credit_card)
32
- serialize_shipping(transaction.shipping)
29
+ serialize_sender(payment.sender)
30
+ serialize_bank(payment.bank)
31
+ serialize_credit_card(payment.credit_card)
32
+ serialize_shipping(payment.shipping)
33
33
 
34
34
 
35
- params[:receiverEmail] = transaction.receiver_email if transaction.receiver_email
35
+ params[:receiverEmail] = payment.receiver_email if payment.receiver_email
36
36
 
37
37
  params.delete_if {|key, value| value.nil? }
38
38
 
@@ -7,15 +7,28 @@ module PagSeguro
7
7
 
8
8
  base_uri "https://ws.pagseguro.uol.com.br/v2/"
9
9
 
10
- def get(path, options = {})
10
+ def get(path, email, token, params = {})
11
+ options = add_credencials(email, token, params)
11
12
  self.class.get(path, options)
12
13
  end
13
14
 
14
- def post(path, params = {})
15
- options = { body: { email: PagSeguro.email, token: PagSeguro.token} }
16
- options[:body].merge!(params)
15
+ def post(path, email, token, params = {})
16
+ options = add_credencials(email, token, params)
17
+
17
18
  response = self.class.post(path, options)
18
19
  response.parsed_response
19
20
  end
21
+
22
+ private
23
+ def add_credencials(email, token, params)
24
+ options = { body:
25
+ {
26
+ email: email || PagSeguro.email,
27
+ token: token || PagSeguro.token
28
+ }
29
+ }
30
+ options[:body].merge!(params)
31
+ options
32
+ end
20
33
  end
21
34
  end
@@ -1,6 +1,10 @@
1
1
  module PagSeguro
2
2
  class Sender
3
3
  include ActiveModel::Validations
4
+ extend Forwardable
5
+
6
+ def_delegators :document, :value
7
+ def_delegators :phone, :area_code, :number
4
8
 
5
9
  validates_presence_of :email, :name, :hash_id, :document, :phone
6
10
 
@@ -1,7 +1,14 @@
1
1
  module PagSeguro
2
2
  class Session < Request
3
+ attr_accessor :token, :email
4
+
3
5
  def create
4
- Response.new post('/sessions')
6
+ Response.new post('/sessions', email, token)
7
+ end
8
+
9
+ def initialize(email = nil, token = nil)
10
+ @email = email
11
+ @token = token
5
12
  end
6
13
  end
7
14
  end
@@ -1,10 +1,14 @@
1
1
  module PagSeguro
2
2
  class Shipping
3
3
  include ActiveModel::Validations
4
+ extend Forwardable
5
+
6
+ def_delegators :address, :country, :postal_code, :street, :number
7
+ def_delegators :address, :complement, :district, :city, :state
4
8
 
5
9
  validates_presence_of :address
6
10
  validates_inclusion_of :type_id, in: %w( 1 2 3 ), allow_blank: true
7
- validates_numericality_of :cost, greater_than: 0
11
+ validates_numericality_of :cost, greater_than: 0, allow_nil: true
8
12
 
9
13
  # Set the shipping type
10
14
  attr_accessor :type_id
@@ -1,91 +1,108 @@
1
1
  module PagSeguro
2
- class Transaction < Request
3
- include ActiveModel::Validations
2
+ class Transaction
3
+ attr_reader :response
4
4
 
5
- validates_presence_of :currency, :payment_method, :items, :sender, :shipping
6
- validates_presence_of :bank, if: :paid_with_eft?
7
- validates_presence_of :credit_card, if: :paid_with_card?
8
- validates_inclusion_of :payment_method, in: %w(creditCard boleto eft)
9
- # Determines for which url PagSeguro will send the order related
10
- # notifications codes.
11
- # Optional. Any change happens in the transaction status, a new notification
12
- # request will be send to this url. You can use that for update the related
13
- # order.
14
- attr_accessor :notification_url
5
+ PAYMENT_STATUS = { '1' => :awaiting_payment, '2' => :pending, '3' => :paid,
6
+ '4' => :available, '5' => :in_dispute, '6' => :refunded, '7' => :canceled}
15
7
 
16
- # Set the payment currency.
17
- # Defaults to BRL.
18
- attr_accessor :currency
8
+ def initialize(response)
9
+ @response = response
10
+ end
19
11
 
20
- # Set the payment method.
21
- # Payment method should be creditCard, boleto or eft.
22
- attr_accessor :payment_method
12
+ def date
13
+ transaction['date']
14
+ end
23
15
 
24
- # Set the payment mode
25
- attr_accessor :payment_mode
16
+ def last_event_date
17
+ transaction['lastEventDate']
18
+ end
26
19
 
27
- # Set the reference code.
28
- # Optional. You can use the reference code to store an identifier so you can
29
- # associate the PagSeguro transaction to a transaction in your system.
30
- # Tipically this is the order id.
31
- attr_accessor :reference
20
+ def code
21
+ transaction['code']
22
+ end
32
23
 
33
- # Get the payment sender.
34
- attr_accessor :sender
24
+ def reference
25
+ transaction['reference']
26
+ end
35
27
 
28
+ def type
29
+ transaction['type']
30
+ end
36
31
 
37
- # Get the payment sender.
38
- attr_accessor :credit_card
32
+ def status
33
+ transaction['status']
34
+ end
39
35
 
40
- attr_accessor :receiver_email
36
+ def status_name
37
+ PAYMENT_STATUS[status]
38
+ end
41
39
 
42
- # Set the bank name.
43
- # Optional. Bank name should be used for eft payment method
44
- # to specify which the buyer should be redirect.
45
- attr_accessor :bank
40
+ def cancellation_source
41
+ transaction['cancellationSource']
42
+ end
46
43
 
47
- attr_accessor :shipping
44
+ def payment_method
45
+ PaymentMethod.new(transaction['paymentMethod'])
46
+ end
48
47
 
49
- attr_accessor :extra_amount
48
+ def payment_link
49
+ transaction['paymentLink']
50
+ end
50
51
 
51
- # Products/items in this payment request.
52
- def items
53
- @items ||= Items.new
52
+ def gross_amount
53
+ transaction['grossAmount']
54
54
  end
55
55
 
56
- # Normalize the items list.
57
- def items=(_items)
58
- _items.each {|item| items << item }
56
+ def discount_amount
57
+ transaction['discountAmount']
59
58
  end
60
59
 
61
- # Calls the PagSeguro web service and register this request for payment.
62
- def register
63
- # raise 'An error' unless self.valid?
64
- params = Serializer.new(self).to_params
65
- Response.new post('/transactions', params)
60
+ def fee_amount
61
+ transaction['feeAmount']
66
62
  end
67
63
 
68
- def initialize(options = {})
69
- @currency = "BRL"
70
- @payment_mode = 'default'
71
- @notification_url = options[:notification_url]
72
- @payment_method = options[:payment_method]
73
- @reference = options[:reference] if options[:reference]
74
- @extra_amount = options[:extra_amount] if options[:extra_amount]
75
- @receiver_email = options[:receiver_email] if options[:receiver_email]
64
+ def escrow_end_date
65
+ transaction['escrowEndDate']
76
66
  end
77
67
 
78
- private
79
- def endpoint
80
- PagSeguro.api_url("checkout")
68
+ def net_amount
69
+ transaction['netAmount']
70
+ end
71
+
72
+ def extra_amount
73
+ transaction['extraAmount']
81
74
  end
82
75
 
83
- def paid_with_card?
84
- payment_method == "creditCard"
76
+ def installment_count
77
+ transaction['installmentCount']
85
78
  end
86
79
 
87
- def paid_with_eft?
88
- payment_method == "eft"
80
+ def items
81
+ Items.new(transaction['paymentMethod'], transaction['itemCount'])
82
+ end
83
+
84
+ def sender
85
+ Sender.new(transaction['sender'])
86
+ end
87
+
88
+ def shipping
89
+ Shipping.new(transaction['shipping'])
90
+ end
91
+
92
+ def valid?
93
+ !transaction.blank?
94
+ end
95
+
96
+ def errors
97
+ response['errors'] ||= {}
98
+
99
+ [response['errors']['error']].compact
100
+ end
101
+
102
+ private
103
+
104
+ def transaction
105
+ response['transaction']
89
106
  end
90
107
  end
91
108
  end