Class: Johnhenry::PaymentsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Johnhenry::PaymentsController
- Defined in:
- app/controllers/johnhenry/payments_controller.rb
Constant Summary
Constants inherited from ApplicationController
ApplicationController::DEFAULT_DESCRIPTION, ApplicationController::DEFAULT_TITLE
Instance Method Summary collapse
- #charge_user!(stripe_token, amount) ⇒ Object
-
#create ⇒ Object
POST /payments POST /payments.json.
-
#destroy ⇒ Object
DELETE /payments/1 DELETE /payments/1.json.
-
#edit ⇒ Object
GET /payments/1/edit.
-
#index ⇒ Object
GET /payments GET /payments.json.
-
#new ⇒ Object
GET /payments/new.
-
#show ⇒ Object
GET /payments/1 GET /payments/1.json.
-
#update ⇒ Object
PATCH/PUT /payments/1 PATCH/PUT /payments/1.json.
Methods inherited from ApplicationController
#admin_ids, #set_meta_tag_defaults
Instance Method Details
#charge_user!(stripe_token, amount) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/controllers/johnhenry/payments_controller.rb', line 67 def charge_user!(stripe_token, amount) Rails.logger.info "Charging #{ current_user.id } : _stripe_token: #{ stripe_token }" # create customer_id if it's a new stripe_token unless current_user.stripe_customer_id customer = Stripe::Customer.create( card: stripe_token, description: current_user.email || "user_id_#{ current_user.id }" ) Rails.logger.info "Created customer: #{ customer.inspect }" current_user.update_attributes stripe_customer_id: customer.id end Rails.logger.info( "About to charge customer_id: #{ current_user.stripe_customer_id }") charge = Stripe::Charge.create( amount: amount * 100, # convert from dollars to cents currency: 'usd', customer: current_user.stripe_customer_id ) Rails.logger.info "Charged #{ charge.inspect }" end |
#create ⇒ Object
POST /payments POST /payments.json
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/controllers/johnhenry/payments_controller.rb', line 27 def create if !signed_in? if params[:email].present? #TODO DRY with other new user generation password = Devise.friendly_token.first(10) user = JohnHenryUser.create! email: params[:email], password: password, password_confirmation: password sign_in(user) else return redirect_to '/', alert: 'You need an account to do that.' end end begin charge_user!(payment_params['stripe_token'], 7) rescue => exc Rails.logger.error(exc.) Rails.logger.error(exc.inspect) return redirect_to '/', alert: 'Payment could not be processed.' end txn_info = { user_id: current_user.id, stripe_customer_id: current_user.stripe_customer_id, amount: 7.0 } @payment = Payment.new(payment_params.merge(txn_info)) respond_to do |format| if @payment.save format.html { redirect_to @payment, notice: 'Payment was successfully made.' } format.json { render action: 'show', status: :created, location: @payment } else format.html { render action: 'new' } format.json { render json: @payment.errors, status: :unprocessable_entity } end end end |
#destroy ⇒ Object
DELETE /payments/1 DELETE /payments/1.json
107 108 109 110 111 112 113 |
# File 'app/controllers/johnhenry/payments_controller.rb', line 107 def destroy @payment.destroy respond_to do |format| format.html { redirect_to payments_url } format.json { head :no_content } end end |
#edit ⇒ Object
GET /payments/1/edit
22 23 |
# File 'app/controllers/johnhenry/payments_controller.rb', line 22 def edit end |
#index ⇒ Object
GET /payments GET /payments.json
7 8 9 |
# File 'app/controllers/johnhenry/payments_controller.rb', line 7 def index @payments = Payment.where(user_id: current_user.id) end |
#new ⇒ Object
GET /payments/new
17 18 19 |
# File 'app/controllers/johnhenry/payments_controller.rb', line 17 def new @payment = Payment.new end |
#show ⇒ Object
GET /payments/1 GET /payments/1.json
13 14 |
# File 'app/controllers/johnhenry/payments_controller.rb', line 13 def show end |
#update ⇒ Object
PATCH/PUT /payments/1 PATCH/PUT /payments/1.json
93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/controllers/johnhenry/payments_controller.rb', line 93 def update respond_to do |format| if @payment.update(payment_params) format.html { redirect_to @payment, notice: 'Payment was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @payment.errors, status: :unprocessable_entity } end end end |