| ## Controller | |
| class RegistrationsController < ApplicationController | |
| before_action :set_registration, only: [:show, :edit, :update, :destroy] | |
| # GET /registrations | |
| def index | |
| @registrations = Registration.all | |
| authenticate_or_request_with_http_basic('Administration') do |username, password| | |
| username == 'admin' && password == 'password' | |
| end | |
| end | |
| # GET /registrations/1 | |
| def show | |
| end | |
| # GET /registrations/new | |
| def new | |
| @registration = Registration.new | |
| @level = Level.find_by id: params["level_id"] | |
| end | |
| # POST /registrations | |
| def create | |
| @registration = Registration.new registration_params.merge(email: params['registration']['email'], | |
| card_token: params['registration']['card_token']) | |
| raise "Please, check registration errors" unless @registration.valid? | |
| @registration.process_payment(params['registration']['email'], params['registration']['card_token']) | |
| @registration.save! | |
| redirect_to @registration, notice: 'Registration was successfully created.' | |
| end | |
| private | |
| def stripe_params | |
| params.permit :stripeEmail, :stripeToken | |
| end | |
| # Use callbacks to share common setup or constraints between actions. | |
| def set_registration | |
| @registration = Registration.find(params[:id]) | |
| end | |
| # Never trust parameters from the scary internet, only allow the white list through. | |
| def registration_params | |
| params.require(:registration).permit(:level_id, :firstname, :lastname, :phone, :email, :card_token) | |
| end | |
| end | |
| ## Model | |
| class Registration < ActiveRecord::Base | |
| belongs_to :level | |
| def process_payment(email, card_token) | |
| customer = Stripe::Customer.create email: email, | |
| card: card_token | |
| Stripe::Charge.create customer: customer.id, | |
| amount: level.price*100, | |
| description: level.name, | |
| currency: 'usd' | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment