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.

Add `Gateway#gateway_dashboard_payment_url` (#12256)

Damian Legawiec435a946443ea
diff --git a/core/app/models/spree/gateway.rb b/core/app/models/spree/gateway.rb
index c7bf5643dc..de8aa66e83 100644
--- a/core/app/models/spree/gateway.rb
+++ b/core/app/models/spree/gateway.rb
@@ -10,6 +10,13 @@ module Spree
CreditCard
end
+ # Override in the gateway to provide a payment url
+ # eg. for Stripe, this would be the payment intent url
+ # https://dashboard.stripe.com/payments/#{payment.transaction_id}
+ def gateway_dashboard_payment_url(_payment)
+ nil
+ end
+
def provider
gateway_options = options
gateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil?
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index 556cbb8717..1461d303ec 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -77,9 +77,7 @@ module Spree
self.whitelisted_ransackable_attributes = %w[number amount state response_code avs_response cvv_response_code cvv_response_message]
# transaction_id is much easier to understand
- def transaction_id
- response_code
- end
+ alias_attribute :transaction_id, :response_code
delegate :currency, to: :order
@@ -194,6 +192,10 @@ module Spree
true
end
+ def gateway_dashboard_payment_url
+ payment_method.try(:gateway_dashboard_payment_url, self)
+ end
+
def captured_amount
capture_events.sum(:amount)
end
diff --git a/core/spec/models/spree/gateway_spec.rb b/core/spec/models/spree/gateway_spec.rb
index 3347dca451..f25dd06e23 100644
--- a/core/spec/models/spree/gateway_spec.rb
+++ b/core/spec/models/spree/gateway_spec.rb
@@ -92,4 +92,23 @@ describe Spree::Gateway, type: :model do
publishable_preference2: 'public2'
})
end
+
+ describe '#gateway_dashboard_payment_url' do
+ let(:payment_method) { create(:credit_card_payment_method) }
+ let(:payment) { create(:payment, payment_method: payment_method, transaction_id: '123') }
+
+ it 'returns nil' do
+ expect(payment_method.gateway_dashboard_payment_url(payment)).to be_nil
+ end
+
+ context 'when implemented' do
+ before do
+ expect(payment_method).to receive(:gateway_dashboard_payment_url).with(payment).and_return("https://dashboard.stripe.com/payments/#{payment.transaction_id}")
+ end
+
+ it 'returns the url' do
+ expect(payment_method.gateway_dashboard_payment_url(payment)).to eq('https://dashboard.stripe.com/payments/123')
+ end
+ end
+ end
end
diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb
index 62f3747f17..02e0cc4d56 100644
--- a/core/spec/models/spree/payment_spec.rb
+++ b/core/spec/models/spree/payment_spec.rb
@@ -1048,4 +1048,22 @@ describe Spree::Payment, type: :model do
end
end
end
+
+ describe '#gateway_dashboard_payment_url' do
+ it 'returns nil' do
+ expect(payment.gateway_dashboard_payment_url).to be_nil
+ end
+
+ let(:payment) { create(:payment, payment_method: gateway, transaction_id: '123') }
+
+ context 'when implemented' do
+ before do
+ expect(gateway).to receive(:gateway_dashboard_payment_url).with(payment).and_return("https://dashboard.stripe.com/payments/#{payment.transaction_id}")
+ end
+
+ it 'returns the url' do
+ expect(payment.gateway_dashboard_payment_url).to eq("https://dashboard.stripe.com/payments/#{payment.transaction_id}")
+ end
+ end
+ end
end