Class: Paypal::NVP::Response

Inherits:
Base
  • Object
show all
Defined in:
lib/paypal/nvp/response.rb

Direct Known Subclasses

Express::Response

Constant Summary collapse

@@attribute_mapping =
{
  :ACK => :ack,
  :ADDRESSSTATUS => :address_status,
  :BILLINGAGREEMENTACCEPTEDSTATUS => :billing_agreement_accepted_status,
  :BUILD => :build,
  :CHECKOUTSTATUS => :checkout_status,
  :CORRELATIONID => :colleration_id,
  :COUNTRYCODE => :country_code,
  :CURRENCYCODE => :currency_code,
  :DESC => :description,
  :NOTIFYURL => :notify_url,
  :TIMESTAMP => :timestamp,
  :TOKEN => :token,
  :VERSION => :version
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#==, formatted_amount, #numeric_attribute?, to_numeric

Constructor Details

#initialize(attributes = {}) ⇒ Response

Returns a new instance of Response.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/paypal/nvp/response.rb', line 24

def initialize(attributes = {})
  attrs = attributes.dup
  @@attribute_mapping.each do |key, value|
    self.send "#{value}=", attrs.delete(key)
  end
  @shipping_options_is_default = attrs.delete(:SHIPPINGOPTIONISDEFAULT) == 'true'
  @success_page_redirect_requested = attrs.delete(:SUCCESSPAGEREDIRECTREQUESTED) == 'true'
  @insurance_option_selected = attrs.delete(:INSURANCEOPTIONSELECTED) == 'true'
  @amount = Payment::Common::Amount.new(
    :total => attrs.delete(:AMT),
    :item => attrs.delete(:ITEMAMT),
    :handing => attrs.delete(:HANDLINGAMT),
    :insurance => attrs.delete(:INSURANCEAMT),
    :ship_disc => attrs.delete(:SHIPDISCAMT),
    :shipping => attrs.delete(:SHIPPINGAMT),
    :tax => attrs.delete(:TAXAMT)
  )
  @ship_to = Payment::Response::ShipTo.new(
    :owner => attrs.delete(:SHIPADDRESSOWNER),
    :status => attrs.delete(:SHIPADDRESSSTATUS),
    :name => attrs.delete(:SHIPTONAME),
    :zip => attrs.delete(:SHIPTOZIP),
    :street => attrs.delete(:SHIPTOSTREET),
    :street2 => attrs.delete(:SHIPTOSTREET2),
    :city => attrs.delete(:SHIPTOCITY),
    :state => attrs.delete(:SHIPTOSTATE),
    :country_code => attrs.delete(:SHIPTOCOUNTRYCODE),
    :country_name => attrs.delete(:SHIPTOCOUNTRYNAME)
  )
  if attrs[:PAYERID]
    @payer = Payment::Response::Payer.new(
      :identifier => attrs.delete(:PAYERID),
      :status => attrs.delete(:PAYERSTATUS),
      :first_name => attrs.delete(:FIRSTNAME),
      :last_name => attrs.delete(:LASTNAME),
      :email => attrs.delete(:EMAIL)
    )
  end
  if attrs[:REFUNDTRANSACTIONID]
    @refund = Payment::Response::Refund.new(
      :transaction_id => attrs.delete(:REFUNDTRANSACTIONID),
      :amount => {
        :total => attrs.delete(:TOTALREFUNDEDAMOUNT),
        :fee => attrs.delete(:FEEREFUNDAMT),
        :gross => attrs.delete(:GROSSREFUNDAMT),
        :net => attrs.delete(:NETREFUNDAMT)
      }
    )
  end
  if attrs[:PROFILEID]
    @recurring = Payment::Recurring.new(
      :identifier => attrs.delete(:PROFILEID),
      # NOTE:
      #  CreateRecurringPaymentsProfile returns PROFILESTATUS
      #  GetRecurringPaymentsProfileDetails returns STATUS
      :description => @description,
      :status => attrs.delete(:STATUS) || attrs.delete(:PROFILESTATUS),
      :start_date => attrs.delete(:PROFILESTARTDATE),
      :name => attrs.delete(:SUBSCRIBERNAME),
      :reference => attrs.delete(:PROFILEREFERENCE),
      :auto_bill => attrs.delete(:AUTOBILLOUTAMT),
      :max_fails => attrs.delete(:MAXFAILEDPAYMENTS),
      :aggregate_amount => attrs.delete(:AGGREGATEAMT),
      :aggregate_optional_amount => attrs.delete(:AGGREGATEOPTIONALAMT),
      :final_payment_date => attrs.delete(:FINALPAYMENTDUEDATE)
    )
    if attrs[:BILLINGPERIOD]
      @recurring.billing = Payment::Recurring::Billing.new(
        :amount => @amount,
        :currency_code => @currency_code,
        :period => attrs.delete(:BILLINGPERIOD),
        :frequency => attrs.delete(:BILLINGFREQUENCY),
        :total_cycles => attrs.delete(:TOTALBILLINGCYCLES),
        :trial_amount_paid => attrs.delete(:TRIALAMTPAID)
      )
    end
    if attrs[:REGULARAMT]
      @recurring.regular_billing = Payment::Recurring::Billing.new(
        :amount => attrs.delete(:REGULARAMT),
        :shipping_amount => attrs.delete(:REGULARSHIPPINGAMT),
        :tax_amount => attrs.delete(:REGULARTAXAMT),
        :currency_code => attrs.delete(:REGULARCURRENCYCODE),
        :period => attrs.delete(:REGULARBILLINGPERIOD),
        :frequency => attrs.delete(:REGULARBILLINGFREQUENCY),
        :total_cycles => attrs.delete(:REGULARTOTALBILLINGCYCLES),
        :paid => attrs.delete(:REGULARAMTPAID)
      )
      @recurring.summary = Payment::Recurring::Summary.new(
        :next_billing_date => attrs.delete(:NEXTBILLINGDATE),
        :cycles_completed => attrs.delete(:NUMCYCLESCOMPLETED),
        :cycles_remaining => attrs.delete(:NUMCYCLESREMAINING),
        :outstanding_balance => attrs.delete(:OUTSTANDINGBALANCE),
        :failed_count => attrs.delete(:FAILEDPAYMENTCOUNT),
        :last_payment_date => attrs.delete(:LASTPAYMENTDATE),
        :last_payment_amount => attrs.delete(:LASTPAYMENTAMT)
      )
    end
  end

  # payment_responses
  payment_responses = []
  attrs.keys.each do |_attr_|
    prefix, index, key = case _attr_.to_s
    when /^PAYMENTREQUEST/, /^PAYMENTREQUESTINFO/
      _attr_.to_s.split('_')
    when /^L_PAYMENTREQUEST/
      _attr_.to_s.split('_')[1..-1]
    end
    if prefix
      payment_responses[index.to_i] ||= {}
      payment_responses[index.to_i][key.to_sym] = attrs.delete(_attr_)
    end
  end
  @payment_responses = payment_responses.collect do |_attrs_|
    Payment::Response.new _attrs_
  end

  # payment_info
  payment_info = []
  attrs.keys.each do |_attr_|
    prefix, index, key = _attr_.to_s.split('_')
    if prefix == 'PAYMENTINFO'
      payment_info[index.to_i] ||= {}
      payment_info[index.to_i][key.to_sym] = attrs.delete(_attr_)
    end
  end
  @payment_info = payment_info.collect do |_attrs_|
    Payment::Response::Info.new _attrs_
  end

  # payment_info
  items = []
  attrs.keys.each do |_attr_|
    key, index = _attr_.to_s.scan(/^L_(.+)(\d)$/).flatten
    if index
      items[index.to_i] ||= {}
      items[index.to_i][key.to_sym] = attrs.delete(_attr_)
    end
  end
  @items = items.collect do |_attrs_|
    Payment::Response::Item.new _attrs_
  end

  # remove duplicated parameters
  attrs.delete(:SHIPTOCOUNTRY) # NOTE: Same with SHIPTOCOUNTRYCODE

  # warn ignored attrs
  attrs.each do |key, value|
    Paypal.log "Ignored Parameter (#{self.class}): #{key}=#{value}", :warn
  end
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def amount
  @amount
end

#descriptionObject

Returns the value of attribute description.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def description
  @description
end

#insurance_option_selectedObject

Returns the value of attribute insurance_option_selected.



21
22
23
# File 'lib/paypal/nvp/response.rb', line 21

def insurance_option_selected
  @insurance_option_selected
end

#itemsObject

Returns the value of attribute items.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def items
  @items
end

#payerObject

Returns the value of attribute payer.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def payer
  @payer
end

#payment_infoObject

Returns the value of attribute payment_info.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def payment_info
  @payment_info
end

#payment_responsesObject

Returns the value of attribute payment_responses.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def payment_responses
  @payment_responses
end

#recurringObject

Returns the value of attribute recurring.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def recurring
  @recurring
end

#refundObject

Returns the value of attribute refund.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def refund
  @refund
end

#ship_toObject

Returns the value of attribute ship_to.



22
23
24
# File 'lib/paypal/nvp/response.rb', line 22

def ship_to
  @ship_to
end

#shipping_options_is_defaultObject

Returns the value of attribute shipping_options_is_default.



21
22
23
# File 'lib/paypal/nvp/response.rb', line 21

def shipping_options_is_default
  @shipping_options_is_default
end

#success_page_redirect_requestedObject

Returns the value of attribute success_page_redirect_requested.



21
22
23
# File 'lib/paypal/nvp/response.rb', line 21

def success_page_redirect_requested
  @success_page_redirect_requested
end