pagseguro-oficial 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +7 -5
  3. data/CHANGELOG.md +0 -1
  4. data/README.md +6 -1
  5. data/docs/transparent_checkout.md +201 -0
  6. data/examples/boleto_transaction_request.rb +94 -0
  7. data/examples/create_session.rb +13 -0
  8. data/examples/credit_card_transaction_request.rb +122 -0
  9. data/examples/installment.rb +20 -9
  10. data/examples/online_debit_transaction.rb +98 -0
  11. data/examples/transaction_by_code.rb +62 -0
  12. data/examples/transaction_by_notification_code.rb +55 -51
  13. data/lib/pagseguro.rb +17 -1
  14. data/lib/pagseguro/bank.rb +8 -0
  15. data/lib/pagseguro/boleto_transaction_request.rb +8 -0
  16. data/lib/pagseguro/credit_card_transaction_request.rb +39 -0
  17. data/lib/pagseguro/document.rb +2 -2
  18. data/lib/pagseguro/errors.rb +11 -5
  19. data/lib/pagseguro/holder.rb +28 -0
  20. data/lib/pagseguro/installment.rb +11 -16
  21. data/lib/pagseguro/installment/collection.rb +25 -0
  22. data/lib/pagseguro/installment/request_serializer.rb +24 -0
  23. data/lib/pagseguro/installment/response.rb +29 -4
  24. data/lib/pagseguro/installment/{serializer.rb → response_serializer.rb} +1 -1
  25. data/lib/pagseguro/items.rb +3 -0
  26. data/lib/pagseguro/online_debit_transaction_request.rb +17 -0
  27. data/lib/pagseguro/payment_method.rb +1 -0
  28. data/lib/pagseguro/payment_releases.rb +3 -0
  29. data/lib/pagseguro/sender.rb +4 -0
  30. data/lib/pagseguro/session.rb +34 -0
  31. data/lib/pagseguro/session/response.rb +32 -0
  32. data/lib/pagseguro/session/response_serializer.rb +18 -0
  33. data/lib/pagseguro/transaction.rb +6 -3
  34. data/lib/pagseguro/transaction/response.rb +1 -1
  35. data/lib/pagseguro/transaction_installment.rb +11 -0
  36. data/lib/pagseguro/transaction_request.rb +143 -0
  37. data/lib/pagseguro/transaction_request/request_serializer.rb +148 -0
  38. data/lib/pagseguro/transaction_request/response.rb +32 -0
  39. data/lib/pagseguro/transaction_request/response_serializer.rb +117 -0
  40. data/lib/pagseguro/version.rb +1 -1
  41. data/spec/fixtures/session/success.xml +4 -0
  42. data/spec/fixtures/transaction_request/success.xml +58 -0
  43. data/spec/fixtures/transactions/search.xml +40 -0
  44. data/spec/pagseguro/bank_spec.rb +5 -0
  45. data/spec/pagseguro/boleto_transaction_request_spec.rb +9 -0
  46. data/spec/pagseguro/credit_card_transaction_request_spec.rb +36 -0
  47. data/spec/pagseguro/document_spec.rb +1 -1
  48. data/spec/pagseguro/errors_spec.rb +5 -2
  49. data/spec/pagseguro/features/create_session_spec.rb +52 -0
  50. data/spec/pagseguro/features/create_transaction_request_spec.rb +58 -0
  51. data/spec/pagseguro/holder_spec.rb +9 -0
  52. data/spec/pagseguro/installment/collection_spec.rb +43 -0
  53. data/spec/pagseguro/installment/request_serializer_spec.rb +16 -0
  54. data/spec/pagseguro/installment/{serializer_spec.rb → response_serializer_spec.rb} +1 -1
  55. data/spec/pagseguro/installment/response_spec.rb +52 -0
  56. data/spec/pagseguro/installment_spec.rb +22 -73
  57. data/spec/pagseguro/online_debit_transaction_request_spec.rb +18 -0
  58. data/spec/pagseguro/payment_method_spec.rb +1 -0
  59. data/spec/pagseguro/payment_request/response_spec.rb +0 -11
  60. data/spec/pagseguro/sender_spec.rb +1 -0
  61. data/spec/pagseguro/session/response_serializer_spec.rb +12 -0
  62. data/spec/pagseguro/session/response_spec.rb +52 -0
  63. data/spec/pagseguro/session_spec.rb +41 -0
  64. data/spec/pagseguro/transaction/search/search_abandoned_spec.rb +27 -0
  65. data/spec/pagseguro/transaction/search/search_by_date_spec.rb +26 -0
  66. data/spec/pagseguro/transaction/search/search_by_reference_spec.rb +26 -0
  67. data/spec/pagseguro/transaction/search_spec.rb +131 -0
  68. data/spec/pagseguro/transaction_installment_spec.rb +6 -0
  69. data/spec/pagseguro/transaction_request/request_serializer_spec.rb +213 -0
  70. data/spec/pagseguro/transaction_request/response_serializer_spec.rb +52 -0
  71. data/spec/pagseguro/transaction_request/response_spec.rb +60 -0
  72. data/spec/pagseguro/transaction_request_spec.rb +125 -0
  73. data/spec/support/helpers.rb +14 -0
  74. metadata +79 -6
