diff --git a/app/models/spree/credit_card.rb b/app/models/spree/credit_card.rb index 46718b5ad8..f075de0e14 100644 --- a/app/models/spree/credit_card.rb +++ b/app/models/spree/credit_card.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class CreditCard < ActiveRecord::Base # Should be able to remove once we reach Spree v2.2.0 @@ -32,28 +34,32 @@ module Spree end def number=(num) - @number = num.gsub(/[^0-9]/, '') rescue nil + @number = begin + num.gsub(/[^0-9]/, '') + rescue StandardError + nil + end end # cc_type is set by jquery.payment, which helpfully provides different # types from Active Merchant. Converting them is necessary. def cc_type=(type) real_type = case type - when 'mastercard', 'maestro' - 'master' - when 'amex' - 'american_express' - when 'dinersclub' - 'diners_club' - else - type + when 'mastercard', 'maestro' + 'master' + when 'amex' + 'american_express' + when 'dinersclub' + 'diners_club' + else + type end self[:cc_type] = real_type end def set_last_digits - number.to_s.gsub!(/\s/,'') - verification_value.to_s.gsub!(/\s/,'') + number.to_s.gsub!(/\s/, '') + verification_value.to_s.gsub!(/\s/, '') self.last_digits ||= number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1) end @@ -93,6 +99,7 @@ module Spree def can_credit?(payment) return false unless payment.completed? return false unless payment.order.payment_state == 'credit_owed' + payment.credit_allowed > 0 end @@ -103,12 +110,12 @@ module Spree def to_active_merchant ActiveMerchant::Billing::CreditCard.new( - :number => number, - :month => month, - :year => year, - :verification_value => verification_value, - :first_name => first_name, - :last_name => last_name + number: number, + month: month, + year: year, + verification_value: verification_value, + first_name: first_name, + last_name: last_name ) end diff --git a/app/models/spree/gateway.rb b/app/models/spree/gateway.rb index 165fffd91a..283a8458de 100644 --- a/app/models/spree/gateway.rb +++ b/app/models/spree/gateway.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spree/concerns/payment_method_distributors' module Spree @@ -23,7 +25,7 @@ module Spree def provider gateway_options = options - gateway_options.delete :login if gateway_options.has_key?(:login) and gateway_options[:login].nil? + gateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil? if gateway_options[:server] ActiveMerchant::Billing::Base.gateway_mode = gateway_options[:server].to_sym end @@ -31,7 +33,7 @@ module Spree end def options - self.preferences.inject({}){ |memo, (key, value)| memo[key.to_sym] = value; memo } + preferences.each_with_object({}){ |(key, value), memo| memo[key.to_sym] = value; } end def method_missing(method, *args) @@ -53,6 +55,7 @@ module Spree def supports?(source) return true unless provider_class.respond_to? :supports? return false unless source.brand + provider_class.supports?(source.brand) end end diff --git a/app/models/spree/gateway/bogus.rb b/app/models/spree/gateway/bogus.rb index de241848ad..2efa234fe9 100644 --- a/app/models/spree/gateway/bogus.rb +++ b/app/models/spree/gateway/bogus.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + module Spree class Gateway::Bogus < Gateway - TEST_VISA = ['4111111111111111','4012888888881881','4222222222222'] - TEST_MC = ['5500000000000004','5555555555554444','5105105105105100'] - TEST_AMEX = ['378282246310005','371449635398431','378734493671000','340000000000009'] - TEST_DISC = ['6011000000000004','6011111111111117','6011000990139424'] + TEST_VISA = ['4111111111111111', '4012888888881881', '4222222222222'].freeze + TEST_MC = ['5500000000000004', '5555555555554444', '5105105105105100'].freeze + TEST_AMEX = ['378282246310005', '371449635398431', '378734493671000', '340000000000009'].freeze + TEST_DISC = ['6011000000000004', '6011111111111117', '6011000990139424'].freeze VALID_CCS = ['1', TEST_VISA, TEST_MC, TEST_AMEX, TEST_DISC].flatten @@ -20,42 +22,41 @@ module Spree def create_profile(payment) # simulate the storage of credit card profile using remote service success = VALID_CCS.include? payment.source.number - payment.source.update_attributes(:gateway_customer_profile_id => generate_profile_id(success)) + payment.source.update(gateway_customer_profile_id: generate_profile_id(success)) end - def authorize(money, credit_card, options = {}) + def authorize(_money, credit_card, _options = {}) profile_id = credit_card.gateway_customer_profile_id - if VALID_CCS.include? credit_card.number or (profile_id and profile_id.starts_with? 'BGS-') - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, :test => true, :authorization => '12345', :avs_result => { :code => 'A' }) + if VALID_CCS.include?(credit_card.number) || profile_id&.starts_with?('BGS-') + ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'A' }) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { :message => 'Bogus Gateway: Forced failure' }, :test => true) + ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true) end end - def purchase(money, credit_card, options = {}) + def purchase(_money, credit_card, _options = {}) profile_id = credit_card.gateway_customer_profile_id - if VALID_CCS.include? credit_card.number or (profile_id and profile_id.starts_with? 'BGS-') - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, :test => true, :authorization => '12345', :avs_result => { :code => 'A' }) + if VALID_CCS.include?(credit_card.number) || profile_id&.starts_with?('BGS-') + ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'A' }) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', :message => 'Bogus Gateway: Forced failure', :test => true) + ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true) end end - def credit(money, credit_card, response_code, options = {}) - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, :test => true, :authorization => '12345') + def credit(_money, _credit_card, _response_code, _options = {}) + ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345') end - def capture(authorization, credit_card, gateway_options) + def capture(authorization, _credit_card, _gateway_options) if authorization.response_code == '12345' - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, :test => true, :authorization => '67890') + ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '67890') else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', :error => 'Bogus Gateway: Forced failure', :test => true) + ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', error: 'Bogus Gateway: Forced failure', test: true) end - end - def void(response_code, credit_card, options = {}) - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, :test => true, :authorization => '12345') + def void(_response_code, _credit_card, _options = {}) + ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345') end def test? @@ -72,14 +73,15 @@ module Spree end private - def generate_profile_id(success) - record = true - prefix = success ? 'BGS' : 'FAIL' - while record - random = "#{prefix}-#{Array.new(6){rand(6)}.join}" - record = CreditCard.where(:gateway_customer_profile_id => random).first - end - random + + def generate_profile_id(success) + record = true + prefix = success ? 'BGS' : 'FAIL' + while record + random = "#{prefix}-#{Array.new(6){ rand(6) }.join}" + record = CreditCard.where(gateway_customer_profile_id: random).first end + random + end end end diff --git a/app/models/spree/gateway/bogus_simple.rb b/app/models/spree/gateway/bogus_simple.rb index 8b0757b220..0a9d64c1a0 100644 --- a/app/models/spree/gateway/bogus_simple.rb +++ b/app/models/spree/gateway/bogus_simple.rb @@ -1,26 +1,26 @@ +# frozen_string_literal: true + # Bogus Gateway that doesn't support payment profiles module Spree class Gateway::BogusSimple < Gateway::Bogus - def payment_profiles_supported? false end - def authorize(money, credit_card, options = {}) + def authorize(_money, credit_card, _options = {}) if VALID_CCS.include? credit_card.number - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, :test => true, :authorization => '12345', :avs_result => { :code => 'A' }) + ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'A' }) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { :message => 'Bogus Gateway: Forced failure' }, :test => true) + ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true) end end - def purchase(money, credit_card, options = {}) + def purchase(_money, credit_card, _options = {}) if VALID_CCS.include? credit_card.number - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, :test => true, :authorization => '12345', :avs_result => { :code => 'A' }) + ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'A' }) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', :message => 'Bogus Gateway: Forced failure', :test => true) + ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true) end end - end end diff --git a/app/models/spree/payment_method.rb b/app/models/spree/payment_method.rb index 01a9be0ce5..c659c9a47a 100644 --- a/app/models/spree/payment_method.rb +++ b/app/models/spree/payment_method.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spree/concerns/payment_method_distributors' module Spree @@ -8,7 +10,7 @@ module Spree acts_as_taggable acts_as_paranoid - DISPLAY = [:both, :front_end, :back_end] + DISPLAY = [:both, :front_end, :back_end].freeze default_scope -> { where(deleted_at: nil) } has_many :credit_cards, class_name: "Spree::CreditCard" # from Spree v.2.3.0 d470b31798f37 @@ -70,20 +72,20 @@ module Spree def self.available(display_on = 'both') all.select do |p| p.active && - (p.display_on == display_on.to_s || p.display_on.blank?) && - (p.environment == Rails.env || p.environment.blank?) + (p.display_on == display_on.to_s || p.display_on.blank?) && + (p.environment == Rails.env || p.environment.blank?) end end def self.active? - where(type: self.to_s, environment: Rails.env, active: true).count > 0 + where(type: to_s, environment: Rails.env, active: true).count > 0 end def method_type type.demodulize.downcase end - def self.find_with_destroyed *args + def self.find_with_destroyed(*args) unscoped { find(*args) } end @@ -99,7 +101,7 @@ module Spree Spree::Config[:auto_capture] end - def supports?(source) + def supports?(_source) true end diff --git a/app/models/spree/payment_method/check.rb b/app/models/spree/payment_method/check.rb index 108d8317d1..8ebdd19fc7 100644 --- a/app/models/spree/payment_method/check.rb +++ b/app/models/spree/payment_method/check.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class PaymentMethod::Check < PaymentMethod def actions @@ -14,11 +16,11 @@ module Spree payment.state != 'void' end - def capture(*args) + def capture(*_args) ActiveMerchant::Billing::Response.new(true, "", {}, {}) end - def void(*args) + def void(*_args) ActiveMerchant::Billing::Response.new(true, "", {}, {}) end diff --git a/spec/models/spree/credit_card_spec.rb b/spec/models/spree/credit_card_spec.rb index a2b812fe33..930cefa995 100644 --- a/spec/models/spree/credit_card_spec.rb +++ b/spec/models/spree/credit_card_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' module Spree describe CreditCard do describe "original specs from Spree" do - let(:valid_credit_card_attributes) { { number: '4111111111111111', verification_value: '123', month: 12, year: Time.now.year + 1 } } + let(:valid_credit_card_attributes) { { number: '4111111111111111', verification_value: '123', month: 12, year: Time.zone.now.year + 1 } } def self.payment_states Spree::Payment.state_machine.states.keys @@ -16,34 +16,32 @@ module Spree let(:credit_card) { Spree::CreditCard.new } before(:each) do - @order = create(:order) - @payment = Spree::Payment.create(:amount => 100, :order => @order) + @payment = Spree::Payment.create(amount: 100, order: @order) @success_response = double('gateway_response', success?: true, authorization: '123', avs_result: { 'code' => 'avs-code' }) @fail_response = double('gateway_response', success?: false) @payment_gateway = mock_model(Spree::PaymentMethod, - payment_profiles_supported?: true, - authorize: @success_response, - purchase: @success_response, - capture: @success_response, - void: @success_response, - credit: @success_response, - environment: 'test' - ) + payment_profiles_supported?: true, + authorize: @success_response, + purchase: @success_response, + capture: @success_response, + void: @success_response, + credit: @success_response, + environment: 'test') @payment.stub payment_method: @payment_gateway end context "#can_capture?" do it "should be true if payment is pending" do - payment = mock_model(Spree::Payment, pending?: true, created_at: Time.now) + payment = mock_model(Spree::Payment, pending?: true, created_at: Time.zone.now) credit_card.can_capture?(payment).should be_true end it "should be true if payment is checkout" do - payment = mock_model(Spree::Payment, pending?: false, checkout?: true, created_at: Time.now) + payment = mock_model(Spree::Payment, pending?: false, checkout?: true, created_at: Time.zone.now) credit_card.can_capture?(payment).should be_true end end @@ -94,24 +92,24 @@ module Spree it "does not run expiration in the past validation if month is not set" do credit_card.month = nil - credit_card.year = Time.now.year + credit_card.year = Time.zone.now.year credit_card.should_not be_valid credit_card.errors[:base].should be_blank end it "does not run expiration in the past validation if year is not set" do - credit_card.month = Time.now.month + credit_card.month = Time.zone.now.month credit_card.year = nil credit_card.should_not be_valid credit_card.errors[:base].should be_blank end - + it "does not run expiration in the past validation if year and month are empty" do credit_card.year = "" credit_card.month = "" credit_card.should_not be_valid credit_card.errors[:card].should be_blank - end + end it "should only validate on create" do credit_card.attributes = valid_credit_card_attributes @@ -147,7 +145,7 @@ module Spree end it "should not raise an exception on non-string input" do - credit_card.number = Hash.new + credit_card.number = ({}) credit_card.number.should be_nil end end @@ -176,12 +174,12 @@ module Spree expect { credit_card.payments.to_a }.not_to raise_error end end - + context "#to_active_merchant" do before do credit_card.number = "4111111111111111" - credit_card.year = Time.now.year - credit_card.month = Time.now.month + credit_card.year = Time.zone.now.year + credit_card.month = Time.zone.now.month credit_card.first_name = "Bob" credit_card.last_name = "Boblaw" credit_card.verification_value = 123 @@ -190,8 +188,8 @@ module Spree it "converts to an ActiveMerchant::Billing::CreditCard object" do am_card = credit_card.to_active_merchant am_card.number.should == "4111111111111111" - am_card.year.should == Time.now.year - am_card.month.should == Time.now.month + am_card.year.should == Time.zone.now.year + am_card.month.should == Time.zone.now.month am_card.first_name.should == "Bob" am_card.last_name = "Boblaw" am_card.verification_value.should == 123 diff --git a/spec/models/spree/gateway_spec.rb b/spec/models/spree/gateway_spec.rb index cdedccd730..f68155edaf 100644 --- a/spec/models/spree/gateway_spec.rb +++ b/spec/models/spree/gateway_spec.rb @@ -1,13 +1,12 @@ +# frozen_string_literal: true + require 'spec_helper' describe Spree::Gateway do class Provider - def initialize(options) - end + def initialize(options); end - def imaginary_method - - end + def imaginary_method; end end class TestGateway < Spree::Gateway diff --git a/spec/models/spree/payment_method_spec.rb b/spec/models/spree/payment_method_spec.rb index e61fd8252c..79920f62e0 100644 --- a/spec/models/spree/payment_method_spec.rb +++ b/spec/models/spree/payment_method_spec.rb @@ -8,11 +8,11 @@ module Spree [nil, 'both', 'front_end', 'back_end'].each do |display_on| Spree::Gateway::Test.create( - :name => 'Display Both', - :display_on => display_on, - :active => true, - :environment => 'test', - :description => 'foofah' + name: 'Display Both', + display_on: display_on, + active: true, + environment: 'test', + description: 'foofah' ) end Spree::PaymentMethod.all.size.should == 4 @@ -34,7 +34,7 @@ module Spree Spree::PaymentMethod.available(:back_end).size.should == 2 end end - + it "orders payment methods by name" do pm1 = create(:payment_method, name: 'ZZ') pm2 = create(:payment_method, name: 'AA')