Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/voucher_adjustments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def create

def destroy
@order.voucher_adjustments.find_by(id: params[:id])&.destroy
@order.update_order! # adjustments, totals and states

update_payment_section
end
Expand All @@ -38,7 +39,7 @@ def add_voucher

adjustment = voucher.create_adjustment(voucher.code, @order)

unless adjustment.valid?
unless adjustment.persisted?
@order.errors.add(:voucher_code, I18n.t('split_checkout.errors.add_voucher_error'))
adjustment.errors.each { |error| @order.errors.import(error) }
return false
Expand Down
13 changes: 9 additions & 4 deletions app/models/voucher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ def display_value
# but vouchers have complicated calculation so we can't easily use Spree::Calculator. We keep
# the same method to stay as consistent as possible.
#
# Creates a new voucher adjustment for the given order with an amount of 0
# The amount will be calculated via VoucherAdjustmentsService#update
def create_adjustment(label, order)
adjustment_attributes = {
amount: 0,
amount: 0, # Amount is updated by VoucherAdjustmentsService below
originator: self,
order: order,
label: label,
Expand All @@ -34,7 +32,14 @@ def create_adjustment(label, order)
tax_category: nil
}

order.adjustments.create(adjustment_attributes)
adjustment = order.adjustments.new(adjustment_attributes)

if adjustment.save
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
end

adjustment
end

# We limit adjustment to the maximum amount needed to cover the order, ie if the voucher
Expand Down
4 changes: 4 additions & 0 deletions spec/models/voucher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@
it 'sets the adjustment as open' do
expect(adjustment.state).to eq("open")
end

it 'updates order totals' do
expect{ subject }.to change(order, :total).by(-25)
end
end
end
47 changes: 40 additions & 7 deletions spec/system/consumer/split_checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,17 @@
create(:voucher, code: 'some_code', enterprise: distributor, amount: 15)
end

before do
visit checkout_step_path(:payment)
end

it "shows voucher input" do
visit checkout_step_path(:payment)
expect(page).to have_field "Enter voucher code"
expect(page).to have_content "Apply voucher"
end

describe "adding voucher to the order" do
before do
visit checkout_step_path(:payment)
end

shared_examples "adding voucher to the order" do
before do
fill_in "Enter voucher code", with: "some_code"
Expand Down Expand Up @@ -764,6 +766,16 @@
"you may not be able to spend the remaining value."
)
end

it "proceeds without requiring payment" do
fill_in "Enter voucher code", with: "some_code"
click_button("Apply")

expect(page).to have_content "No payment required"
click_button "Next - Order summary"
# Expect to be on the Order Summary page
expect(page).to have_content "Delivery details"
end
end

context "voucher doesn't exist" do
Expand All @@ -779,21 +791,42 @@
describe "removing voucher from order" do
before do
voucher.create_adjustment(voucher.code, order)
# Reload the page so we pickup the voucher
visit checkout_step_path(:payment)
end

it "removes voucher" do
accept_confirm "Are you sure you want to remove the voucher?" do
click_on "Remove code"
end
end

it "removes voucher" do
within '#voucher-section' do
expect(page).to have_button("Apply", disabled: true)
expect(page).to have_field "Enter voucher code" # Currently no confirmation msg
end

expect(order.voucher_adjustments.length).to eq(0)
end

it "can re-enter a voucher" do
# Re-enter a voucher code
fill_in "Enter voucher code", with: "some_code"
click_button("Apply")

expect(page).to have_content("$15.00 Voucher")
expect(order.reload.voucher_adjustments.length).to eq(1)
expect(page).to have_content "No payment required"

click_button "Next - Order summary"
# Expect to be on the Order Summary page
expect(page).to have_content "Delivery details"
end

it "can proceed with payment" do
choose "Payment with Fee"
click_button "Next - Order summary"
# Expect to be on the Order Summary page
expect(page).to have_content "Delivery details"
end
end
end
end
Expand Down