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,
  :BUILD => :build,
  :BILLINGAGREEMENTACCEPTEDSTATUS => :billing_agreement_accepted_status,
  :CHECKOUTSTATUS => :checkout_status,
  :CORRELATIONID => :correlation_id,
  :COUNTRYCODE => :country_code,
  :CURRENCYCODE => :currency_code,
  :DESC => :description,
  :NOTIFYURL => :notify_url,
  :TIMESTAMP => :timestamp,
  :TOKEN => :token,
  :VERSION => :version,
  # Some of the attributes below are duplicates of what
  # exists in the payment response, but paypal doesn't
  # prefix these with PAYMENTREQUEST when issuing a
  # GetTransactionDetails response.
  :RECEIVEREMAIL => :receiver_email,
  :RECEIVERID => :receiver_id,
  :SUBJECT => :subject,
  :TRANSACTIONID => :transaction_id,
  :TRANSACTIONTYPE => :transaction_type,
  :PAYMENTTYPE => :payment_type,
  :ORDERTIME => :order_time,
  :PAYMENTSTATUS => :payment_status,
  :PENDINGREASON => :pending_reason,
  :REASONCODE => :reason_code,
  :PROTECTIONELIGIBILITY => :protection_eligibility,
  :PROTECTIONELIGIBILITYTYPE => :protection_eligibility_type,
  :ADDRESSOWNER => :address_owner,
  :ADDRESSSTATUS => :address_status,
  :INVNUM => :invoice_number,
  :CUSTOM => :custom
}

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.



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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/paypal/nvp/response.rb', line 47

def initialize(attributes = {})
  @raw_response = 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),
    :fee => attrs.delete(:FEEAMT)
  )
  @ship_to = Payment::Response::Address.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)
  )
  @bill_to = Payment::Response::Address.new(
    :owner => attrs.delete(:ADDRESSID),
    :status => attrs.delete(:ADDRESSSTATUS),
    :name => attrs.delete(:BILLINGNAME),
    :zip => attrs.delete(:ZIP),
    :street => attrs.delete(:STREET),
    :street2 => attrs.delete(:STREET2),
    :city => attrs.delete(:CITY),
    :state => attrs.delete(:STATE),
    :country_code => attrs.delete(:COUNTRY)
  )
  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),
      :company => attrs.delete(:BUSINESS)
    )
  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 => {
          :period => attrs.delete(:TRIALBILLINGPERIOD),
          :frequency => attrs.delete(:TRIALBILLINGFREQUENCY),
          :total_cycles => attrs.delete(:TRIALTOTALBILLINGCYCLES),
          :currency_code => attrs.delete(:TRIALCURRENCYCODE),
          :amount => attrs.delete(:TRIALAMT),
          :tax_amount => attrs.delete(:TRIALTAXAMT),
          :shipping_amount => attrs.delete(:TRIALSHIPPINGAMT),
          :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
  if attrs[:BILLINGAGREEMENTID]
    @billing_agreement = Payment::Response::Reference.new(
      :identifier => attrs.delete(:BILLINGAGREEMENTID),
      :description => attrs.delete(:BILLINGAGREEMENTDESCRIPTION),
      :status => attrs.delete(:BILLINGAGREEMENTSTATUS)
    )
    billing_agreement_info = Payment::Response::Info.attribute_mapping.keys.inject({}) do |billing_agreement_info, key|
      billing_agreement_info.merge! key => attrs.delete(key)
    end
    @billing_agreement.info = Payment::Response::Info.new billing_agreement_info
    @billing_agreement.info.amount = @amount
  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

  # 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
  attrs.delete(:SALESTAX) # Same as TAXAMT

  # 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.



43
44
45
# File 'lib/paypal/nvp/response.rb', line 43

def amount
  @amount
end

#bill_toObject

Returns the value of attribute bill_to.



43
44
45
# File 'lib/paypal/nvp/response.rb', line 43

def bill_to
  @bill_to
end

#billing_agreementObject

Returns the value of attribute billing_agreement.



43
44
45
# File 'lib/paypal/nvp/response.rb', line 43

def billing_agreement
  @billing_agreement
end

#descriptionObject

Returns the value of attribute description.



43
44
45
# File 'lib/paypal/nvp/response.rb', line 43

def description
  @description
end

#insurance_option_selectedObject

Returns the value of attribute insurance_option_selected.



42
43
44
# File 'lib/paypal/nvp/response.rb', line 42

def insurance_option_selected
  @insurance_option_selected
end

#itemsObject

Returns the value of attribute items.



44
45
46
# File 'lib/paypal/nvp/response.rb', line 44

def items
  @items
end

#payerObject

Returns the value of attribute payer.



43
44
45
# File 'lib/paypal/nvp/response.rb', line 43

def payer
  @payer
end

#payment_infoObject

Returns the value of attribute payment_info.



44
45
46
# File 'lib/paypal/nvp/response.rb', line 44

def payment_info
  @payment_info
end

#payment_responsesObject

Returns the value of attribute payment_responses.



44
45
46
# File 'lib/paypal/nvp/response.rb', line 44

def payment_responses
  @payment_responses
end

#recurringObject

Returns the value of attribute recurring.



43
44
45
# File 'lib/paypal/nvp/response.rb', line 43

def recurring
  @recurring
end

#refundObject

Returns the value of attribute refund.



43
44
45
# File 'lib/paypal/nvp/response.rb', line 43

def refund
  @refund
end

#ship_toObject

Returns the value of attribute ship_to.



43
44
45
# File 'lib/paypal/nvp/response.rb', line 43

def ship_to
  @ship_to
end

#shipping_options_is_defaultObject

Returns the value of attribute shipping_options_is_default.



42
43
44
# File 'lib/paypal/nvp/response.rb', line 42

def shipping_options_is_default
  @shipping_options_is_default
end

#success_page_redirect_requestedObject

Returns the value of attribute success_page_redirect_requested.



42
43
44
# File 'lib/paypal/nvp/response.rb', line 42

def success_page_redirect_requested
  @success_page_redirect_requested
end

Instance Method Details

#to_jsonObject



241
242
243
# File 'lib/paypal/nvp/response.rb', line 241

def to_json
  @raw_response.to_json
end

#to_sObject



237
238
239
# File 'lib/paypal/nvp/response.rb', line 237

def to_s
  @raw_response.to_s
end