Class: ActiveMerchant::Billing::NuveiGateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/active_merchant/billing/gateways/nuvei.rb

Constant Summary collapse

ENDPOINTS_MAPPING =
{
  authenticate: '/getSessionToken',
  purchase: '/payment', # /authorize with transactionType: "Auth"
  capture: '/settleTransaction',
  refund: '/refundTransaction',
  void: '/voidTransaction',
  general_credit: '/payout',
  init_payment: '/initPayment'
}
NETWORK_TOKENIZATION_CARD_MAPPING =
{
  'apple_pay' => 'ApplePay',
  'google_pay' => 'GooglePay'
}

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::RECURRING_DEPRECATION_MESSAGE, Gateway::STANDARD_ERROR_CODE

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods inherited from Gateway

#add_field_to_post_if_present, #add_fields_to_post_if_present, #card_brand, card_brand, #generate_unique_id, inherited, #supported_countries, supported_countries, supported_countries=, supports?, #supports_network_tokenization?, #test?

Methods included from CreditCardFormatting

#expdate, #format, #strftime_yyyymm

Methods included from PostsData

included, #raw_ssl_request, #ssl_get, #ssl_post, #ssl_request

Constructor Details

#initialize(options = {}) ⇒ NuveiGateway

Returns a new instance of NuveiGateway.



30
31
32
33
34
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 30

def initialize(options = {})
  requires!(options, :merchant_id, :merchant_site_id, :secret_key)
  super
  fetch_session_token unless session_token_valid?
end

Instance Method Details

#add_account_funding_transaction(post, payment, options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 151

def (post, payment, options = {})
  return unless options[:is_aft]

  recipient_details = {
    firstName: options[:aft_recipient_first_name],
    lastName: options[:aft_recipient_last_name]
  }.compact

  address_details = {
    firstName: payment.first_name,
    lastName: payment.last_name,
    country: options.dig(:billing_address, :country),
    address: options.dig(:billing_address, :address1),
    city: options.dig(:billing_address, :city),
    state: options.dig(:billing_address, :state)
  }.compact

  post[:billingAddress].merge!(address_details)
  post[:recipientDetails] = recipient_details unless recipient_details.empty?
end

#add_stored_credentials(post, payment, options = {}) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 126

def add_stored_credentials(post, payment, options = {})
  return unless options[:stored_credential]

  post[:savePM] = options[:save_payment_method] || true
  set_initiator_type(post, payment, options)
  set_reason_type(post, options)
end

#authorize(money, payment, options = {}, transaction_type = 'Auth') ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 36

def authorize(money, payment, options = {}, transaction_type = 'Auth')
  post = { transactionType: transaction_type }
  post[:savePM] = options[:save_payment_method] ? options[:save_payment_method].to_s : 'false'

  build_post_data(post)
  add_amount(post, money, options)
  add_payment_method(post, payment, :paymentOption, options)
  add_3ds_global(post, options)
  add_address(post, payment, options)
  add_customer_ip(post, options)
  add_stored_credentials(post, payment, options)
  (post, payment, options)
  add_cardholder_name_verification(post, payment, transaction_type, options)
  post[:userTokenId] = options[:user_token_id] if options[:user_token_id]
  post[:isPartialApproval] = options[:is_partial_approval] ? 1 : 0
  post[:authenticationOnlyType] = options[:authentication_only_type] if options[:authentication_only_type]

  if options[:execute_threed]
    execute_3ds_flow(post, money, payment, transaction_type, options)
  else
    commit(:purchase, post)
  end
end

#capture(money, authorization, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 65

def capture(money, authorization, options = {})
  post = { relatedTransactionId: authorization }

  build_post_data(post)
  add_amount(post, money, options)

  commit(:capture, post)
end

#credit(money, payment, options = {}) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 102

def credit(money, payment, options = {})
  post = { userTokenId: options[:user_token_id] }
  payment_key = payment.is_a?(NetworkTokenizationCreditCard) ? :userPaymentOption : :cardData
  build_post_data(post)
  add_amount(post, money, options)
  options[:is_payout] ? send_payout_transaction(payment_key, post, payment, options) : send_unreferenced_refund_transaction(post, payment, options)
