diff --git a/app/assets/stylesheets/osem-payments.css.scss b/app/assets/stylesheets/osem-payments.css.scss index b74a6324..d5337542 100644 --- a/app/assets/stylesheets/osem-payments.css.scss +++ b/app/assets/stylesheets/osem-payments.css.scss @@ -1,7 +1,6 @@ .price-tags { list-style-type: none; padding-top: 2rem; - // display: inline-block; } .price-tags li { line-height: 40px; diff --git a/app/models/ability.rb b/app/models/ability.rb index 8bdcd6d7..9212ba35 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -81,7 +81,7 @@ class Ability can :index, Ticket can :manage, TicketPurchase, user_id: user.id - can :manage, Payment, user_id: user.id + can [:new], Payment, user_id: user.id can [:create, :destroy], Subscription, user_id: user.id diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 2a2cae65..d2454b74 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -26,7 +26,7 @@ class Ticket < ActiveRecord::Base end def unpaid?(user) - ticket_purchases.find_by(user: user, paid: false).present? + ticket_purchases.unpaid.by_user(user).present? end def total_price(user, paid: false) diff --git a/app/views/conference_registrations/show.html.haml b/app/views/conference_registrations/show.html.haml index 0f08b6c9..24923d07 100644 --- a/app/views/conference_registrations/show.html.haml +++ b/app/views/conference_registrations/show.html.haml @@ -93,7 +93,7 @@ -if @tickets.any? = "(#{@tickets.first.price.symbol}#{humanized_money @total_price})" %ul - .col.md-4 + .col-md-12 - @ticket_payments.each_pair do |ticket_id, tickets| %li = @total_quantity[ticket_id] @@ -102,6 +102,7 @@ for = tickets.first.price.symbol = humanized_money tickets.first.price + %br - if @tickets.any? = link_to 'Buy more tickets', conference_tickets_path(@conference.short_title), class: "btn btn-default" - else diff --git a/app/views/payments/index.html.haml b/app/views/payments/index.html.haml index dd0fb634..b231855f 100644 --- a/app/views/payments/index.html.haml +++ b/app/views/payments/index.html.haml @@ -29,4 +29,4 @@ %tr %td{:colspan => "5"} No payments have been attempted. .pull-right - = link_to "Conference Registration", conference_conference_registrations_path(@conference.short_title), class: 'btn btn-primary' + = link_to "Conference Registration", conference_conference_registration_path(@conference.short_title), class: 'btn btn-primary' diff --git a/config/environments/development.rb b/config/environments/development.rb index d85a731f..2a4ad4c4 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -96,10 +96,10 @@ Osem::Application.configure do end end + #Initialize Payment Gateway with valid credentials + ActiveMerchant::Billing::Base.mode = :test + ::GATEWAY = ActiveMerchant::Billing::StripeGateway.new(:login => ENV['SECRET_KEY']) end -ActiveMerchant::Billing::Base.mode = :test - ::GATEWAY = ActiveMerchant::Billing::StripeGateway.new( - :login => ENV['SECRET_KEY']) diff --git a/config/environments/production.rb b/config/environments/production.rb index d2b43a46..b5a9d46f 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -83,4 +83,8 @@ Osem::Application.configure do # Set the secret_key_base from the env, if not set by any other means config.secret_key_base ||= ENV["SECRET_KEY_BASE"] + + # Initialize Payment Gateway with valid credentials + ActiveMerchant::Billing::Base.mode = :test + ::GATEWAY = ActiveMerchant::Billing::StripeGateway.new(:login => ENV['SECRET_KEY']) end diff --git a/config/environments/test.rb b/config/environments/test.rb index 68c74dab..a6935e24 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -52,4 +52,9 @@ Osem::Application.configure do ActiveSupport::Deprecation.silenced = true end + # Initialize Payment Gateway with valid credentials + ActiveMerchant::Billing::Base.mode = :test + ::GATEWAY = ActiveMerchant::Billing::BogusGateway.new end + + diff --git a/spec/features/payments_spec.rb b/spec/features/payments_spec.rb index ae6b662a..d69f9545 100644 --- a/spec/features/payments_spec.rb +++ b/spec/features/payments_spec.rb @@ -16,7 +16,7 @@ feature Registration do context 'who is not registered' do - scenario 'purchases and pays for a ticket', feature: true, js: true do + scenario 'purchases and pays for a ticket, with gateway producing error', feature: true, js: true do visit root_path click_link 'Register' @@ -37,56 +37,57 @@ feature Registration do fill_in 'last_name', with: 'bar' select Date.current.year + 2, from: 'expiration_year' fill_in 'card_verification_value', with: '123' - fill_in 'credit_card_number', with: '4000000000000000' + fill_in 'credit_card_number', with: '3' click_button 'Charge Card' - expect(flash).to eq('Your card number is incorrect.') + expect(Payment.count).to eq(0) + end + + scenario 'purchases and pays for a ticket, with card producing a transaction failure', feature: true, js: true do + visit root_path + click_link 'Register' + + expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title)) + click_button 'Register' + + fill_in "tickets__#{ticket.id}", with: '2' + expect(current_path).to eq(conference_tickets_path(conference.short_title)) + + click_button 'Continue' + expect(current_path).to eq(new_conference_payment_path(conference.short_title)) + expect(flash).to eq('Please pay here to purchase tickets.') + purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first + expect(purchase.quantity).to eq(2) fill_in 'first_name', with: 'foo' fill_in 'last_name', with: 'bar' select Date.current.year + 2, from: 'expiration_year' fill_in 'card_verification_value', with: '123' - fill_in 'credit_card_number', with: '4000000000000002' + fill_in 'credit_card_number', with: '2' click_button 'Charge Card' - expect(flash).to eq('Your card was declined.') - expect(current_path).to eq(new_conference_payment_path(conference.short_title)) - - fill_in 'first_name', with: 'foo' - fill_in 'last_name', with: 'bar' - select Date.current.year + 2, from: 'expiration_year' - fill_in 'card_verification_value', with: '123' - fill_in 'credit_card_number', with: '4000000000000127' - - click_button 'Charge Card' - - expect(flash).to eq("Your card's security code is incorrect.") - expect(current_path).to eq(new_conference_payment_path(conference.short_title)) - - fill_in 'first_name', with: 'foo' - fill_in 'last_name', with: 'bar' - select Date.current.year + 2, from: 'expiration_year' - fill_in 'card_verification_value', with: '123' - fill_in 'credit_card_number', with: '4000000000000069' - - click_button 'Charge Card' - - expect(flash).to eq('Your card has expired.') - expect(current_path).to eq(new_conference_payment_path(conference.short_title)) - - fill_in 'first_name', with: 'foo' - fill_in 'last_name', with: 'bar' - select Date.current.year + 2, from: 'expiration_year' - fill_in 'card_verification_value', with: '123' - fill_in 'credit_card_number', with: '4000000000000119' - - click_button 'Charge Card' - - expect(flash).to eq('An error occurred while processing your card. Try again in a little bit.') + expect(Payment.count).to eq(0) + end + + scenario 'purchases and pays for a ticket successfully', feature: true, js: true do + visit root_path + click_link 'Register' + + expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title)) + click_button 'Register' + + fill_in "tickets__#{ticket.id}", with: '2' + expect(current_path).to eq(conference_tickets_path(conference.short_title)) + + click_button 'Continue' + expect(current_path).to eq(new_conference_payment_path(conference.short_title)) + expect(flash).to eq('Please pay here to purchase tickets.') + purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first + expect(purchase.quantity).to eq(2) fill_in 'first_name', with: 'foo' fill_in 'last_name', with: 'bar' @@ -96,14 +97,15 @@ feature Registration do click_button 'Charge Card' + expect(current_path).to eq(conference_conference_registration_path(conference.short_title)) + expect(Payment.count).to eq(1) payment = Payment.where(user_id: participant, conference_id: conference.id).first expect(payment.amount).to eq(20) - expect(payment.status).to eq(1) + expect(payment.status).to eq('success') expect(payment.first_name).to eq('foo') - expect(payment.first_name).to eq('bar') + expect(payment.last_name).to eq('bar') expect(payment.last4).not_to be_empty expect(payment.authorization_code).not_to be_empty - expect(current_path).to eq(conference_conference_registrations_path(conference.short_title)) expect(flash).to eq('Thanks! You have purchased your tickets successfully.') end end diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index 70ea5e9e..d077db5a 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -39,24 +39,8 @@ describe Payment do let!(:participant) { create(:user) } let!(:ticket_1) { create(:ticket) } let!(:conference) { create(:conference, tickets: [ticket_1]) } - - it 'creates no ticket purchase or payment if amount is less than 1' do - tickets = { ticket_1.id.to_s => '-1' } - TicketPurchase.purchase(conference, participant, tickets) - - expect(TicketPurchase.count).to eq(0) - expect(Payment.count).to eq(0) - end - - it 'creates no ticket purchase or payment if amount is 0' do - tickets = { ticket_1.id.to_s => '0' } - TicketPurchase.purchase(conference, participant, tickets) - - expect(TicketPurchase.count).to eq(0) - expect(Payment.count).to eq(0) - end - let(:payment) { create(:payment) } + it 'creates a purchase and payment for one ticket' do tickets = { ticket_1.id.to_s => '1' } message = TicketPurchase.purchase(conference, participant, tickets) @@ -68,8 +52,11 @@ describe Payment do expect(purchase.quantity).to eq(1) expect(message.blank?).to be true - payment = Payment.new payment.purchase(participant, conference, 1000) + + purchase = Payment.first + expect(Payment.count).to be(1) + expect(purchase.amount).to eq(10) end end end