@@ -0,0 +1,148 @@
1
+ module PagSeguro
2
+ class TransactionRequest
3
+ class RequestSerializer
4
+ # The transaction request that will be serialized.
5
+ attr_reader :transaction_request
6
+
7
+ def initialize(transaction_request)
8
+ @transaction_request = transaction_request
9
+ end
10
+
11
+ def to_params
12
+ params[:receiverEmail] = PagSeguro.receiver_email
13
+ params[:currency] = transaction_request.currency
14
+ params[:reference] = transaction_request.reference
15
+ params[:extraAmount] = to_amount(transaction_request.extra_amount)
16
+ params[:notificationURL] = transaction_request.notification_url
17
+ params[:paymentMethod] = transaction_request.payment_method
18
+ params[:paymentMode] = transaction_request.payment_mode
19
+ params[:creditCardToken] = transaction_request.credit_card_token if transaction_request.respond_to?(:credit_card_token)
20
+ transaction_request.items.each.with_index(1) do |item, index|
21
+ serialize_item(item, index)
22
+ end
23
+
24
+ serialize_sender(transaction_request.sender)
25
+ serialize_shipping(transaction_request.shipping)
26
+ serialize_extra_params(transaction_request.extra_params)
27
+
28
+ serialize_bank(transaction_request.bank) if transaction_request.respond_to?(:bank)
29
+ serialize_holder(transaction_request.holder) if transaction_request.respond_to?(:holder)
30
+ serialize_billing_address(transaction_request.billing_address) if transaction_request.respond_to?(:billing_address)
31
+ serialize_installment(transaction_request.installment) if transaction_request.respond_to?(:installment)
32
+
33
+ params.delete_if {|key, value| value.nil? }
34
+
35
+ params
36
+ end
37
+
38
+ private
39
+ def params
40
+ @params ||= {}
41
+ end
42
+
43
+ def serialize_item(item, index)
44
+ params["itemId#{index}"] = item.id
45
+ params["itemDescription#{index}"] = item.description
46
+ params["itemAmount#{index}"] = to_amount(item.amount)
47
+ params["itemQuantity#{index}"] = item.quantity
48
+ params["itemShippingCost#{index}"] = to_amount(item.shipping_cost)
49
+ params["itemWeight#{index}"] = item.weight if item.weight
50
+ end
51
+
52
+ def serialize_bank(bank)
53
+ return unless bank
54
+
55
+ params[:bankName] = bank.name
56
+ end
57
+
58
+ def serialize_holder(holder)
59
+ return unless holder
60
+
61
+ params[:creditCardHolderName] = holder.name
62
+ params[:creditCardHolderBirthDate] = holder.birth_date
63
+ params[:creditCardHolderCPF] = holder.document.value
64
+
65
+ serialize_holder_phone(holder.phone)
66
+ end
67
+
68
+ def serialize_holder_phone(phone)
69
+ return unless phone
70
+
71
+ params[:creditCardHolderAreaCode] = phone.area_code
72
+ params[:creditCardHolderPhone] = phone.number
73
+ end
74
+
75
+ def serialize_billing_address(address)
76
+ return unless address
77
+
78
+ params[:billingAddressCountry] = address.country
79
+ params[:billingAddressState] = address.state
80
+ params[:billingAddressCity] = address.city
81
+ params[:billingAddressPostalCode] = address.postal_code
82
+ params[:billingAddressDistrict] = address.district
83
+ params[:billingAddressStreet] = address.street
84
+ params[:billingAddressNumber] = address.number
85
+ params[:billingAddressComplement] = address.complement
86
+ end
87
+
88
+ def serialize_sender(sender)
89
+ return unless sender
90
+
91
+ params[:senderEmail] = sender.email
92
+ params[:senderName] = sender.name
93
+ params[:senderCPF] = sender.cpf
94
+ params[:senderHash] = sender.hash
95
+
96
+ serialize_sender_phone(sender.phone)
97
+ end
98
+
99
+ def serialize_sender_phone(phone)
100
+ return unless phone
101
+
102
+ params[:senderAreaCode] = phone.area_code
103
+ params[:senderPhone] = phone.number
104
+ end
105
+
106
+ def serialize_shipping(shipping)
107
+ return unless shipping
108
+
109
+ params[:shippingType] = shipping.type_id
110
+ params[:shippingCost] = to_amount(shipping.cost)
111
+
112
+ serialize_shipping_address(shipping.address)
113
+ end
114
+
115
+ def serialize_shipping_address(address)
116
+ return unless address
117
+
118
+ params[:shippingAddressCountry] = address.country
119
+ params[:shippingAddressState] = address.state
120
+ params[:shippingAddressCity] = address.city
121
+ params[:shippingAddressPostalCode] = address.postal_code
122
+ params[:shippingAddressDistrict] = address.district
123
+ params[:shippingAddressStreet] = address.street
124
+ params[:shippingAddressNumber] = address.number
125
+ params[:shippingAddressComplement] = address.complement
126
+ end
127
+
128
+ def serialize_installment(installment)
129
+ return unless installment
130
+
131
+ params[:installmentValue] = to_amount(installment.value)
132
+ params[:installmentQuantity] = installment.quantity
133
+ end
134
+
135
+ def serialize_extra_params(extra_params)
136
+ return unless extra_params
137
+
138
+ extra_params.each do |extra_param|
139
+ params[extra_param.keys.first] = extra_param.values.first
140
+ end
141
+ end
142
+
143
+ def to_amount(amount)
144
+ "%.2f" % BigDecimal(amount.to_s).round(2).to_s("F") if amount
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,32 @@
1
+ module PagSeguro
2
+ class TransactionRequest
3
+ class Response
4
+ def initialize(response, transaction_request)
5
+ @response = response
6
+ @transaction_request = transaction_request
7
+ end
8
+
9
+ def serialize
10
+ if success?
11
+ xml = Nokogiri::XML(response.body).css("transaction").first
12
+ transaction_request.update_attributes(ResponseSerializer.new(xml).serialize)
13
+ else
14
+ transaction_request.errors.add(response)
15
+ end
16
+
17
+ transaction_request
18
+ end
19
+
20
+ def success?
21
+ response.success? && response.xml?
22
+ end
23
+
24
+ private
25
+ # The request response.
26
+ attr_reader :response
27
+
28
+ # The TransactionRequest instance.
29
+ attr_reader :transaction_request
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,117 @@
1
+ module PagSeguro
2
+ class TransactionRequest
3
+ class ResponseSerializer
4
+ attr_reader :xml
5
+
6
+ def initialize(xml)
7
+ @xml = xml
8
+ end
9
+
10
+ def serialize
11
+ {}.tap do |data|
12
+ serialize_general(data)
13
+ serialize_amounts(data)
14
+ serialize_dates(data)
15
+ serialize_items(data)
16
+ serialize_sender(data)
17
+ serialize_shipping(data) if xml.css("shipping").any?
18
+ end
19
+ end
20
+
21
+ private
22
+ def serialize_general(data)
23
+ data[:code] = xml.css("> code").text
24
+ data[:reference] = xml.css("reference").text
25
+ data[:type_id] = xml.css("> type").text
26
+ data[:payment_link] = xml.css("paymentLink").text
27
+ data[:status] = xml.css("> status").text
28
+
29
+ data[:payment_method] = {
30
+ type_id: xml.css("paymentMethod > type").text,
31
+ code: xml.css("paymentMethod > code").text
32
+ }
33
+ end
34
+
35
+ def serialize_dates(data)
36
+ data[:created_at] = Time.parse(xml.css("date").text)
37
+
38
+ updated_at = xml.css("lastEventDate").text
39
+ data[:updated_at] = Time.parse(updated_at) unless updated_at.empty?
40
+ end
41
+
42
+ def serialize_amounts(data)
43
+ data[:gross_amount] = BigDecimal(xml.css("grossAmount").text)
44
+ data[:discount_amount] = BigDecimal(xml.css("discountAmount").text)
45
+ data[:net_amount] = BigDecimal(xml.css("netAmount").text)
46
+ data[:extra_amount] = BigDecimal(xml.css("extraAmount").text)
47
+ data[:installment_count] = xml.css("installmentCount").text.to_i
48
+ end
49
+
50
+ def serialize_items(data)
51
+ data[:items] = []
52
+
53
+ xml.css("items > item").each do |node|
54
+ item = {}
55
+ item[:id] = node.css("id").text
56
+ item[:description] = node.css("description").text
57
+ item[:quantity] = node.css("quantity").text.to_i
58
+ item[:amount] = BigDecimal(node.css("amount").text)
59
+
60
+ data[:items] << item
61
+ end
62
+ end
63
+
64
+ def serialize_sender(data)
65
+ sender = {
66
+ name: xml.css("sender > name").text,
67
+ email: xml.css("sender > email").text
68
+ }
69
+
70
+ serialize_phone(sender)
71
+ serialize_document(sender)
72
+ data[:sender] = sender
73
+ end
74
+
75
+ def serialize_document(data)
76
+ data[:document] = {
77
+ type: xml.css("sender > documents > document > type").text,
78
+ value: xml.css("sender > documents > document > value").text
79
+ }
80
+ end
81
+
82
+ def serialize_phone(data)
83
+ data[:phone] = {
84
+ area_code: xml.css("sender > phone > areaCode").text,
85
+ number: xml.css("sender > phone > number").text
86
+ }
87
+ end
88
+
89
+ def serialize_shipping(data)
90
+ shipping = {
91
+ type_id: xml.css("shipping > type").text,
92
+ cost: BigDecimal(xml.css("shipping > cost").text),
93
+ }
94
+
95
+ serialize_address(shipping)
96
+ data[:shipping] = shipping
97
+ end
98
+
99
+ def serialize_address(data)
100
+ data[:address] = {
101
+ street: address_node.css("> street").text,
102
+ number: address_node.css("> number").text,
103
+ complement: address_node.css("> complement").text,
104
+ district: address_node.css("> district").text,
105
+ city: address_node.css("> city").text,
106
+ state: address_node.css("> state").text,
107
+ country: address_node.css("> country").text,
108
+ postal_code: address_node.css("> postalCode").text,
109
+ }
110
+ end
111
+
112
+ def address_node
113
+ @address_node ||= xml.css("shipping > address")
114
+ end
115
+ end
116
+ end
117
+ end
@@ -1,3 +1,3 @@
1
1
  module PagSeguro
