diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index 1a9cf150..121e0c4b 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -14,11 +14,8 @@ class PaymentsController < ApplicationController end def create - @total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false) - @payment = Payment.new payment_params.merge(stripe_customer_email: params[:stripeEmail], stripe_customer_token: params[:stripeToken], - amount: @total_amount_to_pay.cents, user: current_user, conference: @conference) @@ -27,6 +24,7 @@ class PaymentsController < ApplicationController redirect_to conference_conference_registration_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' } else + @total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false) @unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference) render :new end diff --git a/app/models/payment.rb b/app/models/payment.rb index ef2792c3..4ad4df85 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -7,7 +7,6 @@ class Payment < ActiveRecord::Base attr_accessor :stripe_customer_token validates :status, presence: true - validates :amount, presence: true, numericality: { greater_than: 0 } validates :user_id, presence: true validates :conference_id, presence: true @@ -22,13 +21,9 @@ class Payment < ActiveRecord::Base end def purchase - customer = Stripe::Customer.create email: stripe_customer_email, - source: stripe_customer_token, - description: user.name - - gateway_response = Stripe::Charge.create customer: customer.id, + gateway_response = Stripe::Charge.create source: stripe_customer_token, receipt_email: stripe_customer_email, - description: 'ticket purchases', + description: "ticket purchases(#{user.username})", amount: amount_to_pay, currency: conference.tickets.first.price_currency diff --git a/db/migrate/20160606040848_create_payments.rb b/db/migrate/20160606040848_create_payments.rb index 6c56bb5d..6792ad02 100644 --- a/db/migrate/20160606040848_create_payments.rb +++ b/db/migrate/20160606040848_create_payments.rb @@ -2,7 +2,7 @@ class CreatePayments < ActiveRecord::Migration def change create_table :payments do |t| t.string :last4 - t.integer :amount, null: false + t.integer :amount t.string :authorization_code t.integer :status, default: 0, null: false t.integer :user_id, null: false diff --git a/db/schema.rb b/db/schema.rb index 6c3ceaef..6213090c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -265,7 +265,7 @@ ActiveRecord::Schema.define(version: 20160704092023) do create_table "payments", force: :cascade do |t| t.string "last4" - t.integer "amount", null: false + t.integer "amount" t.string "authorization_code" t.integer "status", default: 0, null: false t.integer "user_id", null: false diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index e16a8f67..4512fe80 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -14,25 +14,11 @@ describe Payment do expect(build(:payment)).to be_valid end - it { is_expected.to validate_presence_of(:amount) } - it { is_expected.to validate_presence_of(:status) } it { is_expected.to validate_presence_of(:user_id) } it { is_expected.to validate_presence_of(:conference_id) } - - it 'is not valid with a amount equals zero' do - should_not allow_value(0).for(:amount) - end - - it 'is not valid with a amount smaller than zero' do - should_not allow_value(-1).for(:amount) - end - - it 'is valid with a amount greater than zero' do - should allow_value(1).for(:amount) - end end describe '#amount_to_pay' do