Class: PaymentSessions::PaypalCheckout

Inherits:
PaymentSession
  • Object
show all
Defined in:
app/models/spree/payment_sessions/paypal_checkout.rb

Instance Method Summary collapse

Instance Method Details

#accepted?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/models/spree/payment_sessions/paypal_checkout.rb', line 19

def accepted?
  external_data&.dig('status') == 'COMPLETED'
end

#find_or_create_payment!(metadata = {}) ⇒ Object

Creates or finds the Spree::Payment for this session. Defers creation until paypal_capture_id is present so response_code is always the capture ID (required for refunds via Gateway#credit).



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
# File 'app/models/spree/payment_sessions/paypal_checkout.rb', line 30

def find_or_create_payment!( = {})
  return unless persisted?
  return payment if payment.present?
  return unless paypal_capture_id

  order.with_lock do
    existing_payment = order.payments.where(
      payment_method: payment_method,
      response_code: paypal_capture_id
    ).first

    return existing_payment if existing_payment.present?

    source = create_payment_source!

    order.payments.create!(
      payment_method: payment_method,
      amount: amount,
      response_code: paypal_capture_id,
      source: source,
      skip_source_requirement: true,
      private_metadata: 
    )
  end
end

#paypal_capture_idObject



7
8
9
# File 'app/models/spree/payment_sessions/paypal_checkout.rb', line 7

def paypal_capture_id
  external_data&.dig('purchase_units', 0, 'payments', 'captures', 0, 'id')
end

#paypal_order_idObject



3
4
5
# File 'app/models/spree/payment_sessions/paypal_checkout.rb', line 3

def paypal_order_id
  external_id
end

#paypal_payerObject



11
12
13
# File 'app/models/spree/payment_sessions/paypal_checkout.rb', line 11

def paypal_payer
  external_data&.dig('payer')
end

#paypal_payment_sourceObject



15
16
17
# File 'app/models/spree/payment_sessions/paypal_checkout.rb', line 15

def paypal_payment_source
  external_data&.dig('payment_source')
end

#successful?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/models/spree/payment_sessions/paypal_checkout.rb', line 23

def successful?
  accepted?
end