Class: PaymentSessions::PaypalPlatform

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

Instance Method Summary collapse

Instance Method Details

#accepted?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


36
37
38
# File 'app/models/spree/payment_sessions/paypal_platform.rb', line 36

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

#find_or_create_payment!(metadata = {}) ⇒ Spree::Payment, NilClass

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).

Parameters:

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

Returns:

  • (Spree::Payment, NilClass)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/spree/payment_sessions/paypal_platform.rb', line 56

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_idString, NilClass

Returns:

  • (String, NilClass)


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

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

#paypal_order_idString

Returns:

  • (String)


8
9
10
# File 'app/models/spree/payment_sessions/paypal_platform.rb', line 8

def paypal_order_id
  external_id
end

#paypal_payerHash, NilClass

Returns:

  • (Hash, NilClass)


22
23
24
# File 'app/models/spree/payment_sessions/paypal_platform.rb', line 22

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

#paypal_payment_sourceHash, NilClass

Returns:

  • (Hash, NilClass)


29
30
31
# File 'app/models/spree/payment_sessions/paypal_platform.rb', line 29

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

#successful?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


43
44
45
# File 'app/models/spree/payment_sessions/paypal_platform.rb', line 43

def successful?
  accepted?
end