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.

[SD-1369] Enables user to remove credit card (#10592)

Damian Legawiecf274c8efdb79
diff --git a/api/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb b/api/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb
index cce9b75ff8..f985b4eabb 100644
--- a/api/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb
@@ -6,8 +6,18 @@ module Spree
class CreditCardsController < ::Spree::Api::V2::ResourceController
before_action :require_spree_current_user
+ def destroy
+ spree_authorize! :destroy, resource, resource
+
+ destroy_service.call(card: resource)
+ end
+
private
+ def resource
+ params[:id].eql?('default') ? scope.default.first! : scope.find(params[:id])
+ end
+
def model_class
Spree::CreditCard
end
@@ -28,8 +38,8 @@ module Spree
Spree::Api::Dependencies.storefront_credit_card_serializer.constantize
end
- def resource_finder
- Spree::Api::Dependencies.storefront_credit_card_finder.constantize
+ def destroy_service
+ Spree::Api::Dependencies.storefront_credit_cards_destroy_service.constantize
end
end
end
diff --git a/api/app/models/spree/api_dependencies.rb b/api/app/models/spree/api_dependencies.rb
index 79ed056f44..590a05bdad 100644
--- a/api/app/models/spree/api_dependencies.rb
+++ b/api/app/models/spree/api_dependencies.rb
@@ -19,7 +19,7 @@ module Spree
:storefront_store_serializer, :storefront_address_serializer, :storefront_order_serializer,
:storefront_account_create_address_service, :storefront_account_update_address_service, :storefront_address_finder,
:storefront_account_create_service, :storefront_account_update_service, :storefront_collection_sorter, :error_handler,
- :storefront_cart_empty_service, :storefront_cart_destroy_service
+ :storefront_cart_empty_service, :storefront_cart_destroy_service, :storefront_credit_cards_destroy_service
].freeze
attr_accessor *INJECTION_POINTS
@@ -62,6 +62,9 @@ module Spree
@storefront_account_create_address_service = Spree::Dependencies.account_create_address_service
@storefront_account_update_address_service = Spree::Dependencies.account_update_address_service
+ # credit cards
+ @storefront_credit_cards_destroy_service = Spree::Dependencies.credit_cards_destroy_service
+
# serializers
@storefront_address_serializer = 'Spree::V2::Storefront::AddressSerializer'
@storefront_cart_serializer = 'Spree::V2::Storefront::CartSerializer'
diff --git a/api/config/routes.rb b/api/config/routes.rb
index aef68bef85..d62b7947c9 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -153,7 +153,7 @@ Spree::Core::Engine.add_routes do
namespace :account do
resources :addresses, controller: :addresses
- resources :credit_cards, controller: :credit_cards, only: %i[index show]
+ resources :credit_cards, controller: :credit_cards, only: %i[index show destroy]
resources :orders, controller: :orders, only: %i[index show]
end
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index 0273528764..35913dda14 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -314,6 +314,23 @@ paths:
description: Filter based on payment method ID
- $ref: '#/components/parameters/CreditCardIncludeParam'
- $ref: '#/components/parameters/SparseFieldsParam'
+ '/api/v2/storefront/account/credit_cards/{id}':
+ delete:
+ description: 'Removes selected credit card for the signed in User. It uses soft-delete.'
+ tags:
+ - Account
+ operationId: Remove Credit Card
+ parameters:
+ - $ref: '#/components/parameters/CreditCardIncludeParam'
+ - $ref: '#/components/parameters/SparseFieldsParam'
+ responses:
+ '204':
+ description: Requested Credit Card has been removed from the User Account
+ '404':
+ $ref: '#/components/responses/404NotFound'
+ security:
+ - bearerAuth: []
+ summary: Remove Credit Card
/api/v2/storefront/account/credit_cards/default:
get:
description: Return the User's default Credit Card
diff --git a/api/spec/requests/spree/api/v2/storefront/account/credit_cards_spec.rb b/api/spec/requests/spree/api/v2/storefront/account/credit_cards_spec.rb
index ace6c9b1cc..0ead476fd7 100644
--- a/api/spec/requests/spree/api/v2/storefront/account/credit_cards_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/account/credit_cards_spec.rb
@@ -20,13 +20,15 @@ describe 'Storefront API v2 CreditCards spec', type: :request do
include_context 'API v2 tokens'
describe 'credit_cards#index' do
+ let(:payment_method_id) { credit_cards.first.payment_method_id }
+
context 'with filter options' do
- before { get "/api/v2/storefront/account/credit_cards?filter[payment_method_id]=#{credit_cards.first.payment_method_id}&include=payment_method", headers: headers_bearer }
+ before { get "/api/v2/storefront/account/credit_cards?filter[payment_method_id]=#{payment_method_id}&include=payment_method", headers: headers_bearer }
it_behaves_like 'returns valid user credit cards resource JSON'
it 'returns all user credit_cards' do
- expect(json_response['data'].size).to eq(1)
+ expect(json_response['data'].count).to eq user.credit_cards.where(payment_method_id: payment_method_id).count
end
end
@@ -50,7 +52,7 @@ describe 'Storefront API v2 CreditCards spec', type: :request do
expect(response.status).to eq(200)
expect(json_response['data'].size).to eq(0)
end
-
+
context 'user has a credit cards available on the front end but in different store' do
let(:stores) { [create(:store)] }
let(:display_on) { :front_end }
@@ -60,6 +62,14 @@ describe 'Storefront API v2 CreditCards spec', type: :request do
expect(json_response['data'].size).to eq(0)
end
end
+
+ context 'deleted credit cards are omitted' do
+ let!(:deleted_credit_card) { create(:credit_card, user_id: user.id, payment_method: payment_method, deleted_at: Time.current) }
+
+ it 'returns all user credit_cards' do
+ expect(json_response['data'].map(&:id)).not_to include(deleted_credit_card.id)
+ end
+ end
end
end
@@ -85,9 +95,10 @@ describe 'Storefront API v2 CreditCards spec', type: :request do
end
describe 'credit_cards#show' do
- context 'by "default"' do
- let!(:default_credit_card) { create(:credit_card, user_id: user.id, default: true) }
+ let!(:default_credit_card) { create(:credit_card, user_id: user.id, default: true) }
+ let!(:credit_card) { create(:credit_card, user_id: user.id, default: false) }
+ context 'by "default"' do
before { get '/api/v2/storefront/account/credit_cards/default', headers: headers_bearer }
it 'returns user default credit_card' do
@@ -97,7 +108,19 @@ describe 'Storefront API v2 CreditCards spec', type: :request do
expect(json_response['data']).to have_attribute(:name).with_value(default_credit_card.name)
expect(json_response['data']).to have_attribute(:month).with_value(default_credit_card.month)
expect(json_response['data']).to have_attribute(:year).with_value(default_credit_card.year)
- expect(json_response.size).to eq(1)
+ end
+ end
+
+ context 'by ID' do
+ before { get "/api/v2/storefront/account/credit_cards/#{credit_card.id}", headers: headers_bearer }
+
+ it 'returns proper credit_card' do
+ expect(json_response['data']).to have_id(credit_card.id.to_s)
+ expect(json_response['data']).to have_attribute(:cc_type).with_value(credit_card.cc_type)
+ expect(json_response['data']).to have_attribute(:last_digits).with_value(credit_card.last_digits)
+ expect(json_response['data']).to have_attribute(:name).with_value(credit_card.name)
+ expect(json_response['data']).to have_attribute(:month).with_value(credit_card.month)
+ expect(json_response['data']).to have_attribute(:year).with_value(credit_card.year)
end
end
@@ -106,5 +129,43 @@ describe 'Storefront API v2 CreditCards spec', type: :request do
it_behaves_like 'returns 403 HTTP status'
end
+
+ context 'deleted credit card' do
+ let!(:credit_card) { create(:credit_card, user_id: user.id, default: false) }
+
+ before { credit_card.destroy }
+
+ it 'returns 404 HTTP status' do
+ get "/api/v2/storefront/account/credit_cards/#{credit_card.id}", headers: headers_bearer
+ expect(response.status).to eq(404)
+ end
+ end
+ end
+
+ describe 'credit_cards#destroy' do
+ context 'when credit card exist' do
+ let!(:credit_card) { create(:credit_card, user_id: user.id) }
+
+ it 'deletes a credit card' do
+ delete "/api/v2/storefront/account/credit_cards/#{credit_card.id}", headers: headers_bearer
+
+ expect(credit_card.reload.deleted_at).not_to be_nil
+ expect(response.status).to eq(204)
+ end
+
+ context 'with missing authorization token' do
+ before { delete "/api/v2/storefront/account/credit_cards/#{credit_card.id}" }
+
+ it_behaves_like 'returns 403 HTTP status'
+ end
+ end
+
+ context 'when credit card does not exist' do
+ it 'should not find a credit card' do
+ delete "/api/v2/storefront/account/credit_cards/dummy_id", headers: headers_bearer
+
+ expect(response.status).to eq(404)
+ end
+ end
end
end
diff --git a/core/app/models/concerns/spree/user_payment_source.rb b/core/app/models/concerns/spree/user_payment_source.rb
index 5c9bb1210f..20645f5e00 100644
--- a/core/app/models/concerns/spree/user_payment_source.rb
+++ b/core/app/models/concerns/spree/user_payment_source.rb
@@ -9,7 +9,7 @@ module Spree
end
def payment_sources
- credit_cards.with_payment_profile.not_expired.where(payment_method: Spree::PaymentMethod.active)
+ credit_cards.with_payment_profile.not_expired.where(payment_method: Spree::PaymentMethod.active).not_removed
end
def drop_payment_source(source)
diff --git a/core/app/models/spree/ability.rb b/core/app/models/spree/ability.rb
index 80bd4519db..02abdd74e3 100644
--- a/core/app/models/spree/ability.rb
+++ b/core/app/models/spree/ability.rb
@@ -73,7 +73,7 @@ module Spree
!order.completed? && (order.user == user || order.token && token == order.token)
end
can :manage, ::Spree::Address, user_id: user.id
- can :read, ::Spree::CreditCard, user_id: user.id
+ can [:read, :destroy], ::Spree::CreditCard, user_id: user.id
can :read, ::Spree::Product
can :read, ::Spree::ProductProperty
can :read, ::Spree::Property
diff --git a/core/app/models/spree/app_dependencies.rb b/core/app/models/spree/app_dependencies.rb
index 151c98ecd7..c40c199bdf 100644
--- a/core/app/models/spree/app_dependencies.rb
+++ b/core/app/models/spree/app_dependencies.rb
@@ -13,7 +13,7 @@ module Spree
:products_finder, :taxon_finder, :line_item_by_variant_finder, :cart_estimate_shipping_rates_service,
:account_create_address_service, :account_update_address_service, :account_create_service, :account_update_service,
:address_finder, :collection_sorter, :error_handler, :current_store_finder, :cart_empty_service, :cart_destroy_service,
- :classification_reposition_service
+ :classification_reposition_service, :credit_cards_destroy_service
].freeze
attr_accessor *INJECTION_POINTS
@@ -71,6 +71,9 @@ module Spree
@account_create_address_service = 'Spree::Account::Addresses::Create'
@account_update_address_service = 'Spree::Account::Addresses::Update'
+ # credit cards
+ @credit_cards_destroy_service = 'Spree::CreditCards::Destroy'
+
@classification_reposition_service = 'Spree::Classifications::Reposition'
@error_handler = 'Spree::ErrorReporter'
diff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rb
index 9416d31662..77ead76802 100644
--- a/core/app/models/spree/credit_card.rb
+++ b/core/app/models/spree/credit_card.rb
@@ -5,7 +5,6 @@ module Spree
# to migrate safely from older Spree versions that din't provide safe deletion for CCs
# we need to ensure that we have a connection to the DB and that the `deleted_at` column exists
if !ENV['SPREE_DISABLE_DB_CONNECTION'] &&
- connected? &&
table_exists? &&
connection.column_exists?(:spree_credit_cards, :deleted_at)
acts_as_paranoid
@@ -42,6 +41,7 @@ module Spree
or(where('CAST(spree_credit_cards.year AS DECIMAL) = ?', Time.current.year).
where('CAST(spree_credit_cards.month AS DECIMAL) >= ?', Time.current.month))
}
+ scope :not_removed, -> { where(deleted_at: nil) }
# needed for some of the ActiveMerchant gateways (eg. SagePay)
alias_attribute :brand, :cc_type
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index 48236e9215..19a21441c0 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -112,6 +112,13 @@ module Spree
end
end
+ def source
+ return super if payment_method.nil?
+ return super unless payment_method.source_required?
+
+ payment_method.payment_source_class.unscoped { super }
+ end
+
def money
Spree::Money.new(amount, currency: currency)
end
diff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rb
index e65cdaf718..974e036ddf 100644
--- a/core/app/models/spree/payment_method.rb
+++ b/core/app/models/spree/payment_method.rb
@@ -33,6 +33,8 @@ module Spree
# e.g. CreditCard in the case of a the Gateway payment type
# nil means the payment method doesn't require a source e.g. check
def payment_source_class
+ return unless source_required?
+
raise ::NotImplementedError, 'You must implement payment_source_class method for this gateway.'
end
diff --git a/core/app/services/spree/credit_cards/destroy.rb b/core/app/services/spree/credit_cards/destroy.rb
new file mode 100644
index 0000000000..d385c70b40
--- /dev/null
+++ b/core/app/services/spree/credit_cards/destroy.rb
@@ -0,0 +1,41 @@
+module Spree
+ module CreditCards
+ class Destroy
+ prepend Spree::ServiceModule::Base
+
+ def call(card:)
+ ApplicationRecord.transaction do
+ run :invalidate_payments
+ run :void_payments
+ run :destroy
+ end
+ end
+
+ protected
+
+ def invalidate_payments(card:)
+ payment_scope(card).checkout.each(&:invalidate!)
+
+ success(card: card)
+ end
+
+ def void_payments(card:)
+ payment_scope(card).where.not(state: :checkout).each(&:void!)
+
+ success(card: card)
+ end
+
+ def destroy(card:)
+ if card.destroy
+ success(card: card)
+ else
+ failure(card.errors.full_messages.to_sentance)
+ end
+ end
+
+ def payment_scope(card)
+ card.payments.valid.joins(:order).merge(Spree::Order.incomplete)
+ end
+ end
+ end
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index e3a763df47..327f21ce03 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -880,6 +880,7 @@ en:
created_by: Created By
credit: Credit
credit_card: Credit Card
+ credit_card_remove_confirmation: Are you sure you want to delete the credit card?
credit_cards: Credit Cards
credit_owed: Credit Owed
credited: Credited
diff --git a/core/spec/models/spree/payment_method/store_credit_spec.rb b/core/spec/models/spree/payment_method/store_credit_spec.rb
index fe21b20935..2a10e43f49 100644
--- a/core/spec/models/spree/payment_method/store_credit_spec.rb
+++ b/core/spec/models/spree/payment_method/store_credit_spec.rb
@@ -308,4 +308,16 @@ describe Spree::PaymentMethod::StoreCredit do
it { expect(store_credit_payment_method.available_for_order?(order_without_store_credit)).to be false }
end
end
+
+ describe '#source_required?' do
+ let(:store_credit_payment_method) { create(:store_credit_payment_method) }
+
+ it { expect(store_credit_payment_method.source_required?).to be true }
+ end
+
+ describe '#payment_source_class' do
+ let(:store_credit_payment_method) { create(:store_credit_payment_method) }
+
+ it { expect(store_credit_payment_method.payment_source_class).to eq(Spree::StoreCredit) }
+ end
end
diff --git a/core/spec/models/spree/payment_method_spec.rb b/core/spec/models/spree/payment_method_spec.rb
index ec38a1b060..8c1cbe46e9 100644
--- a/core/spec/models/spree/payment_method_spec.rb
+++ b/core/spec/models/spree/payment_method_spec.rb
@@ -162,4 +162,16 @@ describe Spree::PaymentMethod, type: :model do
end
end
end
+
+ describe '#source_required?' do
+ let(:payment_method) { create(:credit_card_payment_method) }
+
+ it { expect(payment_method.source_required?).to be true }
+ end
+
+ describe '#payment_source_class' do
+ let(:payment_method) { create(:credit_card_payment_method) }
+
+ it { expect(payment_method.payment_source_class).to eq(Spree::CreditCard) }
+ end
end
diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb
index 47afd88126..f2b8c56cc9 100644
--- a/core/spec/models/spree/payment_spec.rb
+++ b/core/spec/models/spree/payment_spec.rb
@@ -926,4 +926,35 @@ describe Spree::Payment, type: :model do
])
end
end
+
+ describe '#source' do
+ context 'with source required enabled' do
+ let(:payment) { create(:payment, payment_method: payment_method, source: card, state: 'completed') }
+ let(:card) { create(:credit_card) }
+ let(:payment_method) { card.payment_method }
+
+ before do
+ allow(payment_method).to receive_messages source_required: true
+ end
+
+ it { expect(payment_method.source_required?).to eq(true) }
+ it { expect(payment.source).to eq(card) }
+
+ context 'when credit card is removed' do
+ before do
+ card.destroy!
+ payment.reload
+ end
+
+ it { expect(payment.source).to eq(card) }
+ it { expect(payment.source.deleted_at).not_to be_nil }
+ end
+ end
+
+ context 'with source required disabled' do
+ let(:payment) { create(:check_payment) }
+
+ it { expect(payment.source).to be_nil }
+ end
+ end
end
diff --git a/core/spec/services/spree/credit_cards/destroy_spec.rb b/core/spec/services/spree/credit_cards/destroy_spec.rb
new file mode 100644
index 0000000000..3f0c491e69
--- /dev/null
+++ b/core/spec/services/spree/credit_cards/destroy_spec.rb
@@ -0,0 +1,63 @@
+require 'spec_helper'
+
+describe Spree::CreditCards::Destroy, type: :service do
+ subject { described_class.call(card: credit_card) }
+
+ let!(:order) { create(:order_with_line_items) }
+ let!(:credit_card) { create(:credit_card, user_id: order.user_id) }
+
+ describe '#call' do
+ let!(:payment) { create(:payment, source: credit_card, order: order) }
+ let!(:complete_payment) { create(:payment, source: credit_card, order: order, state: 'completed') }
+
+ it 'destroy credit_card and update payment state' do
+ res = subject
+
+ expect(res.success).to eq true
+ expect(payment.reload.state).to eq 'invalid'
+ expect(complete_payment.reload.state).to eq 'void'
+ expect(credit_card.deleted_at).not_to be_nil
+ end
+ end
+
+ describe '#invalidate_payments' do
+ let!(:completed_order) { create(:completed_order_with_pending_payment, user_id: order.user_id) }
+ let!(:payment) { create(:payment, source: credit_card, order: order) }
+ let!(:complete_order_payment) { create(:payment, source: credit_card, order: completed_order, state: 'completed') }
+
+ it 'destroy credit_card and invalidate valid checkout payments' do
+ res = subject
+
+ expect(res.success).to eq true
+ expect(payment.reload.state).to eq 'invalid'
+ expect(complete_order_payment.reload.state).to eq 'completed'
+ expect(credit_card.deleted_at).not_to be_nil
+ end
+ end
+
+ describe '#void_payments' do
+ let!(:completed_payment) { create(:payment, source: credit_card, order: order, state: 'completed') }
+ let!(:processing_payment) { create(:payment, source: credit_card, order: order, state: 'processing') }
+ let!(:pending_payment) { create(:payment, source: credit_card, order: order, state: 'pending') }
+
+ it 'destroy credit_card and void valid payments' do
+ res = subject
+
+ expect(res.success).to eq true
+ expect(credit_card.deleted_at).not_to be_nil
+ expect(completed_payment.reload.state).to eq 'void'
+ expect(processing_payment.reload.state).to eq 'void'
+ expect(pending_payment.reload.state).to eq 'void'
+ end
+ end
+
+ describe '#destroy' do
+ it 'remove credit_card' do
+ expect(credit_card.deleted_at).to be_nil
+
+ subject
+
+ expect(credit_card.reload.deleted_at).not_to be_nil
+ end
+ end
+end
diff --git a/frontend/app/assets/javascripts/spree/frontend.js b/frontend/app/assets/javascripts/spree/frontend.js
index df4cb8d775..de78e91688 100644
--- a/frontend/app/assets/javascripts/spree/frontend.js
+++ b/frontend/app/assets/javascripts/spree/frontend.js
@@ -48,6 +48,7 @@ Spree.routes.api_tokens = Spree.pathFor('api_tokens')
Spree.routes.ensure_cart = Spree.pathFor('ensure_cart')
Spree.routes.api_v2_storefront_cart_apply_coupon_code = Spree.localizedPathFor('api/v2/storefront/cart/apply_coupon_code')
Spree.routes.api_v2_storefront_cart_remove_coupon_code = function(couponCode) { return Spree.localizedPathFor('api/v2/storefront/cart/remove_coupon_code/' + couponCode) }
+Spree.routes.api_v2_storefront_destroy_credit_card = function(id) { return Spree.localizedPathFor('api/v2/storefront/account/credit_cards/' + id) }
Spree.routes.product = function(id) { return Spree.localizedPathFor('products/' + id) }
Spree.routes.product_related = function(id) { return Spree.localizedPathFor('products/' + id + '/related') }
Spree.routes.product_carousel = function (taxonId) { return Spree.localizedPathFor('product_carousel/' + taxonId) }
diff --git a/frontend/app/assets/javascripts/spree/frontend/checkout.js b/frontend/app/assets/javascripts/spree/frontend/checkout.js
index 5efbf7db48..68a6bca9f7 100644
--- a/frontend/app/assets/javascripts/spree/frontend/checkout.js
+++ b/frontend/app/assets/javascripts/spree/frontend/checkout.js
@@ -59,3 +59,19 @@ Spree.ready(function () {
return Spree.Checkout
})
+
+$('.js-remove-credit-card').click(function() {
+ if (confirm(Spree.translations.credit_card_remove_confirmation)) {
+ return $.ajax({
+ async: false,
+ method: 'DELETE',
+ url: Spree.routes.api_v2_storefront_destroy_credit_card(this.dataset.id),
+ dataType: 'json',
+ headers: {
+ 'Authorization': 'Bearer ' + SpreeAPI.oauthToken
+ }
+ }).done(function() {
+ location.reload();
+ })
+ }
+})
diff --git a/frontend/app/views/spree/checkout/_credit_card.html.erb b/frontend/app/views/spree/checkout/_credit_card.html.erb
index b697f74a0a..5a7d0f7589 100644
--- a/frontend/app/views/spree/checkout/_credit_card.html.erb
+++ b/frontend/app/views/spree/checkout/_credit_card.html.erb
@@ -5,5 +5,6 @@
<%= credit_card_icon(card.cc_type) %>
<span>****<%= card.last_digits %>,&nbsp</span>
<span><%= card.name %></span>
+ <%= button_tag Spree.t(:remove), type: 'button', class: 'btn text-left d-block p-0 js-remove-credit-card', data: { id: card.id } %>
</label>
</div>
diff --git a/frontend/app/views/spree/shared/_translations.html.erb b/frontend/app/views/spree/shared/_translations.html.erb
index ce0d04d800..f779e77e66 100644
--- a/frontend/app/views/spree/shared/_translations.html.erb
+++ b/frontend/app/views/spree/shared/_translations.html.erb
@@ -4,7 +4,8 @@
{
coupon_code_applied: Spree.t(:coupon_code_applied),
coupon_code_removed: Spree.t(:coupon_code_removed),
- coupon_code_error_icon: image_path('error.svg')
+ coupon_code_error_icon: image_path('error.svg'),
+ credit_card_remove_confirmation: Spree.t(:credit_card_remove_confirmation)
}.to_json
%>
});
diff --git a/frontend/spec/features/checkout_spec.rb b/frontend/spec/features/checkout_spec.rb
index 1203f9383c..d8ff9cdc10 100644
--- a/frontend/spec/features/checkout_spec.rb
+++ b/frontend/spec/features/checkout_spec.rb
@@ -177,6 +177,50 @@ describe 'Checkout', type: :feature, inaccessible: true, js: true do
end
end
+ context 'when credit card already added and available' do
+ let(:bogus) { create(:credit_card_payment_method) }
+ let(:user) { create(:user) }
+ let!(:credit_card) { create(:credit_card, user_id: user.id, payment_method: bogus, gateway_customer_profile_id: 'BGS-WEFWF', name: 'New Credit Card') }
+
+ before do
+ order = OrderWalkthrough.up_to(:payment)
+ allow(order).to receive_messages confirmation_required?: true
+ allow(order).to receive_messages(available_payment_methods: [bogus])
+
+ order.user = user
+ order.update_with_updater!
+
+ allow_any_instance_of(Spree::CheckoutController).to receive_messages(current_order: order)
+ allow_any_instance_of(Spree::CheckoutController).to receive_messages(try_spree_current_user: user)
+ allow_any_instance_of(Spree::OrdersController).to receive_messages(try_spree_current_user: user)
+
+ allow_any_instance_of(Spree::Api::V2::Storefront::Account::CreditCardsController).to receive(:spree_current_user).and_return(user)
+
+ expect_any_instance_of(Spree::CreditCards::Destroy) do |instance|
+ expect(instance).to receive(:call).with(card: credit_card).and_call_original
+ end
+ end
+
+ it 'should be able to remove credit card' do
+ visit spree.checkout_state_path(:payment)
+
+ within("#spree_credit_card_#{credit_card.id}") do
+ expect(page).to have_content(credit_card.name)
+ expect(page).to have_button('Remove')
+
+ accept_confirm do
+ click_button 'Remove'
+ end
+
+ wait_for_ajax
+
+ expect(credit_card.reload.deleted_at).not_to be_nil
+ end
+
+ expect(page).not_to have_content(credit_card.name)
+ end
+ end
+
# regression test for #3945
context 'when Spree::Config[:always_include_confirm_step] is true' do
before do