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
2 changes: 1 addition & 1 deletion app/controllers/checkouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create
payment_method_id: params[:payment_method_id],
offer: @offer,
coupon: @coupon,
amount: params[:amount],
amount: params[:amount] || 0,
invoice: @invoice)

message = checkout.pay
Expand Down
2 changes: 1 addition & 1 deletion app/models/coupon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Coupon < ApplicationRecord

def discounted_price
return amount_off if amount_off
offer.amount * percent_off
offer.amount * (percent_off / 100.to_d)
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Invoice < ApplicationRecord
validates :due_on, presence: true

def status
return status_for_not_offering if offer.amount
return status_for_not_offering if offer.amount?
status_for_offering
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/offer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ class Offer < ApplicationRecord
validates :manager, presence: true
validates :name, presence: true
validates :description, presence: true

def amount
amount? ? attributes['amount'] : 0
end
end
17 changes: 9 additions & 8 deletions app/views/checkouts/_coupon_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
= form_tag(checkouts_path, class: "pure-form") do
%fieldset
.pure-u-1-4
%h5 Coupon
= text_field_tag :coupon,nil, {class: "pure-input-1-4"}
= hidden_field_tag :offer_id, @offer.id
= hidden_field_tag :invoice_id, @invoice.id
%button.pure-button{type: "submit"} Apply Coupon
- if @offer.amount?
= form_tag(checkouts_path, class: "pure-form") do
%fieldset
.pure-u-1-4
%h5 Coupon
= text_field_tag :coupon,nil, {class: "pure-input-1-4"}
= hidden_field_tag :offer_id, @offer.id
= hidden_field_tag :invoice_id, @invoice.id
%button.pure-button{type: "submit"} Apply Coupon
2 changes: 1 addition & 1 deletion app/views/checkouts/_make_payment_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%fieldset
%div.pure-u-1-4
%h5 Payment
- if @offer.deferrable? && @offer.amount
- if @offer.deferrable? && @offer.amount?
= number_field_tag :amount, format_amount(@balance_remaining), {min: 0.00, max: @balance_remaining, step: :any, class: "pure-input-1-4"}
- else
= hidden_field_tag :amount, @balance_remaining
Expand Down
13 changes: 13 additions & 0 deletions features/applying_a_coupon.feature
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ Feature: Applying a Coupon
And I click Apply Coupon
Then I should be redirected to the Invoice page
And I should see a message saying I can't apply a coupon after made a payment

Scenario: User can't use coupons on offers without amounts
Given I am logged in
And I am on an offer page without amount
Then I click on the Purchase link
And I should not see the apply coupon button

Scenario: User can use coupons on deferrable offers
Given I am logged in
And I am on an offer page
Then I click on the Purchase link
And I have a valid coupon code
And I should see the apply coupon button
7 changes: 7 additions & 0 deletions features/steps/common_steps/pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ module Pages
expect(page).to have_content(offer_name)
end

step 'I am on an offer page without amount' do
offer_name = 'Offer Name'
offer = FactoryGirl.create(:offer, name: offer_name, id: 1, due_on: Time.zone.now + 3.days, deferrable: true, amount: nil)
visit "/offers/1?public_token=#{offer.manager.public_token}"
expect(page).to have_content(offer_name)
end

step 'I am on an offer page for an offer that doesn\'t have an amount' do
offer_name = 'Offer Name'
offer = FactoryGirl.create(:offer, name: offer_name, id: 1, amount: nil, due_on: Time.zone.now + 3.days, deferrable: true)
Expand Down
2 changes: 1 addition & 1 deletion spec/models/coupon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
context 'when offer has percent off' do
let(:coupon) { FactoryGirl.create(:coupon, amount_off: nil, percent_off: 0.2) }
it 'returns the offer amount * percent off as a discount price' do
expect(coupon.discounted_price).to eq 20
expect(coupon.discounted_price).to eq 0.2
end
end
end
Expand Down