Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Apply the review's deprioritized cleanups

Damian Legawiecec04986131d9
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb
index f4ada96bc2..a95a647803 100644
--- a/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb
@@ -27,7 +27,7 @@ module SpreeStripe
total = amount.presence || order.total_minus_store_credits
amount_in_cents = Spree::Money.new(total, currency: order.currency).cents
- raise Spree::Core::GatewayError, I18n.t('spree.stripe.payment_session_errors.zero_amount') if amount_in_cents.zero?
+ raise Spree::Core::GatewayError, Spree.t('stripe.payment_session_errors.zero_amount') if amount_in_cents.zero?
gateway_customer = fetch_or_create_customer(order: order)
stripe_payment_method_id = external_data[:stripe_payment_method_id] || external_data['stripe_payment_method_id']
diff --git a/spree/providers/stripe/app/services/spree_stripe/create_payment_session.rb b/spree/providers/stripe/app/services/spree_stripe/create_payment_session.rb
deleted file mode 100644
index 660cc06ef2..0000000000
--- a/spree/providers/stripe/app/services/spree_stripe/create_payment_session.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-module SpreeStripe
- # Reuses the pending Stripe payment session (keeping its amount in sync with
- # the total) or creates a new one.
- class CreatePaymentSession
- # @param owner [Spree::Cart, Spree::Order]
- # @param gateway [SpreeStripe::Gateway]
- # @return [Spree::PaymentSessions::Stripe, nil]
- def call(owner, gateway)
- amount = owner.total_minus_store_credits
- return if Spree::Money.new(amount, currency: owner.currency).cents.zero?
-
- session = owner.payment_sessions.where(payment_method: gateway, status: 'pending').order(:created_at).last
-
- return gateway.create_payment_session(order: owner) unless session
-
- gateway.update_payment_session(payment_session: session, amount: amount) if session.amount != amount
- session
- end
- end
-end
diff --git a/spree/providers/stripe/app/services/spree_stripe/update_customer.rb b/spree/providers/stripe/app/services/spree_stripe/update_customer.rb
index 5c8271463e..549545b2bb 100644
--- a/spree/providers/stripe/app/services/spree_stripe/update_customer.rb
+++ b/spree/providers/stripe/app/services/spree_stripe/update_customer.rb
@@ -3,7 +3,7 @@ module SpreeStripe
class UpdateCustomer
# @param customer [Spree::Customer]
def call(customer:)
- gateway_customers = customer.gateway_customers.for_provider(SpreeStripe::Gateway)
+ gateway_customers = customer.gateway_customers.for_provider(SpreeStripe::Gateway).includes(:payment_method)
return if gateway_customers.empty?
gateway_customers.each do |gateway_customer|
diff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb
index 07d6be3b26..47d2fa201a 100644
--- a/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb
+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb
@@ -80,7 +80,7 @@ RSpec.describe SpreeStripe::Gateway::PaymentSessions do
it 'raises a GatewayError when amount is zero' do
allow(order).to receive(:total_minus_store_credits).and_return(0)
- expect { subject }.to raise_error(Spree::Core::GatewayError, I18n.t('spree.stripe.payment_session_errors.zero_amount'))
+ expect { subject }.to raise_error(Spree::Core::GatewayError, Spree.t('stripe.payment_session_errors.zero_amount'))
end
context 'when the owner is a cart' do
diff --git a/spree/providers/stripe/spec/services/spree_stripe/create_payment_session_spec.rb b/spree/providers/stripe/spec/services/spree_stripe/create_payment_session_spec.rb
deleted file mode 100644
index 8a6a16d5b0..0000000000
--- a/spree/providers/stripe/spec/services/spree_stripe/create_payment_session_spec.rb
+++ /dev/null
@@ -1,51 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe SpreeStripe::CreatePaymentSession do
- subject(:session) { described_class.new.call(order, gateway) }
-
- let(:store) { @default_store }
- let(:gateway) { create(:stripe_gateway, store: store) }
- let(:order) { create(:order_with_line_items, store: store) }
-
- context 'when store credits cover the whole order (zero amount)' do
- before { allow(order).to receive(:total_minus_store_credits).and_return(0) }
-
- it 'skips Stripe and returns nil' do
- expect(gateway).not_to receive(:create_payment_session)
- expect(gateway).not_to receive(:update_payment_session)
- expect(session).to be_nil
- end
- end
-
- context 'when no pending session exists' do
- it 'creates one for the order' do
- new_session = instance_double(Spree::PaymentSessions::Stripe)
- expect(gateway).to receive(:create_payment_session).with(order: order).and_return(new_session)
- expect(session).to eq(new_session)
- end
- end
-
- context 'when a pending session exists with the current amount' do
- let!(:existing) do
- create(:stripe_payment_session, owner: order, payment_method: gateway, amount: order.total_minus_store_credits)
- end
-
- it 'reuses it without creating or updating' do
- expect(gateway).not_to receive(:create_payment_session)
- expect(gateway).not_to receive(:update_payment_session)
- expect(session).to eq(existing)
- end
- end
-
- context 'when a pending session exists with a stale amount' do
- let!(:existing) do
- create(:stripe_payment_session, owner: order, payment_method: gateway, amount: 1)
- end
-
- it 'syncs the amount before reusing it' do
- expect(gateway).to receive(:update_payment_session).
- with(payment_session: existing, amount: order.total_minus_store_credits)
- expect(session).to eq(existing)
- end
- end
-end