Guest User

Untitled

a guest
Oct 17th, 2017
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. class RegistrationsController < ApplicationController
  2. before_action :set_registration, only: [:show, :edit, :update, :destroy]
  3.  
  4. def index
  5. @registrations = Registration.all
  6. end
  7.  
  8. def show
  9. end
  10.  
  11. def new
  12. @registration = Registration.new
  13. @registration.subscription = Subscription.find_by id: params["subscription_id"]
  14. end
  15.  
  16. def create
  17. @registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"],
  18. card_token: stripe_params["stripeToken"])
  19. raise "Please, check registration errors" unless @registration.valid?
  20. @registration.process_payment
  21. @registration.save
  22. redirect_to @registration, notice: 'Registration was successfully created. Thank you!'
  23. rescue Stripe::CardError => e
  24. flash[:error] = e.message
  25. render :new
  26. end
  27.  
  28.  
  29. # POST /registrations/hook
  30. protect_from_forgery except: :webhook
  31. def webhook
  32. event = Stripe::Event.retrieve(params["id"])
  33.  
  34. case event.type
  35. when "invoice.payment_succeeded" #renew subscription
  36. Registration.find_by_customer_id(event.data.object.customer).renew
  37. end
  38. render status: :ok, json: "success"
  39. end
  40.  
  41. private
  42.  
  43. def stripe_params
  44. params.permit :stripeEmail, :stripeToken
  45. end
  46.  
  47. def set_registration
  48. @registration = Registration.find(params[:id])
  49. end
  50.  
  51. def registration_params
  52. params.require(:registration).permit(:subscription_id, :full_name, :company, :telephone, :email, :card_token, :end_date, :customer_id)
  53. end
  54.  
  55. end
  56.  
  57. create_table "registrations", force: :cascade do |t|
  58. t.bigint "subscription_id", null: false
  59. t.string "full_name"
  60. t.string "company"
  61. t.string "email"
  62. t.string "telephone"
  63. t.datetime "created_at", null: false
  64. t.datetime "updated_at", null: false
  65. t.string "card_token"
  66. t.date "end_date"
  67. t.string "customer_id"
  68. t.index ["subscription_id"], name: "index_registrations_on_subscription_id"
  69. end
  70.  
  71. Started POST "/registrations" for 2607:fea8:571f:f at 2017-10-17
  72. Cannot render console from 2607::ee6f! Allowed networks:
  73. Processing by RegistrationsController#create as HTML
  74. 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"}
  75.  
  76.  
  77. Unpermitted parameters: :utf8, :authenticity_token, :registration, :stripeTokenType
  78. Unpermitted parameters: :utf8, :authenticity_token, :registration, :stripeTokenType
  79.  
  80.  
  81. Subscription Load (0.3ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
  82. (0.2ms) BEGIN
  83. 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"]]
  84. (1.2ms) COMMIT
  85. Redirected to http://1fc4a17c.ngrok.io/registrations/2
  86. Completed 302 Found in 2775ms (ActiveRecord: 2.2ms)
  87.  
  88. >> @registration
  89. => <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