2
- VERSION = "2.1.1"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1"?>
2
+ <session>
3
+ <id>620f99e348c24f07877c927b353e49d3</id>
4
+ </session>
@@ -0,0 +1,58 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
2
+ <transaction>
3
+ <date>2011-02-05T15:46:12.000-02:00</date>
4
+ <lastEventDate>2011-02-15T17:39:14.000-03:00</lastEventDate>
5
+ <code>9E884542-81B3-4419-9A75-BCC6FB495EF1</code>
6
+ <reference>REF1234</reference>
7
+ <type>1</type>
8
+ <status>3</status>
9
+ <paymentMethod>
10
+ <type>1</type>
11
+ <code>101</code>
12
+ </paymentMethod>
13
+ <paymentLink>https://pagseguro.uol.com.br/checkout/imprimeBoleto.jhtml?code=314601B208B24A5CA5
14
+ 3260000F7BB0D0</paymentLink>
15
+ <grossAmount>49900.00</grossAmount>
16
+ <discountAmount>0.00</discountAmount>
17
+ <feeAmount>0.00</feeAmount>
18
+ <netAmount>49900.50</netAmount>
19
+ <extraAmount>0.00</extraAmount>
20
+ <installmentCount>1</installmentCount>
21
+ <itemCount>2</itemCount>
22
+ <items>
23
+ <item>
24
+ <id>0001</id>
25
+ <description>Notebook Prata</description>
26
+ <quantity>1</quantity>
27
+ <amount>24300.00</amount>
28
+ </item>
29
+ <item>
30
+ <id>0002</id>
31
+ <description>Notebook Rosa</description>
32
+ <quantity>1</quantity>
33
+ <amount>25600.00</amount>
34
+ </item>
35
+ </items>
36
+ <sender>
37
+ <name>Jose Comprador</name>
38
+ <email>comprador@uol.com.br</email>
39
+ <phone>
40
+ <areaCode>11</areaCode>
41
+ <number>56273440</number>
42
+ </phone>
43
+ </sender>
44
+ <shipping>
45
+ <address>
46
+ <street>Av. Brig. Faria Lima</street>
47
+ <number>1384</number>
48
+ <complement>5o andar</complement>
49
+ <district>Jardim Paulistano</district>
50
+ <postalCode>01452002</postalCode>
51
+ <city>Sao Paulo</city>
52
+ <state>SP</state>
53
+ <country>BRA</country>
54
+ </address>
55
+ <type>1</type>
56
+ <cost>21.50</cost>
57
+ </shipping>
58
+ </transaction>
@@ -0,0 +1,40 @@
1
+ <transactionSearchResult>
2
+ <date>2011-02-16T20:14:35.000-02:00</date>
3
+ <currentPage>1</currentPage>
4
+ <resultsInThisPage>10</resultsInThisPage>
5
+ <totalPages>1</totalPages>
6
+ <transactions>
7
+ <transaction>
8
+ <date>2011-02-05T15:46:12.000-02:00</date>
9
+ <lastEventDate>2011-02-15T17:39:14.000-03:00</lastEventDate>
10
+ <code>9E884542-81B3-4419-9A75-BCC6FB495EF1</code>
11
+ <reference>REF1234</reference>
12
+ <type>1</type>
13
+ <status>3</status>
14
+ <paymentMethod>
15
+ <type>1</type>
16
+ </paymentMethod>
17
+ <grossAmount>49900.00</grossAmount>
18
+ <discountAmount>0.00</discountAmount>
19
+ <feeAmount>0.00</feeAmount>
20
+ <netAmount>49900.00</netAmount>
21
+ <extraAmount>0.00</extraAmount>
22
+ </transaction>
23
+ <transaction>
24
+ <date>2011-02-07T18:57:52.000-02:00</date>
25
+ <lastEventDate>2011-02-14T21:37:24.000-03:00</lastEventDate>
26
+ <code>2FB07A22-68FF-4F83-A356-24153A0C05E1</code>
27
+ <reference>REF5678</reference>
28
+ <type>3</type>
29
+ <status>4</status>
30
+ <paymentMethod>
31
+ <type>3</type>
32
+ </paymentMethod>
33
+ <grossAmount>26900.00</grossAmount>
34
+ <discountAmount>0.00</discountAmount>
35
+ <feeAmount>0.00</feeAmount>
36
+ <netAmount>26900.00</netAmount>
37
+ <extraAmount>0.00</extraAmount>
38
+ </transaction>
39
+ </transactions>
40
+ </transactionSearchResult>
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Bank do
4
+ it_assigns_attribute :name
5
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::BoletoTransactionRequest do
4
+ describe "#payment_method" do
5
+ it "is boleto" do
6
+ expect(subject.payment_method).to eq("boleto")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::CreditCardTransactionRequest do
4
+ it_assigns_attribute :credit_card_token
5
+
6
+ it_ensures_type PagSeguro::TransactionInstallment, :installment
7
+ it_ensures_type PagSeguro::Holder, :holder
8
+ it_ensures_type PagSeguro::Address, :billing_address
9
+
10
+ describe "#payment_method" do
11
+ it "is credit_card" do
12
+ expect(subject.payment_method).to eq("credit_card")
13
+ end
14
+ end
15
+
16
+ it "sets the transaction installment" do
17
+ installment = PagSeguro::TransactionInstallment.new
18
+ payment = described_class.new(installment: installment)
19
+
20
+ expect(payment.installment).to eql(installment)
21
+ end
22
+
23
+ it "sets the holder" do
24
+ holder = PagSeguro::Holder.new
25
+ payment = described_class.new(holder: holder)
26
+
27
+ expect(payment.holder).to eql(holder)
28
+ end
29
+
30
+ it "sets the billing address" do
31
+ address = PagSeguro::Address.new
32
+ payment = described_class.new(billing_address: address)
33
+
34
+ expect(payment.billing_address).to eql(address)
35
+ end
36
+ end