Class: Killbill::Cybersource::CybersourceResponse

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

Constant Summary collapse

UNDEFINED_ERROR_CODES =
[ 151, 152, 250 ]
CANCELED_ERROR_CODES =
[ 101, 102, 104, 150, 207, 232, 234, 235, 236, 237, 238, 239, 240, 241, 243, 246, 247, 254 ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cybersource_response_params(response) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cybersource/models/response.rb', line 25

def self.cybersource_response_params(response)
  {
      :params_merchant_reference_code => extract(response, 'merchantReferenceCode'),
      :params_request_id => extract(response, 'requestID'),
      :params_decision => extract(response, 'decision'),
      :params_reason_code => extract(response, 'reasonCode'),
      :params_request_token => extract(response, 'requestToken'),
      :params_currency => extract(response, 'currency'),
      :params_amount => extract(response, 'amount'),
      :params_authorization_code => extract(response, 'authorizationCode'),
      :params_avs_code => extract(response, 'avsCode'),
      :params_avs_code_raw => extract(response, 'avsCodeRaw'),
      :params_cv_code => extract(response, 'cvCode'),
      :params_authorized_date_time => extract(response, 'authorizedDateTime'),
      :params_processor_response => extract(response, 'processorResponse'),
      :params_reconciliation_id => extract(response, 'reconciliationID') || extract(response, 'reconciliation_id'), # when failure, use passed-in reconciliation id
      :params_subscription_id => extract(response, 'subscriptionID'),
  }
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::Cybersource::CybersourceResponse) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cybersource/models/response.rb', line 12

def self.from_response(api_call, , kb_payment_id, kb_payment_transaction_id, transaction_type, , kb_tenant_id, response, extra_params = {}, model = ::Killbill::Cybersource::CybersourceResponse)
  super(api_call,
        ,
        kb_payment_id,
        kb_payment_transaction_id,
        transaction_type,
        ,
        kb_tenant_id,
        response,
        cybersource_response_params(response).merge!(extra_params),
        model)
end

Instance Method Details

#cancelObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cybersource/models/response.rb', line 45

def cancel
  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'] = 'CANCELED'

  updated_attributes = {
      :message => error_details.to_json,
      :success => false,
      :updated_at => Time.now.utc
  }

  # Update the response row
  update!(updated_attributes)
end

#first_reference_idObject



108
109
110
# File 'lib/cybersource/models/response.rb', line 108

def first_reference_id
  params_request_id
end

#gateway_error_codeObject



116
117
118
# File 'lib/cybersource/models/response.rb', line 116

def gateway_error_code
  params_reason_code
end

#second_reference_idObject



112
113
114
# File 'lib/cybersource/models/response.rb', line 112

def second_reference_id
  params_reconciliation_id
end

#set_correct_status(t_info_plugin) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cybersource/models/response.rb', line 131

def set_correct_status(t_info_plugin)
  # Respect the existing status if the payment was successful, if overridden or if there is no error code
  return if success || (message && message.strip.start_with?('{')) || gateway_error_code.blank?

  if CANCELED_ERROR_CODES.include?(gateway_error_code.to_i)
    t_info_plugin.status = :CANCELED
  elsif UNDEFINED_ERROR_CODES.include?(gateway_error_code.to_i)
    t_info_plugin.status = :UNDEFINED
  else
    t_info_plugin.status = :ERROR
  end
end

#to_transaction_info_plugin(transaction = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/cybersource/models/response.rb', line 120

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

  t_info_plugin.properties << create_plugin_property('cybersourceResponseId', id)
  t_info_plugin.properties << create_plugin_property('processorResponse', params_processor_response)

  set_correct_status(t_info_plugin)

  t_info_plugin
end

#update_and_create_transaction(gw_response) ⇒ Object



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

def update_and_create_transaction(gw_response)
  updated_attributes = {
      :message => gw_response.message,
      :authorization => gw_response.authorization,
      :fraud_review => gw_response.fraud_review?,
      :test => gw_response.test?,
      :avs_result_code => gw_response.avs_result.kind_of?(::ActiveMerchant::Billing::AVSResult) ? gw_response.avs_result.code : gw_response.avs_result['code'],
      :avs_result_message => gw_response.avs_result.kind_of?(::ActiveMerchant::Billing::AVSResult) ? gw_response.avs_result.message : gw_response.avs_result['message'],
      :avs_result_street_match => gw_response.avs_result.kind_of?(::ActiveMerchant::Billing::AVSResult) ? gw_response.avs_result.street_match : gw_response.avs_result['street_match'],
      :avs_result_postal_match => gw_response.avs_result.kind_of?(::ActiveMerchant::Billing::AVSResult) ? gw_response.avs_result.postal_match : gw_response.avs_result['postal_match'],
      :cvv_result_code => gw_response.cvv_result.kind_of?(::ActiveMerchant::Billing::CVVResult) ? gw_response.cvv_result.code : gw_response.cvv_result['code'],
      :cvv_result_message => gw_response.cvv_result.kind_of?(::ActiveMerchant::Billing::CVVResult) ? gw_response.cvv_result.message : gw_response.cvv_result['message'],
      :success => gw_response.success?,
      :updated_at => Time.now.utc
  }.merge(CybersourceResponse.cybersource_response_params(gw_response))

  # Keep original values as much as possible
  updated_attributes.delete_if { |k, v| v.blank? }

  # Update the response row
  update!(updated_attributes)

  # Create the transaction row if needed (cannot have been created before or the state wouldn't have been UNDEFINED)
  if gw_response.success?
    amount = gw_response.params['amount']
    currency = gw_response.params['currency']
    amount_in_cents = amount.nil? ? nil : ::Monetize.from_numeric(amount.to_f, currency).cents.to_i
    build_cybersource_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
end