diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index 161d2101..aa0c038a 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -17,32 +17,23 @@ class PaymentsController < ApplicationController @unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference) @total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false) - customer = Stripe::Customer.create( - :email => params[:stripeEmail], - :source => params[:stripeToken] - ) + @payment = Payment.new payment_params.merge(user: current_user, conference: @conference) + @payment.purchase + @payment.save - gateway_response = Stripe::Charge.create( - :customer => customer.id, - :amount => @total_amount_to_pay.cents, - :description => 'Rails Stripe customer', - :currency => @conference.tickets.first.price_currency - ) + update_purchased_ticket_purchases - payment = Payment.purchase(gateway_response, current_user, @conference) - update_purchased_ticket_purchases(payment) - - redirect_to conference_conference_registration_path(@conference.short_title), - flash: { success: 'Thanks! You have purchased your tickets successfully.' } - - rescue Stripe::CardError => e - flash[:error] = e.message - render 'new' + redirect_to conference_conference_registration_path(@conference.short_title), flash: + { success: 'Thanks! You have purchased your tickets successfully.' } end -private + private - def update_purchased_ticket_purchases(payment) - current_user.ticket_purchases.by_conference(@conference).unpaid.update_all(paid: true, payment_id: payment.id) + def payment_params + params.permit :stripeEmail, :stripeToken + end + + def update_purchased_ticket_purchases + current_user.ticket_purchases.by_conference(@conference).unpaid.update_all(paid: true, payment_id: @payment.id) end end diff --git a/app/models/payment.rb b/app/models/payment.rb index ecc4db5d..95ed9a70 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -3,6 +3,9 @@ class Payment < ActiveRecord::Base belongs_to :user belongs_to :conference + attr_accessor :stripeEmail + attr_accessor :stripeToken + validates :last4, presence: true validates :authorization_code, presence: true validates :status, presence: true @@ -16,12 +19,29 @@ class Payment < ActiveRecord::Base failure: 2 } - def self.purchase(gateway_response, user, conference) - create(last4: gateway_response[:source][:last4], - amount: gateway_response[:amount], - status: (gateway_response[:paid] ? 1 : 0), - authorization_code: gateway_response[:id], - user_id: user.id, - conference_id: conference.id) + def amount_to_pay + Ticket.total_price(conference, user, paid: false).cents + end + + def purchase + customer = Stripe::Customer.create email: stripeEmail, + source: stripeToken, + description: user.name + + gateway_response = Stripe::Charge.create customer: customer.id, + receipt_email: stripeEmail, + description: 'ticket purchases', + amount: amount_to_pay, + currency: conference.tickets.first.price_currency + + self.amount = gateway_response[:amount] + self.last4 = gateway_response[:source][:last4] + self.authorization_code = gateway_response[:id] + self.status = 'success' + true + + rescue Stripe::CardError => e + flash[:error] = e.message + false end end diff --git a/db/migrate/20160606040848_create_payments.rb b/db/migrate/20160606040848_create_payments.rb index b7428ca3..6792ad02 100644 --- a/db/migrate/20160606040848_create_payments.rb +++ b/db/migrate/20160606040848_create_payments.rb @@ -1,9 +1,9 @@ class CreatePayments < ActiveRecord::Migration def change create_table :payments do |t| - t.string :last4, null: false - t.integer :amount, null: false - t.string :authorization_code, null: false + t.string :last4 + t.integer :amount + t.string :authorization_code t.integer :status, default: 0, null: false t.integer :user_id, null: false t.integer :conference_id, null: false diff --git a/db/schema.rb b/db/schema.rb index 836168d8..6213090c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -264,9 +264,9 @@ ActiveRecord::Schema.define(version: 20160704092023) do end create_table "payments", force: :cascade do |t| - t.string "last4", null: false - t.integer "amount", null: false - t.string "authorization_code", null: false + t.string "last4" + t.integer "amount" + t.string "authorization_code" t.integer "status", default: 0, null: false t.integer "user_id", null: false t.integer "conference_id", null: false