diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index 86f646f6..1a9cf150 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -14,10 +14,11 @@ class PaymentsController < ApplicationController end def create - @unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference) @total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false) - @payment = Payment.new payment_params.merge(amount: @total_amount_to_pay.cents, + @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) @@ -26,6 +27,7 @@ class PaymentsController < ApplicationController redirect_to conference_conference_registration_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' } else + @unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference) render :new end end @@ -33,7 +35,7 @@ class PaymentsController < ApplicationController private def payment_params - params.permit :stripeEmail, :stripeToken + params.permit :stripe_customer_email, :stripe_customer_token end def update_purchased_ticket_purchases diff --git a/app/models/payment.rb b/app/models/payment.rb index 52df4071..ef2792c3 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -3,8 +3,8 @@ class Payment < ActiveRecord::Base belongs_to :user belongs_to :conference - attr_accessor :stripeEmail - attr_accessor :stripeToken + attr_accessor :stripe_customer_email + attr_accessor :stripe_customer_token validates :status, presence: true validates :amount, presence: true, numericality: { greater_than: 0 } @@ -22,26 +22,24 @@ class Payment < ActiveRecord::Base end def purchase - begin - customer = Stripe::Customer.create email: stripeEmail, - source: stripeToken, - description: user.name + customer = Stripe::Customer.create email: stripe_customer_email, + source: stripe_customer_token, + 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 + gateway_response = Stripe::Charge.create customer: customer.id, + receipt_email: stripe_customer_email, + description: 'ticket purchases', + amount: amount_to_pay, + currency: conference.tickets.first.price_currency - self.last4 = gateway_response[:source][:last4] - self.authorization_code = gateway_response[:id] - self.status = 'success' - true + self.last4 = gateway_response[:source][:last4] + self.authorization_code = gateway_response[:id] + self.status = 'success' + true - rescue => error - errors.add(:base, error.message) - self.status = 'failure' - false - end + rescue => error + errors.add(:base, error.message) + self.status = 'failure' + false end end diff --git a/app/views/payments/_payment.html.haml b/app/views/payments/_payment.html.haml index 93692b9f..a8de023f 100644 --- a/app/views/payments/_payment.html.haml +++ b/app/views/payments/_payment.html.haml @@ -1,5 +1,4 @@ .div - .col-md-12.table-responsive %table.table.table-hover %thead