Module: SpreeStripe::Gateway::PaymentSessions

Extended by:
ActiveSupport::Concern
Included in:
SpreeStripe::Gateway
Defined in:
app/models/spree_stripe/gateway/payment_sessions.rb

Instance Method Summary collapse

Instance Method Details

#complete_payment_session(payment_session:, params: {}) ⇒ Object

Completes a payment session by verifying with Stripe, creating the Payment record, and patching wallet address data.

Does NOT complete the order — that is handled by Carts::Complete (called by the storefront or by the webhook handler).



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
# File 'app/models/spree_stripe/gateway/payment_sessions.rb', line 79

def complete_payment_session(payment_session:, params: {})
  stripe_pi = retrieve_payment_intent(payment_session.external_id)

  if payment_intent_accepted?(stripe_pi)
    payment_session.process if payment_session.can_process?

    charge = payment_session.stripe_charge

    # Patch wallet billing address (Apple Pay, Google Pay)
    patch_wallet_address(payment_session.order, charge) if charge.present?

    # Create the Payment record
    payment_session.find_or_create_payment!

    # `else` covers requires_capture (manual capture), processing (delayed-notification
    # banks), and requires_action (bank transfer awaiting funds) — all auth-only states.
    payment = payment_session.payment
    if payment.present? && !payment.completed?
      if payment_intent_successful?(stripe_pi)
        payment.process!
      else
        payment.authorize!
      end
    end

    payment_session.complete unless payment_session.completed?
  else
    payment_session.fail if payment_session.can_fail?
  end
end

#create_payment_session(order:, amount: nil, external_data: {}) ⇒ Object



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
# File 'app/models/spree_stripe/gateway/payment_sessions.rb', line 14

def create_payment_session(order:, amount: nil, external_data: {})
  total = amount.presence || order.total_minus_store_credits
  amount_in_cents = Spree::Money.new(total, currency: order.currency).cents

  return nil if amount_in_cents.zero?

  customer = fetch_or_create_customer(order: order)
  stripe_pm_id = external_data[:stripe_payment_method_id] || external_data['stripe_payment_method_id']

  response = create_payment_intent(
    amount_in_cents, order,
    payment_method_id: stripe_pm_id,
    customer_profile_id: customer&.profile_id
  )

  ephemeral_key_response = create_ephemeral_key(customer.profile_id) if customer.present?
  ephemeral_key_secret = ephemeral_key_response&.params&.dig('secret')

  payment_session_class.create!(
    order: order,
    payment_method: self,
    amount: total,
    currency: order.currency,
    status: 'pending',
    external_id: response.authorization,
    customer: order.user,
    customer_external_id: customer&.profile_id,
    external_data: {
      'client_secret' => response.params['client_secret'],
      'ephemeral_key_secret' => ephemeral_key_secret,
      'stripe_payment_method_id' => stripe_pm_id
    }.compact
  )
end

#payment_session_classObject



10
11
12
# File 'app/models/spree_stripe/gateway/payment_sessions.rb', line 10

def payment_session_class
  Spree::PaymentSessions::Stripe
end

#session_required?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'app/models/spree_stripe/gateway/payment_sessions.rb', line 6

def session_required?
  !SpreeStripe::Config[:use_legacy_payment_intents]
end

#update_payment_session(payment_session:, amount: nil, external_data: {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/spree_stripe/gateway/payment_sessions.rb', line 49

def update_payment_session(payment_session:, amount: nil, external_data: {})
  attrs = {}
  amount_in_cents = nil

  if amount.present?
    attrs[:amount] = amount
    amount_in_cents = Spree::Money.new(amount, currency: payment_session.currency).cents
  end

  stripe_pm_id = external_data[:stripe_payment_method_id] || external_data['stripe_payment_method_id']

  update_payment_intent(
    payment_session.external_id,
    amount_in_cents || payment_session.amount_in_cents,
    payment_session.order,
    stripe_pm_id
  )

  if external_data.present?
    attrs[:external_data] = (payment_session.external_data || {}).merge(external_data.stringify_keys)
  end

  payment_session.update!(attrs) if attrs.any?
end