Class: Spree::PaypalPlatform::CreateSource

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/paypal_platform/create_source.rb

Overview

Builds a PaymentSources record from a PayPal Orders payment_source.

PayPal returns one of paypal, apple_pay, or card (Card Fields). Unknown keys raise so a new wallet is not silently dropped.

Instance Method Summary collapse

Constructor Details

#initialize(paypal_payment_source:, gateway:, order: nil, user: nil) ⇒ CreateSource

Returns a new instance of CreateSource.

Parameters:

  • paypal_payment_source (Hash)
  • gateway (Spree::PaypalPlatform::Gateway)
  • order (Spree::Order, NilClass) (defaults to: nil)
  • user (Spree::User, NilClass) (defaults to: nil)


18
19
20
21
22
23
# File 'app/services/spree/paypal_platform/create_source.rb', line 18

def initialize(paypal_payment_source:, gateway:, order: nil, user: nil)
  @paypal_payment_source = paypal_payment_source.with_indifferent_access
  @gateway = gateway
  @user = user || order&.user
  @order = order
end

Instance Method Details

#callSpree::PaymentSource

Returns:

  • (Spree::PaymentSource)

Raises:

  • (ArgumentError)

    when the payload has no known wallet key



29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/spree/paypal_platform/create_source.rb', line 29

def call
  if paypal_payment_source[:paypal].present?
    create_paypal_source
  elsif paypal_payment_source[:apple_pay].present?
    create_apple_pay_source
  elsif paypal_payment_source[:card].present?
    create_card_source
  else
    raise ArgumentError, "Unsupported PayPal payment source: #{paypal_payment_source.keys.join(', ')}"
  end
end