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

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

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

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

    rack env of user creating the payment

  • payment (Payment) (defaults to: nil)

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



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

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

#buildPayment

Build the new Payment

Returns:

  • (Payment)

    a new (unpersisted) Payment



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

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

  if source_attributes[:wallet_payment_source_id].present?
    build_from_wallet_payment_source
  else
    build_source
  end

  @payment
end