diff --git a/app/models/spree/gateway/stripe_connect.rb b/app/models/spree/gateway/stripe_connect.rb deleted file mode 100644 index 91090569ff..0000000000 --- a/app/models/spree/gateway/stripe_connect.rb +++ /dev/null @@ -1,109 +0,0 @@ -# frozen_string_literal: true - -require 'stripe/profile_storer' - -module Spree - class Gateway - class StripeConnect < Gateway - preference :enterprise_id, :integer - - validate :ensure_enterprise_selected - - def method_type - 'stripe' - end - - def provider_class - ActiveMerchant::Billing::StripeGateway - end - - def payment_profiles_supported? - true - end - - def stripe_account_id - StripeAccount.find_by(enterprise_id: preferred_enterprise_id)&.stripe_user_id - end - - # NOTE: the name of this method is determined by Spree::Payment::Processing - def purchase(money, creditcard, gateway_options) - provider.purchase(*options_for_purchase_or_auth(money, creditcard, gateway_options)) - rescue Stripe::StripeError => e - # This will be an error caused by generating a stripe token - failed_activemerchant_billing_response(e.message) - end - - def charge_offline(money, creditcard, gateway_options) - purchase(money, creditcard, gateway_options) - end - - # NOTE: the name of this method is determined by Spree::Payment::Processing - def void(response_code, _creditcard, gateway_options) - gateway_options[:stripe_account] = stripe_account_id - provider.void(response_code, gateway_options) - end - - # NOTE: the name of this method is determined by Spree::Payment::Processing - def credit(money, _creditcard, response_code, gateway_options) - gateway_options[:stripe_account] = stripe_account_id - provider.refund(money, response_code, gateway_options) - end - - def create_profile(payment) - return unless payment.source.gateway_customer_profile_id.nil? - - profile_storer = Stripe::ProfileStorer.new(payment, provider) - profile_storer.create_customer_from_token - end - - private - - # In this gateway, what we call 'secret_key' is the 'login' - def options - options = super - options.merge(login: Stripe.api_key) - end - - def options_for_purchase_or_auth(money, creditcard, gateway_options) - options = {} - options[:description] = "Spree Order ID: #{gateway_options[:order_id]}" - options[:currency] = gateway_options[:currency] - options[:stripe_account] = stripe_account_id - - creditcard = token_from_card_profile_ids(creditcard) - - [money, creditcard, options] - end - - def token_from_card_profile_ids(creditcard) - token_or_card_id = creditcard.gateway_payment_profile_id - customer = creditcard.gateway_customer_profile_id - - return nil if token_or_card_id.blank? - - # Assume the gateway_payment_profile_id is a token generated by StripeJS - return token_or_card_id if customer.blank? - - # Assume the gateway_payment_profile_id is a Stripe card_id - # So generate a new token, using the customer_id and card_id - tokenize_instance_customer_card(customer, token_or_card_id) - end - - def tokenize_instance_customer_card(customer, card) - token = Stripe::Token.create({ card: card, customer: customer }, - stripe_account: stripe_account_id) - token.id - end - - def failed_activemerchant_billing_response(error_message) - ActiveMerchant::Billing::Response.new(false, error_message) - end - - def ensure_enterprise_selected - return if preferred_enterprise_id&.positive? - - errors.add(:stripe_account_owner, I18n.t(:error_required)) - end - end - end -end diff --git a/spec/migrations/convert_stripe_connect_to_stripe_sca_spec.rb b/spec/migrations/convert_stripe_connect_to_stripe_sca_spec.rb index 6a37b1b4a5..a6cd7c244e 100644 --- a/spec/migrations/convert_stripe_connect_to_stripe_sca_spec.rb +++ b/spec/migrations/convert_stripe_connect_to_stripe_sca_spec.rb @@ -3,6 +3,15 @@ require 'spec_helper' require_relative '../../db/migrate/20220118053107_convert_stripe_connect_to_stripe_sca' +module Spree + class Gateway + class StripeConnect < Gateway::StripeSCA + # This class got deleted from the code base but this minimum definition + # is enough for this test. + end + end +end + describe ConvertStripeConnectToStripeSca do let(:owner) { create(:distributor_enterprise) } let(:new_owner) { create(:distributor_enterprise) } diff --git a/spec/models/spree/gateway/stripe_connect_spec.rb b/spec/models/spree/gateway/stripe_connect_spec.rb deleted file mode 100644 index 4b25c81439..0000000000 --- a/spec/models/spree/gateway/stripe_connect_spec.rb +++ /dev/null @@ -1,107 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Spree::Gateway::StripeConnect, type: :model do - let(:provider) do - instance_double(ActiveMerchant::Billing::StripeGateway).tap do |p| - allow(p).to receive(:purchase) - allow(p).to receive(:authorize) - allow(p).to receive(:capture) - allow(p).to receive(:refund) - end - end - - let(:stripe_account_id) { "acct_123" } - - before do - Stripe.api_key = "sk_test_123456" - allow(subject).to receive(:stripe_account_id) { stripe_account_id } - allow(subject).to receive(:options_for_purchase_or_auth).and_return(['money', 'cc', 'opts']) - allow(subject).to receive(:provider).and_return provider - end - - describe "#token_from_card_profile_ids" do - let(:creditcard) { double(:creditcard) } - context "when the credit card provided has a gateway_payment_profile_id" do - before do - allow(creditcard).to receive(:gateway_payment_profile_id) { "token_or_card_id123" } - allow(subject).to receive(:tokenize_instance_customer_card) { "tokenized" } - end - - context "when the credit card provided has a gateway_customer_profile_id" do - before { allow(creditcard).to receive(:gateway_customer_profile_id) { "customer_id123" } } - - it "requests a new token via tokenize_instance_customer_card" do - result = subject.send(:token_from_card_profile_ids, creditcard) - expect(result).to eq "tokenized" - end - end - - context "when the credit card provided does not have a gateway_customer_profile_id" do - before { allow(creditcard).to receive(:gateway_customer_profile_id) { nil } } - it "returns the gateway_payment_profile_id (assumed to be a token already)" do - result = subject.send(:token_from_card_profile_ids, creditcard) - expect(result).to eq "token_or_card_id123" - end - end - end - - context "when the credit card provided does not have a gateway_payment_profile_id" do - before { allow(creditcard).to receive(:gateway_payment_profile_id) { nil } } - before { allow(creditcard).to receive(:gateway_customer_profile_id) { "customer_id123" } } - - it "returns nil....?" do - result = subject.send(:token_from_card_profile_ids, creditcard) - expect(result).to be nil - end - end - end - - describe "#tokenize_instance_customer_card" do - let(:customer_id) { "customer123" } - let(:card_id) { "card123" } - let(:token_mock) { { id: "test_token123" } } - - before do - stub_request(:post, "https://api.stripe.com/v1/tokens") - .with(body: { "card" => "card123", "customer" => "customer123" }) - .to_return(body: JSON.generate(token_mock)) - end - - it "requests a new token for the customer and card from Stripe, and returns the id of the response" do - expect(subject.send(:tokenize_instance_customer_card, customer_id, - card_id)).to eq token_mock[:id] - end - end - - describe "#credit" do - let(:gateway_options) { { some: 'option' } } - let(:money) { double(:money) } - let(:response_code) { double(:response_code) } - - before do - subject.credit(money, double(:creditcard), response_code, gateway_options) - end - - it "delegates to ActiveMerchant::Billing::StripeGateway#refund" do - expect(provider).to have_received(:refund) - end - - it "adds the stripe_account to the gateway options hash" do - expect(provider).to have_received(:refund).with(money, response_code, - hash_including(stripe_account: stripe_account_id)) - end - end - - describe "#charging offline" do - let(:gateway_options) { { some: 'option' } } - let(:money) { double(:money) } - let(:card) { double(:creditcard) } - - it "uses #purchase to charge offline" do - subject.charge_offline(money, card, gateway_options) - expect(provider).to have_received(:purchase).with('money', 'cc', 'opts') - end - end -end