Class: Spree::Wallet::DefaultPaymentBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/wallet/default_payment_builder.rb

Overview

This class is responsible for building a default payment on an order, using a payment source that is already in the user’s “wallet”.

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ DefaultPaymentBuilder

Returns a new instance of DefaultPaymentBuilder.



4
5
6
# File 'app/models/spree/wallet/default_payment_builder.rb', line 4

def initialize(order)
  @order = order
end

Instance Method Details

#buildPayment

Build a payment to be added to an order prior to moving into the “payment” state.

Returns:

  • (Payment)

    the unsaved payment to be added, or nil if none.



12
13
14
15
16
17
18
19
20
21
# File 'app/models/spree/wallet/default_payment_builder.rb', line 12

def build
  credit_card = order.user.try!(:default_credit_card)

  if credit_card.try!(:valid?) && order.payments.from_credit_card.count == 0
    Spree::Payment.new(
      payment_method_id: credit_card.payment_method_id,
      source: credit_card,
    )
  end
end