move stripe API call to model, schema changes
This commit is contained in:
parent
47e16e5653
commit
be6d046154
4 changed files with 46 additions and 35 deletions
|
|
@ -17,32 +17,23 @@ class PaymentsController < ApplicationController
|
||||||
@unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference)
|
@unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference)
|
||||||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
||||||
|
|
||||||
customer = Stripe::Customer.create(
|
@payment = Payment.new payment_params.merge(user: current_user, conference: @conference)
|
||||||
:email => params[:stripeEmail],
|
@payment.purchase
|
||||||
:source => params[:stripeToken]
|
@payment.save
|
||||||
)
|
|
||||||
|
|
||||||
gateway_response = Stripe::Charge.create(
|
update_purchased_ticket_purchases
|
||||||
:customer => customer.id,
|
|
||||||
:amount => @total_amount_to_pay.cents,
|
|
||||||
:description => 'Rails Stripe customer',
|
|
||||||
:currency => @conference.tickets.first.price_currency
|
|
||||||
)
|
|
||||||
|
|
||||||
payment = Payment.purchase(gateway_response, current_user, @conference)
|
redirect_to conference_conference_registration_path(@conference.short_title), flash:
|
||||||
update_purchased_ticket_purchases(payment)
|
{ success: 'Thanks! You have purchased your tickets successfully.' }
|
||||||
|
|
||||||
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'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def update_purchased_ticket_purchases(payment)
|
def payment_params
|
||||||
current_user.ticket_purchases.by_conference(@conference).unpaid.update_all(paid: true, payment_id: payment.id)
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ class Payment < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :conference
|
belongs_to :conference
|
||||||
|
|
||||||
|
attr_accessor :stripeEmail
|
||||||
|
attr_accessor :stripeToken
|
||||||
|
|
||||||
validates :last4, presence: true
|
validates :last4, presence: true
|
||||||
validates :authorization_code, presence: true
|
validates :authorization_code, presence: true
|
||||||
validates :status, presence: true
|
validates :status, presence: true
|
||||||
|
|
@ -16,12 +19,29 @@ class Payment < ActiveRecord::Base
|
||||||
failure: 2
|
failure: 2
|
||||||
}
|
}
|
||||||
|
|
||||||
def self.purchase(gateway_response, user, conference)
|
def amount_to_pay
|
||||||
create(last4: gateway_response[:source][:last4],
|
Ticket.total_price(conference, user, paid: false).cents
|
||||||
amount: gateway_response[:amount],
|
end
|
||||||
status: (gateway_response[:paid] ? 1 : 0),
|
|
||||||
authorization_code: gateway_response[:id],
|
def purchase
|
||||||
user_id: user.id,
|
customer = Stripe::Customer.create email: stripeEmail,
|
||||||
conference_id: conference.id)
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
class CreatePayments < ActiveRecord::Migration
|
class CreatePayments < ActiveRecord::Migration
|
||||||
def change
|
def change
|
||||||
create_table :payments do |t|
|
create_table :payments do |t|
|
||||||
t.string :last4, null: false
|
t.string :last4
|
||||||
t.integer :amount, null: false
|
t.integer :amount
|
||||||
t.string :authorization_code, null: false
|
t.string :authorization_code
|
||||||
t.integer :status, default: 0, null: false
|
t.integer :status, default: 0, null: false
|
||||||
t.integer :user_id, null: false
|
t.integer :user_id, null: false
|
||||||
t.integer :conference_id, null: false
|
t.integer :conference_id, null: false
|
||||||
|
|
|
||||||
|
|
@ -264,9 +264,9 @@ ActiveRecord::Schema.define(version: 20160704092023) do
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "payments", force: :cascade do |t|
|
create_table "payments", force: :cascade do |t|
|
||||||
t.string "last4", null: false
|
t.string "last4"
|
||||||
t.integer "amount", null: false
|
t.integer "amount"
|
||||||
t.string "authorization_code", null: false
|
t.string "authorization_code"
|
||||||
t.integer "status", default: 0, null: false
|
t.integer "status", default: 0, null: false
|
||||||
t.integer "user_id", null: false
|
t.integer "user_id", null: false
|
||||||
t.integer "conference_id", null: false
|
t.integer "conference_id", null: false
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue