Class: Spree::PaymentCreate

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

Overview

Service object for creating new payments on an Order

Instance Method Summary (collapse)

Constructor Details

- (PaymentCreate) initialize(order, attributes, payment: nil, request_env: {})

Returns a new instance of PaymentCreate

Parameters:

  • order (Order)

    The order for the new payment

  • attributes (Hash, ActionController::Parameters)

    attributes which are assigned to the new payment

    • :payment_method_id Id of payment method used for this payment

    • :source_attributes Attributes used to build the source of this payment. Usually a CreditCard

      • :existing_card_id (Integer) Deprecated: The id of an existing CreditCard object to use

      • :wallet_payment_source_id (Integer): The id of a WalletPaymentSource to use

  • request_env (Hash)

    rack env of user creating the payment

  • payment (Payment)

    Internal use only. Instead of making a new payment, change the attributes for an existing one.



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

def initialize(order, attributes, payment: nil, request_env: {})
  @order = order
  @payment = payment

  # If AC::Params are passed in, attributes.to_h gives us a hash of only
  # the permitted attributes.
  @attributes = attributes.to_h.with_indifferent_access
  @source_attributes = @attributes.delete(:source_attributes) || {}
  @request_env = request_env
end

Instance Method Details

- (Payment) build

Build the new Payment

Returns:

  • (Payment)

    a new (unpersisted) Payment



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/spree/payment_create.rb', line 25

def build
  @payment ||= order.payments.new
  @payment.request_env = @request_env if @request_env
  @payment.attributes = @attributes

  if source_attributes[:existing_card_id].present?
    Spree::Deprecation.warn(
      "Passing existing_card_id to PaymentCreate is deprecated. Use wallet_payment_source_id instead.",
      caller,
    )
    build_existing_card
  elsif source_attributes[:wallet_payment_source_id].present?
    build_from_wallet_payment_source
  else
    build_source
  end

  @payment
end