Skip to content

Commit

Permalink
Merge pull request #68 from bonobos/add-store-credits-cancel-updates
Browse files Browse the repository at this point in the history
Update StoreCredit#cancel to handle auths
  • Loading branch information
athal7 committed May 29, 2015
2 parents 3701c01 + 7db6224 commit f904ac8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 14 deletions.
18 changes: 15 additions & 3 deletions core/app/models/spree/payment_method/store_credit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,18 @@ def credit(amount_in_cents, auth_code, gateway_options)
end

def cancel(auth_code)
store_credit_event = StoreCreditEvent.find_by(authorization_code: auth_code, action: Spree::StoreCredit::CAPTURE_ACTION)
store_credit_event = auth_or_capture_event(auth_code)
store_credit = store_credit_event.try(:store_credit)

return false if !store_credit_event || !store_credit
store_credit.credit(store_credit_event.amount, auth_code, store_credit.currency)
if store_credit_event.nil? || store_credit.nil?
return false
elsif store_credit_event.capture_action?
store_credit.credit(store_credit_event.amount, auth_code, store_credit.currency)
elsif store_credit_event.authorization_action?
store_credit.void(auth_code)
else
return false
end
end

def source_required?
Expand Down Expand Up @@ -111,5 +118,10 @@ def handle_action(action, action_name, auth_code)
end
end

def auth_or_capture_event(auth_code)
capture_event = StoreCreditEvent.find_by(authorization_code: auth_code, action: Spree::StoreCredit::CAPTURE_ACTION)
auth_event = StoreCreditEvent.find_by(authorization_code: auth_code, action: Spree::StoreCredit::AUTHORIZE_ACTION)
return capture_event || auth_event
end
end
end
8 changes: 8 additions & 0 deletions core/app/models/spree/store_credit_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class StoreCreditEvent < ActiveRecord::Base

delegate :currency, to: :store_credit

def capture_action?
action == Spree::StoreCredit::CAPTURE_ACTION
end

def authorization_action?
action == Spree::StoreCredit::AUTHORIZE_ACTION
end

def display_amount
Spree::Money.new(amount, { currency: currency })
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
factory :store_credit_auth_event, class: Spree::StoreCreditEvent do
action { Spree::StoreCredit::AUTHORIZE_ACTION }
end

factory :store_credit_capture_event do
action { Spree::StoreCredit::CAPTURE_ACTION }
end
end
end
35 changes: 24 additions & 11 deletions core/spec/models/spree/payment_method/store_credit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,26 +255,39 @@
let(:auth_code) { "1-SC-20141111111111" }
let(:captured_amount) { 10.0 }

let!(:capture_event) { create(:store_credit_auth_event,
action: Spree::StoreCredit::CAPTURE_ACTION,
authorization_code: auth_code,
amount: captured_amount,
store_credit: store_credit) }
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) }

context "store credit event found" do
it "creates a store credit for the same amount that was captured" do
expect_any_instance_of(Spree::StoreCredit).to receive(:credit).with(captured_amount, auth_code, store_credit.currency)
subject
end
end

context "store credit event not found" do
subject do
Spree::PaymentMethod::StoreCredit.new.cancel('INVALID')
context "capture event not found" do
context "auth event found" do
let!(:store_credit_event) { create(:store_credit_auth_event,
authorization_code: auth_code,
amount: captured_amount,
store_credit: store_credit) }

it "creates a store credit for the same amount that was captured" do
expect_any_instance_of(Spree::StoreCredit).to receive(:void).with(auth_code)
subject
end
end

it "returns false" do
expect(subject).to be false
context "store credit event not found" do
subject do
Spree::PaymentMethod::StoreCredit.new.cancel('INVALID')
end

it "returns false" do
expect(subject).to be false
end
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions core/spec/models/spree/store_credit_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,46 @@
end
end

describe "#capture_action?" do
subject { event.capture_action? }

context "for capture events" do
let(:event) { create(:store_credit_capture_event) }

it "returns true" do
expect(subject).to eq true
end
end

context "for non-capture events" do
let(:event) { create(:store_credit_auth_event) }

it "returns false" do
expect(subject).to eq false
end
end
end

describe "#authorization_action?" do
subject { event.authorization_action? }

context "for auth events" do
let(:event) { create(:store_credit_auth_event) }

it "returns true" do
expect(subject).to eq true
end
end

context "for non-auth events" do
let(:event) { create(:store_credit_capture_event) }

it "returns false" do
expect(subject).to eq false
end
end
end

describe "#display_amount" do
let(:event_amount) { 120.0 }

Expand Down

0 comments on commit f904ac8

Please sign in to comment.