diff --git a/app/models/spree/payment/processing.rb b/app/models/spree/payment/processing.rb index ad7eb87aca..22ff948283 100644 --- a/app/models/spree/payment/processing.rb +++ b/app/models/spree/payment/processing.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + module Spree class Payment < ActiveRecord::Base module Processing def process! - if payment_method && payment_method.source_required? + if payment_method&.source_required? if source if !processing? if payment_method.supports?(source) @@ -13,11 +15,11 @@ module Spree end else invalidate! - raise Core::GatewayError.new(Spree.t(:payment_method_not_supported)) + raise Core::GatewayError, Spree.t(:payment_method_not_supported) end end else - raise Core::GatewayError.new(Spree.t(:payment_processing_failed)) + raise Core::GatewayError, Spree.t(:payment_processing_failed) end end end @@ -34,6 +36,7 @@ module Spree def capture! return true if completed? + started_processing! protect_from_connection_error do check_environment @@ -55,29 +58,30 @@ module Spree 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) + response = payment_method.void(response_code, source, gateway_options) else # Standard ActiveMerchant void usage - response = payment_method.void(self.response_code, gateway_options) + response = payment_method.void(response_code, gateway_options) end record_response(response) if response.success? self.response_code = response.authorization - self.void + void else gateway_error(response) end end end - def credit!(credit_amount=nil) + def credit!(credit_amount = nil) protect_from_connection_error do check_environment @@ -94,12 +98,12 @@ module Spree 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' + order: order, + source: self, + payment_method: payment_method, + amount: credit_amount.abs * -1, + response_code: response.authorization, + state: 'completed' ) else gateway_error(response) @@ -136,28 +140,29 @@ module Spree def partial_credit(amount) return if amount > credit_allowed + started_processing! credit!(amount) end def gateway_options - options = { :email => order.email, - :customer => order.email, - :ip => order.last_ip_address, + options = { email: order.email, + customer: order.email, + 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 } + order_id: gateway_order_id } - options.merge!({ :shipping => order.ship_total * 100, - :tax => order.tax_total * 100, - :subtotal => order.item_total * 100, - :discount => order.promo_total * 100, - :currency => currency }) + options.merge!({ shipping: order.ship_total * 100, + tax: order.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.merge!({ billing_address: order.bill_address.try(:active_merchant_hash), + shipping_address: order.ship_address.try(:active_merchant_hash) }) options end @@ -193,49 +198,48 @@ module Spree self.cvv_response_message = response.cvv_result['message'] end end - self.send("#{success_state}!") + send("#{success_state}!") else - self.send(failure_state) + send(failure_state) gateway_error(response) end end def record_response(response) - log_entries.create(:details => response.to_yaml) + log_entries.create(details: response.to_yaml) end def protect_from_connection_error - begin - yield - rescue ActiveMerchant::ConnectionError => e - gateway_error(e) - end + yield + rescue ActiveMerchant::ConnectionError => e + gateway_error(e) end def gateway_error(error) - if error.is_a? ActiveMerchant::Billing::Response - text = error.params['message'] || error.params['response_reason_text'] || error.message - elsif error.is_a? ActiveMerchant::ConnectionError - text = Spree.t(:unable_to_connect_to_gateway) - else - text = error.to_s - end + text = if error.is_a? ActiveMerchant::Billing::Response + error.params['message'] || error.params['response_reason_text'] || error.message + elsif error.is_a? ActiveMerchant::ConnectionError + Spree.t(:unable_to_connect_to_gateway) + else + error.to_s + end logger.error(Spree.t(:gateway_error)) logger.error(" #{error.to_yaml}") - raise Core::GatewayError.new(text) + raise Core::GatewayError, text end # Saftey check to make sure we're not accidentally performing operations on a live gateway. # Ex. When testing in staging environment with a copy of production data. def check_environment return if payment_method.environment == Rails.env + message = Spree.t(:gateway_config_unavailable) + " - #{Rails.env}" - raise Core::GatewayError.new(message) + raise Core::GatewayError, message end # The unique identifier to be passed in to the payment gateway def gateway_order_id - "#{order.number}-#{self.identifier}" + "#{order.number}-#{identifier}" end end end