Module: Spree::Payment::Processing

Included in:
Spree::Payment
Defined in:
app/models/spree/payment/processing.rb

Instance Method Summary collapse

Instance Method Details

#authorize!Object


12
13
14
# File 'app/models/spree/payment/processing.rb', line 12

def authorize!
  handle_payment_preconditions { process_authorization }
end

#cancel!Object


66
67
68
# File 'app/models/spree/payment/processing.rb', line 66

def cancel!
  payment_method.cancel(response_code)
end

#capture!(amount = nil) ⇒ Object

Takes the amount in cents to capture. Can be used to capture partial amounts of a payment.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/spree/payment/processing.rb', line 23

def capture!(amount=nil)
  amount ||= money.money.cents
  return true if completed?
  started_processing!
  protect_from_connection_error do
    check_environment
    # Standard ActiveMerchant capture usage
    response = payment_method.capture(
      amount,
      response_code,
      gateway_options
    )

    money = ::Money.new(amount, currency)
    capture_events.create!(amount: money.to_f)
    handle_response(response, :complete, :failure)
  end
end

#gateway_optionsObject


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/spree/payment/processing.rb', line 70

def gateway_options
  order.reload
  options = { :email       => order.email,
              :customer    => order.email,
              :customer_id => order.user_id,
              :ip          => order.last_ip_address,
              # Need to pass in a unique identifier here to make some
              # payment gateways happy.
              #
              # For more information, please see Spree::Payment#set_unique_identifier
              :order_id    => gateway_order_id }

  options.merge!({ :shipping => order.ship_total * 100,
                   :tax      => order.additional_tax_total * 100,
                   :subtotal => order.item_total * 100,
                   :discount => order.promo_total * 100,
                   :currency => currency })

  options.merge!({ :billing_address  => order.bill_address.try(:active_merchant_hash),
                  :shipping_address => order.ship_address.try(:active_merchant_hash) })

  options
end

#process!Object


4
5
6
7
8
9
10
# File 'app/models/spree/payment/processing.rb', line 4

def process!
  if payment_method && payment_method.auto_capture?
    purchase!
  else
    authorize!
  end
end

#purchase!Object

Captures the entire amount of a payment.


17
18
19
# File 'app/models/spree/payment/processing.rb', line 17

def purchase!
  handle_payment_preconditions { process_purchase }
end

#void_transaction!Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/spree/payment/processing.rb', line 42

def void_transaction!
  return true if void?
  protect_from_connection_error do
    check_environment

    if payment_method.payment_profiles_supported?
      # Gateways supporting payment profiles will need access to credit card object because this stores the payment profile information
      # so supply the authorization itself as well as the credit card, rather than just the authorization code
      response = payment_method.void(self.response_code, source, gateway_options)
    else
      # Standard ActiveMerchant void usage
      response = payment_method.void(self.response_code, gateway_options)
    end
    record_response(response)

    if response.success?
      self.response_code = response.authorization
      self.void
    else
      gateway_error(response)
    end
  end
end