end

#purchase(money, payment, options = {}) ⇒ Object



60
61
62
63
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 60

def purchase(money, payment, options = {})
  fetch_session_token if payment.is_a?(String)
  authorize(money, payment, options, 'Sale')
end

#refund(money, authorization, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 74

def refund(money, authorization, options = {})
  post = { relatedTransactionId: authorization }

  build_post_data(post)
  add_amount(post, money, options)

  commit(:refund, post)
end

#scrub(transcript) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 196

def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r(("cardNumber\\?":\\?")[^"\\]*)i, '\1[FILTERED]').
    gsub(%r(("cardCvv\\?":\\?")\d+), '\1[FILTERED]').
    gsub(%r(("merchantId\\?":\\?")\d+), '\1[FILTERED]').
    gsub(%r(("merchantSiteId\\?":\\?")\d+), '\1[FILTERED]').
    gsub(%r(("merchantKey\\?":\\?")\d+), '\1[FILTERED]').
    gsub(%r(("accountNumber\\?":\\?")\d+), '\1[FILTERED]').
    gsub(%r(("cryptogram\\?":\\?")[^"\\]*)i, '\1[FILTERED]')
end

#send_payout_transaction(payment_key, post, payment, options = {}) ⇒ Object



110
111
112
113
114
115
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 110

def send_payout_transaction(payment_key, post, payment, options = {})
  add_payment_method(post, payment, payment_key, options)
  add_customer_ip(post, options)
  url_details(post, options)
  commit(:general_credit, post.compact)
end

#send_unreferenced_refund_transaction(post, payment, options = {}) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 117

def send_unreferenced_refund_transaction(post, payment, options = {})
  post[:paymentOption] = { userPaymentOptionId: options[:user_payment_option_id] } if options[:user_payment_option_id]
  unless options[:user_payment_option_id]
    add_payment_method(post, payment, :paymentOption, options)
    post[:paymentOption][:card].slice!(:cardNumber, :cardHolderName, :expirationMonth, :expirationYear, :CVV)
  end
  commit(:refund, post.compact)
end

#set_initiator_type(post, payment, options) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 134

def set_initiator_type(post, payment, options)
  stored_credential = options[:stored_credential]
  return unless stored_credential

  is_initial_transaction = stored_credential[:initial_transaction]
  stored_credentials_mode = is_initial_transaction ? '0' : '1'

  if payment.is_a?(CreditCard)
    post[:paymentOption] ||= {}
    post[:paymentOption][:card] ||= {}
    post[:paymentOption][:card][:storedCredentials] ||= {}
    post[:paymentOption][:card][:storedCredentials][:storedCredentialsMode] = stored_credentials_mode
  end

  post[:isRebilling] = stored_credentials_mode
end

#set_reason_type(post, options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 172

def set_reason_type(post, options)
  reason_type = options[:stored_credential][:reason_type]

  case reason_type
  when 'recurring'
    reason_type = 'RECURRING'
  when 'installment'
    reason_type = 'INSTALLMENTS'
  when 'unscheduled'
    reason_type = 'ADDCARD'
  end

  unless reason_type == 'ADDCARD'
    fetch_session_token
    post[:relatedTransactionId] = options[:stored_credential][:network_transaction_id] if options[:stored_credential][:network_transaction_id]
  end

  post[:authenticationOnlyType] = reason_type
end

#store(credit_card, options = {}) ⇒ Object



97
98
99
100
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 97

def store(credit_card, options = {})
  options[:save_payment_method] = true
  authorize(0, credit_card, options)
end

#supports_scrubbing?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 192

def supports_scrubbing?
  true
end

#verify(credit_card, options = {}) ⇒ Object



90
91
92
93
94
95
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 90

def verify(credit_card, options = {})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(0, credit_card, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end

#void(authorization, options = {}) ⇒ Object



83
84
85
86
87
88
# File 'lib/active_merchant/billing/gateways/nuvei.rb', line 83

def void(authorization, options = {})
  post = { relatedTransactionId: authorization }
  build_post_data(post)

  commit(:void, post)
end