Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RegistrationsController < ApplicationController
- before_action :set_registration, only: [:show, :edit, :update, :destroy]
- def index
- @registrations = Registration.all
- end
- def show
- end
- def new
- @registration = Registration.new
- @registration.subscription = Subscription.find_by id: params["subscription_id"]
- end
- def create
- @registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"],
- card_token: stripe_params["stripeToken"])
- raise "Please, check registration errors" unless @registration.valid?
- @registration.process_payment
- @registration.save
- redirect_to @registration, notice: 'Registration was successfully created. Thank you!'
- rescue Stripe::CardError => e
- flash[:error] = e.message
- render :new
- end
- # POST /registrations/hook
- protect_from_forgery except: :webhook
- def webhook
- event = Stripe::Event.retrieve(params["id"])
- case event.type
- when "invoice.payment_succeeded" #renew subscription
- Registration.find_by_customer_id(event.data.object.customer).renew
- end
- render status: :ok, json: "success"
- end
- private
- def stripe_params
- params.permit :stripeEmail, :stripeToken
- end
- def set_registration
- @registration = Registration.find(params[:id])
- end
- def registration_params
- params.require(:registration).permit(:subscription_id, :full_name, :company, :telephone, :email, :card_token, :end_date, :customer_id)
- end
- end
- create_table "registrations", force: :cascade do |t|
- t.bigint "subscription_id", null: false
- t.string "full_name"
- t.string "company"
- t.string "email"
- t.string "telephone"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.string "card_token"
- t.date "end_date"
- t.string "customer_id"
- t.index ["subscription_id"], name: "index_registrations_on_subscription_id"
- end
- Started POST "/registrations" for 2607:fea8:571f:f at 2017-10-17
- Cannot render console from 2607::ee6f! Allowed networks:
- Processing by RegistrationsController#create as HTML
- Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXXXXX==", "registration"=>{"subscription_id"=>"1", "full_name"=>"NAME", "company"=>"COMPANY", "telephone"=>"9999999"}, "stripeToken"=>"tok_XXXCCCC7777777", "stripeTokenType"=>"card", "stripeEmail"=>"NAME@mail.com"}
- Unpermitted parameters: :utf8, :authenticity_token, :registration, :stripeTokenType
- Unpermitted parameters: :utf8, :authenticity_token, :registration, :stripeTokenType
- Subscription Load (0.3ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
- (0.2ms) BEGIN
- SQL (0.5ms) INSERT INTO "registrations" ("subscription_id", "full_name", "company", "email", "telephone", "created_at", "updated_at", "card_token") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["subscription_id", 1], ["full_name", "NAME"], ["company", "COMPANY"], ["email", "NAME@mail.com"], ["telephone", "9999999"], ["created_at", "2017-10-17"], ["updated_at", "2017-10-17"], ["card_token", "tok_XXXXXXXX"]]
- (1.2ms) COMMIT
- Redirected to http://1fc4a17c.ngrok.io/registrations/2
- Completed 302 Found in 2775ms (ActiveRecord: 2.2ms)
- >> @registration
- => <Registration id: 2, subscription_id: 1, full_name: "NAME", company: "COMPANY", email: "NAME@mail.com", telephone: "99999999", created_at: "2017-10-17", updated_at: "2017-10-17", card_token: "tok_xxxxxxx", end_date: nil, customer_id: nil>
Add Comment
Please, Sign In to add comment