pagseguro-oficial 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +1 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE-2.0.txt +177 -0
- data/README.md +320 -0
- data/Rakefile +5 -0
- data/examples/abandoned_transactions.rb +26 -0
- data/examples/boot.rb +9 -0
- data/examples/invalid_transaction_by_notification_code.rb +9 -0
- data/examples/payment_request.rb +49 -0
- data/examples/transaction_by_notification_code.rb +5 -0
- data/examples/transactions_by_date.rb +23 -0
- data/lib/pagseguro/address.rb +40 -0
- data/lib/pagseguro/errors.rb +28 -0
- data/lib/pagseguro/exceptions.rb +3 -0
- data/lib/pagseguro/extensions/ensure_type.rb +9 -0
- data/lib/pagseguro/extensions/mass_assignment.rb +11 -0
- data/lib/pagseguro/item.rb +30 -0
- data/lib/pagseguro/items.rb +27 -0
- data/lib/pagseguro/notification.rb +21 -0
- data/lib/pagseguro/payment_method.rb +38 -0
- data/lib/pagseguro/payment_request/response.rb +30 -0
- data/lib/pagseguro/payment_request/serializer.rb +91 -0
- data/lib/pagseguro/payment_request.rb +96 -0
- data/lib/pagseguro/payment_status.rb +33 -0
- data/lib/pagseguro/phone.rb +12 -0
- data/lib/pagseguro/report.rb +124 -0
- data/lib/pagseguro/request.rb +78 -0
- data/lib/pagseguro/sender.rb +23 -0
- data/lib/pagseguro/shipping.rb +57 -0
- data/lib/pagseguro/transaction/response.rb +12 -0
- data/lib/pagseguro/transaction/serializer.rb +115 -0
- data/lib/pagseguro/transaction.rb +167 -0
- data/lib/pagseguro/version.rb +3 -0
- data/lib/pagseguro-oficial.rb +1 -0
- data/lib/pagseguro.rb +94 -0
- data/locales/pt-BR.yml +115 -0
- data/pagseguro-oficial.gemspec +32 -0
- data/spec/fixtures/by_date/success.xml +85 -0
- data/spec/fixtures/invalid_code.xml +7 -0
- data/spec/fixtures/payment_request/failure.xml +7 -0
- data/spec/fixtures/payment_request/success.xml +5 -0
- data/spec/fixtures/transactions/additional.xml +53 -0
- data/spec/fixtures/transactions/success.xml +53 -0
- data/spec/pagseguro/address_spec.rb +17 -0
- data/spec/pagseguro/errors_spec.rb +91 -0
- data/spec/pagseguro/item_spec.rb +20 -0
- data/spec/pagseguro/items_spec.rb +56 -0
- data/spec/pagseguro/notification_spec.rb +18 -0
- data/spec/pagseguro/pagseguro_spec.rb +54 -0
- data/spec/pagseguro/payment_method_spec.rb +41 -0
- data/spec/pagseguro/payment_request/response_spec.rb +24 -0
- data/spec/pagseguro/payment_request/serializer_spec.rb +142 -0
- data/spec/pagseguro/payment_request_spec.rb +107 -0
- data/spec/pagseguro/payment_status_spec.rb +24 -0
- data/spec/pagseguro/phone_spec.rb +6 -0
- data/spec/pagseguro/request_spec.rb +75 -0
- data/spec/pagseguro/sender_spec.rb +9 -0
- data/spec/pagseguro/shipping_spec.rb +40 -0
- data/spec/pagseguro/transaction/serializer_spec.rb +61 -0
- data/spec/pagseguro/transaction_spec.rb +118 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/ensure_type_macro.rb +17 -0
- data/spec/support/mass_assignment_macro.rb +11 -0
- metadata +289 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
module PagSeguro
|
2
|
+
class PaymentRequest
|
3
|
+
include Extensions::MassAssignment
|
4
|
+
include Extensions::EnsureType
|
5
|
+
|
6
|
+
# Set the payment currency.
|
7
|
+
# Defaults to BRL.
|
8
|
+
attr_accessor :currency
|
9
|
+
|
10
|
+
# Get the payment sender.
|
11
|
+
attr_reader :sender
|
12
|
+
|
13
|
+
# Get the shipping info.
|
14
|
+
attr_reader :shipping
|
15
|
+
|
16
|
+
# Set the redirect url.
|
17
|
+
# The URL that will be used by PagSeguro to redirect the user after
|
18
|
+
# the payment information is processed. Typically this is a
|
19
|
+
# confirmation page on your web site.
|
20
|
+
attr_accessor :redirect_url
|
21
|
+
|
22
|
+
# Set the extra amount to be applied to the transaction's total.
|
23
|
+
# This value can be used to add an extra charge to the transaction
|
24
|
+
# or provide a discount, if negative.
|
25
|
+
attr_accessor :extra_amount
|
26
|
+
|
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
|
32
|
+
|
33
|
+
# Set the payment request duration, in seconds.
|
34
|
+
attr_accessor :max_age
|
35
|
+
|
36
|
+
# How many times the payment redirect uri returned by the payment
|
37
|
+
# web service can be accessed.
|
38
|
+
# Optional. After this payment request is submitted, the payment
|
39
|
+
# redirect uri returned by the payment web service will remain valid
|
40
|
+
# for the number of uses specified here.
|
41
|
+
attr_accessor :max_uses
|
42
|
+
|
43
|
+
# Determines for which url PagSeguro will send the order related
|
44
|
+
# notifications codes.
|
45
|
+
# Optional. Any change happens in the transaction status, a new notification
|
46
|
+
# request will be send to this url. You can use that for update the related
|
47
|
+
# order.
|
48
|
+
attr_accessor :notification_url
|
49
|
+
|
50
|
+
# Determines for which url PagSeguro will send the buyer when he doesn't
|
51
|
+
# complete the payment.
|
52
|
+
attr_accessor :abandon_url
|
53
|
+
|
54
|
+
# The email that identifies the request. Defaults to PagSeguro.email
|
55
|
+
attr_accessor :email
|
56
|
+
|
57
|
+
# The token that identifies the request. Defaults to PagSeguro.token
|
58
|
+
attr_accessor :token
|
59
|
+
|
60
|
+
|
61
|
+
# Products/items in this payment request.
|
62
|
+
def items
|
63
|
+
@items ||= Items.new
|
64
|
+
end
|
65
|
+
|
66
|
+
# Set the payment sender.
|
67
|
+
def sender=(sender)
|
68
|
+
@sender = ensure_type(Sender, sender)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Set the shipping info.
|
72
|
+
def shipping=(shipping)
|
73
|
+
@shipping = ensure_type(Shipping, shipping)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Calls the PagSeguro web service and register this request for payment.
|
77
|
+
def register
|
78
|
+
params = Serializer.new(self).to_params.merge({
|
79
|
+
email: email,
|
80
|
+
token: token
|
81
|
+
})
|
82
|
+
Response.new Request.post("checkout", params)
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
def before_initialize
|
87
|
+
self.currency = "BRL"
|
88
|
+
self.email = PagSeguro.email
|
89
|
+
self.token = PagSeguro.token
|
90
|
+
end
|
91
|
+
|
92
|
+
def endpoint
|
93
|
+
PagSeguro.api_url("checkout")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module PagSeguro
|
2
|
+
class PaymentStatus
|
3
|
+
STATUSES = {
|
4
|
+
"0" => :initiated,
|
5
|
+
"1" => :waiting_payment,
|
6
|
+
"2" => :in_analysis,
|
7
|
+
"3" => :paid,
|
8
|
+
"4" => :available,
|
9
|
+
"5" => :in_dispute,
|
10
|
+
"6" => :refunded,
|
11
|
+
"7" => :cancelled,
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
# The payment status id.
|
15
|
+
attr_reader :id
|
16
|
+
|
17
|
+
def initialize(id)
|
18
|
+
@id = id
|
19
|
+
end
|
20
|
+
|
21
|
+
# Dynamically define helpers.
|
22
|
+
STATUSES.each do |id, _status|
|
23
|
+
define_method "#{_status}?" do
|
24
|
+
_status == status
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Return a readable status.
|
29
|
+
def status
|
30
|
+
STATUSES.fetch(id.to_s) { raise "PagSeguro::PaymentStatus#id isn't mapped" }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
module PagSeguro
|
2
|
+
class Report
|
3
|
+
# Set the report options.
|
4
|
+
#
|
5
|
+
# # +per_page+: the page size.
|
6
|
+
# # +starts_at+: the report's starting date. Can't be older than 6 months.
|
7
|
+
# # +ends_at+: the report's ending date. Can't be greater than 30 days from the starting date.
|
8
|
+
#
|
9
|
+
attr_reader :options
|
10
|
+
|
11
|
+
# Set the errors from the report request.
|
12
|
+
attr_reader :errors
|
13
|
+
|
14
|
+
# Return the current page.
|
15
|
+
attr_reader :page
|
16
|
+
|
17
|
+
def initialize(item_class, path, options, page = 0)
|
18
|
+
@item_class = item_class
|
19
|
+
@path = path
|
20
|
+
@options = options
|
21
|
+
@page = page
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return the list of transactions.
|
25
|
+
# Each item will be wrapped in a PagSeguro::Transaction instance.
|
26
|
+
# Notice that transactions instantiated by the report won't have all attributes.
|
27
|
+
# If you need additional attributes, do a PagSeguro::Transaction.find_by_notification_code
|
28
|
+
# call. Remember that this will perform an additional HTTP request.
|
29
|
+
def transactions
|
30
|
+
xml do |xml|
|
31
|
+
@transactions ||= xml.css("transactionSearchResult transaction").map do |node|
|
32
|
+
Transaction.load_from_xml(node)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# The report's creation date.
|
38
|
+
def created_at
|
39
|
+
xml do |xml|
|
40
|
+
@created_at ||= Time.parse xml.css("transactionSearchResult > date").text
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# How many results the report returned on the given page.
|
45
|
+
def results
|
46
|
+
xml do |xml|
|
47
|
+
@results ||= xml.css("transactionSearchResult > resultsInThisPage").text.to_i
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# How many pages the report returned.
|
52
|
+
def total_pages
|
53
|
+
xml do |xml|
|
54
|
+
@total_pages ||= xml.css("transactionSearchResult > totalPages").text.to_i
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Detect if the report has a next page.
|
59
|
+
def next_page?
|
60
|
+
page.zero? || page < total_pages
|
61
|
+
end
|
62
|
+
|
63
|
+
# Detect if the report has a previous page.
|
64
|
+
def previous_page?
|
65
|
+
page > 1
|
66
|
+
end
|
67
|
+
|
68
|
+
# Move the page pointer to the next page.
|
69
|
+
def next_page!
|
70
|
+
return unless next_page?
|
71
|
+
@page += 1
|
72
|
+
clear!
|
73
|
+
end
|
74
|
+
|
75
|
+
# Move the page pointer to the previous page.
|
76
|
+
def previous_page!
|
77
|
+
return unless previous_page?
|
78
|
+
@page -= 1
|
79
|
+
clear!
|
80
|
+
end
|
81
|
+
|
82
|
+
# Detect if the report request returned errors.
|
83
|
+
def valid?
|
84
|
+
fetch { errors.empty? }
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
def perform_request_and_serialize
|
89
|
+
@response = Request.get(@path, {
|
90
|
+
initialDate: options[:starts_at].xmlschema,
|
91
|
+
finalDate: options[:ends_at].xmlschema,
|
92
|
+
page: page,
|
93
|
+
maxPageResults: options.fetch(:per_page, 50)
|
94
|
+
})
|
95
|
+
|
96
|
+
@errors = Errors.new(@response)
|
97
|
+
end
|
98
|
+
|
99
|
+
def fetched?
|
100
|
+
@fetched
|
101
|
+
end
|
102
|
+
|
103
|
+
def fetched!
|
104
|
+
@fetched = true
|
105
|
+
end
|
106
|
+
|
107
|
+
def clear!
|
108
|
+
@fetched = false
|
109
|
+
end
|
110
|
+
|
111
|
+
def fetch(&block)
|
112
|
+
unless fetched?
|
113
|
+
perform_request_and_serialize
|
114
|
+
fetched!
|
115
|
+
end
|
116
|
+
|
117
|
+
instance_eval(&block)
|
118
|
+
end
|
119
|
+
|
120
|
+
def xml(&block)
|
121
|
+
valid? && block.call(@response.data)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module PagSeguro
|
2
|
+
module Request
|
3
|
+
extend self
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
# Delegates the <tt>:config</tt> and <tt>:configure</tt> methods
|
7
|
+
# to the <tt>:request</tt> method, which returns a Aitch::Namespace instance.
|
8
|
+
def_delegators :request, :config, :configure
|
9
|
+
|
10
|
+
# Perform a GET request.
|
11
|
+
#
|
12
|
+
# # +path+: the path that will be requested. Must be something like <tt>"transactions/code/739D69-79C052C05280-55542D9FBB33-CAB2B1"</tt>.
|
13
|
+
# # +data+: the data that will be sent as query string. Must be a Hash.
|
14
|
+
# # +headers+: any additional header that will be sent through the request.
|
15
|
+
#
|
16
|
+
def get(path, data = {}, headers = {})
|
17
|
+
execute :get, path, data, headers
|
18
|
+
end
|
19
|
+
|
20
|
+
# Perform a POST request.
|
21
|
+
#
|
22
|
+
# # +path+: the path that will be requested. Must be something like <tt>"checkout"</tt>.
|
23
|
+
# # +data+: the data that will be sent as body data. Must be a Hash.
|
24
|
+
# # +headers+: any additional header that will be sent through the request.
|
25
|
+
#
|
26
|
+
def post(path, data = {}, headers = {})
|
27
|
+
execute :post, path, data, headers
|
28
|
+
end
|
29
|
+
|
30
|
+
# Perform the specified HTTP request. It will include the API credentials,
|
31
|
+
# encoding and additional headers.
|
32
|
+
def execute(request_method, path, data, headers) # :nodoc:
|
33
|
+
request.public_send(
|
34
|
+
request_method,
|
35
|
+
PagSeguro.api_url(path),
|
36
|
+
extended_data(data),
|
37
|
+
extended_headers(request_method, headers)
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def request
|
43
|
+
@request ||= Aitch::Namespace.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def extended_data(data)
|
47
|
+
data.merge(
|
48
|
+
email: data[:email] || PagSeguro.email,
|
49
|
+
token: data[:token] || PagSeguro.token,
|
50
|
+
charset: PagSeguro.encoding
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
def extended_headers(request_method, headers)
|
55
|
+
headers.merge __send__("headers_for_#{request_method}")
|
56
|
+
end
|
57
|
+
|
58
|
+
def headers_for_post
|
59
|
+
{
|
60
|
+
"Accept-Charset" => PagSeguro.encoding,
|
61
|
+
"Content-Type" => "application/x-www-form-urlencoded; charset=#{PagSeguro.encoding}"
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def headers_for_get
|
66
|
+
{
|
67
|
+
"Accept-Charset" => PagSeguro.encoding
|
68
|
+
}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
Request.configure do |config|
|
73
|
+
config.default_headers = {
|
74
|
+
"lib-description" => "ruby:#{PagSeguro::VERSION}",
|
75
|
+
"language-engine-description" => "ruby:#{RUBY_VERSION}"
|
76
|
+
}
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module PagSeguro
|
2
|
+
class Sender
|
3
|
+
include Extensions::MassAssignment
|
4
|
+
include Extensions::EnsureType
|
5
|
+
|
6
|
+
# Get the sender phone.
|
7
|
+
attr_reader :phone
|
8
|
+
|
9
|
+
# Set the sender name.
|
10
|
+
attr_accessor :name
|
11
|
+
|
12
|
+
# Set the sender e-mail.
|
13
|
+
attr_accessor :email
|
14
|
+
|
15
|
+
# Set the CPF document.
|
16
|
+
attr_accessor :cpf
|
17
|
+
|
18
|
+
# Set the sender phone.
|
19
|
+
def phone=(phone)
|
20
|
+
@phone = ensure_type(Phone, phone)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module PagSeguro
|
2
|
+
class Shipping
|
3
|
+
# Set the available shipping type.
|
4
|
+
TYPE = {
|
5
|
+
pac: 1,
|
6
|
+
sedex: 2,
|
7
|
+
not_specified: 3
|
8
|
+
}
|
9
|
+
|
10
|
+
# Define the error class for invalid type assignment.
|
11
|
+
InvalidShippingTypeError = Class.new(StandardError)
|
12
|
+
|
13
|
+
include Extensions::MassAssignment
|
14
|
+
include Extensions::EnsureType
|
15
|
+
|
16
|
+
# Define the shipping type id.
|
17
|
+
attr_reader :type_id
|
18
|
+
|
19
|
+
# Get the shipping type name.
|
20
|
+
attr_reader :type_name
|
21
|
+
|
22
|
+
# Get the address object.
|
23
|
+
attr_reader :address
|
24
|
+
|
25
|
+
# Set the shipping cost.
|
26
|
+
attr_accessor :cost
|
27
|
+
|
28
|
+
# Set the shipping address info.
|
29
|
+
def address=(address)
|
30
|
+
@address = ensure_type(Address, address)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Set the shipping type.
|
34
|
+
# It raises the PagSeguro::Shipping::InvalidShippingTypeError exception
|
35
|
+
# when trying to assign an invalid type name.
|
36
|
+
def type_name=(type_name)
|
37
|
+
type_name = type_name.to_sym
|
38
|
+
@type_id = TYPE.fetch(type_name) {
|
39
|
+
raise InvalidShippingTypeError, "invalid #{type_name.inspect} type name"
|
40
|
+
}
|
41
|
+
@type_name = type_name
|
42
|
+
end
|
43
|
+
|
44
|
+
# Set the shipping type id.
|
45
|
+
# It raises the PagSeguro::Shipping::InvalidShippingTypeError exception
|
46
|
+
# when trying to assign an invalid type id.
|
47
|
+
def type_id=(id)
|
48
|
+
type_id = id.to_i
|
49
|
+
|
50
|
+
raise InvalidShippingTypeError,
|
51
|
+
"invalid #{id.inspect} type id" unless TYPE.value?(type_id)
|
52
|
+
|
53
|
+
@type_id = type_id
|
54
|
+
@type_name = TYPE.key(type_id)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|