Class: PayPal::ExpressCheckout::Response::Base
- Inherits:
-
Object
- Object
- PayPal::ExpressCheckout::Response::Base
show all
- Extended by:
- Utils
- Includes:
- Fields
- Defined in:
- lib/paypal/express_checkout/response/base.rb
Direct Known Subclasses
Account, Address, Authorization, CallbackRequest, CallbackResponse, Capture, Checkout, Details, ManageProfile, Notification, Payment, Profile, Reference, Refund, Search, TransactionDetails
Constant Summary
Constants included
from Fields
Fields::ASSOCIATIONS, Fields::ATTRIBUTES
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#build_association(association, association_class, initial_fields, prefix = "L_", suffix = "n", object = self) ⇒ Object
-
#build_billings(prefix = "L_", suffix = "n") ⇒ Object
-
#build_fmfs(prefix = "L_", suffix = "n") ⇒ Object
-
#build_payment_items(prefix = "L_", suffix = "n", object = self) ⇒ Object
-
#build_payments(prefix = 'PAYMENTREQUEST_n_', suffix = '') ⇒ Object
-
#build_shipping_options(prefix = "L_", suffix = "n") ⇒ Object
-
#build_values ⇒ Object
-
#errors ⇒ Object
-
#initialize(response = nil) ⇒ Base
constructor
-
#params ⇒ Object
-
#success? ⇒ Boolean
-
#valid? ⇒ Boolean
Methods included from Utils
convert_to_time, mapping
Methods included from Fields
#field_map, #group_collect, #group_fields, #has_fields?, #has_many?, included, #inverted_field_map
Constructor Details
#initialize(response = nil) ⇒ Base
Returns a new instance of Base.
12
13
14
15
|
# File 'lib/paypal/express_checkout/response/base.rb', line 12
def initialize(response = nil)
@response = response
build_values
end
|
Instance Attribute Details
#response ⇒ Object
Returns the value of attribute response.
8
9
10
|
# File 'lib/paypal/express_checkout/response/base.rb', line 8
def response
@response
end
|
Instance Method Details
#build_association(association, association_class, initial_fields, prefix = "L_", suffix = "n", object = self) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/paypal/express_checkout/response/base.rb', line 68
def build_association(association, association_class, initial_fields, prefix="L_", suffix="n", object=self)
initial_fields = Array(initial_fields)
detection_fields = ASSOCIATIONS[association].map{|k,v| initial_fields.include?(k) ? v : nil}.compact
index = 0
index_prefix = prefix.gsub(/n/, index.to_s)
index_suffix = suffix.gsub(/n/, index.to_s)
while detection_fields.inject(false) { |memo, field| memo || params["#{index_prefix}#{field}#{index_suffix}"] }
attributes = {}
ASSOCIATIONS[association].invert.each do |name, method|
build_method = "build_#{method}"
value = params["#{index_prefix}#{name}#{index_suffix}"]
attributes[method] = object.respond_to?(build_method) ? object.send(build_method, value) : value
end
object.send("#{association}").send("<<", association_class.new(attributes))
index += 1
index_prefix = prefix.gsub(/n/, index.to_s)
index_suffix = suffix.gsub(/n/, index.to_s)
end
object.send(association)
end
|
#build_billings(prefix = "L_", suffix = "n") ⇒ Object
60
61
62
|
# File 'lib/paypal/express_checkout/response/base.rb', line 60
def build_billings(prefix="L_", suffix="n")
build_association(:billings, PayPal::ExpressCheckout::Billing, :type, prefix, suffix)
end
|
#build_fmfs(prefix = "L_", suffix = "n") ⇒ Object
64
65
66
|
# File 'lib/paypal/express_checkout/response/base.rb', line 64
def build_fmfs(prefix="L_", suffix="n")
build_association(:fmfs, PayPal::ExpressCheckout::Fmf, [:pending_id, :report_id, :deny_id], prefix, suffix)
end
|
#build_payment_items(prefix = "L_", suffix = "n", object = self) ⇒ Object
52
53
54
|
# File 'lib/paypal/express_checkout/response/base.rb', line 52
def build_payment_items(prefix="L_", suffix="n", object=self)
build_association(:payment_items, PayPal::ExpressCheckout::PaymentItem, :amount, prefix, suffix, object)
end
|
#build_payments(prefix = 'PAYMENTREQUEST_n_', suffix = '') ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/paypal/express_checkout/response/base.rb', line 40
def build_payments(prefix='PAYMENTREQUEST_n_', suffix='')
build_association(
:payments,
PayPal::ExpressCheckout::Payment,
:amount,
prefix,
suffix
).each_with_index do |p, index|
build_payment_items("L_#{prefix}".gsub(/n/, index.to_s), "n", p)
end
end
|
#build_shipping_options(prefix = "L_", suffix = "n") ⇒ Object
56
57
58
|
# File 'lib/paypal/express_checkout/response/base.rb', line 56
def build_shipping_options(prefix="L_", suffix="n")
build_association(:shipping_options, PayPal::ExpressCheckout::ShippingOption, :name, prefix, suffix)
end
|
#build_values ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/paypal/express_checkout/response/base.rb', line 23
def build_values
params.each_pair do |key, value|
unless key.to_s =~ /^L\_|\_(\d)+\_/
attr_name = inverted_field_map[key.to_s]
if respond_to?("#{attr_name}=")
value = send("build_#{attr_name}", value) if respond_to?("build_#{attr_name}")
send("#{attr_name}=", value)
end
end
end
self.class.associations.each do |association|
if respond_to?("build_#{association}")
send("build_#{association}")
end
end
end
|
#errors ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/paypal/express_checkout/response/base.rb', line 89
def errors
@errors ||= begin
index = 0
[].tap do |errors|
while params["L_ERRORCODE#{index}"]
errors << {
:code => params["L_ERRORCODE#{index}"],
:messages => [
params["L_SHORTMESSAGE#{index}"],
params["L_LONGMESSAGE#{index}"]
]
}
index += 1
end
end
end
end
|
#params ⇒ Object
17
18
19
20
21
|
# File 'lib/paypal/express_checkout/response/base.rb', line 17
def params
@params ||= CGI.parse(response.body_str).inject({}) do |buffer, (name, value)|
buffer.merge(name => value.first)
end
end
|
#success? ⇒ Boolean
108
109
110
|
# File 'lib/paypal/express_checkout/response/base.rb', line 108
def success?
ack == "Success"
end
|
#valid? ⇒ Boolean
112
113
114
|
# File 'lib/paypal/express_checkout/response/base.rb', line 112
def valid?
errors.empty? && success?
end
|