Module: SpreeAdyen::Gateway::PaymentSetupSessions

Extended by:
ActiveSupport::Concern
Included in:
SpreeAdyen::Gateway
Defined in:
app/models/spree_adyen/gateway/payment_setup_sessions.rb

Instance Method Summary collapse

Instance Method Details

#complete_payment_setup_session(setup_session:, params: {}) ⇒ Object

Completes a setup session by checking with Adyen and creating a payment source from the stored payment method. Idempotent — if the source was already created by the AUTHORISATION webhook, returns the session unchanged.

Parameters:

  • setup_session (Spree::PaymentSetupSessions::Adyen)
  • params (Hash) (defaults to: {})

    must include :session_result (or :external_data with :redirect_result)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/spree_adyen/gateway/payment_setup_sessions.rb', line 47

def complete_payment_setup_session(setup_session:, params: {})
  session_result = params[:session_result] || params['session_result']

  if session_result.blank?
    external_data = params[:external_data] || params['external_data'] || {}
    redirect_result = external_data[:redirect_result] || external_data['redirect_result']

    raise Spree::Core::GatewayError, 'session_result or redirect_result is required' if redirect_result.blank?

    return complete_setup_session_from_redirect(setup_session, redirect_result)
  end

  complete_setup_session_from_result(setup_session, session_result)
end

#create_adyen_setup_session(customer, channel, return_url, currency = nil) ⇒ Spree::PaymentResponse

Creates a zero-auth tokenization session via Adyen’s Sessions API.

Parameters:

  • customer (Spree::User)
  • channel (String)

    Web, iOS, Android

  • return_url (String)
  • currency (String, nil) (defaults to: nil)

    defaults to USD

Returns:

  • (Spree::PaymentResponse)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/spree_adyen/gateway/payment_setup_sessions.rb', line 69

def create_adyen_setup_session(customer, channel, return_url, currency = nil)
  payload = SpreeAdyen::PaymentSetupSessions::RequestPayloadPresenter.new(
    customer: customer,
    merchant_account: ,
    payment_method: self,
    channel: channel,
    return_url: return_url,
    currency: currency
  ).to_h

  response = send_request do
    client.checkout.payments_api.sessions(payload, headers: { 'Idempotency-Key' => SecureRandom.uuid })
  end
  response_body = response.response

  if response.status.to_i == 201
    success(response_body.id, response_body)
  else
    failure(response_body.slice('pspReference', 'message').values.join(' - '))
  end
end

#create_payment_setup_session(customer:, external_data: {}) ⇒ Spree::PaymentSetupSessions::Adyen

Creates an Adyen zero-auth tokenization session via the Sessions API and persists a Spree::PaymentSetupSessions::Adyen record.

Parameters:

  • customer (Spree::User)

    the customer to vault the payment method for

  • external_data (Hash) (defaults to: {})

    additional data (e.g., channel, return_url, currency)

Returns:

  • (Spree::PaymentSetupSessions::Adyen)

    the created setup session



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/spree_adyen/gateway/payment_setup_sessions.rb', line 20

def create_payment_setup_session(customer:, external_data: {})
  channel = external_data[:channel] || external_data['channel'] || Spree::PaymentSessions::Adyen::AVAILABLE_CHANNELS[:web]
  return_url = external_data[:return_url] || external_data['return_url'] || default_setup_return_url
  currency = external_data[:currency] || external_data['currency']

  response = create_adyen_setup_session(customer, channel, return_url, currency)

  payment_setup_session_class.create!(
    customer: customer,
    payment_method: self,
    status: 'pending',
    external_id: response.params['id'],
    external_data: external_data.to_h.stringify_keys.merge(
      'session_data' => response.params['sessionData'],
      'channel' => channel,
      'return_url' => return_url,
      'shopper_reference' => "customer_#{customer.id}"
    )
  )
end

#payment_setup_session_classObject



10
11
12
# File 'app/models/spree_adyen/gateway/payment_setup_sessions.rb', line 10

def payment_setup_session_class
  Spree::PaymentSetupSessions::Adyen
end

#setup_session_supported?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'app/models/spree_adyen/gateway/payment_setup_sessions.rb', line 6

def setup_session_supported?
  true
end