Skip to content

Commit

Permalink
Only credit back allowed amount
Browse files Browse the repository at this point in the history
When a payment is cancelled, previously it would attempt to return the
whole amount to the store credit even if it had been entirely or
partially refunded.
  • Loading branch information
Gregor MacDougall committed Jul 3, 2017
1 parent 7c4cc74 commit 04eccbe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
24 changes: 16 additions & 8 deletions core/app/models/spree/payment_method/store_credit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ def credit(amount_in_cents, auth_code, gateway_options = {})
currency = gateway_options[:currency] || store_credit.currency
originator = gateway_options[:originator]

store_credit.credit(
amount_in_cents / 100.0.to_d,
auth_code,
currency,
action_originator: originator
) if amount_in_cents > 0
if amount_in_cents > 0
store_credit.credit(
::Money.new(amount_in_cents, currency).to_d,
auth_code,
currency,
action_originator: originator
)
else
ActiveMerchant::Billing::Response.new(true, '')
end
end

handle_action(action, :credit, auth_code)
Expand All @@ -76,8 +80,12 @@ def cancel(auth_code)
if store_credit_event.nil? || store_credit.nil?
ActiveMerchant::Billing::Response.new(false, '', {}, {})
elsif store_credit_event.capture_action?
amount_in_cents = (store_credit_event.amount * 100).round
credit(amount_in_cents, auth_code)
amount = if store_credit_event.originator_type == 'Spree::Payment'
store_credit_event.originator.credit_allowed_money.cents
else
(store_credit_event.amount * 100).round
end
credit(amount, auth_code, { originator: store_credit_event.originator })
elsif store_credit_event.authorization_action?
void(auth_code)
else
Expand Down
48 changes: 38 additions & 10 deletions core/spec/models/spree/payment_method/store_credit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@
end
end

context "when attempting to credit for zero" do
let(:credit_amount) { 0 }

it "does not credit the store credit" do
expect_any_instance_of(Spree::StoreCredit).to_not receive(:credit)
subject
end

it "returns a success response" do
expect(subject.success?).to be true
end
end

context "when the store credit isn't credited successfully" do
before { allow_any_instance_of(Spree::StoreCredit).to receive_messages(credit: false) }

Expand Down Expand Up @@ -263,19 +276,34 @@
end

context "capture event found" do
let!(:store_credit_event) {
create(:store_credit_capture_event,
authorization_code: auth_code,
amount: captured_amount,
store_credit: store_credit)
}
let!(:store_credit_event) do
create(
:store_credit_capture_event,
authorization_code: auth_code,
amount: captured_amount,
store_credit: store_credit,
originator: originator
)
end
let(:payment) { create(:payment, order: order, amount: 5) }
let(:originator) { nil }

it_behaves_like "a spree payment method"

it "refunds the capture amount" do
expect { subject }.to change{ store_credit.reload.amount_remaining }.
from(original_amount - captured_amount).
to(original_amount)
context "when originator is nil" do
it "refunds the event amount" do
expect { subject }.to change{ store_credit.reload.amount_remaining }.
from(90).to(100)
end
end

context "when the originator is the payment" do
let(:originator) { payment }

it "refunds the payment credit allowed amount" do
expect { subject }.to change{ store_credit.reload.amount_remaining }.
from(90).to(95)
end
end
end

Expand Down

0 comments on commit 04eccbe

Please sign in to comment.