Class: Killbill::PaypalExpress::PaypalExpressResponse

Inherits:
Killbill::Plugin::ActiveMerchant::ActiveRecord::Response
  • Object
show all
Defined in:
lib/paypal_express/models/response.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cancel_pending_payment(transaction_plugin_info) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/paypal_express/models/response.rb', line 91

def self.cancel_pending_payment(transaction_plugin_info)
   where( :api_call => 'build_form_descriptor',
          :kb_payment_id => transaction_plugin_info.kb_payment_id,
          :kb_payment_transaction_id => transaction_plugin_info.kb_transaction_payment_id).update_all( :success => false,
                                                                                                       :updated_at => Time.now.utc,
                                                                                                       :message => { :payment_plugin_status => :CANCELED, :exception_message => 'Token expired. Payment Canceled by Janitor.' }.to_json)
end

.create_response(api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, payment_processor_account_id, kb_tenant_id, gw_response, extra_params = {}, model = ::Killbill::PaypalExpress::PaypalExpressResponse) ⇒ Object



66
67
68
69
70
# File 'lib/paypal_express/models/response.rb', line 66

def self.create_response(api_call, , kb_payment_id, kb_payment_transaction_id, transaction_type, , kb_tenant_id, gw_response, extra_params = {}, model = ::Killbill::PaypalExpress::PaypalExpressResponse)
  response = model.from_response(api_call, , kb_payment_id, kb_payment_transaction_id, transaction_type, , kb_tenant_id, gw_response, extra_params, model)
  response.save!(shared_activerecord_options)
  response
end

.from_response(api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, payment_processor_account_id, kb_tenant_id, response, extra_params = {}, model = ::Killbill::PaypalExpress::PaypalExpressResponse) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/paypal_express/models/response.rb', line 13

