mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
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)
|
||||
@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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue