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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Drop `activemerchant` (#13655)
Damian Legawiec5eb9fcc70c4f
diff --git a/docs/developer/upgrades/5.3-to-5.4.mdx b/docs/developer/upgrades/5.3-to-5.4.mdxindex 047e5acf9f..9054a213e5 100644--- a/docs/developer/upgrades/5.3-to-5.4.mdx+++ b/docs/developer/upgrades/5.3-to-5.4.mdx@@ -135,6 +135,67 @@ zone = current_store.checkout_zonezone = Spree::Zone.default_tax```+### ActiveMerchant removed from Spree Core++Spree 5.4 no longer depends on the `activemerchant` gem. All internal usages have been replaced with lightweight Spree-native classes:++| ActiveMerchant class | Spree replacement |+|---|---|+| `ActiveMerchant::Billing::Response` | `Spree::PaymentResponse` |+| `ActiveMerchant::ConnectionError` | `Spree::PaymentConnectionError` |++`Spree::PaymentResponse` is a drop-in replacement with the same constructor signature:++```ruby+Spree::PaymentResponse.new(success, message, params = {}, options = {})+```++It supports the same methods: `success?`, `message`, `params`, `authorization`, `avs_result`, `cvv_result`, `test?`, and YAML serialization (for log entries).++#### Updating custom payment methods++If you have a custom payment method (gateway) that returns `ActiveMerchant::Billing::Response`, update it to return `Spree::PaymentResponse` instead:++```ruby+# Before+def authorize(money, source, options = {})+ # ...+ ActiveMerchant::Billing::Response.new(true, 'Success', {},+ authorization: transaction_id, test: test?)+end++# After+def authorize(money, source, options = {})+ # ...+ Spree::PaymentResponse.new(true, 'Success', {},+ authorization: transaction_id, test: test?)+end+```++If your gateway rescues `ActiveMerchant::ConnectionError`, update it to rescue `Spree::PaymentConnectionError`:++```ruby+# Before+rescue ActiveMerchant::ConnectionError => e++# After+rescue Spree::PaymentConnectionError => e+```++#### Using `spree_gateway` or other ActiveMerchant-based extensions++If you use the [`spree_gateway`](https://github.com/spree/spree_gateway) gem or another extension that depends on ActiveMerchant, add `activemerchant` directly to your application's `Gemfile`:++```ruby+gem 'activemerchant'+```++These extensions will continue to work — Spree's `LogEntry#parsed_details` still deserializes `ActiveMerchant::Billing::Response` objects when the gem is present.++#### Address hash method renamed++`Spree::Address#active_merchant_hash` has been renamed to `#gateway_hash`. A backwards-compatible alias is provided, so existing code continues to work without changes.+### (Optional) Install API v2Spree 5.4 ships with API v3, which marks deprecation of API v2. API v2 is still available but you need to install it separately. If you currently use API v2, you can do this by running the following command:diff --git a/server/Gemfile.lock b/server/Gemfile.lockindex 651492955d..f63b71099f 100644--- a/server/Gemfile.lock+++ b/server/Gemfile.lock@@ -49,7 +49,6 @@ PATHtypelizer (~> 0.8)spree_core (5.4.0.beta)active_storage_validations (= 1.3.0)- activemerchant (~> 1.67)acts-as-taggable-onacts_as_list (>= 0.8)any_ascii (~> 0.3.2)@@ -148,12 +147,6 @@ GEMactivejob (8.1.2)activesupport (= 8.1.2)globalid (>= 0.3.6)- activemerchant (1.137.0)- activesupport (>= 4.2)- builder (>= 2.1.2, < 4.0.0)- i18n (>= 0.6.9)- nokogiri (~> 1.4)- rexml (~> 3.3, >= 3.3.4)activemodel (8.1.2)activesupport (= 8.1.2)activerecord (8.1.2)@@ -867,7 +860,6 @@ CHECKSUMSactive_link_to (1.0.5) sha256=4830847b3d14589df1e9fc62038ceec015257fce975ec1c2a77836c461b139baactive_storage_validations (1.3.0) sha256=788e4189d51c7b55049bf2825ede964baa2fe531e22a31c59f67a962ebf8d71eactivejob (8.1.2) sha256=908dab3713b101859536375819f4156b07bdf4c232cc645e7538adb9e302f825- activemerchant (1.137.0) sha256=fdf5e33f5c94d1981bd632d72e282c0a5afaf06bac6df3f933c08a95e98bd5ceactivemodel (8.1.2) sha256=e21358c11ce68aed3f9838b7e464977bc007b4446c6e4059781e1d5c03bcf33eactiverecord (8.1.2) sha256=acfbe0cadfcc50fa208011fe6f4eb01cae682ebae0ef57145ba45380c74bcc44activestorage (8.1.2) sha256=8a63a48c3999caeee26a59441f813f94681fc35cc41aba7ce1f836add04fba76diff --git a/spree/admin/app/views/spree/admin/payment_methods/descriptions/_gateway.html.erb b/spree/admin/app/views/spree/admin/payment_methods/descriptions/_gateway.html.erbindex 1cc19ab02a..da7ff3ddf1 100644--- a/spree/admin/app/views/spree/admin/payment_methods/descriptions/_gateway.html.erb+++ b/spree/admin/app/views/spree/admin/payment_methods/descriptions/_gateway.html.erb@@ -1 +1 @@-Classic ActiveMerchant gateway provided by community.\ No newline at end of file+Classic payment gateway provided by community.\ No newline at end of filediff --git a/spree/core/app/models/spree/address.rb b/spree/core/app/models/spree/address.rbindex 171c2ec0b1..f121d8aa4f 100644--- a/spree/core/app/models/spree/address.rb+++ b/spree/core/app/models/spree/address.rb@@ -175,8 +175,8 @@ module Spreeattributes.except('id', 'created_at', 'updated_at', 'country_id').all? { |_, v| v.nil? }end- # Generates an ActiveMerchant compatible address hash- def active_merchant_hash+ # Generates an address hash for payment gateway options+ def gateway_hash{name: full_name,address1: address1,@@ -188,6 +188,7 @@ module Spreephone: phone}end+ alias_method :active_merchant_hash, :gateway_hashdef require_phone?# We want to collect phone number for quick checkout but not to validate itdiff --git a/spree/core/app/models/spree/credit_card.rb b/spree/core/app/models/spree/credit_card.rbindex 83c68cc1e2..feeab3d808 100644--- a/spree/core/app/models/spree/credit_card.rb+++ b/spree/core/app/models/spree/credit_card.rb@@ -2,7 +2,6 @@ module Spreeclass CreditCard < Spree.base_classhas_prefix_id :card # Stripe: card_- include ActiveMerchant::Billing::CreditCardMethodsinclude Spree::Metafieldsinclude Spree::Metadatainclude Spree::PaymentSourceConcern@@ -48,7 +47,6 @@ module Spree}scope :not_removed, -> { where(deleted_at: nil) }- # needed for some of the ActiveMerchant gateways (eg. SagePay)alias_attribute :brand, :cc_typestore_accessor :private_metadata, :wallet@@ -59,14 +57,6 @@ module Spreewallet&.[]('type')end- # ActiveMerchant::Billing::CreditCard added this accessor used by some gateways.- # More info: https://github.com/spree/spree/issues/6209- #- # Returns or sets the track data for the card- #- # @return [String]- attr_accessor :track_data-CARD_TYPES = {visa: /^4\d{12}(\d{3})?(\d{3})?$/,master: /^(5[1-5]\d{4}|677189|222[1-9]\d{2}|22[3-9]\d{3}|2[3-6]\d{4}|27[01]\d{3}|2720\d{2})\d{10}$/,@@ -151,10 +141,6 @@ module Spreebrand.present? ? brand.upcase : Spree.t(:no_cc_type)end- # ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it.- # Looking at the ActiveMerchant source code we should probably be calling #to_active_merchant before passing- # the object to ActiveMerchant but this should do for now.- ## Returns the first name of the cardholder.# @return [String]def first_name@@ -167,19 +153,6 @@ module Spreename.to_s.split(/[[:space:]]/, 2)[1]end- # Returns an ActiveMerchant::Billing::CreditCard object.- # @return [ActiveMerchant::Billing::CreditCard]- 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- )- end-def self.json_api_permitted_attributes['number', 'month', 'year', 'expiry', 'verification_value', 'first_name', 'last_name',diff --git a/spree/core/app/models/spree/gateway.rb b/spree/core/app/models/spree/gateway.rbindex cfe3cc9d74..ce9cf4af77 100644--- a/spree/core/app/models/spree/gateway.rb+++ b/spree/core/app/models/spree/gateway.rb@@ -20,9 +20,6 @@ module Spreedef providergateway_options = optionsgateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil?- if gateway_options[:server]- ActiveMerchant::Billing::Base.mode = gateway_options[:server].to_sym- end@provider ||= provider_class.new(gateway_options)enddiff --git a/spree/core/app/models/spree/gateway/bogus.rb b/spree/core/app/models/spree/gateway/bogus.rbindex a9c28396cf..e9d7dfc798 100644--- a/spree/core/app/models/spree/gateway/bogus.rb+++ b/spree/core/app/models/spree/gateway/bogus.rb@@ -32,39 +32,39 @@ module Spreedef authorize(_money, credit_card, _options = {})profile_id = credit_card.gateway_customer_profile_idif 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: 'D' })+ Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'D' })else- ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true)+ Spree::PaymentResponse.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true)endenddef purchase(_money, credit_card, _options = {})profile_id = credit_card.gateway_customer_profile_idif 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: 'M' })+ Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'M' })else- ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true)+ Spree::PaymentResponse.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true)endenddef credit(_money, _credit_card, _response_code, _options = {})- ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')+ Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')enddef capture(_money, authorization, _gateway_options)if authorization == '12345'- ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true)+ Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true)else- ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', error: 'Bogus Gateway: Forced failure', test: true)+ Spree::PaymentResponse.new(false, 'Bogus Gateway: Forced failure', error: 'Bogus Gateway: Forced failure', test: true)endenddef void(_response_code, _credit_card, _options = {})- ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: 'void-12345')+ Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: 'void-12345')enddef cancel(_response_code, _payment = nil)- ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')+ Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')enddef test?diff --git a/spree/core/app/models/spree/gateway/bogus_simple.rb b/spree/core/app/models/spree/gateway/bogus_simple.rbindex 056abe944b..d9707a94e9 100644--- a/spree/core/app/models/spree/gateway/bogus_simple.rb+++ b/spree/core/app/models/spree/gateway/bogus_simple.rb@@ -7,17 +7,17 @@ module Spreedef 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' })+ Spree::PaymentResponse.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)+ Spree::PaymentResponse.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true)endenddef 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' })+ Spree::PaymentResponse.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)+ Spree::PaymentResponse.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true)endendenddiff --git a/spree/core/app/models/spree/log_entry.rb b/spree/core/app/models/spree/log_entry.rbindex 675834f84a..8f74df18a8 100644--- a/spree/core/app/models/spree/log_entry.rb+++ b/spree/core/app/models/spree/log_entry.rb@@ -17,11 +17,14 @@ module Spreeenddef parsed_details- @details ||= if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.1.0')- YAML.safe_load(details, permitted_classes: [ActiveMerchant::Billing::Response])- else- YAML.safe_load(details, [ActiveMerchant::Billing::Response])- end+ @details ||= YAML.safe_load(+ details,+ permitted_classes: [+ Spree::PaymentResponse,+ ActiveSupport::HashWithIndifferentAccess,+ (ActiveMerchant::Billing::Response if defined?(ActiveMerchant::Billing::Response))+ ].compact+ )endendenddiff --git a/spree/core/app/models/spree/payment.rb b/spree/core/app/models/spree/payment.rbindex 1c742db595..cd3e036351 100644--- a/spree/core/app/models/spree/payment.rb+++ b/spree/core/app/models/spree/payment.rb@@ -348,7 +348,7 @@ module Spreereturn if source.respond_to?(:imported) && source.importedpayment_method.create_profile(self)- rescue ActiveMerchant::ConnectionError => e+ rescue Spree::PaymentConnectionError => egateway_error eenddiff --git a/spree/core/app/models/spree/payment/gateway_options.rb b/spree/core/app/models/spree/payment/gateway_options.rbindex c943bb9508..1a3a61f5cf 100644--- a/spree/core/app/models/spree/payment/gateway_options.rb+++ b/spree/core/app/models/spree/payment/gateway_options.rb@@ -51,11 +51,11 @@ module Spreeenddef billing_address- order.bill_address.try(:active_merchant_hash)+ order.bill_address.try(:gateway_hash)enddef shipping_address- order.ship_address.try(:active_merchant_hash)+ order.ship_address.try(:gateway_hash)enddef hash_methodsdiff --git a/spree/core/app/models/spree/payment/processing.rb b/spree/core/app/models/spree/payment/processing.rbindex 9dd3680b59..52e1f23224 100644--- a/spree/core/app/models/spree/payment/processing.rb+++ b/spree/core/app/models/spree/payment/processing.rb@@ -48,7 +48,7 @@ module Spreeamount ||= money.amount_in_centsstarted_processing!protect_from_connection_error do- # Standard ActiveMerchant capture usage+ # Capture the payment amountresponse = payment_method.capture(amount,response_code,@@ -71,7 +71,7 @@ module Spree# so supply the authorization itself as well as the credit card, rather than just the authorization coderesponse = payment_method.void(response_code, source, gateway_options)else- # Standard ActiveMerchant void usage+ # Standard void usageresponse = payment_method.void(response_code, gateway_options)endrecord_response(response)@@ -165,15 +165,15 @@ module Spreedef protect_from_connection_erroryield- rescue ActiveMerchant::ConnectionError => e+ rescue Spree::PaymentConnectionError => efailure!gateway_error(e)enddef gateway_error(error)- text = if error.is_a? ActiveMerchant::Billing::Response+ text = if error.is_a? Spree::PaymentResponseerror.params['message'] || error.params['response_reason_text'] || error.message- elsif error.is_a? ActiveMerchant::ConnectionError+ elsif error.is_a? Spree::PaymentConnectionErrorSpree.t(:unable_to_connect_to_gateway)elseerror.to_sdiff --git a/spree/core/app/models/spree/payment_connection_error.rb b/spree/core/app/models/spree/payment_connection_error.rbnew file mode 100644index 0000000000..27a11efeaa--- /dev/null+++ b/spree/core/app/models/spree/payment_connection_error.rb@@ -0,0 +1,3 @@+module Spree+ class PaymentConnectionError < StandardError; end+enddiff --git a/spree/core/app/models/spree/payment_method/check.rb b/spree/core/app/models/spree/payment_method/check.rbindex f75abdce60..96786120ef 100644--- a/spree/core/app/models/spree/payment_method/check.rb+++ b/spree/core/app/models/spree/payment_method/check.rb@@ -37,7 +37,7 @@ module Spreeprivatedef simulated_successful_billing_response- ActiveMerchant::Billing::Response.new(true, '', {}, {})+ Spree::PaymentResponse.new(true, '', {}, {})endendenddiff --git a/spree/core/app/models/spree/payment_method/store_credit.rb b/spree/core/app/models/spree/payment_method/store_credit.rbindex abdecdf850..094257382c 100644--- a/spree/core/app/models/spree/payment_method/store_credit.rb+++ b/spree/core/app/models/spree/payment_method/store_credit.rb@@ -22,7 +22,7 @@ module Spreedef authorize(amount_in_cents, store_credit, gateway_options = {})if store_credit.nil?- ActiveMerchant::Billing::Response.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})+ Spree::PaymentResponse.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})elseaction = lambda do |store_credit|store_credit.authorize(@@ -58,7 +58,7 @@ module Spreeendif event.blank?- ActiveMerchant::Billing::Response.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})+ Spree::PaymentResponse.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})elsecapture(amount_in_cents, event.authorization_code, gateway_options)end@@ -111,14 +111,14 @@ module Spreestore_credit.with_lock doif response = action.call(store_credit)# note that we only need to return the auth code on an 'auth', but it's innocuous to always return- ActiveMerchant::Billing::Response.new(+ Spree::PaymentResponse.new(true,Spree.t('store_credit_payment_method.successful_action', action: action_name),{},authorization: auth_code || response)else- ActiveMerchant::Billing::Response.new(false, store_credit.errors.full_messages.to_sentence, {}, {})+ Spree::PaymentResponse.new(false, store_credit.errors.full_messages.to_sentence, {}, {})endendend@@ -128,7 +128,7 @@ module Spreestore_credit = StoreCreditEvent.find_by(authorization_code: auth_code).try(:store_credit)if store_credit.nil?- ActiveMerchant::Billing::Response.new(+ Spree::PaymentResponse.new(false,Spree.t('store_credit_payment_method.unable_to_find_for_action', auth_code: auth_code, action: action_name),{},diff --git a/spree/core/app/models/spree/payment_response.rb b/spree/core/app/models/spree/payment_response.rbnew file mode 100644index 0000000000..d16e8fb0ba--- /dev/null+++ b/spree/core/app/models/spree/payment_response.rb@@ -0,0 +1,70 @@+module Spree+ # Lightweight value object representing a payment gateway response.+ #+ # This is Spree's native replacement for +ActiveMerchant::Billing::Response+.+ # It carries the same interface so existing payment method implementations+ # (Bogus, Check, StoreCredit, and third-party gateways) can return it without+ # any callers needing to change.+ #+ # @example Successful authorization+ # Spree::PaymentResponse.new(true, 'Transaction approved', {},+ # authorization: 'ch_abc123',+ # avs_result: { code: 'D' },+ # cvv_result: { code: 'M', message: 'Match' },+ # test: true)+ #+ # @example Failed charge+ # Spree::PaymentResponse.new(false, 'Card declined', { message: 'Insufficient funds' })+ #+ class PaymentResponse+ # @return [HashWithIndifferentAccess] raw gateway params+ attr_reader :params++ # @return [String] human-readable result message+ attr_reader :message++ # @return [Boolean] whether the response came from a test/sandbox environment+ attr_reader :test++ # @return [String, nil] gateway authorization / transaction reference+ attr_reader :authorization++ # @return [Hash] AVS (Address Verification System) result with +"code"+ key+ attr_reader :avs_result++ # @return [Hash] CVV result with +"code"+ and +"message"+ keys+ attr_reader :cvv_result++ # @param success [Boolean] whether the gateway action succeeded+ # @param message [String] human-readable result message+ # @param params [Hash] raw key/value pairs returned by the gateway+ # @param options [Hash] additional metadata+ # @option options [String] :authorization gateway transaction reference+ # @option options [Hash] :avs_result must contain +:code++ # @option options [Hash] :cvv_result may contain +:code+ and +:message++ # @option options [Boolean] :test sandbox/test flag+ def initialize(success, message, params = {}, options = {})+ @success = success+ @message = message+ @params = params.with_indifferent_access+ @test = options[:test] || false+ @authorization = options[:authorization]+ @avs_result = options[:avs_result] ? { 'code' => options[:avs_result][:code] } : { 'code' => nil }+ @cvv_result = if options[:cvv_result]+ { 'code' => options[:cvv_result][:code], 'message' => options[:cvv_result][:message] }+ else+ { 'code' => nil, 'message' => nil }+ end+ end++ # @return [Boolean] whether the gateway action succeeded+ def success?+ @success+ end++ # @return [Boolean] whether the response came from a test/sandbox environment+ def test?+ @test+ end+ end+enddiff --git a/spree/core/app/models/spree/refund.rb b/spree/core/app/models/spree/refund.rbindex 6ddd871444..bd472d2de0 100644--- a/spree/core/app/models/spree/refund.rb+++ b/spree/core/app/models/spree/refund.rb@@ -83,7 +83,7 @@ module Spreeupdate_orderend- # return an activemerchant response object if successful or else raise an error+ # return a payment response object if successful or else raise an errordef process!(credit_cents)refund_total_in_cents = calculate_refund_amount(credit_cents)@@ -102,7 +102,7 @@ module Spreeendresponse- rescue ActiveMerchant::ConnectionError => e+ rescue Spree::PaymentConnectionError => eRails.logger.error(Spree.t(:gateway_error) + " #{e.inspect}")raise Core::GatewayError, Spree.t(:unable_to_connect_to_gateway)enddiff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rbindex deb77dc942..03bed98e47 100644--- a/spree/core/lib/spree/core.rb+++ b/spree/core/lib/spree/core.rb@@ -11,7 +11,7 @@ require 'action_cable/engine'require 'mail'require 'action_mailer/railtie'-require 'active_merchant'+require 'acts_as_list'require 'acts-as-taggable-on'require 'awesome_nested_set'diff --git a/spree/core/spec/models/spree/credit_card_spec.rb b/spree/core/spec/models/spree/credit_card_spec.rbindex 8fa35d3b85..d3667a41b3 100644--- a/spree/core/spec/models/spree/credit_card_spec.rb+++ b/spree/core/spec/models/spree/credit_card_spec.rb@@ -22,10 +22,6 @@ describe Spree::CreditCard, type: :model doSpree::Payment.state_machine.states.keysend- it 'responds to track_data' do- expect(credit_card.respond_to?(:track_data)).to be true- end-describe '#can_capture?' doshared_examples 'can be captured' doit 'can be captured' do@@ -325,26 +321,6 @@ describe Spree::CreditCard, type: :model doendend- describe '#to_active_merchant' do- before do- credit_card.number = '4111111111111111'- credit_card.year = Time.current.year- credit_card.month = Time.current.month- credit_card.name = 'Ludwig van Beethoven'- credit_card.verification_value = 123- end-- it 'converts to an ActiveMerchant::Billing::CreditCard object' do- am_card = credit_card.to_active_merchant- expect(am_card.number).to eq('4111111111111111')- expect(am_card.year).to eq(Time.current.year)- expect(am_card.month).to eq(Time.current.month)- expect(am_card.first_name).to eq('Ludwig')- expect(am_card.last_name).to eq('van Beethoven')- expect(am_card.verification_value).to eq('123')- end- end-it 'ensures only one credit card per user is default at a time' douser = FactoryBot.create(:user)first = FactoryBot.create(:credit_card, user: user, default: true)diff --git a/spree/core/spec/models/spree/log_entry_spec.rb b/spree/core/spec/models/spree/log_entry_spec.rbindex 260dfc7d16..14ef032f32 100644--- a/spree/core/spec/models/spree/log_entry_spec.rb+++ b/spree/core/spec/models/spree/log_entry_spec.rb@@ -2,13 +2,13 @@ require 'spec_helper'describe Spree::LogEntry, type: :model dolet(:log_entry) { create(:log_entry, details: details) }- let(:details) { "--- !ruby/object:ActiveMerchant::Billing::Response\nparams: {}\nmessage: 'Bogus Gateway: Forced success'\nsuccess: true\ntest: true\nauthorization: \nfraud_review: \nerror_code: \nemv_authorization: \nnetwork_transaction_id: \navs_result:\n code: \n message: \n street_match: \n postal_match: \ncvv_result:\n code: \n message: \n" }+ let(:details) { Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true).to_yaml }describe '#parsed_details' dosubject { log_entry.parsed_details }- it 'deserializes log entry with billing response' do- expect(subject).to be_instance_of(ActiveMerchant::Billing::Response)+ it 'deserializes log entry with payment response' do+ expect(subject).to be_instance_of(Spree::PaymentResponse)expect(subject.message).to eq('Bogus Gateway: Forced success')endenddiff --git a/spree/core/spec/models/spree/payment/gateway_options_spec.rb b/spree/core/spec/models/spree/payment/gateway_options_spec.rbindex 640b5b1ff9..d1c99d7fe3 100644--- a/spree/core/spec/models/spree/payment/gateway_options_spec.rb+++ b/spree/core/spec/models/spree/payment/gateway_options_spec.rb@@ -37,11 +37,11 @@ RSpec.describe Spree::Payment::GatewayOptions, type: :model doendlet(:bill_address) do- double Spree::Address, active_merchant_hash: { bill: :address }+ double Spree::Address, gateway_hash: { bill: :address }endlet(:ship_address) do- double Spree::Address, active_merchant_hash: { ship: :address }+ double Spree::Address, gateway_hash: { ship: :address }enddescribe '#order' dodiff --git a/spree/core/spec/models/spree/payment/store_credit_spec.rb b/spree/core/spec/models/spree/payment/store_credit_spec.rbindex 551381d3da..7dd73503e5 100644--- a/spree/core/spec/models/spree/payment/store_credit_spec.rb+++ b/spree/core/spec/models/spree/payment/store_credit_spec.rb@@ -22,7 +22,7 @@ describe 'Payment' doendlet(:successful_response) do- ActiveMerchant::Billing::Response.new(+ Spree::PaymentResponse.new(true,Spree.t('store_credit_payment_method.successful_action', action: :cancel),{},@@ -31,7 +31,7 @@ describe 'Payment' doendlet(:failed_response) do- ActiveMerchant::Billing::Response.new(+ Spree::PaymentResponse.new(false,Spree.t('store_credit_payment_method.unable_to_find_for_action', action: :cancel,auth_code: payment.response_code),diff --git a/spree/core/spec/models/spree/payment_response_spec.rb b/spree/core/spec/models/spree/payment_response_spec.rbnew file mode 100644index 0000000000..eb5c0186bd--- /dev/null+++ b/spree/core/spec/models/spree/payment_response_spec.rb@@ -0,0 +1,94 @@+require 'spec_helper'++RSpec.describe Spree::PaymentResponse do+ describe '#initialize' do+ context 'with minimal arguments' do+ subject { described_class.new(true, 'Success') }++ it 'sets success, message and defaults' do+ expect(subject).to be_success+ expect(subject.message).to eq('Success')+ expect(subject.params).to eq({})+ expect(subject.test).to be false+ expect(subject.authorization).to be_nil+ expect(subject.avs_result).to eq('code' => nil)+ expect(subject.cvv_result).to eq('code' => nil, 'message' => nil)+ end+ end++ context 'with all options' do+ subject do+ described_class.new(+ true,+ 'Approved',+ { 'transaction_id' => 'tx_1' },+ authorization: 'auth_123',+ avs_result: { code: 'D' },+ cvv_result: { code: 'M', message: 'Match' },+ test: true+ )+ end++ it 'stores authorization' do+ expect(subject.authorization).to eq('auth_123')+ end++ it 'stores avs_result with string keys' do+ expect(subject.avs_result).to eq('code' => 'D')+ end++ it 'stores cvv_result with string keys' do+ expect(subject.cvv_result).to eq('code' => 'M', 'message' => 'Match')+ end++ it 'stores test flag' do+ expect(subject).to be_test+ end++ it 'stores params with indifferent access' do+ expect(subject.params[:transaction_id]).to eq('tx_1')+ expect(subject.params['transaction_id']).to eq('tx_1')+ end+ end+ end++ describe '#success?' do+ it 'returns true for a successful response' do+ expect(described_class.new(true, 'ok')).to be_success+ end++ it 'returns false for a failed response' do+ expect(described_class.new(false, 'fail')).not_to be_success+ end+ end++ describe '#test?' do+ it 'returns true when test option is set' do+ expect(described_class.new(true, '', {}, test: true)).to be_test+ end++ it 'returns false by default' do+ expect(described_class.new(true, '')).not_to be_test+ end+ end++ describe 'YAML serialization' do+ it 'round-trips through YAML.safe_load' do+ original = described_class.new(true, 'Captured', { 'ref' => '42' },+ authorization: 'auth_1', test: true,+ avs_result: { code: 'Y' },+ cvv_result: { code: 'M', message: 'Match' })++ yaml = original.to_yaml+ restored = YAML.safe_load(yaml, permitted_classes: [described_class, ActiveSupport::HashWithIndifferentAccess])++ expect(restored).to be_a(described_class)+ expect(restored).to be_success+ expect(restored.message).to eq('Captured')+ expect(restored.authorization).to eq('auth_1')+ expect(restored.params['ref']).to eq('42')+ expect(restored.avs_result['code']).to eq('Y')+ expect(restored.cvv_result['code']).to eq('M')+ end+ end+enddiff --git a/spree/core/spec/models/spree/payment_spec.rb b/spree/core/spec/models/spree/payment_spec.rbindex 0ac39c817a..91876f4dbf 100644--- a/spree/core/spec/models/spree/payment_spec.rb+++ b/spree/core/spec/models/spree/payment_spec.rb@@ -30,13 +30,13 @@ describe Spree::Payment, type: :model dolet(:amount_in_cents) { (payment.amount * 100).round }let!(:success_response) do- ActiveMerchant::Billing::Response.new(true, '', {}, authorization: '123',- cvv_result: cvv_code,- avs_result: { code: avs_code })+ Spree::PaymentResponse.new(true, '', {}, authorization: '123',+ cvv_result: { code: cvv_code, message: nil },+ avs_result: { code: avs_code })endlet(:failed_response) do- ActiveMerchant::Billing::Response.new(false, '', {}, {})+ Spree::PaymentResponse.new(false, '', {}, {})endbefore do@@ -356,7 +356,7 @@ describe Spree::Payment, type: :model doexpect(payment.response_code).to eq('123')expect(payment.avs_response).to eq(avs_code)expect(payment.cvv_response_code).to eq(cvv_code)- expect(payment.cvv_response_message).to eq(ActiveMerchant::Billing::CVVResult::MESSAGES[cvv_code])+ expect(payment.cvv_response_message).to be_nilendit 'makes payment pending' do@@ -379,11 +379,11 @@ describe Spree::Payment, type: :model docontext 'when there is an error connecting to the gateway' dolet(:connection_error_message) { 'gateway_error' }- let(:connection_error) { ActiveMerchant::ConnectionError.new(connection_error_message, nil) }+ let(:connection_error) { Spree::PaymentConnectionError.new(connection_error_message) }before { allow(gateway).to receive(:authorize).and_raise(connection_error) }- it 'raises ActiveMerchant::ConnectionError and marks payment as failed' do+ it 'raises Spree::Core::GatewayError and marks payment as failed' doexpect(payment).to receive(:failure!)expect(payment).not_to receive(:pend)expect { payment.authorize! }.to raise_error(Spree::Core::GatewayError)@@ -454,11 +454,11 @@ describe Spree::Payment, type: :model docontext 'when there is an error connecting to the gateway' dolet(:connection_error_message) { 'gateway_error' }- let(:connection_error) { ActiveMerchant::ConnectionError.new(connection_error_message, nil) }+ let(:connection_error) { Spree::PaymentConnectionError.new(connection_error_message) }before { allow(gateway).to receive(:purchase).and_raise(connection_error) }- it 'raises ActiveMerchant::ConnectionError and marks payment as failed' do+ it 'raises Spree::Core::GatewayError and marks payment as failed' doexpect(payment).to receive(:failure!)expect(payment).not_to receive(:pend)expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)@@ -596,11 +596,11 @@ describe Spree::Payment, type: :model docontext 'when there is an error connecting to the gateway' dolet(:connection_error_message) { 'gateway_error' }- let(:connection_error) { ActiveMerchant::ConnectionError.new(connection_error_message, nil) }+ let(:connection_error) { Spree::PaymentConnectionError.new(connection_error_message) }before { allow(gateway).to receive(:capture).and_raise(connection_error) }- it 'raises ActiveMerchant::ConnectionError and marks payment as failed' do+ it 'raises Spree::Core::GatewayError and marks payment as failed' doexpect(payment).to receive(:failure!)expect(payment).not_to receive(:pend)expect { payment.capture! }.to raise_error(Spree::Core::GatewayError)@@ -671,11 +671,11 @@ describe Spree::Payment, type: :model docontext 'when there is an error connecting to the gateway' dolet(:connection_error_message) { 'gateway_error' }- let(:connection_error) { ActiveMerchant::ConnectionError.new(connection_error_message, nil) }+ let(:connection_error) { Spree::PaymentConnectionError.new(connection_error_message) }before { allow(gateway).to receive(:void).and_raise(connection_error) }- it 'raises ActiveMerchant::ConnectionError and marks payment as failed' do+ it 'raises Spree::Core::GatewayError and marks payment as failed' doexpect(payment).to receive(:failure!)expect(payment).not_to receive(:pend)expect { payment.void_transaction! }.to raise_error(Spree::Core::GatewayError)@@ -820,8 +820,8 @@ describe Spree::Payment, type: :model docontext 'when there is an error connecting to the gateway' doit 'calls gateway_error' do- message = double('gateway_error')- connection_error = ActiveMerchant::ConnectionError.new(message, nil)+ message = 'gateway_error'+ connection_error = Spree::PaymentConnectionError.new(message)expect(gateway).to receive(:create_profile).and_raise(connection_error)expect doSpree::Payment.create(diff --git a/spree/core/spec/models/spree/refund_spec.rb b/spree/core/spec/models/spree/refund_spec.rbindex e835bb5598..525bfc58c7 100644--- a/spree/core/spec/models/spree/refund_spec.rb+++ b/spree/core/spec/models/spree/refund_spec.rb@@ -25,7 +25,7 @@ describe Spree::Refund, type: :model dolet(:refund_reason) { create(:refund_reason) }let(:gateway_response) do- ActiveMerchant::Billing::Response.new(+ Spree::PaymentResponse.new(gateway_response_success,gateway_response_message,gateway_response_params,@@ -146,15 +146,15 @@ describe Spree::Refund, type: :model doendend- context 'with an activemerchant gateway connection error' do+ context 'with a gateway connection error' dobefore do- message = double('gateway_error')+ message = 'gateway_error'expect(payment.payment_method).to receive(:credit).with(amount_in_cents,payment.source,payment.transaction_id,originator: an_instance_of(Spree::Refund)- ).and_raise(ActiveMerchant::ConnectionError.new(message, nil))+ ).and_raise(Spree::PaymentConnectionError.new(message))endit 'raises Spree::Core::GatewayError' dodiff --git a/spree/core/spree_core.gemspec b/spree/core/spree_core.gemspecindex 29a6033e0c..7c4d32e3d0 100644--- a/spree/core/spree_core.gemspec+++ b/spree/core/spree_core.gemspec@@ -30,7 +30,7 @@ Gem::Specification.new do |s|s.add_dependency 'rails', '>= 7.2', '< 8.2'- s.add_dependency 'activemerchant', '~> 1.67'+s.add_dependency 'acts_as_list', '>= 0.8's.add_dependency 'acts-as-taggable-on's.add_dependency 'awesome_nested_set', '~> 3.3', '>= 3.3.1'