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



25
26
27
28
# File 'app/models/spree/payment/processing.rb', line 25

def authorize!
  started_processing!
  gateway_action(source, :authorize, :pend)
end

#cancel!Object



111
112
113
114
115
116
117
# File 'app/models/spree/payment/processing.rb', line 111

def cancel!
  if payment_method.respond_to?(:cancel)
    payment_method.cancel(response_code)
  else
    credit!
  end
end

#capture!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/spree/payment/processing.rb', line 35

def capture!
  return true if completed?
  started_processing!
  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.capture(self, source, gateway_options)
    else
      # Standard ActiveMerchant capture usage
      response = payment_method.capture(money.money.cents,
                                        response_code,
                                        gateway_options)
    end

    handle_response(response, :complete, :failure)
  end
end

#credit!(credit_amount = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/spree/payment/processing.rb', line 80

def credit!(credit_amount=nil)
  protect_from_connection_error do
    check_environment

    credit_amount ||= credit_allowed >= order.outstanding_balance.abs ? order.outstanding_balance.abs : credit_allowed.abs
    credit_amount = credit_amount.to_f
    credit_cents = Spree::Money.new(credit_amount, currency: currency).money.cents

    if payment_method.payment_profiles_supported?
      response = payment_method.credit(credit_cents, source, response_code, gateway_options)
    else
      response = payment_method.credit(credit_cents, response_code, gateway_options)
    end

    record_response(response)

    if response.success?
      self.class.create!(
        :order => order,
        :source => self,
        :payment_method => payment_method,
        :amount => credit_amount.abs * -1,
        :response_code => response.authorization,
        :state => 'completed'
      )
    else
      gateway_error(response)
    end
  end
end

#gateway_optionsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/spree/payment/processing.rb', line 125

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.tax_total * 100,
                   :subtotal => order.item_total * 100,
                   :discount => order.discount_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

#partial_credit(amount) ⇒ Object



119
120
121
122
123
# File 'app/models/spree/payment/processing.rb', line 119

def partial_credit(amount)
  return if amount > credit_allowed
  started_processing!
  credit!(amount)
end

#process!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/spree/payment/processing.rb', line 4

def process!
  if payment_method && payment_method.source_required?
    if source
      if !processing?
        if payment_method.supports?(source)
          if payment_method.auto_capture?
            purchase!
          else
            authorize!
          end
        else
          invalidate!
          raise Core::GatewayError.new(Spree.t(:payment_method_not_supported))
        end
      end
    else
      raise Core::GatewayError.new(Spree.t(:payment_processing_failed))
    end
  end
end

#purchase!Object



30
31
32
33
# File 'app/models/spree/payment/processing.rb', line 30

def purchase!
  started_processing!
  gateway_action(source, :purchase, :complete)
end

#void_transaction!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/spree/payment/processing.rb', line 56

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