From a466886a32ec930c6e98f6b23d01133baef05dd3 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Thu, 19 Nov 2020 13:44:41 -0800 Subject: [PATCH] fix rubocop warnings --- app/controllers/api/customers_controller.rb | 14 +++++++++++--- app/models/spree/gateway/stripe_sca.rb | 13 +++++-------- app/models/spree/payment/processing.rb | 15 ++++----------- app/services/recurring_payments.rb | 2 +- lib/stripe/credit_card_cloner.rb | 4 ++-- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/app/controllers/api/customers_controller.rb b/app/controllers/api/customers_controller.rb index e0610e5e32..33ac9d403d 100644 --- a/app/controllers/api/customers_controller.rb +++ b/app/controllers/api/customers_controller.rb @@ -13,9 +13,8 @@ module Api client_secret = RecurringPayments.setup_for(@customer) if params[:customer][:allow_charges] - if @customer.update(params[:customer]) - @customer.gateway_recurring_payment_client_secret = client_secret - @customer.gateway_shop_id = @customer.enterprise.stripe_account&.stripe_user_id + if @customer.update(customer_params) + add_recurring_payment_info(@customer, client_secret) render json: @customer, serializer: CustomerSerializer, status: :ok else invalid_resource!(@customer) @@ -25,5 +24,14 @@ module Api def customer_params params.require(:customer).permit(:code, :email, :enterprise_id, :allow_charges) end + + private + + def add_recurring_payment_info(customer, client_secret) + return unless client_secret + + customer.gateway_recurring_payment_client_secret = client_secret + customer.gateway_shop_id = customer.enterprise.stripe_account&.stripe_user_id + end end end diff --git a/app/models/spree/gateway/stripe_sca.rb b/app/models/spree/gateway/stripe_sca.rb index a6a25aced4..b12ff59200 100644 --- a/app/models/spree/gateway/stripe_sca.rb +++ b/app/models/spree/gateway/stripe_sca.rb @@ -47,22 +47,19 @@ module Spree # NOTE: the name of this method is determined by Spree::Payment::Processing def charge_offline(money, creditcard, gateway_options) - options = basic_options(gateway_options) - customer_id, payment_method_id = + customer, payment_method = Stripe::CreditCardCloner.new.find_or_clone(creditcard, stripe_account_id) - options[:customer] = customer_id - options[:off_session] = true - provider.purchase(money, payment_method_id, options) + options = basic_options(gateway_options).merge(customer: customer, off_session: true) + provider.purchase(money, payment_method, options) rescue Stripe::StripeError => e failed_activemerchant_billing_response(e.message) end # NOTE: the name of this method is determined by Spree::Payment::Processing def authorize(money, creditcard, gateway_options) - authorize_response = provider.authorize(*options_for_authorize(money, - creditcard, - gateway_options)) + authorize_response = + provider.authorize(*options_for_authorize(money, creditcard, gateway_options)) Stripe::AuthorizeResponsePatcher.new(authorize_response).call! rescue Stripe::StripeError => e failed_activemerchant_billing_response(e.message) diff --git a/app/models/spree/payment/processing.rb b/app/models/spree/payment/processing.rb index de0c05d422..eda657863e 100644 --- a/app/models/spree/payment/processing.rb +++ b/app/models/spree/payment/processing.rb @@ -15,10 +15,8 @@ module Spree raise Core::GatewayError, Spree.t(:payment_method_not_supported) end - if offline - charge_offline! - elsif payment_method.auto_capture? - purchase! + if payment_method.auto_capture? + purchase!(offline) else authorize! end @@ -29,14 +27,9 @@ module Spree gateway_action(source, :authorize, :pend) end - def purchase! + def purchase!(offline = false) started_processing! - gateway_action(source, :purchase, :complete) - end - - def charge_offline! - started_processing! - gateway_action(source, :charge_offline, :complete) + gateway_action(source, offline ? :charge_offline : :purchase, :complete) end def capture! diff --git a/app/services/recurring_payments.rb b/app/services/recurring_payments.rb index 1c622fd6a8..d6c9f1052f 100644 --- a/app/services/recurring_payments.rb +++ b/app/services/recurring_payments.rb @@ -9,7 +9,7 @@ class RecurringPayments Stripe::CreditCardCloner.new.find_or_clone(card, stripe_account) setup_intent = Stripe::SetupIntent.create( { payment_method: payment_method_id, customer: customer_id }, - { stripe_account: stripe_account } + stripe_account: stripe_account ) setup_intent.client_secret end diff --git a/lib/stripe/credit_card_cloner.rb b/lib/stripe/credit_card_cloner.rb index fb5e2d3d98..9bbd009342 100644 --- a/lib/stripe/credit_card_cloner.rb +++ b/lib/stripe/credit_card_cloner.rb @@ -73,7 +73,7 @@ module Stripe loop do response = Stripe::Customer.list({ email: email, starting_after: starting_after }, - { stripe_account: connected_account_id }) + stripe_account: connected_account_id) customers += response.data break unless response.has_more @@ -88,7 +88,7 @@ module Stripe loop do options = { customer: customer_id, type: 'card', starting_after: starting_after } - response = Stripe::PaymentMethod.list(options, { stripe_account: connected_account_id }) + response = Stripe::PaymentMethod.list(options, stripe_account: connected_account_id) payment_methods += response.data break unless response.has_more