def self.from_response(api_call, , kb_payment_id, kb_payment_transaction_id, transaction_type, , kb_tenant_id, response, extra_params = {}, model = ::Killbill::PaypalExpress::PaypalExpressResponse)
  super(api_call,
        ,
        kb_payment_id,
        kb_payment_transaction_id,
        transaction_type,
        ,
        kb_tenant_id,
        response,
        {
            :token                                  => extract(response, 'Token'),
            :payer_id                               => extract(response, 'PayerInfo', 'PayerID'),
            :billing_agreement_id                   => extract(response, 'billing_agreement_id'),
            :payer_name                             => [extract(response, 'PayerInfo', 'PayerName', 'FirstName'), extract(response, 'PayerInfo', 'PayerName', 'MiddleName'), extract(response, 'PayerInfo', 'PayerName', 'LastName')].compact.join(' '),
            :payer_email                            => extract(response, 'PayerInfo', 'Payer'),
            :payer_country                          => extract(response, 'PayerInfo', 'PayerCountry'),
            :contact_phone                          => extract(response, 'ContactPhone'),
            :ship_to_address_name                   => extract(response, 'ShipToAddress', 'Name'),
            :ship_to_address_company                => extract(response, 'PayerInfo', 'PayerBusiness'),
            :ship_to_address_address1               => extract(response, 'ShipToAddress', 'Street1'),
            :ship_to_address_address2               => extract(response, 'ShipToAddress', 'Street2'),
            :ship_to_address_city                   => extract(response, 'ShipToAddress', 'CityName'),
            :ship_to_address_state                  => extract(response, 'ShipToAddress', 'StateOrProvince'),
            :ship_to_address_country                => extract(response, 'ShipToAddress', 'Country'),
            :ship_to_address_zip                    => extract(response, 'ShipToAddress', 'PostalCode'),
            :ship_to_address_phone                  => (extract(response, 'ContactPhone') || extract(response, 'ShipToAddress', 'Phone')),
            :receiver_info_business                 => (extract(response, 'ReceiverInfo', 'Business') || extract(response, 'PaymentTransactionDetails', 'ReceiverInfo', 'Business')),
            :receiver_info_receiver                 => (extract(response, 'ReceiverInfo', 'Receiver') || extract(response, 'PaymentTransactionDetails', 'ReceiverInfo', 'Receiver')),
            :receiver_info_receiverid               => (extract(response, 'ReceiverInfo', 'ReceiverID') || extract(response, 'PaymentTransactionDetails', 'ReceiverInfo', 'ReceiverID')),
            :payment_info_transactionid             => (extract(response, 'PaymentInfo', 'TransactionID') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'TransactionID')),
            :payment_info_parenttransactionid       => (extract(response, 'PaymentInfo', 'ParentTransactionID') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ParentTransactionID')),
            :payment_info_receiptid                 => (extract(response, 'PaymentInfo', 'ReceiptID') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ReceiptID')),
            :payment_info_transactiontype           => (extract(response, 'PaymentInfo', 'TransactionType') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'TransactionType')),
            :payment_info_paymenttype               => (extract(response, 'PaymentInfo', 'PaymentType') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'PaymentType')),
            :payment_info_paymentdate               => (extract(response, 'PaymentInfo', 'PaymentDate') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'PaymentDate')),
            :payment_info_grossamount               => (extract(response, 'PaymentInfo', 'GrossAmount') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'GrossAmount')),
            :payment_info_feeamount                 => (extract(response, 'PaymentInfo', 'FeeAmount') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'FeeAmount')),
            :payment_info_taxamount                 => (extract(response, 'PaymentInfo', 'TaxAmount') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'TaxAmount')),
            :payment_info_exchangerate              => (extract(response, 'PaymentInfo', 'ExchangeRate') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ExchangeRate')),
            :payment_info_paymentstatus             => (extract(response, 'PaymentInfo', 'PaymentStatus') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'PaymentStatus')),
            :payment_info_pendingreason             => (extract(response, 'PaymentInfo', 'PendingReason') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'PendingReason')),
            :payment_info_reasoncode                => (ignore_none(extract(response, 'PaymentInfo', 'ReasonCode')) || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ReasonCode') || extract(response, 'error_codes')),
            :payment_info_protectioneligibility     => (extract(response, 'PaymentInfo', 'ProtectionEligibility') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ProtectionEligibility')),
            :payment_info_protectioneligibilitytype => (extract(response, 'PaymentInfo', 'ProtectionEligibilityType') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ProtectionEligibilityType')),
            :payment_info_shipamount                => (extract(response, 'PaymentInfo', 'ShipAmount') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ShipAmount')),
            :payment_info_shiphandleamount          => (extract(response, 'PaymentInfo', 'ShipHandleAmount') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ShipHandleAmount')),
            :payment_info_shipdiscount              => (extract(response, 'PaymentInfo', 'ShipDiscount') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'ShipDiscount')),
            :payment_info_insuranceamount           => (extract(response, 'PaymentInfo', 'InsuranceAmount') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'InsuranceAmount')),
            :payment_info_subject                   => (extract(response, 'PaymentInfo', 'Subject') || extract(response, 'PaymentTransactionDetails', 'PaymentInfo', 'Subject'))
        }.merge!(extra_params),
        model)
end

.ignore_none(value) ⇒ Object



9
10
11
# File 'lib/paypal_express/models/response.rb', line 9

def self.ignore_none(value)
  value == 'none' ? nil : value
end

.initial_payment_account_processor_id(kb_account_id, kb_tenant_id, token) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/paypal_express/models/response.rb', line 81

def self.(, kb_tenant_id, token)
  return nil if token.blank?
  response = where(:api_call => 'initiate_express_checkout',
                   :success => true,
                   :kb_account_id => ,
                   :kb_tenant_id => kb_tenant_id,
                   :token => token).last
  response.nil? ? nil : response.
end

.last_token(kb_account_id, kb_tenant_id) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/paypal_express/models/response.rb', line 73

def self.last_token(, kb_tenant_id)
  response = where(:api_call => 'initiate_express_checkout',
                   :success => true,
                   :kb_account_id => ,
                   :kb_tenant_id => kb_tenant_id).last
  response.nil? ? nil : response.token
end

.sensitive_fieldsObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/paypal_express/models/response.rb', line 99

def self.sensitive_fields
  [:payer_name,
   :contact_phone,
   :ship_to_address_name,
   :ship_to_address_company,
   :ship_to_address_address1,
   :ship_to_address_address2,
   :ship_to_address_city,
   :ship_to_address_state,
   :ship_to_address_country,
   :ship_to_address_zip,
   :ship_to_address_phone]
end

Instance Method Details

#gateway_error_codeObject



113
114
115
# File 'lib/paypal_express/models/response.rb', line 113

def gateway_error_code
  payment_info_reasoncode
end

#to_transaction_info_plugin(transaction = nil) ⇒ Object



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
# File 'lib/paypal_express/models/response.rb', line 117

def to_transaction_info_plugin(transaction=nil)
  t_info_plugin = super(transaction)

  t_info_plugin.properties << create_plugin_property('message', token)
  t_info_plugin.properties << create_plugin_property('payerId', payer_id)
  t_info_plugin.properties << create_plugin_property('baid', billing_agreement_id)
  t_info_plugin.properties << create_plugin_property('payerName', payer_name)
  t_info_plugin.properties << create_plugin_property('payerEmail', payer_email)
  t_info_plugin.properties << create_plugin_property('payerCountry', payer_country)
  t_info_plugin.properties << create_plugin_property('contactPhone', contact_phone)
  t_info_plugin.properties << create_plugin_property('shipToAddressName', ship_to_address_name)
  t_info_plugin.properties << create_plugin_property('shipToAddressPayerBusiness', ship_to_address_company)
  t_info_plugin.properties << create_plugin_property('shipToAddressStreet1', ship_to_address_address1)
  t_info_plugin.properties << create_plugin_property('shipToAddressStreet2', ship_to_address_address2)
  t_info_plugin.properties << create_plugin_property('shipToAddressCityName', ship_to_address_city)
  t_info_plugin.properties << create_plugin_property('shipToAddressStateOrProvince', ship_to_address_state)
  t_info_plugin.properties << create_plugin_property('shipToAddressCountry', ship_to_address_country)
  t_info_plugin.properties << create_plugin_property('shipToAddressPostalCode', ship_to_address_zip)
  t_info_plugin.properties << create_plugin_property('shipToAddressContactPhone', ship_to_address_phone)
  t_info_plugin.properties << create_plugin_property('receiverInfoBusiness', receiver_info_business)
  t_info_plugin.properties << create_plugin_property('receiverInfoReceiver', receiver_info_receiver)
  t_info_plugin.properties << create_plugin_property('receiverInfoReceiverID', receiver_info_receiverid)
  t_info_plugin.properties << create_plugin_property('paymentInfoTransactionID', payment_info_transactionid)
  t_info_plugin.properties << create_plugin_property('paymentInfoParentTransactionID', payment_info_parenttransactionid)
  t_info_plugin.properties << create_plugin_property('paymentInfoReceiptID', payment_info_receiptid)
  t_info_plugin.properties << create_plugin_property('paymentInfoTransactionType', payment_info_transactiontype)
  t_info_plugin.properties << create_plugin_property('paymentInfoPaymentType', payment_info_paymenttype)
  t_info_plugin.properties << create_plugin_property('paymentInfoPaymentDate', payment_info_paymentdate)
  t_info_plugin.properties << create_plugin_property('paymentInfoGrossAmount', payment_info_grossamount)
  t_info_plugin.properties << create_plugin_property('paymentInfoFeeAmount', payment_info_feeamount)
  t_info_plugin.properties << create_plugin_property('paymentInfoTaxAmount', payment_info_taxamount)
  t_info_plugin.properties << create_plugin_property('paymentInfoExchangeRate', payment_info_exchangerate)
  t_info_plugin.properties << create_plugin_property('paymentInfoPaymentStatus', payment_info_paymentstatus)
  t_info_plugin.properties << create_plugin_property('paymentInfoPendingReason', payment_info_pendingreason)
  t_info_plugin.properties << create_plugin_property('paymentInfoReasonCode', payment_info_reasoncode)
  t_info_plugin.properties << create_plugin_property('paymentInfoProtectionEligibility', payment_info_protectioneligibility)
  t_info_plugin.properties << create_plugin_property('paymentInfoProtectionEligibilityType', payment_info_protectioneligibilitytype)
  t_info_plugin.properties << create_plugin_property('paymentInfoShipAmount', payment_info_shipamount)
  t_info_plugin.properties << create_plugin_property('paymentInfoShipHandleAmount', payment_info_shiphandleamount)
  t_info_plugin.properties << create_plugin_property('paymentInfoShipDiscount', payment_info_shipdiscount)
  t_info_plugin.properties << create_plugin_property('paymentInfoInsuranceAmount', payment_info_insuranceamount)
  t_info_plugin.properties << create_plugin_property('paymentInfoSubject', payment_info_subject)
  t_info_plugin.properties << create_plugin_property('paypalExpressResponseId', id)

  t_info_plugin
end

#transition_to_failure(is_plugin_failure = true, params = {}) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/paypal_express/models/response.rb', line 172

def transition_to_failure(is_plugin_failure = true, params = {})
  begin
    error_details = JSON.parse(message)
    original_message = nil
  rescue
    error_details = {}
    original_message = message
  end
  error_details['original_message'] = original_message unless original_message.blank?
  error_details['payment_plugin_status'] = is_plugin_failure ? 'CANCELED' : 'ERROR'

  updated_attributes = {
      :message => error_details.to_json,
      :success => false,
      :updated_at => Time.now.utc
  }.merge!(params)

  # Update the response row
  update!(updated_attributes)
end

#transition_to_payment_failure(transaction_id) ⇒ Object



168
169
170
# File 'lib/paypal_express/models/response.rb', line 168

def transition_to_payment_failure(transaction_id)
  transition_to_failure false, {:authorization => transaction_id}
end

#transition_to_plugin_failureObject



164
165
166
# File 'lib/paypal_express/models/response.rb', line 164

def transition_to_plugin_failure
  transition_to_failure
end

#transition_to_success(transaction_id, amount, currency) ⇒ Object



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
# File 'lib/paypal_express/models/response.rb', line 193

def transition_to_success(transaction_id, amount, currency)
  begin
    new_message = JSON.parse(message)
    original_message = nil
  rescue
    new_message = {}
    original_message = message
  end
  new_message['original_message'] = original_message unless original_message.blank?
  new_message['payment_plugin_status'] = 'PROCESSED'
  new_message['janitor'] = 'Janitor transitioned the response to success'

  updated_attributes = {
      :message => new_message.to_json,
      :authorization => transaction_id,
      :success => true,
      :updated_at => Time.now.utc
  }

  # Update the response row
  update!(updated_attributes)

  amount_in_cents = nil
  unless amount.nil?
    amount_in_cents = Monetize.from_numeric(amount.to_f, currency).cents.to_i
    # Paypal search api returns a negative amount in case of successful refund
    amount_in_cents = -amount_in_cents if amount_in_cents < 0
  end
  # Create the transaction row if needed (cannot have been created before or the state wouldn't have been UNDEFINED)
  build_paypal_express_transaction(:kb_account_id => ,
                                   :kb_tenant_id => kb_tenant_id,
                                   :amount_in_cents => amount_in_cents,
                                   :currency => currency,
                                   :api_call => api_call,
                                   :kb_payment_id => kb_payment_id,
                                   :kb_payment_transaction_id => kb_payment_transaction_id,
                                   :transaction_type => transaction_type,
                                   :payment_processor_account_id => ,
                                   :txn_id => txn_id,
                                   :created_at => updated_at,
                                   :updated_at => updated_at).save!
end